2014-05-19 19:07:18 +02:00
|
|
|
package org.gcube.applicationsupportlayer.social.mailing;
|
|
|
|
|
2016-05-16 19:18:23 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
2017-01-20 14:45:53 +01:00
|
|
|
import java.util.Set;
|
2016-05-16 19:18:23 +02:00
|
|
|
|
|
|
|
import javax.mail.Message;
|
|
|
|
import javax.mail.Multipart;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.MimeBodyPart;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.internet.MimeMultipart;
|
|
|
|
|
2024-01-18 17:52:46 +01:00
|
|
|
import org.gcube.social_networking.social_networking_client_library.LibClient;
|
|
|
|
import org.gcube.social_networking.socialnetworking.model.shared.Comment;
|
|
|
|
import org.gcube.social_networking.socialnetworking.model.shared.Post;
|
|
|
|
import org.gcube.social_networking.socialnetworking.model.shared.Notification;
|
|
|
|
import org.gcube.social_networking.socialnetworking.model.shared.NotificationType;
|
2017-01-18 12:37:27 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.UserManager;
|
2016-05-16 19:18:23 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2014-05-19 19:07:18 +02:00
|
|
|
/**
|
|
|
|
* The email cached object
|
|
|
|
* @author Massimiliano Assante, ISTI-CNR
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class NotificationMail {
|
2016-05-16 19:18:23 +02:00
|
|
|
private static final Logger _log = LoggerFactory.getLogger(NotificationMail.class);
|
|
|
|
|
2014-05-19 19:07:18 +02:00
|
|
|
private Notification notification2Send;
|
2017-01-18 12:37:27 +01:00
|
|
|
private UserManager userManager;
|
2014-05-19 19:07:18 +02:00
|
|
|
private String vreName;
|
|
|
|
private String portalName;
|
|
|
|
private String senderEmail;
|
2016-05-16 19:18:23 +02:00
|
|
|
private String portalURL;
|
|
|
|
private String siteLandingPagePath;
|
2014-11-03 19:31:09 +01:00
|
|
|
private String[] hashtags;
|
2017-01-20 14:45:53 +01:00
|
|
|
private Set<String> mentionedVReGroups;
|
2024-01-18 17:52:46 +01:00
|
|
|
|
|
|
|
private static LibClient libClient;
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
libClient = new LibClient();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//private static DatabookStore store = new DBCassandraAstyanaxImpl();
|
2016-05-16 19:18:23 +02:00
|
|
|
|
2017-01-20 14:45:53 +01:00
|
|
|
public NotificationMail(UserManager userManager, String portalURL, String siteLandingPagePath, Notification notification2Send, String vreName, String portalName, String senderEmail, Set<String> mentionedVReGroups, String ... hashtags) {
|
2014-05-19 19:07:18 +02:00
|
|
|
super();
|
2017-01-18 12:37:27 +01:00
|
|
|
this.userManager = userManager;
|
2016-05-16 19:18:23 +02:00
|
|
|
this.portalURL = portalURL;
|
|
|
|
this.siteLandingPagePath = siteLandingPagePath;
|
2014-05-19 19:07:18 +02:00
|
|
|
this.notification2Send = notification2Send;
|
|
|
|
this.vreName = vreName;
|
|
|
|
this.portalName = portalName;
|
|
|
|
this.senderEmail = senderEmail;
|
2014-11-03 19:31:09 +01:00
|
|
|
this.hashtags = hashtags;
|
2017-01-20 14:45:53 +01:00
|
|
|
this.mentionedVReGroups = mentionedVReGroups;
|
2014-05-19 19:07:18 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 19:18:23 +02:00
|
|
|
protected Message getMessageNotification(Session session) throws Exception {
|
|
|
|
|
|
|
|
GCubeUser user = null;
|
|
|
|
try {
|
2017-01-18 12:37:27 +01:00
|
|
|
user = userManager.getUserByUsername(notification2Send.getUserid());
|
2016-05-16 19:18:23 +02:00
|
|
|
} catch (Exception e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
_log.warn("While trying to get email for user/group: " + notification2Send.getUserid());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String email = user.getEmail();
|
|
|
|
|
|
|
|
Message msg2Return = new MimeMessage(session);
|
2022-05-05 14:10:17 +02:00
|
|
|
|
2016-05-16 19:18:23 +02:00
|
|
|
if (notification2Send.getType() == NotificationType.POST_ALERT ||
|
|
|
|
notification2Send.getType() == NotificationType.COMMENT ||
|
|
|
|
notification2Send.getType() == NotificationType.MENTION ||
|
|
|
|
notification2Send.getType() == NotificationType.OWN_COMMENT) {
|
|
|
|
String[] splits = senderEmail.split("@");
|
2018-10-08 12:34:59 +02:00
|
|
|
StringBuilder sb = new StringBuilder(splits[0]);
|
|
|
|
sb.append("+")
|
|
|
|
.append(notification2Send.getSubjectid())
|
|
|
|
.append("_")
|
|
|
|
.append(AppType.POST)
|
|
|
|
.append("@")
|
|
|
|
.append(splits[1]);
|
|
|
|
senderEmail = sb.toString();
|
2016-05-16 19:18:23 +02:00
|
|
|
}
|
|
|
|
if (notification2Send.getType() == NotificationType.MESSAGE) {
|
|
|
|
String[] splits = senderEmail.split("@");
|
|
|
|
senderEmail = splits[0] + "+" + notification2Send.getSubjectid() + "$" + AppType.MSG + "@" + splits[1];
|
2018-10-08 12:34:59 +02:00
|
|
|
StringBuilder sb = new StringBuilder(splits[0]);
|
|
|
|
sb.append("+")
|
|
|
|
.append(notification2Send.getSubjectid())
|
|
|
|
.append("_")
|
|
|
|
.append(AppType.MSG)
|
|
|
|
.append("@")
|
|
|
|
.append(splits[1]);
|
|
|
|
senderEmail = sb.toString();
|
2016-05-16 19:18:23 +02:00
|
|
|
}
|
|
|
|
// EMAIL SENDER
|
|
|
|
msg2Return.setHeader("Content-Type", "text/html; charset=UTF-8");
|
2018-06-25 16:01:04 +02:00
|
|
|
String senderEmailText = new StringBuilder(notification2Send.getSenderFullName()).append(" on ").append(portalName).toString();
|
|
|
|
msg2Return.setFrom(new InternetAddress(senderEmail, senderEmailText));
|
2016-05-16 19:18:23 +02:00
|
|
|
msg2Return.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
|
|
|
|
|
|
|
|
// retrieve post/comments from its id (if possible)
|
2024-01-18 17:52:46 +01:00
|
|
|
Post post = null;
|
2016-05-16 19:18:23 +02:00
|
|
|
List<Comment> comments = null;
|
|
|
|
String commentKey = null;
|
2024-01-18 17:52:46 +01:00
|
|
|
String vreNameFromPost = null;
|
2016-05-16 19:18:23 +02:00
|
|
|
|
2017-01-25 11:55:12 +01:00
|
|
|
if (notification2Send.getType() == NotificationType.COMMENT ||
|
|
|
|
notification2Send.getType() == NotificationType.OWN_COMMENT ||
|
|
|
|
notification2Send.getType() == NotificationType.LIKE ||
|
|
|
|
notification2Send.getType() == NotificationType.MENTION) {
|
2016-05-16 19:18:23 +02:00
|
|
|
try{
|
2024-01-18 17:52:46 +01:00
|
|
|
String postId = notification2Send.getSubjectid();
|
|
|
|
post = libClient.readPostLib(postId);
|
|
|
|
comments = libClient.getAllCommentsByPostIdLib(postId);
|
2016-05-16 19:18:23 +02:00
|
|
|
Collections.sort(comments); // sort them
|
|
|
|
commentKey = notification2Send.getCommentKey();
|
|
|
|
|
|
|
|
// try to set vreName when notification is created in infrastructure scope
|
2022-05-05 14:10:17 +02:00
|
|
|
String[] splittedVREName = post.getVreid().split("/");
|
2016-05-16 19:18:23 +02:00
|
|
|
if(vreName == null)
|
|
|
|
vreName = splittedVREName[splittedVREName.length - 1];
|
2017-01-25 11:55:12 +01:00
|
|
|
|
2024-01-18 17:52:46 +01:00
|
|
|
// if the notification is a comment, extract the vre name from the post and not from the scope
|
2017-01-25 11:55:12 +01:00
|
|
|
if(notification2Send.getType().equals(NotificationType.COMMENT) ||
|
|
|
|
notification2Send.getType().equals(NotificationType.OWN_COMMENT) ||
|
|
|
|
notification2Send.getType().equals(NotificationType.LIKE) ||
|
|
|
|
notification2Send.getType().equals(NotificationType.MENTION)
|
|
|
|
)
|
2024-01-18 17:52:46 +01:00
|
|
|
vreNameFromPost = splittedVREName[splittedVREName.length - 1];
|
2016-05-16 19:18:23 +02:00
|
|
|
|
|
|
|
} catch(Exception e){
|
2018-10-08 12:34:59 +02:00
|
|
|
_log.error("Unable to retrieve posts/comments", e);
|
2016-05-16 19:18:23 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-25 11:55:12 +01:00
|
|
|
|
2024-01-18 17:52:46 +01:00
|
|
|
String vreNameToUse = (vreNameFromPost == null) ? vreName : vreNameFromPost;
|
2017-01-25 11:55:12 +01:00
|
|
|
|
2016-06-16 11:08:37 +02:00
|
|
|
_log.debug("VRE Name for the email's subject is going to be "
|
2024-01-18 17:52:46 +01:00
|
|
|
+ vreNameToUse + "[vreNameFromPost is " + vreNameFromPost + ", vreName is " + vreName + "]");
|
2022-05-05 14:10:17 +02:00
|
|
|
try{
|
|
|
|
// set subject
|
|
|
|
msg2Return.setSubject(SocialMailingUtil.getSubjectByNotificationType(notification2Send, vreNameToUse, user.getFirstName(), mentionedVReGroups, hashtags));
|
|
|
|
_log.debug("message subject set");
|
|
|
|
final MimeBodyPart textPart = new MimeBodyPart();
|
|
|
|
textPart.setContent(SocialMailingUtil.getTextEmail(notification2Send, user.getFirstName(), portalURL, siteLandingPagePath, email, post, comments, commentKey, hashtags), "text/plain; charset=UTF-8");
|
|
|
|
final MimeBodyPart htmlPart = new MimeBodyPart();
|
|
|
|
htmlPart.setContent(SocialMailingUtil.getHTMLEmail(vreNameToUse, notification2Send, user.getFirstName(), portalURL, siteLandingPagePath, email, post, comments, commentKey, hashtags), "text/html; charset=UTF-8");
|
|
|
|
final Multipart mp = new MimeMultipart("alternative");
|
|
|
|
mp.addBodyPart(textPart);
|
|
|
|
mp.addBodyPart(htmlPart);
|
|
|
|
_log.debug("message added body parts");
|
|
|
|
// Set Multipart as the message's content
|
|
|
|
msg2Return.setContent(mp);
|
|
|
|
_log.debug("message setContent DONE");
|
|
|
|
msg2Return.setSentDate(new Date());
|
|
|
|
} catch(Exception e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-05-16 19:18:23 +02:00
|
|
|
return msg2Return;
|
|
|
|
}
|
|
|
|
|
2014-05-19 19:07:18 +02:00
|
|
|
protected Notification getNotification2Send() {
|
|
|
|
return notification2Send;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getVreName() {
|
|
|
|
return vreName;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getPortalName() {
|
|
|
|
return portalName;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getSenderEmail() {
|
|
|
|
return senderEmail;
|
|
|
|
}
|
2016-05-16 19:18:23 +02:00
|
|
|
|
2014-11-03 19:31:09 +01:00
|
|
|
protected String[] getHashtags() {
|
|
|
|
return hashtags;
|
|
|
|
}
|
2016-05-16 19:18:23 +02:00
|
|
|
|
|
|
|
public String getPortalURL() {
|
|
|
|
return portalURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSiteLandingPagePath() {
|
|
|
|
return siteLandingPagePath;
|
|
|
|
}
|
|
|
|
|
2017-01-20 14:45:53 +01:00
|
|
|
public Set<String> getMentionedVReGroups() {
|
|
|
|
return mentionedVReGroups;
|
|
|
|
}
|
|
|
|
|
2014-05-19 19:07:18 +02:00
|
|
|
}
|