Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Merge branch 'feature/message-id-for-email' of dlab/hive
Browse files Browse the repository at this point in the history
from pull-request 1322

* refs/heads/feature/message-id-for-email:
  NotificationMail: Add Message-ID for email thread

Reviewed-by: 채수원 <sw.chae@navercorp.com>
  • Loading branch information
doortts committed Sep 24, 2014
2 parents 28c134d + 87694d4 commit b5adeeb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
8 changes: 3 additions & 5 deletions app/models/CommitComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@

import models.enumeration.ResourceType;
import models.resource.Resource;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;

@Entity
public class CommitComment extends CodeComment {
Expand Down
5 changes: 5 additions & 0 deletions app/models/IssueComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public ResourceType getType() {
public Long getAuthorId() {
return authorId;
}

@Override
public Resource getContainer() {
return issue.asResource();
}
};
}

Expand Down
62 changes: 59 additions & 3 deletions app/models/NotificationMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import models.enumeration.UserState;
import models.resource.Resource;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.HtmlEmail;
import org.joda.time.DateTime;
import org.jsoup.Jsoup;
Expand All @@ -40,8 +39,12 @@
import scala.concurrent.duration.Duration;
import utils.Config;
import utils.Markdown;
import utils.RouteUtil;
import utils.Url;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
Expand Down Expand Up @@ -135,6 +138,59 @@ private void sendMail() {
);
}

/**
* An email which has Message-ID and/or References header based the given
* NotificationEvent if possible. The headers help MUA to bind the emails
* into a thread.
*/
public static class EventEmail extends HtmlEmail {
private NotificationEvent event;

public EventEmail(NotificationEvent event) {
this.event = event;
}

@Override
protected MimeMessage createMimeMessage(Session aSession) {
return new MimeMessage(aSession) {
@Override
protected void updateMessageID() throws MessagingException {
if (event != null && event.eventType.isCreating()) {
setHeader("Message-ID",
String.format("<%s@%s>",
event.getUrlToView(),
Config.getHostname()));
} else {
super.updateMessageID();
}
}
};
}

public void addReferences() {
if (event == null || event.resourceType == null ||
event.resourceId == null) {
return;
}

Resource resource = Resource.get(
event.resourceType, event.resourceId);

if (resource == null) {
return;
}

Resource container = resource.getContainer();

if (container != null) {
String reference = RouteUtil.getUrl(
container.getType(), container.getId());
addHeader("References",
"<" + reference + "@" + Config.getHostname() + ">");
}
}
}

/**
* Sends notification mails for the given event.
*
Expand Down Expand Up @@ -176,7 +232,7 @@ private static void sendNotification(NotificationEvent event) {
}

for (String langCode : usersByLang.keySet()) {
final HtmlEmail email = new HtmlEmail();
final EventEmail email = new EventEmail(event);

try {
email.setFrom(Config.getEmailFromSmtp(), event.getSender().name);
Expand All @@ -196,7 +252,7 @@ private static void sendNotification(NotificationEvent event) {
email.setHtmlMsg(getHtmlMessage(lang, message, urlToView, event.getResource()));
email.setTextMsg(getPlainMessage(lang, message, Url.create(urlToView)));
email.setCharset("utf-8");
email.addHeader("References", "<" + reference + "@" + Config.getHostname() + ">");
email.addReferences();
email.setSentDate(event.created);
Mailer.send(email);
String escapedTitle = email.getSubject().replace("\"", "\\\"");
Expand Down
5 changes: 5 additions & 0 deletions app/models/ReviewComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public Long getAuthorId() {
public void delete() {
ReviewComment.this.delete();
}

@Override
public Resource getContainer() {
return thread.asResource();
}
};
}

Expand Down
13 changes: 13 additions & 0 deletions app/models/enumeration/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@ public int compare(EventType o1, EventType o2) {
});
}

public boolean isCreating() {
switch(this) {
case NEW_ISSUE:
case NEW_POSTING:
case NEW_PULL_REQUEST:
case NEW_COMMENT:
case NEW_REVIEW_COMMENT:
case NEW_COMMIT:
return true;
default:
return false;
}
}
}

0 comments on commit b5adeeb

Please sign in to comment.