Search in sources :

Example 36 with Conversation

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

the class DirectMessageServlet method doPost.

/**
 * This function fires when a user submits the form on the chat page. It gets the logged-in
 * username from the session, the conversation title from the URL, and the chat message from the
 * submitted form data. It creates a new Message from that data, adds it to the model, and then
 * redirects back to the chat page.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Part image = request.getPart("image");
    String submitText = request.getParameter("submitText");
    String requestUrl = request.getRequestURI();
    String loggedInUsername = (String) request.getSession().getAttribute("user");
    String otherUsername = requestUrl.substring("/direct/".length());
    User loggedInUser = userStore.getUser(loggedInUsername);
    User otherUser = userStore.getUser(otherUsername);
    if (loggedInUser == null) {
        // user was not found, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    if (otherUser == null || loggedInUser.getId().equals(otherUser.getId())) {
        response.sendRedirect("/conversations");
        return;
    }
    String convoName = loggedInUsername.compareTo(otherUsername) < 0 ? "direct:" + loggedInUsername + "-" + otherUsername : "direct:" + otherUsername + "-" + loggedInUsername;
    Conversation conversation = conversationStore.getConversationWithTitle(convoName);
    if (conversation == null) {
        // couldn't find conversation, redirect to conversation list
        response.sendRedirect("/conversations");
        return;
    }
    String conversationTitle = requestUrl.substring("/chat/".length());
    if (submitText != null && conversation.getConversationUsers().contains(loggedInUser.getId())) {
        String messageContent = request.getParameter("message");
        if (messageContent.isEmpty()) {
            request.setAttribute("error", "Message body cannot be empty.");
            request.setAttribute("conversationUsers", conversation.getConversationUsers());
            request.setAttribute("conversation", conversation);
            request.setAttribute("messages", messageStore.getMessagesInConversation(conversation.getId()));
            request.setAttribute("loggedInUser", loggedInUser);
            request.setAttribute("otherUser", otherUser);
            request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
            return;
        }
        if (messageContent.isEmpty()) {
            request.setAttribute("error", "Message body cannot be empty.");
            request.setAttribute("conversationUsers", conversation.getConversationUsers());
            request.setAttribute("conversation", conversation);
            request.setAttribute("messages", messageStore.getMessagesInConversation(conversation.getId()));
            request.setAttribute("loggedInUser", loggedInUser);
            request.setAttribute("otherUser", otherUser);
            request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
            return;
        }
        // this removes any HTML from the message content
        String cleanedMessageContent = Jsoup.clean(messageContent, "", Whitelist.none(), new OutputSettings().prettyPrint(false));
        String finalMessageContent = TextFormatter.formatForDisplay(cleanedMessageContent);
        createMessage(request, cleanedMessageContent, loggedInUser, conversation, false);
    } else if (image != null) {
        ImageStorage imageStorage = new ImageStorage();
        String imageName = imageStorage.storeImage(image);
        createMessage(request, imageName, loggedInUser, conversation, true);
    }
    // redirect to a GET request
    response.sendRedirect("/direct/" + otherUser.getName());
}
Also used : User(codeu.model.data.User) OutputSettings(org.jsoup.nodes.Document.OutputSettings) ImageStorage(codeu.utils.ImageStorage) Part(javax.servlet.http.Part) Conversation(codeu.model.data.Conversation)

Example 37 with Conversation

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

the class DefaultDataStore method addRandomConversations.

private void addRandomConversations() {
    for (int i = 1; i <= DEFAULT_CONVERSATION_COUNT; i++) {
        User user = getRandomElement(users);
        String title = "Conversation_" + i;
        Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), title, Instant.now());
        PersistentStorageAgent.getInstance().writeThrough(conversation);
        conversations.add(conversation);
    }
}
Also used : User(codeu.model.data.User) Conversation(codeu.model.data.Conversation)

Example 38 with Conversation

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

the class DefaultDataStore method addRandomActivities.

private void addRandomActivities() {
    for (int i = 0; i < DEFAULT_ACTIVITY_COUNT; i++) {
        String activityType;
        Random random = new Random();
        int max = 4, min = 0;
        int randomNum = random.nextInt(max - min + 1) + min;
        if (randomNum == 0) {
            activityType = "joinedApp";
        } else if (randomNum == 1) {
            activityType = "joinedConvo";
        } else if (randomNum == 2) {
            activityType = "leftConvo";
        } else if (randomNum == 3) {
            activityType = "createdConvo";
        } else {
            activityType = "messageSent";
        }
        UUID conversationId, associatedUserId;
        User user = getRandomElement(users);
        associatedUserId = user.getId();
        Conversation conversation = getRandomElement(conversations);
        associatedUserId = conversation.getOwnerId();
        if (activityType.equals("joinedApp")) {
            conversationId = new UUID(0L, 0L);
        } else if (activityType.equals("createdConvo")) {
            conversationId = conversation.getId();
        } else {
            conversation = getRandomElement(conversations);
            conversationId = conversation.getId();
        }
        String activityMessage = generateActivityContent(user, conversation, activityType);
        Activity activity = new Activity(UUID.randomUUID(), associatedUserId, conversationId, Instant.now(), activityType, activityMessage, conversation.getConversationUsers(), conversation.getIsPublic());
        PersistentStorageAgent.getInstance().writeThrough(activity);
        activities.add(activity);
    }
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation)

Example 39 with Conversation

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

the class DefaultDataStore method addRandomMessages.

private void addRandomMessages() {
    for (int i = 0; i < DEFAULT_MESSAGE_COUNT; i++) {
        Conversation conversation = getRandomElement(conversations);
        User author = getRandomElement(users);
        String content = getRandomMessageContent();
        Message message = new Message(UUID.randomUUID(), conversation.getId(), author.getId(), content, Instant.now(), false);
        PersistentStorageAgent.getInstance().writeThrough(message);
        messages.add(message);
    }
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation)

Example 40 with Conversation

use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.

the class ConversationStoreTest method testAddConversation.

@Test
public void testAddConversation() {
    Conversation inputConversation = new Conversation(UUID.randomUUID(), UUID.randomUUID(), "test_conversation", Instant.now());
    conversationStore.addConversation(inputConversation);
    Conversation resultConversation = conversationStore.getConversationWithTitle("test_conversation");
    assertEquals(inputConversation, resultConversation);
    Mockito.verify(mockPersistentStorageAgent).writeThrough(inputConversation);
}
Also used : Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Aggregations

Conversation (codeu.model.data.Conversation)74 Test (org.junit.Test)46 User (codeu.model.data.User)43 Message (codeu.model.data.Message)29 ArrayList (java.util.ArrayList)16 UUID (java.util.UUID)16 Activity (codeu.model.data.Activity)6 Instant (java.time.Instant)6 HashSet (java.util.HashSet)3 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 ImageStorage (codeu.utils.ImageStorage)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 OutputSettings (org.jsoup.nodes.Document.OutputSettings)2 ConversationFilterer (codeu.utils.ConversationFilterer)1 MessageFilterer (codeu.utils.MessageFilterer)1 DateTimeException (java.time.DateTimeException)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1