diff --git a/pom.xml b/pom.xml index 4cb3752..fed865f 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,12 @@ [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) provided + + org.gcube.applicationsupportlayer + aslsocial + [0.1.0-SNAPSHOT, 1.0.0-SNAPSHOT) + provided + org.gcube.core gcf diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/MentionNotificationsThread.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/MentionNotificationsThread.java new file mode 100644 index 0000000..cfede0c --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/MentionNotificationsThread.java @@ -0,0 +1,37 @@ +package org.gcube.portlets.user.shareupdates.server; + +import java.util.ArrayList; + +import org.gcube.applicationsupportlayer.social.NotificationsManager; +import org.gcube.common.core.utils.logging.GCUBEClientLog; + +/** + * + * @author Massimiliano Assante ISTI-CNR + * + */ +public class MentionNotificationsThread implements Runnable { + private static GCUBEClientLog _log = new GCUBEClientLog(MentionNotificationsThread.class); + + private String postText; + private String postId; + private NotificationsManager nm; + private ArrayList userIds; + + + public MentionNotificationsThread(String postId, String postText, NotificationsManager nm, ArrayList userIds) { + super(); + this.postId = postId; + this.postText = postText; + this.nm = nm; + this.userIds = userIds; + } + + @Override + public void run() { + for (String userIdToNotify : userIds) { + boolean result = nm.notifyUserTag(userIdToNotify, postId, postText); + _log.trace("Sending Notification for post mention to: " + userIdToNotify + " result?"+ result); + } + } +} diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java index 68db631..57fd935 100644 --- a/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java @@ -19,6 +19,8 @@ import javax.net.ssl.X509TrustManager; import org.apache.commons.validator.routines.UrlValidator; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; +import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager; +import org.gcube.applicationsupportlayer.social.NotificationsManager; import org.gcube.common.core.utils.logging.GCUBEClientLog; import org.gcube.portal.custom.communitymanager.OrganizationsUtil; import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; @@ -112,8 +114,8 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar if (user == null) { _log.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL"); user = "test.user"; -// user = "massimiliano.assante"; -// SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec/devVRE"); + user = "massimiliano.assante"; + SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec/devVRE"); withinPortal = false; } else { @@ -122,84 +124,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar System.out.println("SessionID = " + sessionID); return SessionManager.getInstance().getASLSession(sessionID, user); } - /** - * this method extractPeopleTags from the user post - * @param postText text with tagged people: txt .. extractPeopleTags(String postText) { - ArrayList toReturn = new ArrayList(); - String toParse = "" + postText + ""; - - HtmlCleaner cleaner = new HtmlCleaner(); - // parse the string HTML - TagNode pageData = cleaner.clean(toParse); - TagNode[] inputElements = pageData.getElementsByName("input", true); - if (inputElements != null) { - for (int i = 0; i < inputElements.length; i++) { - System.out.println("Found input " + inputElements[i].getAttributes().get("value")); - toReturn.add(inputElements[i].getAttributes().get("value")); - } - } else { - _log.trace("No person tags in this post"); - } - return toReturn; - } - /** - * remove all the html and leave the text - * @param html - * @return the text inside the html - */ - private static String html2text(String html) { - return Jsoup.parse(html).text().replace(" "," "); - } - - - /** - * this method is used when posting a feed - * - * It converts the tagged people etc etc - * - * @return a String ready to be posted - */ - private String transformPost(String postText) { - ArrayList taggedPeople = extractPeopleTags(postText); - if (taggedPeople == null || taggedPeople.size() == 0) { //there are no tagged people, remove html and go - String escapedFeedText = escapeHtml(postText); //here escape html to avoid xss attacks - String html = "" + escapedFeedText + ""; - return html2text(html); - } else { - _log.trace("postText curing: " + postText); - // this is needed to reconstruct the place of people tags, selfexplaining i think - int i = 0; - while (postText.contains(""; - String postWithPlaceHolders = html2text(html); - _log.trace("before cure: " + postWithPlaceHolders); - /* - * at this point you have the html removed and somthing like "text text text _usr_place_holder_[0] text text text _usr_place_holder_[1]" - * you need to replace _usr_place_holder_[i](s) with the people with same index in taggedPeople ArrayList - */ - String escapedFeedText = escapeHtml(postWithPlaceHolders); //here escape html to avoid xss attacks - ArrayList usernames = getSelectedUserIds(taggedPeople); - i = 0; - for (String tagged : taggedPeople) { - String username = (i < usernames.size()) ? usernames.get(i) : ""; - String taggedHTML = ""+tagged+" "; - escapedFeedText = escapedFeedText.replace("_usr_place_holder_["+i+"]", taggedHTML); - i++; - } - _log.trace("After cure: " + escapedFeedText); - return escapedFeedText; - } - } + /** * @@ -207,7 +132,8 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar public ClientFeed share(String postText, FeedType feedType, PrivacyLevel pLevel, String vreId, String linkTitle, String linkDesc, String url, String urlThumbnail, String host) { String escapedFeedText = transformPost(postText); - String username = getASLSession().getUsername(); + ASLSession session = getASLSession(); + String username = session.getUsername(); String email = username+"@isti.cnr.it"; String fullName = username+" FULL"; String thumbnailURL = "images/Avatar_default.png"; @@ -257,13 +183,103 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar } if (!result) return null; + //everything went fine ClientFeed cf = new ClientFeed(toShare.getKey(), toShare.getType().toString(), username, feedDate, toShare.getUri(), replaceAmpersand(toShare.getDescription()), fullName, email, thumbnailURL, toShare.getLinkTitle(), toShare.getLinkDescription(), toShare.getUriThumbnail(), toShare.getLinkHost()); + + //send the notification to the mentioned users + ArrayList mentionedUserIds = getSelectedUserIds(extractPeopleTags(postText)); + if (mentionedUserIds != null && mentionedUserIds.size() > 0) { + NotificationsManager nm = new ApplicationNotificationsManager(session); + Thread thread = new Thread(new MentionNotificationsThread(toShare.getKey(), postText, nm, mentionedUserIds)); + thread.start(); + } + return cf; } + + /** + * this method is used when posting a feed + * + * It converts the tagged people etc etc + * + * @return a String ready to be posted + */ + private String transformPost(String postText) { + ArrayList taggedPeople = extractPeopleTags(postText); + if (taggedPeople == null || taggedPeople.size() == 0) { //there are no tagged people, remove html and go + String escapedFeedText = escapeHtml(postText); //here escape html to avoid xss attacks + String html = "" + escapedFeedText + ""; + return html2text(html); + } else { + _log.trace("postText curing: " + postText); + // this is needed to reconstruct the place of people tags, selfexplaining i think + int i = 0; + while (postText.contains(""; + String postWithPlaceHolders = html2text(html); + _log.trace("before cure: " + postWithPlaceHolders); + /* + * at this point you have the html removed and somthing like "text text text _usr_place_holder_[0] text text text _usr_place_holder_[1]" + * you need to replace _usr_place_holder_[i](s) with the people with same index in taggedPeople ArrayList + */ + String escapedFeedText = escapeHtml(postWithPlaceHolders); //here escape html to avoid xss attacks + ArrayList usernames = getSelectedUserIds(taggedPeople); + i = 0; + for (String tagged : taggedPeople) { + String username = (i < usernames.size()) ? usernames.get(i) : ""; + String taggedHTML = ""+tagged+" "; + escapedFeedText = escapedFeedText.replace("_usr_place_holder_["+i+"]", taggedHTML); + i++; + } + _log.trace("After cure: " + escapedFeedText); + return escapedFeedText; + } + } + + /** + * this method extractPeopleTags from the user post + * @param postText text with tagged people: txt .. extractPeopleTags(String postText) { + ArrayList toReturn = new ArrayList(); + String toParse = "" + postText + ""; + HtmlCleaner cleaner = new HtmlCleaner(); + // parse the string HTML + TagNode pageData = cleaner.clean(toParse); + TagNode[] inputElements = pageData.getElementsByName("input", true); + if (inputElements != null) { + for (int i = 0; i < inputElements.length; i++) { + System.out.println("Found input " + inputElements[i].getAttributes().get("value")); + toReturn.add(inputElements[i].getAttributes().get("value")); + } + } else { + _log.trace("No person tags in this post"); + } + return toReturn; + } + /** + * remove all the html and leave the text + * @param html + * @return the text inside the html + */ + private static String html2text(String html) { + return Jsoup.parse(html).text().replace(" "," "); + } + + + private UserSettings getUserSettingsFromSession() { return (UserSettings) getASLSession().getAttribute(UserInfo.USER_INFO_ATTR); }