Search in sources :

Example 16 with Activity

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

the class PersonalActivityServlet method doGet.

/**
 * This function fires when a user navigates to the activity feed page. It gets a list of all
 * current messages and forwards them to activityFeed.jsp.
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        response.sendRedirect("/activityFeed");
        return;
    }
    User user = userStore.getUser(username);
    UUID userID = user.getId();
    List<Conversation> conversations = sort(conversationStore.getAllConversations());
    List<Activity> conversationActivities = new ArrayList<>();
    // retrieve activities for the conversations a user has joined
    for (Conversation conversation : conversations) {
        if (conversation.getConversationUsers() != null) {
            List<UUID> conversationUsers = conversation.getConversationUsers();
            if (conversationUsers.contains(user.getId())) {
                conversationActivities.add(activityStore.getActivityWithConversationID(conversation.getId()));
            }
        }
    }
    List<Activity> tailoredActivities = new ArrayList<>(conversationActivities);
    // retrieve user activities
    List<Activity> userActivities = activityStore.getActivitiesWithUserID(userID);
    if (userActivities != null) {
        tailoredActivities.addAll(userActivities);
    }
    // remove any duplicates
    Set<Activity> hashSet = new HashSet<>(tailoredActivities);
    tailoredActivities.clear();
    tailoredActivities.addAll(hashSet);
    List<Activity> privacyActivities = sort(activityStore.getActivitiesPerPrivacy(user, tailoredActivities));
    request.setAttribute("activities", privacyActivities);
    request.getRequestDispatcher("/WEB-INF/view/personalActivityFeed.jsp").forward(request, response);
}
Also used : User(codeu.model.data.User) ArrayList(java.util.ArrayList) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 17 with Activity

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

the class RegisterServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());
    String confirmPassword = request.getParameter("confirmPassword");
    if (!username.matches("[\\w*\\s*]*")) {
        request.setAttribute("error", "Please enter only letters, numbers, and spaces.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (password == null || password.length() < 8) {
        request.setAttribute("error", "Please enter a password that is at least 8 characters.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (confirmPassword == null) {
        request.setAttribute("error", "Please enter a confirmation password.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (password != null && !password.equals(confirmPassword)) {
        request.setAttribute("error", "Your password and confirmation password do not match.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (userStore.isUserRegistered(username)) {
        request.setAttribute("error", "That username is already taken.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (email.equals("")) {
        request.setAttribute("error", "Please enter an email.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    User user = new User(UUID.randomUUID(), username, passwordHash, null, Instant.now(), email);
    userStore.addUser(user);
    String message = "created an account!";
    UUID userId = user.getId();
    UUID conversationId = new UUID(0L, 0L);
    Activity activity = new Activity(UUID.randomUUID(), userId, conversationId, Instant.now(), "joinedApp", message, new ArrayList<UUID>(), true);
    activityStore.addActivity(activity);
    response.sendRedirect("/login");
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) UUID(java.util.UUID)

Example 18 with Activity

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

the class PersistentDataStore method loadActivities.

/**
 * Loads all Activity objects from the Datastore service and returns them in a List.
 *
 * @throws codeu.model.store.persistence.PersistentDataStoreException if an error was detected
 *     during the load from the Datastore service
 */
public List<Activity> loadActivities() throws PersistentDataStoreException {
    List<Activity> activities = new ArrayList<>();
    // Retrieve all activities from the datastore.
    Query query = new Query("chat-activities");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            UUID memberId = UUID.fromString((String) entity.getProperty("member_id"));
            UUID conversationId = UUID.fromString((String) entity.getProperty("conversation_id"));
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            String activityType = (String) entity.getProperty("activity_type");
            String activityMessage = (String) entity.getProperty("activity_message");
            List<String> strings = entity.getProperty("users") == null ? new ArrayList<String>() : (List<String>) entity.getProperty("users");
            List<UUID> users = new ArrayList<UUID>();
            for (String str : strings) {
                users.add(UUID.fromString(str));
            }
            boolean isPublic = entity.getProperty("isPublic") == null ? true : ((String) entity.getProperty("isPublic")).equals("true");
            Activity activity = new Activity(uuid, memberId, conversationId, creationTime, activityType, activityMessage, users, isPublic);
            activities.add(activity);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return activities;
}
Also used : Instant(java.time.Instant) ArrayList(java.util.ArrayList) Activity(codeu.model.data.Activity) UUID(java.util.UUID)

Example 19 with Activity

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

the class ConversationServletTest method testDoPost_StoreActivities.

@Test
public void testDoPost_StoreActivities() throws IOException, ServletException {
    Mockito.when(mockRequest.getParameter("conversationTitle")).thenReturn("test_conversation");
    Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
    User fakeUser = new User(UUID.randomUUID(), "test_username", "password", "test biography", Instant.now(), "codeUChatTestEmail@gmail.com");
    Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
    Mockito.when(mockConversationStore.isTitleTaken("test_conversation")).thenReturn(false);
    conversationServlet.doPost(mockRequest, mockResponse);
    ArgumentCaptor<Conversation> conversationArgumentCaptor = ArgumentCaptor.forClass(Conversation.class);
    Mockito.verify(mockConversationStore).addConversation(conversationArgumentCaptor.capture());
    Assert.assertEquals(conversationArgumentCaptor.getValue().getTitle(), "test_conversation");
    ArgumentCaptor<Activity> activityArgumentCaptor = ArgumentCaptor.forClass(Activity.class);
    Mockito.verify(mockActivityStore).addActivity(activityArgumentCaptor.capture());
    Assert.assertEquals(activityArgumentCaptor.getValue().getActivityType(), "createdConvo");
    Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 20 with Activity

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

the class PersonalActivityServletTest method testDoGet.

@Test
public void testDoGet() throws IOException, ServletException {
    UUID fakeUserID = UUID.randomUUID();
    UUID fakeConversationID = UUID.randomUUID();
    User fakeUser = new User(fakeUserID, "test_username", "password", "test biography", Instant.now(), "test@gmail.com");
    Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
    List<Activity> sampleActivities = new ArrayList<>();
    sampleActivities.add(new Activity(UUID.randomUUID(), fakeUserID, fakeConversationID, Instant.ofEpochMilli(2000), "joinedApp", "testMessage", new ArrayList<UUID>(), true));
    sampleActivities.add(new Activity(UUID.randomUUID(), fakeUserID, fakeConversationID, Instant.ofEpochMilli(1000), "createdConvo", "testMessage", new ArrayList<UUID>(), true));
    List<Activity> privateSampleActivities = sort(mockActivityStore.getActivitiesPerPrivacy(fakeUser, sampleActivities));
    List<Activity> sortedSampleActivities = sort(privateSampleActivities);
    personalActivityServlet.doGet(mockRequest, mockResponse);
    Mockito.verify(mockRequest).setAttribute("activities", sortedSampleActivities);
    Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Also used : User(codeu.model.data.User) ArrayList(java.util.ArrayList) Activity(codeu.model.data.Activity) UUID(java.util.UUID) Test(org.junit.Test)

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