From ed484080f00a0273ee2eaaa4cc2c0b29000ee5ba Mon Sep 17 00:00:00 2001 From: "costantino.perciante" Date: Wed, 17 Feb 2016 17:27:46 +0000 Subject: [PATCH] Added support to show discussions in email's body. Version updated to 0.16 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@124273 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 2 +- .../ApplicationNotificationsManager.java | 20 ++- .../social/NotificationsManager.java | 12 +- .../social/mailing/EmailPlugin.java | 161 ++++++++++++++++-- .../social/AppTest.java | 1 - 5 files changed, 170 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 5c9dffa..7ae8671 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.applicationsupportlayer aslsocial - 0.15.0-SNAPSHOT + 0.16.0-SNAPSHOT jar Social Portal ASL Extension diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java index ee91721..60ebacb 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java @@ -486,7 +486,7 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen * {@inheritDoc} */ @Override - public boolean notifyOwnCommentReply(String userIdToNotify, String feedid, String feedText) { + public boolean notifyOwnCommentReply(String userIdToNotify, String feedid, String feedText, String commentKey) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.OWN_COMMENT, @@ -501,14 +501,15 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen false, aslSession.getUsername(), aslSession.getUserFullName(), - aslSession.getUserAvatarId()); + aslSession.getUserAvatarId(), + commentKey); return saveNotification(not); } /** * {@inheritDoc} */ @Override - public boolean notifyCommentReply(String userIdToNotify, String feedid, String commentText, String feedOwnerFullName, String feedOwnerId) { + public boolean notifyCommentReply(String userIdToNotify, String feedid, String commentText, String feedOwnerFullName, String feedOwnerId, String commentKey) { String notificationText = (aslSession.getUsername().compareTo(feedOwnerId) == 0) ? "also commented on his post: " + "
\"" @@ -528,14 +529,15 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen false, aslSession.getUsername(), aslSession.getUserFullName(), - aslSession.getUserAvatarId()); + aslSession.getUserAvatarId(), + commentKey); return saveNotification(not); } /** * {@inheritDoc} */ @Override - public boolean notifyCommentOnFavorite(String userIdToNotify, String feedid, String commentText) { + public boolean notifyCommentOnFavorite(String userIdToNotify, String feedid, String commentText, String commentKey) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.COMMENT, @@ -549,14 +551,15 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen false, aslSession.getUsername(), aslSession.getUserFullName(), - aslSession.getUserAvatarId()); + aslSession.getUserAvatarId(), + commentKey); return saveNotification(not); } /** * {@inheritDoc} */ @Override - public boolean notifyUserTag(String userIdToNotify, String feedid, String feedText) { + public boolean notifyUserTag(String userIdToNotify, String feedid, String feedText, String commentKey) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.MENTION, @@ -569,7 +572,8 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen false, aslSession.getUsername(), aslSession.getUserFullName(), - aslSession.getUserAvatarId()); + aslSession.getUserAvatarId(), + commentKey); return saveNotification(not); } /** diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/NotificationsManager.java b/src/main/java/org/gcube/applicationsupportlayer/social/NotificationsManager.java index 8e14a41..42ebe0e 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/NotificationsManager.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/NotificationsManager.java @@ -182,9 +182,10 @@ public interface NotificationsManager { * @param userIdToNotify the user you want to notify * @param feedid the liked feedid * @param feedText the liked feed text or a portion of it + * @param commentKey when sending email, stop the shown discussion at that comment * @return true if the notification is correctly delivered, false otherwise */ - boolean notifyOwnCommentReply(String userIdToNotify, String feedid, String feedText); + boolean notifyOwnCommentReply(String userIdToNotify, String feedid, String feedText, String commentKey); /** * use to notify a user that commented on a post (Not his) that someone commented too * @@ -192,27 +193,30 @@ public interface NotificationsManager { * @param feedid the liked feedid * @param feedText the liked feed text or a portion of it * @param feedOwnerFullName the full name of the user who created this post + * @param commentKey when sending email, stop the shown discussion at that comment * @return true if the notification is correctly delivered, false otherwise */ - boolean notifyCommentReply(String userIdToNotify, String feedid, String feedText, String feedOwnerFullName, String feedOwnerId); + boolean notifyCommentReply(String userIdToNotify, String feedid, String feedText, String feedOwnerFullName, String feedOwnerId, String commentKey); /** * use to notify a user that someone commented on one of his favorite posts * * @param userIdToNotify the user you want to notify * @param feedid the liked feedid * @param commentText the commentText + * @param commentKey when sending email, stop the shown discussion at that comment * @return true if the notification is correctly delivered, false otherwise */ - boolean notifyCommentOnFavorite(String userIdToNotify, String feedid, String commentText); + boolean notifyCommentOnFavorite(String userIdToNotify, String feedid, String commentText, String commentKey); /** * use to notify a user that he was mentioned (tagged) on a post * * @param userIdToNotify the user you want to notify * @param feedid the liked feedid * @param feedText the liked feed text or a portion of it + * @param commentKey when sending email, stop the shown discussion at that comment * @return true if the notification is correctly delivered, false otherwise */ - boolean notifyUserTag(String userIdToNotify, String feedid, String commentText); + boolean notifyUserTag(String userIdToNotify, String feedid, String commentText, String commentKey); /** * use to notify a user he got one of his post liked * diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java index 545da37..10b8361 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java @@ -1,7 +1,11 @@ package org.gcube.applicationsupportlayer.social.mailing; +import java.text.Format; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; +import java.util.List; import javax.mail.Message; import javax.mail.Multipart; @@ -12,6 +16,10 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.gcube.portal.custom.communitymanager.OrganizationsUtil; +import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl; +import org.gcube.portal.databook.server.DatabookStore; +import org.gcube.portal.databook.shared.Comment; +import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.Notification; import org.gcube.portal.databook.shared.NotificationType; import org.jsoup.Jsoup; @@ -38,6 +46,7 @@ public class EmailPlugin { public static final String WRITE_ABOVE_MESSAGE_REPLY = "- Write ABOVE THIS LINE to reply via email"; public static final String WRITE_ABOVE_TO_REPLY = WRITE_ABOVE_MESSAGE_REPLY + ", reply with empty msg to subscribe -"; private static EmailPlugin singleton; + private static DatabookStore store = new DBCassandraAstyanaxImpl(); public static EmailPlugin getInstance() { if (singleton == null) @@ -51,7 +60,15 @@ public class EmailPlugin { protected static ArrayList BUFFER_EMAILS = new ArrayList(); - private static String getHTMLEmail(Notification notification2Save, String userFirstName, String portalURL, String email, String ... hashtags) { + private static String getHTMLEmail(Notification notification2Save, + String userFirstName, + String portalURL, + String email, + String vreName, + Feed feed, + List comments, + String commentKey, + String ... hashtags) { String removedMarkup = notification2Save.getDescription().replaceAll("&", "&"); if (hashtags != null && hashtags.length > 0) { _log.debug("editing hyperlinks for mail client"); @@ -74,11 +91,76 @@ public class EmailPlugin { body.append(""); + String htmlPost = ""; + if (notification2Save.getType() == NotificationType.POST_ALERT || notification2Save.getType() == NotificationType.COMMENT || notification2Save.getType() == NotificationType.MENTION || notification2Save.getType() == NotificationType.OWN_COMMENT) { body.append("
").append(WRITE_ABOVE_TO_REPLY).append("

"); + + try{ + + // data formatter + Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a"); + + // escape html + String feedTextNoHtml = convertHTML2Text(feed.getDescription()); + + // build up html post + comments + if(notification2Save.getType() == NotificationType.POST_ALERT || (comments.size() == 0 && notification2Save.getType() == NotificationType.MENTION)) + htmlPost = "
" + + feed.getFullName() + + ": " + + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) + + "

" + formatter.format(feed.getTime()) + "

" + +"
"; + else + htmlPost = "
\"" + + feed.getFullName() + + ": " + + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) + + "

" + formatter.format(feed.getTime()) + "

" + +"\"
"; + + for (int i = 0; i < comments.size(); i++) { + + String commentTextNoHtml = convertHTML2Text(comments.get(i).getText()); + + if((commentKey != null && comments.get(i).getKey().equals(commentKey)) && !(notification2Save.getType() == NotificationType.POST_ALERT)){ + htmlPost += "
\"" + + comments.get(i).getFullName() + + ": " + + commentTextNoHtml + + "

" + formatter.format(comments.get(i).getTime()) + "

" + +"\"
"; + + break; + } + else + htmlPost += "
\"" + + comments.get(i).getFullName() + + ": " + + commentTextNoHtml + + "

" + formatter.format(comments.get(i).getTime()) + "

" + +"\"
"; + } + + }catch(Exception e){ + _log.error("Unable to reconstruct the discussion to put into the email body.", e); + } } if (notification2Save.getType() == NotificationType.MESSAGE) { body.append("
").append(WRITE_ABOVE_MESSAGE_REPLY).append("

"); @@ -87,8 +169,10 @@ public class EmailPlugin { body.append("
") .append("Dear ").append(userFirstName).append(",") //dear .append("

").append(sender).append(" ").append(removedMarkup) // has done something - .append(getActionLink(notification2Save, portalURL)).append("

") //Goto - .append("

") + .append(getActionLink(notification2Save, portalURL)) //Goto + .append(htmlPost) + .append("

") + .append("

") .append("

") .append("This message was sent to unsubscribe.") .append("

") .append("

") - .append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain") + .append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain ") .append("information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, you are strictly prohibited from disclosing, distributing, copying, or in any way using this message.") .append("If you have received this communication in error, please notify the and destroy and delete any copies you may have received.") .append("

") @@ -109,7 +193,15 @@ public class EmailPlugin { } - private static String getTextEmail(Notification notification2Save, String userFirstName, String portalURL, String email, String[] hashtags) { + private static String getTextEmail( + Notification notification2Save, + String userFirstName, + String portalURL, + String email, + Feed feed, + List comments, + String commentKey, + String[] hashtags) { String removedMarkup = convertHTML2Text(notification2Save.getDescription()); @@ -130,11 +222,48 @@ public class EmailPlugin { StringBuilder body = new StringBuilder(); + String discussion = ""; if (notification2Save.getType() == NotificationType.POST_ALERT || notification2Save.getType() == NotificationType.COMMENT || notification2Save.getType() == NotificationType.MENTION || notification2Save.getType() == NotificationType.OWN_COMMENT) { body.append(WRITE_ABOVE_TO_REPLY).append("\n\n"); + + // build discussion + discussion += "\n\nDiscussion:\n"; + + // data formatter + Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a"); + + // escape html + String feedTextNoHtml = convertHTML2Text(feed.getDescription()); + + // build up post + comments + discussion = + "\t" + + "[" + formatter.format(feed.getTime()) + "] " + + feed.getFullName() + + ": " + + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) + + "\n"; + + + for (int i = 0; i < comments.size(); i++) { + + String commentTextNoHtml = convertHTML2Text(comments.get(i).getText()); + + discussion += + "\t\t\t" + + "[" + formatter.format(comments.get(i).getTime()) + "] " + + comments.get(i).getFullName() + + ": " + + commentTextNoHtml + + "\n"; + + if(commentKey != null && comments.get(i).getKey().equals(commentKey)) + break; + } + } if (notification2Save.getType() == NotificationType.MESSAGE) { body.append(WRITE_ABOVE_MESSAGE_REPLY).append("\n\n"); @@ -142,7 +271,8 @@ public class EmailPlugin { body.append("Dear ").append(userFirstName).append(",") //dear .append("\n").append(sender).append(" ").append(removedMarkup) // has done something - .append("\nsee: ").append(portalURL).append(notification2Save.getUri()) + .append("\nsee: ").append(portalURL).append(notification2Save.getUri()).append( " if you want to reply.") + .append(discussion) .append("\n\n\n----\n") .append("This message was sent to ") .append(email) @@ -256,11 +386,18 @@ public class EmailPlugin { msg2Return.setSubject(getSubjectByNotificationType(notification2Save, portalUrl, vreName, user.getFirstName(), hashtags)); + // retrieve post/comments from its id + String feedId = notification2Save.getSubjectid(); + Feed feed = store.readFeed(feedId); + List comments = store.getAllCommentByFeed(feedId); + Collections.sort(comments); // sort them + String commentKey = notification2Save.getCommentKey(); + final MimeBodyPart textPart = new MimeBodyPart(); - textPart.setContent(getTextEmail(notification2Save, user.getFirstName(), portalUrl, email, hashtags), "text/plain; charset=UTF-8"); + textPart.setContent(getTextEmail(notification2Save, user.getFirstName(), portalUrl, email, feed, comments, commentKey, hashtags), "text/plain; charset=UTF-8"); final MimeBodyPart htmlPart = new MimeBodyPart(); - htmlPart.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalUrl, email, hashtags), "text/html; charset=UTF-8"); + htmlPart.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalUrl, email, vreName, feed, comments, commentKey, hashtags), "text/html; charset=UTF-8"); final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); @@ -371,13 +508,13 @@ public class EmailPlugin { switch (notification2Save.getType()) { case LIKE: - actionLink.append("\">").append(" See the Post").append(""); + actionLink.append("\">").append(" Open Post").append(""); break; case COMMENT: - actionLink.append("\">").append(" See the Post").append(""); + actionLink.append("\">").append(" Open Post").append(""); break; case MENTION: - actionLink.append("\">").append(" See the Post").append(""); + actionLink.append("\">").append(" Open Post").append(""); break; case MESSAGE: actionLink.append("\">").append(" Go to Message").append(""); @@ -401,7 +538,7 @@ public class EmailPlugin { actionLink.append("\">").append(" Go to Folder").append(""); break; case OWN_COMMENT: - actionLink.append("\">").append(" See the Post").append(""); + actionLink.append("\">").append(" Open Post").append(""); break; case POST_ALERT: actionLink.append("\">").append("See this News").append(""); diff --git a/src/test/java/org/gcube/applicationsupportlayer/social/AppTest.java b/src/test/java/org/gcube/applicationsupportlayer/social/AppTest.java index 6ef4243..8a02727 100644 --- a/src/test/java/org/gcube/applicationsupportlayer/social/AppTest.java +++ b/src/test/java/org/gcube/applicationsupportlayer/social/AppTest.java @@ -6,7 +6,6 @@ import junit.framework.TestSuite; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; -import org.gcube.portal.databook.shared.NotificationType; /** * Unit test for simple App.