/** * Create a comment from the given email. * * @param message * @param target * @throws MessagingException * @throws MailHandlerException * @throws IOException * @throws NoSuchAlgorithmException */ @Transactional public static Comment saveComment(IMAPMessage message, Resource target) throws MessagingException, MailHandlerException, IOException, NoSuchAlgorithmException { User author = IMAPMessageUtil.extractSender(message); if (!AccessControl.isProjectResourceCreatable(author, target.getProject(), target.getType())) { throw new PermissionDenied( cannotCreateMessage(author, target.getProject(), target.getType())); } Content parsedMessage = extractContent(message); Comment comment = makeNewComment(target, author, parsedMessage.body); comment.save(); Map<String, Attachment> relatedAttachments = saveAttachments(parsedMessage.attachments, comment.asResource()); if (new ContentType(parsedMessage.type).match(MimeType.HTML)) { // replace cid with attachments comment.contents = replaceCidWithAttachments(comment.contents, relatedAttachments); comment.update(); } new OriginalEmail(message.getMessageID(), comment.asResource()).save(); // Add the event addEvent(NotificationEvent.forNewComment(comment, author), message.getAllRecipients(), author); return comment; }
@Transactional protected static ReviewComment saveReviewComment( Resource target, User sender, Content content, String messageID, Address[] allRecipients) throws MessagingException, IOException, NoSuchAlgorithmException { ReviewComment comment; CommentThread thread = CommentThread.find.byId(Long.valueOf(target.getId())); if (thread == null) { throw new IllegalArgumentException(); } comment = new ReviewComment(); comment.setContents(content.body); comment.author = new UserIdent(sender); comment.thread = thread; comment.save(); Map<String, Attachment> relatedAttachments = saveAttachments(content.attachments, comment.asResource()); if (new ContentType(content.type).match(MimeType.HTML)) { // replace cid with attachments comment.setContents(replaceCidWithAttachments(comment.getContents(), relatedAttachments)); comment.update(); } new OriginalEmail(messageID, comment.asResource()).save(); // Add the event if (thread.isOnPullRequest()) { addEvent( NotificationEvent.forNewComment(sender, thread.pullRequest, comment), allRecipients, sender); } else { try { String commitId; if (thread instanceof CodeCommentThread) { commitId = ((CodeCommentThread) thread).commitId; } else if (thread instanceof NonRangedCodeCommentThread) { commitId = ((NonRangedCodeCommentThread) thread).commitId; } else { throw new IllegalArgumentException(); } addEvent( NotificationEvent.forNewCommitComment(target.getProject(), comment, commitId, sender), allRecipients, sender); } catch (Exception e) { Logger.warn("Failed to send a notification", e); } } return comment; }