Search in sources :

Example 1 with CommentThread

use of org.shredzone.cilla.core.model.CommentThread in project cilla by shred.

the class CommentServiceImpl method createNew.

@Override
public Comment createNew(Commentable commentable) {
    CommentThread thread = commentable.getThread();
    Comment comment = new Comment();
    comment.setText(new FormattedText());
    comment.setCreation(new Date());
    comment.setPublished(false);
    comment.setCreator(userDao.fetch(securityService.getAuthenticatedUser().getUserId()));
    comment.setThread(thread);
    thread.getComments().add(comment);
    return comment;
}
Also used : Comment(org.shredzone.cilla.core.model.Comment) CommentThread(org.shredzone.cilla.core.model.CommentThread) FormattedText(org.shredzone.cilla.core.model.embed.FormattedText) Date(java.util.Date)

Example 2 with CommentThread

use of org.shredzone.cilla.core.model.CommentThread in project cilla by shred.

the class CommentFormHandlerImpl method handleDelete.

/**
 * Handles a delete request.
 *
 * @param commentable
 *            {@link Commentable} the comment belongs to
 * @param req
 *            {@link HttpServletRequest} with the form data
 */
private void handleDelete(Commentable commentable, HttpServletRequest req) {
    String delId = req.getParameter("cmtDelete");
    if (delId != null) {
        long deleteId = getReplyToId(delId);
        Comment deleteComment = commentDao.fetch(deleteId);
        CommentThread thread = commentable.getThread();
        if (deleteComment != null && deleteComment.getThread().equals(thread)) {
            commentService.remove(deleteComment);
            commentThreadService.evict(commentable);
        }
    }
}
Also used : Comment(org.shredzone.cilla.core.model.Comment) CommentThread(org.shredzone.cilla.core.model.CommentThread)

Example 3 with CommentThread

use of org.shredzone.cilla.core.model.CommentThread in project cilla by shred.

the class CommentFormHandlerImpl method handleComment.

@Override
public void handleComment(Commentable commentable, HttpServletRequest req, boolean enabled) {
    handleDelete(commentable, req);
    CommentThread thread = commentable.getThread();
    if (req.getParameter("cmtPosted") == null) {
        // There is no comment posting to be handled
        return;
    }
    if (!(thread.isCommentable() && enabled)) {
        // The thread does not accept new comments
        log.info("Trying to comment thread ID {} which does not allow comments.", thread.getId());
        return;
    }
    String commentText = getMultiline(req.getParameter("cmtComment"));
    if (commentText.isEmpty()) {
        // There was no comment text
        rejectComment(req, "cilla.comment.error.empty");
        return;
    }
    if (commentText.length() > maxCommentLength) {
        rejectComment(req, "cilla.comment.error.toolong");
        return;
    }
    Comment comment = new Comment();
    CillaUserDetails cud = securityService.getAuthenticatedUser();
    if (cud != null) {
        User user = userDao.fetch(cud.getUserId());
        comment.setCreator(user);
        comment.setMail(user.getMail());
        comment.setName(user.getName());
    } else {
        String name = getSimple(req.getParameter("cmtName"));
        String mail = getSimple(req.getParameter("cmtMail"));
        String url = getSimple(req.getParameter("cmtUrl"));
        if (userDao.fetchByLogin(name) != null) {
            rejectComment(req, "cilla.comment.error.badname");
            return;
        }
        comment.setMail(mail);
        comment.setName(name);
        comment.setUrl(url);
    }
    int posX = getReplyToInteger(req.getParameter("x"));
    int posY = getReplyToInteger(req.getParameter("y"));
    if (!captchaService.isValidCaptcha(req.getSession(), posX, posY)) {
        // Wrong captcha given, do not accept the comment.
        log.info("A wrong captcha was given for thread ID {}", thread.getId());
        rejectComment(req, "cilla.comment.error.badcaptcha");
        return;
    }
    long replyToId = getReplyToId(req.getParameter("cmtReplyTo"));
    Comment replyComment = commentDao.fetch(replyToId);
    if (replyComment != null && !replyComment.getThread().equals(thread)) {
        // The Comment belongs to another thread, so the replyToId was
        // probably forged. We will silently ignore the replyTo comment.
        log.info("Ignored forged replyToId {} for thread ID {}", replyToId, thread.getId());
        replyComment = null;
    }
    // Comment looks good...
    comment.setThread(thread);
    comment.setReplyTo(replyComment);
    comment.setText(new FormattedText(commentText, TextFormat.PLAIN));
    commentService.postComment(comment, req);
    commentThreadService.evict(commentable);
    if (comment.isPublished()) {
        setMessage(req, "cilla.comment.ispublished");
    } else {
        setMessage(req, "cilla.comment.ismoderated");
    }
}
Also used : Comment(org.shredzone.cilla.core.model.Comment) User(org.shredzone.cilla.core.model.User) CommentThread(org.shredzone.cilla.core.model.CommentThread) CillaUserDetails(org.shredzone.cilla.service.security.CillaUserDetails) FormattedText(org.shredzone.cilla.core.model.embed.FormattedText)

Aggregations

Comment (org.shredzone.cilla.core.model.Comment)3 CommentThread (org.shredzone.cilla.core.model.CommentThread)3 FormattedText (org.shredzone.cilla.core.model.embed.FormattedText)2 Date (java.util.Date)1 User (org.shredzone.cilla.core.model.User)1 CillaUserDetails (org.shredzone.cilla.service.security.CillaUserDetails)1