Search in sources :

Example 26 with Message

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

the class PersistentDataStoreTest method testSaveAndLoadMessages.

@Test
public void testSaveAndLoadMessages() throws PersistentDataStoreException {
    UUID idOne = UUID.randomUUID();
    UUID conversationOne = UUID.randomUUID();
    UUID authorOne = UUID.randomUUID();
    String contentOne = "test content one";
    Instant creationOne = Instant.ofEpochMilli(1000);
    Message inputMessageOne = new Message(idOne, conversationOne, authorOne, contentOne, creationOne);
    UUID idTwo = UUID.randomUUID();
    UUID conversationTwo = UUID.randomUUID();
    UUID authorTwo = UUID.randomUUID();
    String contentTwo = "test content one";
    Instant creationTwo = Instant.ofEpochMilli(2000);
    Message inputMessageTwo = new Message(idTwo, conversationTwo, authorTwo, contentTwo, creationTwo);
    // save
    persistentDataStore.writeThrough(inputMessageOne);
    persistentDataStore.writeThrough(inputMessageTwo);
    // load
    List<Message> resultMessages = persistentDataStore.loadMessages();
    // confirm that what we saved matches what we loaded
    Message resultMessageOne = resultMessages.get(0);
    Assert.assertEquals(idOne, resultMessageOne.getId());
    Assert.assertEquals(conversationOne, resultMessageOne.getConversationId());
    Assert.assertEquals(authorOne, resultMessageOne.getAuthorId());
    Assert.assertEquals(contentOne, resultMessageOne.getContent());
    Assert.assertEquals(creationOne, resultMessageOne.getCreationTime());
    Message resultMessageTwo = resultMessages.get(1);
    Assert.assertEquals(idTwo, resultMessageTwo.getId());
    Assert.assertEquals(conversationTwo, resultMessageTwo.getConversationId());
    Assert.assertEquals(authorTwo, resultMessageTwo.getAuthorId());
    Assert.assertEquals(contentTwo, resultMessageTwo.getContent());
    Assert.assertEquals(creationTwo, resultMessageTwo.getCreationTime());
}
Also used : Message(codeu.model.data.Message) Instant(java.time.Instant) UUID(java.util.UUID) Test(org.junit.Test)

Example 27 with Message

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

the class PersistentStorageAgentTest method testWriteThroughMessage.

@Test
public void testWriteThroughMessage() {
    Message message = new Message(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), "test content", Instant.now());
    persistentStorageAgent.writeThrough(message);
    Mockito.verify(mockPersistentDataStore).writeThrough(message);
}
Also used : Message(codeu.model.data.Message) Test(org.junit.Test)

Example 28 with Message

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

the class ChatServlet method doGet.

/**
 * This function fires when a user navigates to the chat page. It gets the conversation title from
 * the URL, finds the corresponding Conversation, and fetches the messages in that Conversation.
 * It then forwards to chat.jsp for rendering.
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String requestUrl = request.getRequestURI();
    String conversationTitle = requestUrl.substring("/chat/".length());
    Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
    if (conversation == null) {
        // couldn't find conversation, redirect to conversation list
        System.out.println("Conversation was null: " + conversationTitle);
        response.sendRedirect("/conversations");
        return;
    }
    UUID conversationId = conversation.getId();
    List<Message> messages = messageStore.getMessagesInConversation(conversationId);
    List<User> conversationUsers = conversation.getConversationUsers();
    request.setAttribute("conversation", conversation);
    request.setAttribute("messages", messages);
    request.setAttribute("conversationUsers", conversationUsers);
    request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation) UUID(java.util.UUID)

Example 29 with Message

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

the class ChatServlet 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 {
    String button = request.getParameter("button");
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    String requestUrl = request.getRequestURI();
    String conversationTitle = requestUrl.substring("/chat/".length());
    Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
    if (conversation == null) {
        // couldn't find conversation, redirect to conversation list
        response.sendRedirect("/conversations");
        return;
    }
    if ("joinButton".equals(button)) {
        conversation.getConversationUsers().add(user);
        String activityMessage = " joined " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>.";
        Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "joinedConvo", activityMessage);
        activityStore.addActivity(activity);
    }
    if ("leaveButton".equals(button)) {
        conversation.getConversationUsers().remove(user);
        String activityMessage = " left " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>.";
        Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "leftConvo", activityMessage);
        activityStore.addActivity(activity);
    }
    if (button == null && conversation.getConversationUsers().contains(user)) {
        String messageContent = request.getParameter("message");
        // this removes any HTML from the message content
        String cleanedMessageContent = Jsoup.clean(messageContent, "", Whitelist.none(), new OutputSettings().prettyPrint(false));
        String finalMessageContent = TextFormatter.formatForDisplay(cleanedMessageContent);
        Message message = new Message(UUID.randomUUID(), conversation.getId(), user.getId(), finalMessageContent, Instant.now());
        messageStore.addMessage(message);
        String activityMessage = " sent a message in " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>" + ": " + finalMessageContent;
        Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "messageSent", activityMessage);
        activityStore.addActivity(activity);
    }
    // redirect to a GET request
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) OutputSettings(org.jsoup.nodes.Document.OutputSettings) Message(codeu.model.data.Message) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation)

Example 30 with Message

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

the class ProfileServlet method doGet.

/**
 * This function fires when a user navigates to a user's profile page. It gets the username from
 * the URL, finds the corresponding User, and fetches the messages posted by that user. It then
 * forwards to profile.jsp for rendering.
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String requestUrl = request.getRequestURI();
    String name = requestUrl.substring("/profile/".length());
    User user = userStore.getUser(name);
    List<Message> messages = null;
    if (user != null) {
        messages = messageStore.getMessagesByAuthorSorted(user.getId());
    }
    request.setAttribute("messages", messages);
    request.setAttribute("user", user);
    request.getRequestDispatcher("/WEB-INF/view/profile.jsp").forward(request, response);
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message)

Aggregations

Message (codeu.model.data.Message)39 Test (org.junit.Test)23 Conversation (codeu.model.data.Conversation)22 User (codeu.model.data.User)19 UUID (java.util.UUID)15 ArrayList (java.util.ArrayList)9 Instant (java.time.Instant)6 Activity (codeu.model.data.Activity)2 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)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)1