Search in sources :

Example 1 with ImageStorage

use of codeu.utils.ImageStorage 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();
    Part image = request.getPart("image");
    String biography = request.getParameter("submitBiography");
    String name = requestUrl.substring("/profile/".length());
    User user = userStore.getUser(name);
    if (biography != null) {
        if (request.getParameter("newBio").isEmpty()) {
            User loggedInUser = userStore.getUser((String) request.getSession().getAttribute("user"));
            List<Activity> activities = null;
            List<Activity> activitiesPermitted;
            if (user != null) {
                activitiesPermitted = loggedInUser == null ? sort(activityStore.getAllPublicActivitiesWithUserId(user.getId())) : sort(activityStore.getAllPermittedActivitiesWithUserId(user.getId(), loggedInUser.getId()));
                activities = activityStore.getActivitiesPerPrivacy(user, activitiesPermitted);
            }
            request.setAttribute("error", "Please specify a biography.");
            request.setAttribute("activities", activities);
            request.setAttribute("loggedInUser", loggedInUser);
            request.setAttribute("user", user);
            request.getRequestDispatcher("/WEB-INF/view/profile.jsp").forward(request, response);
            return;
        }
        user.setBio(request.getParameter("newBio"));
    }
    if (image != null) {
        ImageStorage imageStorage = new ImageStorage();
        String imageName = imageStorage.storeImage(image);
        user.setProfilePicture(imageName);
    }
    response.sendRedirect(requestUrl);
}
Also used : User(codeu.model.data.User) ImageStorage(codeu.utils.ImageStorage) Part(javax.servlet.http.Part) Activity(codeu.model.data.Activity)

Example 2 with ImageStorage

use of codeu.utils.ImageStorage in project codeu-2018-team12 by codeu-2018-team12.

the class DirectMessageServlet method doPost.

/**
 * This function fires when a user submits the form on the chat page. It gets the logged-in
 * username from the session, the conversation title from the URL, and the chat message from the
 * submitted form data. It creates a new Message from that data, adds it to the model, and then
 * redirects back to the chat page.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Part image = request.getPart("image");
    String submitText = request.getParameter("submitText");
    String requestUrl = request.getRequestURI();
    String loggedInUsername = (String) request.getSession().getAttribute("user");
    String otherUsername = requestUrl.substring("/direct/".length());
    User loggedInUser = userStore.getUser(loggedInUsername);
    User otherUser = userStore.getUser(otherUsername);
    if (loggedInUser == null) {
        // user was not found, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    if (otherUser == null || loggedInUser.getId().equals(otherUser.getId())) {
        response.sendRedirect("/conversations");
        return;
    }
    String convoName = loggedInUsername.compareTo(otherUsername) < 0 ? "direct:" + loggedInUsername + "-" + otherUsername : "direct:" + otherUsername + "-" + loggedInUsername;
    Conversation conversation = conversationStore.getConversationWithTitle(convoName);
    if (conversation == null) {
        // couldn't find conversation, redirect to conversation list
        response.sendRedirect("/conversations");
        return;
    }
    String conversationTitle = requestUrl.substring("/chat/".length());
    if (submitText != null && conversation.getConversationUsers().contains(loggedInUser.getId())) {
        String messageContent = request.getParameter("message");
        if (messageContent.isEmpty()) {
            request.setAttribute("error", "Message body cannot be empty.");
            request.setAttribute("conversationUsers", conversation.getConversationUsers());
            request.setAttribute("conversation", conversation);
            request.setAttribute("messages", messageStore.getMessagesInConversation(conversation.getId()));
            request.setAttribute("loggedInUser", loggedInUser);
            request.setAttribute("otherUser", otherUser);
            request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
            return;
        }
        if (messageContent.isEmpty()) {
            request.setAttribute("error", "Message body cannot be empty.");
            request.setAttribute("conversationUsers", conversation.getConversationUsers());
            request.setAttribute("conversation", conversation);
            request.setAttribute("messages", messageStore.getMessagesInConversation(conversation.getId()));
            request.setAttribute("loggedInUser", loggedInUser);
            request.setAttribute("otherUser", otherUser);
            request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
            return;
        }
        // this removes any HTML from the message content
        String cleanedMessageContent = Jsoup.clean(messageContent, "", Whitelist.none(), new OutputSettings().prettyPrint(false));
        String finalMessageContent = TextFormatter.formatForDisplay(cleanedMessageContent);
        createMessage(request, cleanedMessageContent, loggedInUser, conversation, false);
    } else if (image != null) {
        ImageStorage imageStorage = new ImageStorage();
        String imageName = imageStorage.storeImage(image);
        createMessage(request, imageName, loggedInUser, conversation, true);
    }
    // redirect to a GET request
    response.sendRedirect("/direct/" + otherUser.getName());
}
Also used : User(codeu.model.data.User) OutputSettings(org.jsoup.nodes.Document.OutputSettings) ImageStorage(codeu.utils.ImageStorage) Part(javax.servlet.http.Part) Conversation(codeu.model.data.Conversation)

Example 3 with ImageStorage

use of codeu.utils.ImageStorage in project codeu-2018-team12 by codeu-2018-team12.

the class ChatServlet method doPost.

/**
 * This function fires when a user submits the form on the chat page. It gets the logged-in
 * username from the session, the conversation title from the URL, and the chat message from the
 * submitted form data. It creates a new Message from that data, adds it to the model, and then
 * redirects back to the chat page.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Part image = request.getPart("image");
    String button = request.getParameter("button");
    String submitText = request.getParameter("submitText");
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    String requestUrl = request.getRequestURI();
    String conversationTitle = requestUrl.substring("/chat/".length());
    Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
    if (conversation == null) {
        // couldn't find conversation, redirect to conversation list
        response.sendRedirect("/conversations");
        return;
    }
    if ("joinButton".equals(button)) {
        joinConversation(user, conversation);
    } else if ("leaveButton".equals(button)) {
        leaveConversation(user, conversation);
    } else if (submitText != null && conversation.getConversationUsers().contains(user.getId())) {
        String messageContent = request.getParameter("message");
        if (messageContent.isEmpty()) {
            request.setAttribute("error", "Message body cannot be empty.");
            request.setAttribute("conversation", conversation);
            request.setAttribute("messages", messageStore.getMessagesInConversation(conversation.getId()));
            request.setAttribute("conversationUsers", conversation.getConversationUsers());
            request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
            return;
        }
        // this removes any HTML from the message content
        String cleanedMessageContent = Jsoup.clean(messageContent, "", Whitelist.none(), new OutputSettings().prettyPrint(false));
        String finalMessageContent = TextFormatter.formatForDisplay(cleanedMessageContent);
        createMessage(request, finalMessageContent, user, conversation, false);
    } else if (image != null && conversation.getConversationUsers().contains(user.getId())) {
        ImageStorage imageStorage = new ImageStorage();
        String imageName = imageStorage.storeImage(image);
        createMessage(request, imageName, user, conversation, true);
    }
    // redirect to a GET request
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) OutputSettings(org.jsoup.nodes.Document.OutputSettings) ImageStorage(codeu.utils.ImageStorage) Conversation(codeu.model.data.Conversation)

Aggregations

User (codeu.model.data.User)3 ImageStorage (codeu.utils.ImageStorage)3 Conversation (codeu.model.data.Conversation)2 Part (javax.servlet.http.Part)2 OutputSettings (org.jsoup.nodes.Document.OutputSettings)2 Activity (codeu.model.data.Activity)1