Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/main/java/codeu/controller/ChatServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,42 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
}

String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
String[] reqParams = requestUrl.split("/");
String conversationTitle = reqParams[2];

Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
response.sendRedirect("/conversations");
return;
if (reqParams.length > 3 && reqParams[3].equals("add_user")) {
System.out.println("ADD USER");
}

String messageContent = request.getParameter("message");
else if (reqParams.length > 3 && reqParams[3].equals("remove_user")) {
System.out.println("REMOVE USER");
}

// send button
else {
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);

if (conversation == null) {
// couldn't find conversation, redirect to conversation list
response.sendRedirect("/conversations");
return;
}

// this removes any HTML from the message content
String cleanedMessageContent = Jsoup.clean(messageContent, Whitelist.none());
String messageContent = request.getParameter("message");

Message message =
new Message(
UUID.randomUUID(),
conversation.getId(),
user.getId(),
cleanedMessageContent,
Instant.now());
// this removes any HTML from the message content
String cleanedMessageContent = Jsoup.clean(messageContent, Whitelist.none());

messageStore.addMessage(message);
Message message =
new Message(
UUID.randomUUID(),
conversation.getId(),
user.getId(),
cleanedMessageContent,
Instant.now());

messageStore.addMessage(message);
}

// redirect to a GET request
response.sendRedirect("/chat/" + conversationTitle);
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/codeu/controller/ConversationServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,21 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
return;
}

String recipient = request.getParameter("recipient");
if (!recipient.matches("[\\w*]*")) {
request.setAttribute("error", "Please enter only letters and numbers.");
request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
return;
}

if (!userStore.isUserRegistered(recipient)) {
request.setAttribute("error", "Invalid User.");
request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
return;
}

Conversation conversation =
new Conversation(UUID.randomUUID(), user.getId(), conversationTitle, Instant.now());
new Conversation(UUID.randomUUID(), user.getId(), userStore.getUser(recipient).getId(), conversationTitle, Instant.now());

conversationStore.addConversation(conversation);
response.sendRedirect("/chat/" + conversationTitle);
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/codeu/model/data/Conversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package codeu.model.data;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

