Skip to content
11 changes: 11 additions & 0 deletions src/main/java/com/google/codeu/data/Datastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,15 @@ public HashSet<User> notSeenBy(User user){
}
return notSeen;
}

/** Returns empty chat room lists of a user */
public ArrayList<String> openedChats(User user) {
// System.out.println("Openedchats");
user.addChats("uauaua");
for(String s : user.getOngoing()) {
System.out.println(s);
}
return user.getOngoing();
}
}

30 changes: 30 additions & 0 deletions src/main/java/com/google/codeu/data/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class User {
private String imgUrl;
private HashSet<String> likes;
private HashSet<String> notLikes;
private ArrayList<String> ongoingChats;
private HashSet<String> hasChatsWith;

public User(String email, String aboutMe, HashSet<String> likes, HashSet<String> notLikes, String name, String breed, String gender, String birthday, String weight, ArrayList<String> address, String imgUrl) {
this.email = email.trim();
Expand All @@ -31,6 +33,34 @@ public User(String email, String aboutMe, HashSet<String> likes, HashSet<String>
this.notLikes = notLikes;
}

public void addHasChats(String recipient) {
if(hasChatsWith == null) {
hasChatsWith = new HashSet<>();
}
hasChatsWith.add(recipient);
}

public void addChats(String recipient) {
if(hasChatsWith != null && hasChatsWith.contains(recipient)) {
return;
}
if(ongoingChats == null) {
ongoingChats = new ArrayList<String>();
}
// System.out.println("Add to chats" + recipient);

ongoingChats.add(recipient);
}

public ArrayList<String> getOngoing() {
if(ongoingChats == null) {
return new ArrayList<String>();
}
System.out.println("Get ongoing...");

return ongoingChats;
}

public String getEmail() {
return email;
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/google/codeu/servlets/ChatServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.codeu.data.Datastore;
import com.google.codeu.data.Message;
import com.google.gson.Gson;
import com.google.codeu.data.User;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -71,9 +72,20 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
boolean isDirectMessage = true;

Message message = new Message(user, userText, recipient, sentimentScore, isDirectMessage);


System.out.println("new message " + user);
datastore.storeMessage(message);

User recipientObject = datastore.getUser(recipient);
User userObject = datastore.getUser(user);
System.out.println("recipient " + recipient);

recipientObject.addHasChats(user);
userObject.addHasChats(recipient);

datastore.storeUser(recipientObject);
datastore.storeUser(userObject);

response.sendRedirect("/chat.html?user=" + recipient);
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/google/codeu/servlets/LikeServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,20 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
// yes means 2 users like each other, no means 1 of them does not like the other.
if (("like".equals(status)) && (datastore.isLiked(user, prospectiveMatch))) {
System.out.println("It's a match! We will start chatting...");
// Add current user to prospectiveMatch's ongoing chats

System.out.println("Add chats " + prospectiveMatch + " " + user);
prospectiveMatch.addChats(userEmail);
user.addChats(prospectiveEmail);

datastore.storeUser(prospectiveMatch);
datastore.storeUser(user);

System.out.println("Add " + userEmail + " to " + prospectiveMatch + prospectiveMatch.getName());

response.setContentType("text/html");
response.getWriter().print("yes");

} else {
response.setContentType("text/html");
response.getWriter().print("no");
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/google/codeu/servlets/OngoingServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.google.codeu.servlets;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.Sentiment;
import com.google.codeu.data.Datastore;
import com.google.codeu.data.Message;
import com.google.gson.Gson;
import com.google.codeu.data.User;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

/** Handles fetching and saving {@link Message} instances. */
@WebServlet("/ongoing")
public class OngoingServlet extends HttpServlet {

private Datastore datastore;

@Override
public void init() {
datastore = new Datastore();
}

/**
* Responds with a JSON representation of {@link Message} data for a specific user. Responds with
* an empty array if the user is not provided.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

// UserService userService = UserServiceFactory.getUserService();
// String sender = userService.getCurrentUser().getEmail();

response.setContentType("application/json");

String user = request.getParameter("user");
if (user == null || user.equals("")) {
// Request is invalid, return empty array
response.getWriter().println("[]");
return;
}

User userObject = datastore.getUser(user);
if(userObject == null) {
System.out.println("The user is not in the datastore");
} else {
System.out.println("Has user");
System.out.println(userObject + userObject.getName());
}

ArrayList<String> ongoingChats = datastore.openedChats(userObject);
// ArrayList<String> ongoingChats = userObject.getOngoing();

if(ongoingChats == null || ongoingChats.size() == 0) {
// System.out.println("no ongoing " + userObject.getName());
response.getWriter().println("[]");
return;
}

// System.out.println("Has ongoing " + userObject.getName());


Gson gson = new Gson();
String json = gson.toJson(ongoingChats);
System.out.println("Has ongoing json" + json);
response.getWriter().println(json);
}
}
4 changes: 3 additions & 1 deletion src/main/webapp/css/ImagePage.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ h2 {
display: block;
margin-left: auto;
margin-right: auto;
width: 100%;
width: 200px;
height: 200px;
overflow: hidden;
}

/* Button image styling */
Expand Down
40 changes: 40 additions & 0 deletions src/main/webapp/js/chat-list-loader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const urlParams = new URLSearchParams(window.location.search);
const parameterUsername = urlParams.get('user');

/** Fetches chats and add them to the page. **/
function fetchChats() {
const url = '/chat-list';
Expand All @@ -14,9 +17,25 @@ function fetchChats() {
}

chats.forEach((chat) => {
console.log(chat);
const chatDiv = buildChatDiv(chat);
chatContainer.appendChild(chatDiv);
})

// Build empty chat lists for matching users who haven't started chatting yet
const ongoingurl = '/ongoing?user=' + parameterUsername;
console.log("outside ongingurl fetching");
fetch(ongoingurl)
.then((response) => {
return response.json();
})
.then((users) => {
users.forEach((user) => {
console.log(user);
const chatDiv = buildEmptyChatDiv(user);
chatContainer.appendChild(chatDiv);
})
});
});
}

Expand All @@ -33,6 +52,27 @@ function checkLoggedIn() {
})
}

/** Build an element that displays an empty chatroom */
function buildEmptyChatDiv(user) {
// console.log("Inside empty div");
const headerDiv = document.createElement('div');
headerDiv.classList.add('chat-header');

const bodyDiv = document.createElement('div');
bodyDiv.classList.add('chat-body');
bodyDiv.innerHTML = '<p>Start chating now!</p>';

const chatDiv = document.createElement('div');
chatDiv.classList.add('chat-div');
chatDiv.appendChild(headerDiv);
chatDiv.appendChild(bodyDiv);

headerDiv.appendChild(document.createTextNode(user));
chatDiv.setAttribute('onclick', "location.href='/chat.html?user=" + user + "'");

return chatDiv;
}

/**
* Builds an element that displays the chat.
* @param {Chat} chat
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/js/chat-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function fetchMessages() {
} else {
messagesContainer.innerHTML = '';
}

messages.forEach((message) => {
const messageDiv = buildMessageDiv(message);
messagesContainer.appendChild(messageDiv);
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/js/navigation-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function addNavigation() {
createListItem(createLink('/image-page.html', 'Swipe')));

navigationElement.appendChild(
createListItem(createLink('/chat-list.html', 'Your Chats', 'chat-list-link')));
createListItem(createLink('/chat-list.html?user=' + loginStatus.username, 'Your Chats', 'chat-list-link')));

navigationElement.appendChild(
createListItem(createLink('/user-profile.html', 'Your Profile', 'user-profile-link')));
Expand Down
19 changes: 17 additions & 2 deletions src/main/webapp/js/swipe-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,25 @@ function postLike() {
return response.text();
})
.then((answer) => {
console.log("answer is: ", answer);
console.log("answer is: ", answer);
if (answer === "yes") {
// Hey Cindy, you can open chat page here.
console.log("Chat page should open!");

// A confirm window for opening the chatroom pops up
var startChat = confirm("Start a chat with " + targetEmail)
if(startChat === true) {
window.location.replace("chat.html?user=" + targetEmail);
} else {
fetch("/like?email=" + targetEmail + "&status=like", { method: "POST" })
.then((response) => {
return response.text();
})
.then((answer) => {
fetchProspect();
});
// fetchProspect();
}

} else {
console.log("No chat page!");
fetchProspect();
Expand Down
15 changes: 15 additions & 0 deletions src/main/webapp/js/user-page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ function showMessageFormIfLoggedIn() {
});
}

function showHasNewChat() {
const ongoingurl = '/ongoing?user=' + parameterUsername;
// console.log("outside ongingurl fetching");
fetch(ongoingurl)
.then((response) => {
return response.json();
})
.then((users) => {
if(users.length !== 0) {
alert("You have a new chat room opened!")
}
});
}

/** Fetches messages and add them to the page. */
function fetchMessages() {
const url = '/messages?user=' + parameterUsername;
Expand Down Expand Up @@ -137,6 +151,7 @@ function fetchImageUploadUrlAndShowForm() {
function buildUI() {
setPageTitle();
showMessageFormIfLoggedIn();
showHasNewChat();
fetchMessages();
const config = {removePlugins: [ 'ImageUpload' ]};
ClassicEditor.create(document.getElementById('message-input'), config);
Expand Down