[Notice] Announcing the End of Demo Server [Read me]
doortts doortts 2018-08-31
mail: Add previous content in the notification mail
@3e8084d7e0f200ae9355600b62a34c04635c3c33
app/controllers/IssueApp.java
--- app/controllers/IssueApp.java
+++ app/controllers/IssueApp.java
@@ -37,6 +37,8 @@
 import java.io.IOException;
 import java.util.*;
 
+import static utils.JodaDateUtil.getOptionalShortDate;
+
 @AnonymousCheck
 public class IssueApp extends AbstractPostingApp {
     private static final String EXCEL_EXT = "xls";
@@ -897,6 +899,16 @@
         if(StringUtils.isNotEmpty(comment.parentCommentId)){
             comment.setParentComment(IssueComment.find.byId(Long.valueOf(comment.parentCommentId)));
         }
+
+        if(issue.comments.size() == 0) {
+            User user = User.find.byId(issue.authorId);
+            comment.previousContents = getPrevious("Original issue", issue.body, issue.updatedDate, user.loginId);
+        } else {
+            Comment previousComment = issue.comments.get(issue.comments.size() - 1);
+            User user = User.find.byId(previousComment.authorId);
+            comment.previousContents = getPrevious("Previous comment", previousComment.contents, previousComment.createdDate, user.loginId);
+        }
+
         Comment savedComment = saveComment(project, issue, comment);
 
         if( containsStateTransitionRequest() ){
@@ -912,6 +924,10 @@
         return redirect(RouteUtil.getUrl(savedComment));
     }
 
+    private static String getPrevious(String templateTitle, String contents, Date updatedDate, String authorLoginId) {
+        return "\n\n<br />\n\n--- " + templateTitle + " from @" + authorLoginId + "  " + getOptionalShortDate(updatedDate) + " ---\n\n<br />\n\n" + contents;
+    }
+
     // Just made for compatibility. No meanings.
     public static Result updateComment(String ownerName, String projectName, Long number, Long commentId) throws IOException {
         return newComment(ownerName, projectName, number);
app/models/Comment.java
--- app/models/Comment.java
+++ app/models/Comment.java
@@ -44,6 +44,9 @@
     @Transient
     public String parentCommentId;
 
+    @Transient
+    public String previousContents;
+
     public Comment() {
         createdDate = new Date();
     }
app/models/NotificationEvent.java
--- app/models/NotificationEvent.java
+++ app/models/NotificationEvent.java
@@ -142,11 +142,12 @@
                 }
             case NEW_ISSUE:
             case NEW_POSTING:
-            case NEW_COMMENT:
             case NEW_PULL_REQUEST:
             case NEW_COMMIT:
             case COMMENT_UPDATED:
                 return newValue;
+            case NEW_COMMENT:
+                return newValue + oldValue;
             case ISSUE_BODY_CHANGED:
             case POSTING_BODY_CHANGED:
                 return DiffUtil.getDiffText(oldValue, newValue);
@@ -240,7 +241,7 @@
             case POSTING_BODY_CHANGED:
                 return DiffUtil.getDiffPlainText(oldValue, newValue);
             default:
-                return getMessage(lang);
+                return getMessage(lang).replaceAll("\n\n<br />\n", "\n\n");
         }
     }
 
@@ -760,7 +761,7 @@
         notiEvent.title = formatReplyTitle(post);
         notiEvent.eventType = eventType;
         notiEvent.receivers = getMandatoryReceivers(comment, eventType);
-        notiEvent.oldValue = null;
+        notiEvent.oldValue = comment.previousContents;
         notiEvent.newValue = comment.contents;
         notiEvent.resourceType = comment.asResource().getType();
         notiEvent.resourceId = comment.asResource().getId();
app/utils/JodaDateUtil.java
--- app/utils/JodaDateUtil.java
+++ app/utils/JodaDateUtil.java
@@ -117,7 +117,6 @@
         return dateTime.toString("yyyyMMddHHmm", Locale.getDefault());
     }
 
-
     public static String geYMDDate(Date date){
         if (date == null) {
             return "";
@@ -125,4 +124,29 @@
         DateTime dateTime = new DateTime(date);
         return dateTime.toString("yyyy-MM-dd", Locale.getDefault());
     }
+
+    public static String getOptionalShortDate(Date date){
+        if (date == null) {
+            return "";
+        }
+        DateTime targetTime = new DateTime(date);
+        DateTime currentTime = new DateTime(new Date());
+
+        if(isSameYear(targetTime, currentTime)) {
+            if(isSameDay(targetTime, currentTime)) {
+                return targetTime.toString("'at' h:mm a", Locale.getDefault());
+            }
+            return targetTime.toString("MMM d 'at' h:mm a", Locale.getDefault());
+        } else {
+            return targetTime.toString("YY.MM.dd at h:mm a", Locale.getDefault());
+        }
+    }
+
+    private static boolean isSameYear(DateTime targetTime, DateTime currentTime) {
+        return currentTime.toString("YYYY").equals(targetTime.toString("YYYY"));
+    }
+
+    private static boolean isSameDay(DateTime targetTime, DateTime currentTime) {
+        return currentTime.toString("YYYYMMdd").equals(targetTime.toString("YYYYMMdd"));
+    }
 }
app/utils/Markdown.java
--- app/utils/Markdown.java
+++ app/utils/Markdown.java
@@ -46,7 +46,7 @@
                     .allowElements("input")
                     .allowAttributes("type", "disabled", "checked").onElements("input")
                     .toFactory())
-            .and(new HtmlPolicyBuilder().allowElements("pre").toFactory())
+            .and(new HtmlPolicyBuilder().allowElements("pre", "br", "hr").toFactory())
             .and(new HtmlPolicyBuilder()
                     .allowAttributes("class", "id", "style", "width", "height").globally().toFactory());
 
Add a comment
List