Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
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
15 changes: 14 additions & 1 deletion src/main/java/shef/data/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ public class User {
private String bio;
private String profilePageUrl;
private boolean isCurrentUser;
private boolean isFollowedByCurrentUser;
private long followingCount;
private long followersCount;

public User(String key, String email, String username, String location, String profilePicKey, String bio, boolean isCurrentUser) {

public User(String key, String email, String username, String location, String profilePicKey, String bio, boolean isCurrentUser, boolean isFollowedByCurrentUser, long followingCount, long followersCount) {
this.key = key;
this.email = email;
this.username = username;
this.location = location;
this.profilePicKey = profilePicKey;
this.bio = bio;
this.isCurrentUser = isCurrentUser;
this.isFollowedByCurrentUser = isFollowedByCurrentUser;
this.followingCount = followingCount;
this.followersCount = followersCount;
}

public User(String key, String username, String profilePicKey) {
this.key = key;
this.username = username;
this.profilePicKey = profilePicKey;
}
}
92 changes: 92 additions & 0 deletions src/main/java/shef/servlets/FollowersServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package shef.servlets;

import shef.data.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;

/** Servlet that retrieves follower information. */
@WebServlet("/followers")
public class FollowersServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserService userService = UserServiceFactory.getUserService();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// Get the key from the query string.
String userKeyString = request.getParameter("key");
Key userKey;
if(userKeyString == null && userService.isUserLoggedIn()) {
userKey = KeyFactory.createKey("User", userService.getCurrentUser().getUserId());
} else {
userKey = KeyFactory.stringToKey(userKeyString);
}

List<String> followers;
try {
Entity userEntity = datastore.get(userKey);
followers = (List<String>) userEntity.getProperty("followers");
} catch(EntityNotFoundException e) {
// User doesn't exist.
e.printStackTrace();
return;
}


LinkedList<User> followerUsers = new LinkedList<User>();

if(followers != null) {
for(String keyString : followers) {
try {
Key key = KeyFactory.stringToKey(keyString);
Entity userEntity = datastore.get(key);
String name = (String) userEntity.getProperty("username");
String profilePicKey = (String) userEntity.getProperty("profile-pic");

User user = new User(keyString, name, profilePicKey);
followerUsers.add(user);
} catch(EntityNotFoundException e) {
e.printStackTrace();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user's key isn't found (maybe the user's account was deleted or something), then this try-catch will completely interrupt the servlet and return no followers to the user. I think this behavior can be improved with a continue statement, so the problematic user is skipped, but the loop still gets all the other value users.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it also might be better to run a query on Datastore users as opposed to iterating through a loop of keys and getting user data (I think individual calls to separate APIs are costly).

return;
}
}
}

// Convert to JSON and send it as the response.
Gson gson = new Gson();

response.setContentType("application/json");
response.getWriter().println(gson.toJson(followerUsers));
}
}
150 changes: 150 additions & 0 deletions src/main/java/shef/servlets/FollowingServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package shef.servlets;

import shef.data.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;

/** Servlet that updates and retrieves following information. */
@WebServlet("/following")
public class FollowingServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserService userService = UserServiceFactory.getUserService();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// Get the key from the query string.
String userKeyString = request.getParameter("key");
Key userKey;
if(userKeyString == null && userService.isUserLoggedIn()) {
userKey = KeyFactory.createKey("User", userService.getCurrentUser().getUserId());
} else {
userKey = KeyFactory.stringToKey(userKeyString);
}

List<String> following;
try {
Entity userEntity = datastore.get(userKey);
following = (List<String>) userEntity.getProperty("following");
} catch(EntityNotFoundException e) {
// User doesn't exist.
e.printStackTrace();
return;
}

LinkedList<User> usersFollowed = new LinkedList<User>();

if(following != null) {
for(String keyString : following) {
try {
Key key = KeyFactory.stringToKey(keyString);
Entity userEntity = datastore.get(key);
String name = (String) userEntity.getProperty("username");
String profilePicKey = (String) userEntity.getProperty("profile-pic");

User user = new User(keyString, name, profilePicKey);
usersFollowed.add(user);
} catch(EntityNotFoundException e) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible, please auto-reformat before submitting. There are a lot of small formatting inconsistencies.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore.

e.printStackTrace();
return;
}
}
}

// Convert to JSON and send it as the response.
Gson gson = new Gson();

response.setContentType("application/json");
response.getWriter().println(gson.toJson(usersFollowed));
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserService userService = UserServiceFactory.getUserService();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// Retrieve the key of the user to follow.
String userToFollowKeyString = request.getParameter("user");
Key userToFollowKey = KeyFactory.stringToKey(userToFollowKeyString);
Entity userToFollow;

// Get the current user's key.
Key currentUserKey = KeyFactory.createKey("User", userService.getCurrentUser().getUserId());
String currentUserKeyString = KeyFactory.keyToString(currentUserKey);
Entity currentUser;

boolean unfollow = Boolean.parseBoolean(request.getParameter("unfollow"));

try {
// Retrieve the current user from Datastore and add to their following list.
currentUser = datastore.get(currentUserKey);
ArrayList<String> following = (ArrayList<String>) currentUser.getProperty("following");
// Retrieve the user to follow, and add the current user as a follow
userToFollow = datastore.get(userToFollowKey);
ArrayList<String> followers = (ArrayList<String>) userToFollow.getProperty("followers");

if(unfollow) {
if(following != null) {
following.remove(userToFollowKeyString);
}
if(followers != null) {
followers.remove(currentUserKeyString);
}
} else {
if(following == null) {
following = new ArrayList<String>();
}
if(followers == null) {
followers = new ArrayList<String>();
}
following.add(userToFollowKeyString);
followers.add(currentUserKeyString);
currentUser.setProperty("following", following);
userToFollow.setProperty("followers", followers);
}

} catch(EntityNotFoundException e) {
/** This means the current user, for whom we're trying to set following for, doesn't exist.
* This should never happen. If it does, multiple things have gone seriously wrong.
*/
e.printStackTrace();
return;
}

// Store the user in Datastore.
datastore.put(currentUser);
datastore.put(userToFollow);
response.sendRedirect("/profile-page.html?key=" + userToFollowKeyString);
}
}
60 changes: 0 additions & 60 deletions src/main/java/shef/servlets/TestDisplayRecipesServlet.java

This file was deleted.

17 changes: 16 additions & 1 deletion src/main/java/shef/servlets/UserServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,27 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
String location = (String) userEntity.getProperty("location");
String profilePicKey = (String) userEntity.getProperty("profile-pic");
String bio = (String) userEntity.getProperty("bio");
List<String> following = (List<String>) userEntity.getProperty("following");
List<String> followers = (List<String>) userEntity.getProperty("followers");

long followingCount = 0;
long followersCount = 0;
boolean isFollowedByCurrentUser = false;

if(following != null) {
followingCount = following.size();
}
if(followers != null) {
followersCount = followers.size();
String currentUserKeyString = KeyFactory.createKeyString("User", userService.getCurrentUser().getUserId());
isFollowedByCurrentUser = followers.contains(currentUserKeyString);
}

// Since we chose to store the id as a string in Datastore, it is referred to as "name".
String id = (String) userKey.getName();
boolean isCurrentUser = id.equals(userService.getCurrentUser().getUserId());

User user = new User(keyString, email, username, location, profilePicKey, bio, isCurrentUser);
User user = new User(keyString, email, username, location, profilePicKey, bio, isCurrentUser, isFollowedByCurrentUser, followingCount, followersCount);

// Convert to JSON and send it as the response.
Gson gson = new Gson();
Expand Down
Loading