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);
}
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;
}
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);
}
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);
}
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;
}
Aggregations