@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; }
/** * 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 public static Issue saveIssue( String subject, Project project, User sender, Content parsedMessage, String messageId, Address[] recipients) throws MessagingException, IOException, NoSuchAlgorithmException { Issue issue = new Issue(project, sender, subject, parsedMessage.body); issue.save(); NotificationEvent event = NotificationEvent.forNewIssue(issue, sender); Map<String, Attachment> relatedAttachments = saveAttachments(parsedMessage.attachments, issue.asResource()); if (new ContentType(parsedMessage.type).match(MimeType.HTML)) { // replace cid with attachments issue.body = replaceCidWithAttachments(issue.body, relatedAttachments); issue.update(); } new OriginalEmail(messageId, issue.asResource()).save(); // Add the event addEvent(event, recipients, sender); return issue; }
private static void addEvent(NotificationEvent event, Address[] recipients, User sender) { HashSet<User> emailUsers = new HashSet<>(); emailUsers.add(sender); for (Address addr : recipients) { emailUsers.add(User.findByEmail(((InternetAddress) addr).getAddress())); } event.receivers.removeAll(emailUsers); NotificationEvent.add(event); }