diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 8cf3658..b6fda02 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,9 @@ + + uses + diff --git a/pom.xml b/pom.xml index 03c2e01..90a594f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portal social-networking-library-ws war - 2.2.4-SNAPSHOT + 2.3.0-SNAPSHOT social-networking-library-ws Rest interface for the social networking library. @@ -24,7 +24,6 @@ distro UTF-8 UTF-8 - 1.8 @@ -190,16 +189,6 @@ social-data-search-client [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) compile - - - - - - - - - - @@ -259,13 +248,6 @@ jersey-bean-validation ${version.jersey} - - - - - - - io.swagger swagger-jersey2-jaxrs @@ -306,7 +288,7 @@ org.gcube.applicationsupportlayer aslsocial - [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + [1.7.1-SNAPSHOT,2.0.0-SNAPSHOT) compile diff --git a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java index 8cccf06..257fa1b 100644 --- a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java +++ b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java @@ -198,7 +198,7 @@ public class Posts { Status status = Status.CREATED; // parse - String feedText = post.getText(); + String postText = post.getText(); String previewTitle = post.getPreviewtitle(); String previewDescription = post.getPreviewdescription(); String previewHost = post.getPreviewhost(); @@ -217,7 +217,7 @@ public class Posts { logger.debug("Trying to share user feed..."); Feed res = SocialUtils.shareUserUpdate( username, - feedText, + postText, context, previewTitle, previewDescription, @@ -228,7 +228,7 @@ public class Posts { ); if(res != null){ - logger.debug("Feed correctly written by user " + username); + logger.debug("Post correctly written by user " + username); responseBean.setResult(res); responseBean.setSuccess(true); return Response.status(status).entity(responseBean).build(); diff --git a/src/main/java/org/gcube/portal/social/networking/ws/utils/SocialUtils.java b/src/main/java/org/gcube/portal/social/networking/ws/utils/SocialUtils.java index 74f02aa..ee4cff3 100644 --- a/src/main/java/org/gcube/portal/social/networking/ws/utils/SocialUtils.java +++ b/src/main/java/org/gcube/portal/social/networking/ws/utils/SocialUtils.java @@ -3,9 +3,12 @@ package org.gcube.portal.social.networking.ws.utils; import static org.gcube.resources.discovery.icclient.ICFactory.client; import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.UUID; import javax.xml.parsers.DocumentBuilder; @@ -25,13 +28,17 @@ import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.FeedType; import org.gcube.portal.databook.shared.PrivacyLevel; import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException; +import org.gcube.portal.notifications.bean.GenericItemBean; +import org.gcube.portal.notifications.thread.MentionNotificationsThread; import org.gcube.portal.notifications.thread.PostNotificationsThread; import org.gcube.portal.social.networking.caches.SocialNetworkingSiteFinder; import org.gcube.portal.social.networking.liferay.ws.GroupManagerWSBuilder; import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder; +import org.gcube.portlets.widgets.pickitem.shared.ItemBean; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.impl.QueryBox; +import org.gcube.social_networking.socialutillibrary.Utils; import org.gcube.socialnetworking.socialtoken.SocialMessageParser; import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.UserManager; @@ -54,6 +61,37 @@ public class SocialUtils { // name of the portlet for vre notification public static final String NEWS_FEED_PORTLET_CLASSNAME = "org.gcube.portlets.user.newsfeed.server.NewsServiceImpl"; + /** + * + * @param mentions the set of string containing the usernames + * @return a list of existing usernames associated with their fullnames + */ + private static ArrayList getUsersFromUsernames(Set mentions) { + if (mentions.isEmpty()) + return new ArrayList<>(); + ArrayList toReturn = new ArrayList<>(); + UserManager uManager = UserManagerWSBuilder.getInstance().getUserManager(); + for (String username : mentions) { + try{ + GCubeUser user = uManager.getUserByUsername(username); + String fullName = user.getFirstName() + " " + user.getLastName(); + toReturn.add(new GenericItemBean(""+user.getUserId(), username, fullName, "")); + }catch(Exception e){ + logger.error("Unable to get user informations for username=" + username); + } + } + return toReturn; + } + + // utility method + private static ArrayList convertToItemBean(Collection items) { + ArrayList toReturn = new ArrayList<>(items.size()); + for (GenericItemBean item : items) { + toReturn.add(new ItemBean(item.getId(), item.getName(), item.getAlternativeName(), item.getThumbnailURL())); + } + return toReturn; + } + /** * Method used when an application needs to publish something. * @param feedText @@ -74,25 +112,23 @@ public class SocialUtils { boolean notifyGroup ){ - /* - String escapedFeedText = org.gcube.social_networking.socialutillibrary.Utils.escapeHtmlAndTransformUrl(postText); - List hashtags = org.gcube.social_networking.socialutillibrary.Utils.getHashTags(postText); - if (hashtags != null && !hashtags.isEmpty()) - escapedFeedText = org.gcube.social_networking.socialutillibrary.Utils.convertHashtagsAnchorHTML(escapedFeedText, hashtags); - */ - SocialMessageParser messageParser = new SocialMessageParser(postText); - String escapedFeedText = messageParser.getParsedMessage(); + String escapedPostText = messageParser.getParsedMessage(); List hashtags = messageParser.getHashtags(); - - logger.info("Escaped post text is " + escapedFeedText); + ArrayList mentionedUsers = getUsersFromUsernames(Utils.getMentionedUsernames(postText)); + ArrayList mentionedUsersToConvertInHTML = convertToItemBean(mentionedUsers); + + SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(ScopeProvider.instance.get()); + + escapedPostText = Utils.convertMentionUsernamesAnchorHTML(escapedPostText, mentionedUsersToConvertInHTML, site.getSiteURL(), site.getSiteLandingPagePath()); + logger.info("*** Escaped post text is " + escapedPostText); + String scope = ScopeProvider.instance.get(); - String appId = caller.getClient().getId(); - - + String appId = caller.getClient().getId(); + Feed toWrite = - buildFeed( - escapedFeedText, + buildPost( + escapedPostText, uriParams == null ? "" : uriParams, previewTitle == null ? "" : previewTitle, previewDescription == null ? "" : previewDescription, @@ -117,6 +153,20 @@ public class SocialUtils { } }).start(); + + // build the notification manager + + SocialNetworkingUser user = new SocialNetworkingUser(appId, "", applicationProfile.getName(), applicationProfile.getImageUrl()); + NotificationsManager nm = new ApplicationNotificationsManager( + UserManagerWSBuilder.getInstance().getUserManager(), + site, + scope, + user, + NEWS_FEED_PORTLET_CLASSNAME); + + if (!mentionedUsers.isEmpty()) + new Thread(new MentionNotificationsThread(toWrite.getKey(), toWrite.getDescription(), nm, null, mentionedUsers)).start(); + if(notifyGroup){ logger.debug("Sending notifications for " + appId + " " + scope); @@ -133,16 +183,6 @@ public class SocialUtils { logger.debug("Company id and name " + groupId + " " + groupName); - // build the notification manager - SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(ScopeProvider.instance.get()); - SocialNetworkingUser user = new SocialNetworkingUser(appId, "", applicationProfile.getName(), applicationProfile.getImageUrl()); - NotificationsManager nm = new ApplicationNotificationsManager( - UserManagerWSBuilder.getInstance().getUserManager(), - site, - scope, - user, - NEWS_FEED_PORTLET_CLASSNAME); - // start notification thread new Thread(new PostNotificationsThread( UserManagerWSBuilder.getInstance().getUserManager(), @@ -153,7 +193,6 @@ public class SocialUtils { new HashSet(hashtags), new HashSet()) ).start(); - }catch (Exception e) { logger.debug("Feed succesfully created but unable to send notifications."); } @@ -176,7 +215,7 @@ public class SocialUtils { * @param previewThumbnailUrl the image url to show in the preview * @return a feed instance ready to be written */ - private static Feed buildFeed( + private static Feed buildPost( String description, String uriParams, String previewTitle, @@ -341,41 +380,33 @@ public class SocialUtils { String previewHost, String previewUrl, String urlThumbnail, - boolean notifyGroup - ) { + boolean notifyGroup) { - /* - String escapedFeedText = org.gcube.social_networking.socialutillibrary.Utils.escapeHtmlAndTransformUrl(postText); - - List hashtags = org.gcube.social_networking.socialutillibrary.Utils.getHashTags(postText); - if (hashtags != null && !hashtags.isEmpty()) - escapedFeedText = org.gcube.social_networking.socialutillibrary.Utils.convertHashtagsAnchorHTML(escapedFeedText, hashtags); - */ - SocialMessageParser messageParser = new SocialMessageParser(postText); - String escapedFeedText = messageParser.getParsedMessage(); + String escapedPostText = messageParser.getParsedMessage(); List hashtags = messageParser.getHashtags(); - - + //check if any mention exists + ArrayList mentionedUsers = getUsersFromUsernames(Utils.getMentionedUsernames(postText)); + ArrayList mentionedUsersToConvertInHTML = convertToItemBean(mentionedUsers); + + SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(ScopeProvider.instance.get()); + + escapedPostText = Utils.convertMentionUsernamesAnchorHTML(escapedPostText, mentionedUsersToConvertInHTML, site.getSiteURL(), site.getSiteLandingPagePath()); + GCubeUser user; // retrieve group information UserManager uManager = UserManagerWSBuilder.getInstance().getUserManager(); - try{ - + try { user = uManager.getUserByUsername(userId); - - }catch(Exception e){ - - logger.error("Unable to get user informations, feed write fails.", e); + } catch(Exception e){ + logger.error("Unable to get user informations, post write fails.", e); return null; - } String email = user.getEmail(); String fullName = user.getFirstName() + " " + user.getLastName(); String thumbnailURL = user.getUserAvatarURL(); - String linkTitle = previewTitle == null ? "" : previewTitle; String linkDesc = previewDescription == null ? "" : previewDescription; String host = previewHost == null ? "" : previewHost; @@ -385,16 +416,16 @@ public class SocialUtils { //this means the user has shared a file without text in it. String textToPost = ""; - if (escapedFeedText.trim().compareTo(NO_TEXT_FILE_SHARE) == 0) { + if (escapedPostText.trim().compareTo(NO_TEXT_FILE_SHARE) == 0) { textToPost = org.gcube.social_networking.socialutillibrary.Utils.convertFileNameAnchorHTML(url); } else { - textToPost = escapedFeedText; + textToPost = escapedPostText; } Feed toShare = new Feed(UUID.randomUUID().toString(), FeedType.PUBLISH, userId, new Date(), vreId, url, urlThumbnail, textToPost, PrivacyLevel.SINGLE_VRE, fullName, email, thumbnailURL, linkTitle, linkDesc, host); - logger.info("Attempting to save Feed with text: " + textToPost + " Level = " + PrivacyLevel.SINGLE_VRE + " Timeline = " + vreId); + logger.info("Attempting to save Post with text: " + textToPost + " Level = " + PrivacyLevel.SINGLE_VRE + " Timeline = " + vreId); boolean result = CassandraConnection.getInstance().getDatabookStore().saveUserFeed(toShare); @@ -419,40 +450,32 @@ public class SocialUtils { CassandraConnection.getInstance().getDatabookStore().saveHashTags(toShare.getKey(), vreId, hashtags); } catch (FeedIDNotFoundException e) { - logger.error("Error writing onto VRES Time Line" + vreId); } - logger.trace("Success writing onto " + vreId); } if (!result) return null; + SocialNetworkingUser socialUser = + new SocialNetworkingUser(userId, email, fullName, thumbnailURL); + NotificationsManager nm = new ApplicationNotificationsManager(UserManagerWSBuilder.getInstance().getUserManager(), site, vreId, socialUser, NEWS_FEED_PORTLET_CLASSNAME); + if (!mentionedUsers.isEmpty()) + new Thread(new MentionNotificationsThread(toShare.getKey(), toShare.getDescription(), nm, null, mentionedUsers)).start(); + //send the notification about this posts to everyone in the group if notifyGroup is true if (vreId != null && vreId.compareTo("") != 0 && notifyGroup) { - try{ - - // get the site from the http request - SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(ScopeProvider.instance.get()); - + try{ // retrieve group information GroupManager gManager = GroupManagerWSBuilder.getInstance().getGroupManager(); - - GCubeUser userInfo = uManager.getUserByUsername(userId); - SocialNetworkingUser socialUser = - new SocialNetworkingUser(userId, userInfo.getEmail(), userInfo.getFullname(), userInfo.getUserAvatarURL()); - - // handle the scope String name = new ScopeBean(vreId).name(); // scope such as devVR long groupId = gManager.getGroupId(name); String groupName = gManager.getGroup(groupId).getGroupName(); logger.debug("Company id and name " + groupId + " " + groupName); - - NotificationsManager nm = new ApplicationNotificationsManager(UserManagerWSBuilder.getInstance().getUserManager(), site, vreId, socialUser, NEWS_FEED_PORTLET_CLASSNAME); new Thread( new PostNotificationsThread( UserManagerWSBuilder.getInstance().getUserManager(), @@ -464,7 +487,7 @@ public class SocialUtils { new HashSet(hashtags)) ).start(); - logger.debug("Start sending notifications for feed written by " + userId); + logger.debug("Start sending notifications for post written by " + userId); }catch(Exception e){ logger.error("Unable to notify users", e); }