Search in sources :

Example 6 with Activity

use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.

the class ActivityStoreTest method testAddActivity.

@Test
public void testAddActivity() {
    UUID activtiyId = UUID.randomUUID();
    Activity inputActivity = new Activity(activtiyId, UUID.randomUUID(), UUID.randomUUID(), Instant.now(), "joinedConvo", "testMessage", new ArrayList<UUID>(), true);
    activityStore.addActivity(inputActivity);
    Activity resultActivity = activityStore.getActivityWithId(activtiyId);
    assertEquals(inputActivity, resultActivity);
    Mockito.verify(mockPersistentStorageAgent).writeThrough(inputActivity);
}
Also used : Activity(codeu.model.data.Activity) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with Activity

use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.

the class ActivityStoreTest method testGetActivitiesPerPrivacy_General.

@Test
public void testGetActivitiesPerPrivacy_General() {
    UUID allContentId = UUID.randomUUID();
    UUID noContentID = UUID.randomUUID();
    User allContentUser = new User(allContentId, "allContentUser", "password", null, Instant.now(), "codeUChatTestEmail@gmail.com");
    User noContentUser = new User(noContentID, "noContentUser", "password", null, Instant.now(), "codeUChatTestEmail@gmail.com");
    allContentUser.setActivityFeedPrivacy("allContent");
    allContentUser.setProfilePrivacy("allContent");
    noContentUser.setActivityFeedPrivacy("noContent");
    noContentUser.setProfilePrivacy("noContent");
    Activity activity1 = new Activity(allContentId, allContentId, allContentId, Instant.ofEpochMilli(2000), "leftConvo", "test_message", users, true);
    Activity activity2 = new Activity(allContentId, allContentId, allContentId, Instant.ofEpochMilli(2000), "leftConvo", "test_message", users, true);
    Activity activity3 = new Activity(noContentID, noContentID, noContentID, Instant.ofEpochMilli(2000), "leftConvo", "test_message", users, true);
    Activity activity4 = new Activity(noContentID, noContentID, noContentID, Instant.ofEpochMilli(2000), "leftConvo", "test_message", users, true);
    List<Activity> activityList = new ArrayList<>();
    activityList.add(activity1);
    activityList.add(activity2);
    activityList.add(activity3);
    activityList.add(activity4);
    List<Activity> activitiesPerPrivacy = activityStore.getActivitiesPerPrivacy(allContentUser, activityList);
    Assert.assertEquals(activity1, activitiesPerPrivacy.get(0));
    Assert.assertEquals(activity2, activitiesPerPrivacy.get(1));
    Assert.assertEquals(2, activitiesPerPrivacy.size());
}
Also used : User(codeu.model.data.User) ArrayList(java.util.ArrayList) Activity(codeu.model.data.Activity) UUID(java.util.UUID) Test(org.junit.Test)

Example 8 with Activity

use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.

the class ConversationServlet method doPost.

/**
 * This function fires when a user submits the form on the conversations page. It gets the
 * logged-in username from the session and the new conversation title from the submitted form
 * data. It uses this to create a new Conversation object that it adds to the model.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them create a conversation
        response.sendRedirect("/conversations");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them create a conversation
        System.out.println("User not found: " + username);
        response.sendRedirect("/conversations");
        return;
    }
    String conversationTitle = request.getParameter("conversationTitle");
    if (conversationTitle.isEmpty()) {
        User loggedInUser = userStore.getUser(username);
        List<Conversation> conversations = sort(conversationStore.getAllPermittedConversations(loggedInUser.getId()));
        request.setAttribute("conversations", conversations);
        request.setAttribute("error", "Please specify a name for this chat.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    if (!conversationTitle.matches("[\\w*]*")) {
        request.setAttribute("error", "Please enter only letters and numbers.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    if (conversationStore.isTitleTaken(conversationTitle)) {
        // conversation title is already taken, just go into that conversation instead of creating a
        // new one
        User loggedInUser = userStore.getUser(username);
        List<Conversation> conversations = sort(conversationStore.getAllPermittedConversations(loggedInUser.getId()));
        request.setAttribute("conversations", conversations);
        request.setAttribute("error", "This conversation name is already taken.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), conversationTitle, Instant.now());
    conversationStore.addConversation(conversation);
    String activityMessage = " created a new conversation: " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>.";
    Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "createdConvo", activityMessage, conversation.getConversationUsers(), conversation.getIsPublic());
    activityStore.addActivity(activity);
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation)

Example 9 with Activity

use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.

the class ProfileServlet method doPost.

/**
 * This function fires when a user submits the form on the profile page.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String requestUrl = request.getRequestURI();
    Part image = request.getPart("image");
    String biography = request.getParameter("submitBiography");
    String name = requestUrl.substring("/profile/".length());
    User user = userStore.getUser(name);
    if (biography != null) {
        if (request.getParameter("newBio").isEmpty()) {
            User loggedInUser = userStore.getUser((String) request.getSession().getAttribute("user"));
            List<Activity> activities = null;
            List<Activity> activitiesPermitted;
            if (user != null) {
                activitiesPermitted = loggedInUser == null ? sort(activityStore.getAllPublicActivitiesWithUserId(user.getId())) : sort(activityStore.getAllPermittedActivitiesWithUserId(user.getId(), loggedInUser.getId()));
                activities = activityStore.getActivitiesPerPrivacy(user, activitiesPermitted);
            }
            request.setAttribute("error", "Please specify a biography.");
            request.setAttribute("activities", activities);
            request.setAttribute("loggedInUser", loggedInUser);
            request.setAttribute("user", user);
            request.getRequestDispatcher("/WEB-INF/view/profile.jsp").forward(request, response);
            return;
        }
        user.setBio(request.getParameter("newBio"));
    }
    if (image != null) {
        ImageStorage imageStorage = new ImageStorage();
        String imageName = imageStorage.storeImage(image);
        user.setProfilePicture(imageName);
    }
    response.sendRedirect(requestUrl);
}
Also used : User(codeu.model.data.User) ImageStorage(codeu.utils.ImageStorage) Part(javax.servlet.http.Part) Activity(codeu.model.data.Activity)

Example 10 with Activity

use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.

the class ChatServlet method joinConversation.

/**
 * Adds a user to a conversation
 */
private void joinConversation(User user, Conversation conversation) {
    addConversationFriends(user, conversation);
    conversation.addUser(user.getId());
    String activityMessage = " joined " + "<a href=\"/chat/" + conversation.getTitle() + "\">" + conversation.getTitle() + "</a>.";
    Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "leftConvo", activityMessage, conversation.getConversationUsers(), conversation.getIsPublic());
    activityStore.addActivity(activity);
}
Also used : Activity(codeu.model.data.Activity)

Aggregations

Activity (codeu.model.data.Activity)26 User (codeu.model.data.User)16 Test (org.junit.Test)13 UUID (java.util.UUID)11 ArrayList (java.util.ArrayList)8 Conversation (codeu.model.data.Conversation)6 Message (codeu.model.data.Message)2 Email (codeu.utils.Email)2 Instant (java.time.Instant)2 ActivityStore (codeu.model.store.basic.ActivityStore)1 UserStore (codeu.model.store.basic.UserStore)1 ImageStorage (codeu.utils.ImageStorage)1 HashSet (java.util.HashSet)1 HttpSession (javax.servlet.http.HttpSession)1 Part (javax.servlet.http.Part)1