Search in sources :

Example 31 with Conversation

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

the class PersistentStorageAgentTest method testWriteThroughConversation.

@Test
public void testWriteThroughConversation() {
    Conversation conversation = new Conversation(UUID.randomUUID(), UUID.randomUUID(), "test_conversation", Instant.now());
    persistentStorageAgent.writeThrough(conversation);
    Mockito.verify(mockPersistentDataStore).writeThrough(conversation);
}
Also used : Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 32 with Conversation

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

the class ConversationFilterer method filterConversationsByCreationDate.

private HashSet<Conversation> filterConversationsByCreationDate(HashSet<Conversation> convos, String dateString, int comp) {
    ZonedDateTime date = ZonedDateTime.parse(dateString, formatter);
    HashSet<Conversation> filteredConvos = new HashSet<Conversation>();
    for (Conversation convo : convos) {
        ZonedDateTime convoDate = ZonedDateTime.ofInstant(convo.getCreationTime(), ZoneId.systemDefault());
        convoDate = convoDate.truncatedTo(ChronoUnit.DAYS);
        int dateComp = convoDate.compareTo(date);
        if (dateComp > 0 && comp > 0) {
            filteredConvos.add(convo);
        } else if (dateComp == 0 && comp == 0) {
            filteredConvos.add(convo);
        } else if (dateComp < 0 && comp < 0) {
            filteredConvos.add(convo);
        }
    }
    return filteredConvos;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Conversation(codeu.model.data.Conversation) HashSet(java.util.HashSet)

Example 33 with Conversation

use of codeu.model.data.Conversation 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 34 with Conversation

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

the class SearchServlet method doGet.

/**
 * This function fires when a user requests the /search URL. It finds the set of users that match
 * the given string and forwards that information to search.jsp
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    User loggedInUser = userStore.getUser((String) request.getSession().getAttribute("user"));
    String search = null;
    if (request.getParameter("searchuser") != null) {
        search = request.getParameter("searchuser");
        List<User> result = userStore.searchUsersSorted(search);
        request.setAttribute("users", result);
    } else if (request.getParameter("searchconvo") != null) {
        search = request.getParameter("searchconvo");
        List<Conversation> conversations = loggedInUser == null ? ConversationStore.sort(conversationStore.getAllPublicConversations()) : ConversationStore.sort(conversationStore.getAllPermittedConversations(loggedInUser.getId()));
        try {
            ConversationFilterer filterer = new ConversationFilterer(conversations, userStore.getUsers());
            List<Conversation> result = filterer.filterConversations(search);
            request.setAttribute("conversations", result);
        } catch (DateTimeException dte) {
            System.out.println(dte);
            request.setAttribute("conversations", new ArrayList<Conversation>());
        } catch (UnsupportedOperationException uoe) {
            System.out.println(uoe);
            request.setAttribute("conversations", new ArrayList<Conversation>());
        }
    } else if (request.getParameter("searchmessage") != null) {
        search = request.getParameter("searchmessage");
        String convoTitle = request.getParameter("searchbutton");
        Conversation convo = conversationStore.getConversationWithTitle(convoTitle);
        List<Message> messages = convo == null ? new ArrayList<Message>() : MessageStore.sort(messageStore.getMessagesInConversation(convo.getId()));
        try {
            MessageFilterer filterer = new MessageFilterer(messages, userStore.getUsers());
            List<Message> result = filterer.filterMessages(search);
            request.setAttribute("messages", result);
        } catch (DateTimeException dte) {
            System.out.println(dte);
            request.setAttribute("messages", new ArrayList<Message>());
        } catch (UnsupportedOperationException uoe) {
            System.out.println(uoe);
            request.setAttribute("messages", new ArrayList<Message>());
        }
    }
    request.getRequestDispatcher("/WEB-INF/view/search.jsp").forward(request, response);
}
Also used : ConversationFilterer(codeu.utils.ConversationFilterer) User(codeu.model.data.User) Message(codeu.model.data.Message) ArrayList(java.util.ArrayList) Conversation(codeu.model.data.Conversation) DateTimeException(java.time.DateTimeException) MessageFilterer(codeu.utils.MessageFilterer) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with Conversation

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

the class ChatServlet method hasSingleCommonConversation.

/**
 * Determines if two users have exactly one shared conversation that they have joined
 *
 * @return boolean
 * @param u1 first user
 * @param u2 second user
 */
private boolean hasSingleCommonConversation(UUID u1, UUID u2) {
    int count = 0;
    List<Conversation> conversations = conversationStore.getAllConversations();
    for (Conversation c : conversations) {
        if (c.getConversationUsers().contains(u1) && c.getConversationUsers().contains(u2)) {
            count++;
        }
    }
    return count == 1;
}
Also used : Conversation(codeu.model.data.Conversation)

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