/**
Expand All @@ -24,8 +25,10 @@
public class Conversation {
public final UUID id;
public final UUID owner;
public final UUID recipient;
public final Instant creation;
public final String title;
// public List<UUID> users;

/**
* Constructs a new Conversation.
Expand All @@ -35,11 +38,15 @@ public class Conversation {
* @param title the title of this Conversation
* @param creation the creation time of this Conversation
*/
public Conversation(UUID id, UUID owner, String title, Instant creation) {
public Conversation(UUID id, UUID owner, UUID recipient, String title, Instant creation) {
this.id = id;
this.owner = owner;
this.recipient = recipient;
this.creation = creation;
this.title = title;

// this.addUser(owner);
// this.addUser(recipient);
}

/** Returns the ID of this Conversation. */
Expand All @@ -52,6 +59,11 @@ public UUID getOwnerId() {
return owner;
}

/** Returns the ID of the User who created this Conversation. */
public UUID getRecipientId() {
return recipient;
}

/** Returns the title of this Conversation. */
public String getTitle() {
return title;
Expand All @@ -61,4 +73,32 @@ public String getTitle() {
public Instant getCreationTime() {
return creation;
}

/** Returns the creation time of this Conversation. */
// public List<UUID> getUsers() {
// return users;
// }
//
// public void addUser(UUID u) {
// users.add(u);
// }
//
// public void removeUser(UUID u) {
// users.remove(u);
// }

public boolean hasUser(UUID u) {

// return this.users.contains(u);

if (u.equals(owner)) {
return true;
}

if (u.equals(recipient)) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void addRandomConversations() {
User user = getRandomElement(users);
String title = "Conversation_" + i;
Conversation conversation =
new Conversation(UUID.randomUUID(), user.getId(), title, Instant.now());
new Conversation(UUID.randomUUID(), user.getId(), UUID.randomUUID(), title, Instant.now());
PersistentStorageAgent.getInstance().writeThrough(conversation);
conversations.add(conversation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public List<Conversation> loadConversations() throws PersistentDataStoreExceptio
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
UUID ownerUuid = UUID.fromString((String) entity.getProperty("owner_uuid"));
UUID recipientUuid = UUID.fromString((String) entity.getProperty("recipient_uuid"));
String title = (String) entity.getProperty("title");
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
Conversation conversation = new Conversation(uuid, ownerUuid, title, creationTime);
Conversation conversation = new Conversation(uuid, ownerUuid, recipientUuid, title, creationTime);
conversations.add(conversation);
} catch (Exception e) {
// In a production environment, errors should be very rare. Errors which may
Expand Down Expand Up @@ -146,7 +147,7 @@ public List<Message> loadMessages() throws PersistentDataStoreException {
return messages;
}

/** Write a User object to the Datastore service. */
/** Write a User object to the Datastore service (updates if it already exists). */
public void writeThrough(User user) {
Entity userEntity = new Entity("chat-users");
userEntity.setProperty("uuid", user.getId().toString());
Expand All @@ -156,7 +157,7 @@ public void writeThrough(User user) {
datastore.put(userEntity);
}

/** Write a Message object to the Datastore service. */
/** Write a Message object to the Datastore service (updates if it already exists). */
public void writeThrough(Message message) {
Entity messageEntity = new Entity("chat-messages");
messageEntity.setProperty("uuid", message.getId().toString());
Expand All @@ -167,11 +168,12 @@ public void writeThrough(Message message) {
datastore.put(messageEntity);
}

/** Write a Conversation object to the Datastore service. */
/** Write a Conversation object to the Datastore service (updates if it already exists). */
public void writeThrough(Conversation conversation) {
Entity conversationEntity = new Entity("chat-conversations");
conversationEntity.setProperty("uuid", conversation.getId().toString());
conversationEntity.setProperty("owner_uuid", conversation.getOwnerId().toString());
conversationEntity.setProperty("recipient_uuid", conversation.getRecipientId().toString());
conversationEntity.setProperty("title", conversation.getTitle());
conversationEntity.setProperty("creation_time", conversation.getCreationTime().toString());
datastore.put(conversationEntity);
Expand Down
33 changes: 32 additions & 1 deletion src/main/webapp/WEB-INF/view/chat.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<%@ page import="codeu.model.data.Conversation" %>
<%@ page import="codeu.model.data.Message" %>
<%@ page import="codeu.model.store.basic.UserStore" %>
<%@ page import="java.util.UUID" %>
<%
Conversation conversation = (Conversation) request.getAttribute("conversation");
List<Message> messages = (List<Message>) request.getAttribute("messages");
Expand Down Expand Up @@ -47,7 +48,7 @@ List<Message> messages = (List<Message>) request.getAttribute("messages");
<body onload="scrollChat()">

<nav>
<a id="navTitle" href="/">CodeU Chat App</a>
<a id="navTitle" href="/">CodeU2 Chat App</a>
<a href="/conversations">Conversations</a>
<% if (request.getSession().getAttribute("user") != null) { %>
<a>Hello <%= request.getSession().getAttribute("user") %>!</a>
Expand All @@ -64,6 +65,36 @@ List<Message> messages = (List<Message>) request.getAttribute("messages");

<hr/>


<% if (request.getSession().getAttribute("user") != null) { %>
<form action="/chat/<%= conversation.getTitle() %>/add_user" method="POST">
<button type="submit">Add User</button>
</form>
<% } else { %>
<p><a href="/login">Login</a> to add and remove users.</p>
<% } %>

<% if (request.getSession().getAttribute("user") != null) { %>
<form action="/chat/<%= conversation.getTitle() %>/remove_user" method="POST">
<button type="submit">Remove User</button>
</form>
<% } %>


<%
UUID ownerId = conversation.getOwnerId();
UUID recipientId = conversation.getRecipientId();
String owner = UserStore.getInstance().getUser(ownerId).getName();
String recipient = UserStore.getInstance().getUser(recipientId).getName();
%>

<p>From: <%= owner %></p>
<p>To: <%= recipient %></p>

<hr/>



<div id="chat">
<ul>
<%
Expand Down
50 changes: 41 additions & 9 deletions src/main/webapp/WEB-INF/view/conversations.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
--%>
<%@ page import="java.util.List" %>
<%@ page import="codeu.model.data.Conversation" %>
<%@ page import="codeu.model.data.User" %>
<%@ page import="codeu.model.store.basic.UserStore" %>
<%@ page import="java.util.UUID" %>

<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -45,11 +48,16 @@
<% if(request.getSession().getAttribute("user") != null){ %>
<h1>New Conversation</h1>
<form action="/conversations" method="POST">
<div class="form-group">
<label class="form-control-label">Title:</label>
<div class="form-group">
<label class="form-control-label">Title:</label>
<input type="text" name="conversationTitle">
</div>

<div class="form-group">
<label class="form-control-label">To:</label>
<input type="text" name="recipient">
</div>

<button type="submit">Create</button>
</form>

Expand All @@ -59,28 +67,52 @@
<h1>Conversations</h1>

<%
List<Conversation> conversations =
(List<Conversation>) request.getAttribute("conversations");
if(conversations == null || conversations.isEmpty()){
int numConvos = 0;
if (request.getSession().getAttribute("user") == null) {
%>

<p>Please <a href="/login">login</a> to view your conversations.</p>

<%
} else {
%>
<p>Create a conversation to get started.</p>

<%
}
else{
List<Conversation> conversations = (List<Conversation>) request.getAttribute("conversations");
if (conversations != null && !conversations.isEmpty()) {
%>
<ul class="mdl-list">
<%
String username = (String) request.getSession().getAttribute("user");
UUID userId = UserStore.getInstance().getUser(username).getId();

for(Conversation conversation : conversations){
if (conversation.hasUser(userId)) {
numConvos = numConvos + 1;
%>
<li><a href="/chat/<%= conversation.getTitle() %>">
<%= conversation.getTitle() %></a></li>
<%
}
}
%>
</ul>

<%
}
}
%>

<%
if (numConvos == 0) {
%>

<p>You are not a member of any conversations. Either create a new one or wait for a friend to message you.</p>

<%
}
}
%>

<hr/>
</div>
</body>
Expand Down
Loading