Search in sources :

Example 26 with User

use of codeu.model.data.User 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());
        PersistentStorageAgent.getInstance().writeThrough(message);
        messages.add(message);
    }
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation)

Example 27 with User

use of codeu.model.data.User 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);
        PersistentStorageAgent.getInstance().writeThrough(activity);
        activities.add(activity);
    }
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation)

Example 28 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class PersistentDataStore method loadUsers.

/**
 * Loads all User 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<User> loadUsers() throws PersistentDataStoreException {
    List<User> users = new ArrayList<>();
    // Retrieve all users from the datastore.
    Query query = new Query("chat-users");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            String userName = (String) entity.getProperty("username");
            String password = (String) entity.getProperty("password");
            String biography = (String) entity.getProperty("biography");
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            if (password != null && !password.startsWith("$2a$")) {
                password = BCrypt.hashpw(password, BCrypt.gensalt());
            }
            User user = new User(uuid, userName, password, biography, creationTime);
            users.add(user);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return users;
}
Also used : User(codeu.model.data.User) Instant(java.time.Instant) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 29 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class ConversationServlet method doPost.

/**
 * This function fires when a user submits the form on the conversations page. It gets the
 * logged-in username from the session and the new conversation title from the submitted form
 * data. It uses this to create a new Conversation object that it adds to the model.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them create a conversation
        response.sendRedirect("/conversations");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them create a conversation
        System.out.println("User not found: " + username);
        response.sendRedirect("/conversations");
        return;
    }
    String conversationTitle = request.getParameter("conversationTitle");
    if (conversationTitle.isEmpty()) {
        List<Conversation> conversations = conversationStore.getAllConversationsSorted();
        request.setAttribute("conversations", conversations);
        request.setAttribute("error", "Please specify a name for this chat.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    if (!conversationTitle.matches("[\\w*]*")) {
        request.setAttribute("error", "Please enter only letters and numbers.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    if (conversationStore.isTitleTaken(conversationTitle)) {
        // conversation title is already taken, just go into that conversation instead of creating a
        // new one
        response.sendRedirect("/chat/" + conversationTitle);
        return;
    }
    Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), conversationTitle, Instant.now());
    conversationStore.addConversation(conversation);
    String activityMessage = " created a new conversation: " + "<a href=\"/chat/" + conversationTitle + "\">" + conversationTitle + "</a>.";
    Activity activity = new Activity(UUID.randomUUID(), user.getId(), conversation.getId(), Instant.now(), "createdConvo", activityMessage);
    activityStore.addActivity(activity);
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation)

Example 30 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class ProfileServlet method doPost.

/**
 * This function fires when a user submits the form on the profile page.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String requestUrl = request.getRequestURI();
    String name = requestUrl.substring("/profile/".length());
    User user = userStore.getUser(name);
    user.setBio(request.getParameter("newBio"));
    response.sendRedirect(requestUrl);
}
Also used : User(codeu.model.data.User)

Aggregations

User (codeu.model.data.User)91 Test (org.junit.Test)60 Conversation (codeu.model.data.Conversation)36 Message (codeu.model.data.Message)23 UserStore (codeu.model.store.basic.UserStore)9 UUID (java.util.UUID)9 HttpSession (javax.servlet.http.HttpSession)8 Activity (codeu.model.data.Activity)7 Instant (java.time.Instant)7 ArrayList (java.util.ArrayList)7 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 Comparator (java.util.Comparator)2 ActivityStore (codeu.model.store.basic.ActivityStore)1 OutputSettings (org.jsoup.nodes.Document.OutputSettings)1