added support for mentions in user's post as well

This commit is contained in:
Massimiliano Assante 2019-09-19 11:24:20 +02:00
parent 2f4583fd60
commit 8a06144560
4 changed files with 98 additions and 90 deletions

View File

@ -4,6 +4,9 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="social-util-library-1.7.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/social-util-library/social-util-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/social-networking-library-ws/target/classes"/>
<property name="context-root" value="social-networking-library-ws"/>
</wb-module>

22
pom.xml
View File

@ -12,7 +12,7 @@
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-library-ws</artifactId>
<packaging>war</packaging>
<version>2.2.4-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>
<name>social-networking-library-ws</name>
<description>Rest interface for the social networking library.</description>
<properties>
@ -24,7 +24,6 @@
<distroDirectory>distro</distroDirectory>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java-version>1.8</java-version>
</properties>
<scm>
@ -190,16 +189,6 @@
<artifactId>social-data-search-client</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
<!-- <exclusions> -->
<!-- <exclusion> -->
<!-- <artifactId>jackson-core</artifactId> -->
<!-- <groupId>com.fasterxml.jackson.core</groupId> -->
<!-- </exclusion> -->
<!-- <exclusion> -->
<!-- <artifactId>jackson-dataformat-yaml</artifactId> -->
<!-- <groupId>com.fasterxml.jackson.dataformat</groupId> -->
<!-- </exclusion> -->
<!-- </exclusions> -->
</dependency>
<dependency>
@ -259,13 +248,6 @@
<artifactId>jersey-bean-validation</artifactId>
<version>${version.jersey}</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>javax.servlet</groupId> -->
<!-- <artifactId>servlet-api</artifactId> -->
<!-- <version>3.0-alpha-1</version> -->
<!-- <scope>compile</scope> -->
<!-- </dependency> -->
<!-- Generate REST documentation -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
@ -306,7 +288,7 @@
<dependency>
<groupId>org.gcube.applicationsupportlayer</groupId>
<artifactId>aslsocial</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<version>[1.7.1-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -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();

View File

@ -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<GenericItemBean> getUsersFromUsernames(Set<String> mentions) {
if (mentions.isEmpty())
return new ArrayList<>();
ArrayList<GenericItemBean> 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<ItemBean> convertToItemBean(Collection<GenericItemBean> items) {
ArrayList<ItemBean> 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<String> 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<String> hashtags = messageParser.getHashtags();
logger.info("Escaped post text is " + escapedFeedText);
ArrayList<GenericItemBean> mentionedUsers = getUsersFromUsernames(Utils.getMentionedUsernames(postText));
ArrayList<ItemBean> 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<String>(hashtags),
new HashSet<String>())
).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<String> 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<String> hashtags = messageParser.getHashtags();
//check if any mention exists
ArrayList<GenericItemBean> mentionedUsers = getUsersFromUsernames(Utils.getMentionedUsernames(postText));
ArrayList<ItemBean> 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<String>(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);
}