use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.
the class PersonalActivityServlet method doGet.
/**
* This function fires when a user navigates to the activity feed page. It gets a list of all
* current messages and forwards them to activityFeed.jsp.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
response.sendRedirect("/activityFeed");
return;
}
User user = userStore.getUser(username);
UUID userID = user.getId();
List<Conversation> conversations = sort(conversationStore.getAllConversations());
List<Activity> conversationActivities = new ArrayList<>();
// retrieve activities for the conversations a user has joined
for (Conversation conversation : conversations) {
if (conversation.getConversationUsers() != null) {
List<UUID> conversationUsers = conversation.getConversationUsers();
if (conversationUsers.contains(user.getId())) {
conversationActivities.add(activityStore.getActivityWithConversationID(conversation.getId()));
}
}
}
List<Activity> tailoredActivities = new ArrayList<>(conversationActivities);
// retrieve user activities
List<Activity> userActivities = activityStore.getActivitiesWithUserID(userID);
if (userActivities != null) {
tailoredActivities.addAll(userActivities);
}
// remove any duplicates
Set<Activity> hashSet = new HashSet<>(tailoredActivities);
tailoredActivities.clear();
tailoredActivities.addAll(hashSet);
List<Activity> privacyActivities = sort(activityStore.getActivitiesPerPrivacy(user, tailoredActivities));
request.setAttribute("activities", privacyActivities);
request.getRequestDispatcher("/WEB-INF/view/personalActivityFeed.jsp").forward(request, response);
}
use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.
the class RegisterServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());
String confirmPassword = request.getParameter("confirmPassword");
if (!username.matches("[\\w*\\s*]*")) {
request.setAttribute("error", "Please enter only letters, numbers, and spaces.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
if (password == null || password.length() < 8) {
request.setAttribute("error", "Please enter a password that is at least 8 characters.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
if (confirmPassword == null) {
request.setAttribute("error", "Please enter a confirmation password.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
if (password != null && !password.equals(confirmPassword)) {
request.setAttribute("error", "Your password and confirmation password do not match.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
if (userStore.isUserRegistered(username)) {
request.setAttribute("error", "That username is already taken.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
if (email.equals("")) {
request.setAttribute("error", "Please enter an email.");
request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
return;
}
User user = new User(UUID.randomUUID(), username, passwordHash, null, Instant.now(), email);
userStore.addUser(user);
String message = "created an account!";
UUID userId = user.getId();
UUID conversationId = new UUID(0L, 0L);
Activity activity = new Activity(UUID.randomUUID(), userId, conversationId, Instant.now(), "joinedApp", message, new ArrayList<UUID>(), true);
activityStore.addActivity(activity);
response.sendRedirect("/login");
}
use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.
the class PersistentDataStore method loadActivities.
/**
* Loads all Activity objects from the Datastore service and returns them in a List.
*
* @throws codeu.model.store.persistence.PersistentDataStoreException if an error was detected
* during the load from the Datastore service
*/
public List<Activity> loadActivities() throws PersistentDataStoreException {
List<Activity> activities = new ArrayList<>();
// Retrieve all activities from the datastore.
Query query = new Query("chat-activities");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
UUID memberId = UUID.fromString((String) entity.getProperty("member_id"));
UUID conversationId = UUID.fromString((String) entity.getProperty("conversation_id"));
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
String activityType = (String) entity.getProperty("activity_type");
String activityMessage = (String) entity.getProperty("activity_message");
List<String> strings = entity.getProperty("users") == null ? new ArrayList<String>() : (List<String>) entity.getProperty("users");
List<UUID> users = new ArrayList<UUID>();
for (String str : strings) {
users.add(UUID.fromString(str));
}
boolean isPublic = entity.getProperty("isPublic") == null ? true : ((String) entity.getProperty("isPublic")).equals("true");
Activity activity = new Activity(uuid, memberId, conversationId, creationTime, activityType, activityMessage, users, isPublic);
activities.add(activity);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return activities;
}
use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.
the class ConversationServletTest method testDoPost_StoreActivities.
@Test
public void testDoPost_StoreActivities() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("conversationTitle")).thenReturn("test_conversation");
Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
User fakeUser = new User(UUID.randomUUID(), "test_username", "password", "test biography", Instant.now(), "codeUChatTestEmail@gmail.com");
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
Mockito.when(mockConversationStore.isTitleTaken("test_conversation")).thenReturn(false);
conversationServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<Conversation> conversationArgumentCaptor = ArgumentCaptor.forClass(Conversation.class);
Mockito.verify(mockConversationStore).addConversation(conversationArgumentCaptor.capture());
Assert.assertEquals(conversationArgumentCaptor.getValue().getTitle(), "test_conversation");
ArgumentCaptor<Activity> activityArgumentCaptor = ArgumentCaptor.forClass(Activity.class);
Mockito.verify(mockActivityStore).addActivity(activityArgumentCaptor.capture());
Assert.assertEquals(activityArgumentCaptor.getValue().getActivityType(), "createdConvo");
Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
use of codeu.model.data.Activity in project codeu-2018-team12 by codeu-2018-team12.
the class PersonalActivityServletTest method testDoGet.
@Test
public void testDoGet() throws IOException, ServletException {
UUID fakeUserID = UUID.randomUUID();
UUID fakeConversationID = UUID.randomUUID();
User fakeUser = new User(fakeUserID, "test_username", "password", "test biography", Instant.now(), "test@gmail.com");
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
List<Activity> sampleActivities = new ArrayList<>();
sampleActivities.add(new Activity(UUID.randomUUID(), fakeUserID, fakeConversationID, Instant.ofEpochMilli(2000), "joinedApp", "testMessage", new ArrayList<UUID>(), true));
sampleActivities.add(new Activity(UUID.randomUUID(), fakeUserID, fakeConversationID, Instant.ofEpochMilli(1000), "createdConvo", "testMessage", new ArrayList<UUID>(), true));
List<Activity> privateSampleActivities = sort(mockActivityStore.getActivitiesPerPrivacy(fakeUser, sampleActivities));
List<Activity> sortedSampleActivities = sort(privateSampleActivities);
personalActivityServlet.doGet(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("activities", sortedSampleActivities);
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Aggregations