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
This commit is contained in:
costantino.perciante 2016-02-17 17:27:46 +00:00
parent 01727dcfb3
commit ed484080f0
5 changed files with 170 additions and 26 deletions

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.applicationsupportlayer</groupId>
<artifactId>aslsocial</artifactId>
<version>0.15.0-SNAPSHOT</version>
<version>0.16.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Social Portal ASL Extension</name>
<description>

View File

@ -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: "
+ "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; border-left: 3px solid #ccc; font-style: italic;\">\""
@ -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);
}
/**

View File

@ -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
*

View File

@ -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<NotificationMail> BUFFER_EMAILS = new ArrayList<NotificationMail>();
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<Comment> comments,
String commentKey,
String ... hashtags) {
String removedMarkup = notification2Save.getDescription().replaceAll("&amp;", "&");
if (hashtags != null && hashtags.length > 0) {
_log.debug("editing hyperlinks for mail client");
@ -74,11 +91,76 @@ public class EmailPlugin {
body.append("<body>");
String htmlPost = "";
if (notification2Save.getType() == NotificationType.POST_ALERT ||
notification2Save.getType() == NotificationType.COMMENT ||
notification2Save.getType() == NotificationType.MENTION ||
notification2Save.getType() == NotificationType.OWN_COMMENT) {
body.append("<div>").append(WRITE_ABOVE_TO_REPLY).append("</div><br />");
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 = "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic; font-weight:bold\">"
+ feed.getFullName()
+ ": "
+ (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml)
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(feed.getTime()) + "</p>"
+"</div>";
else
htmlPost = "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic\">\""
+ feed.getFullName()
+ ": "
+ (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml)
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(feed.getTime()) + "</p>"
+"\"</div>";
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 += "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 100px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic; font-weight:bold\">\""
+ comments.get(i).getFullName()
+ ": "
+ commentTextNoHtml
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(comments.get(i).getTime()) + "</p>"
+"\"</div>";
break;
}
else
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 100px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic;\">\""
+ comments.get(i).getFullName()
+ ": "
+ commentTextNoHtml
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(comments.get(i).getTime()) + "</p>"
+"\"</div>";
}
}catch(Exception e){
_log.error("Unable to reconstruct the discussion to put into the email body.", e);
}
}
if (notification2Save.getType() == NotificationType.MESSAGE) {
body.append("<div>").append(WRITE_ABOVE_MESSAGE_REPLY).append("</div><br />");
@ -87,8 +169,10 @@ public class EmailPlugin {
body.append("<div style=\"color:#000; font-size:13px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif;\">")
.append("Dear ").append(userFirstName).append(",") //dear <user>
.append("<p>").append(sender).append(" ").append(removedMarkup) // has done something
.append(getActionLink(notification2Save, portalURL)).append("</p>") //Goto
.append("</div><br />")
.append(getActionLink(notification2Save, portalURL)) //Goto
.append(htmlPost)
.append("</p>")
.append("</div><br/>")
.append("<p><div style=\"color:#999999; font-size:11px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif;\">")
.append("This message was sent to <a href=\"mailto:")
.append(email)
@ -99,7 +183,7 @@ public class EmailPlugin {
.append(portalURL).append("/group/data-e-infrastructure-gateway/notifications\" style=\"color:#3b5998;text-decoration:none\" target=\"_blank\">unsubscribe</a>.")
.append("</div></p>")
.append("<p><div style=\"color:#999999; font-size:10px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; padding-top:15px;\">")
.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 <sender> and destroy and delete any copies you may have received.")
.append("</div></p>")
@ -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<Comment> 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 <user>
.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<Comment> 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("</a>");
actionLink.append("\">").append(" Open Post").append("</a>");
break;
case COMMENT:
actionLink.append("\">").append(" See the Post").append("</a>");
actionLink.append("\">").append(" Open Post").append("</a>");
break;
case MENTION:
actionLink.append("\">").append(" See the Post").append("</a>");
actionLink.append("\">").append(" Open Post").append("</a>");
break;
case MESSAGE:
actionLink.append("\">").append(" Go to Message").append("</a>");
@ -401,7 +538,7 @@ public class EmailPlugin {
actionLink.append("\">").append(" Go to Folder").append("</a>");
break;
case OWN_COMMENT:
actionLink.append("\">").append(" See the Post").append("</a>");
actionLink.append("\">").append(" Open Post").append("</a>");
break;
case POST_ALERT:
actionLink.append("\">").append("See this News").append("</a>");

View File

@ -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.