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