added support for hashtags in comments (at creation, edit and delete time)

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/news-feed@148721 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-05-16 14:10:04 +00:00
parent ac5ad08b69
commit 90b34f01a0
2 changed files with 76 additions and 22 deletions

View File

@ -5,9 +5,6 @@
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/target/generated-sources/gwt"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="gcube-widgets-2.2.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/gcube-widgets/gcube-widgets">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/>
<property name="context-root" value="news-feed"/>
</wb-module>

View File

@ -36,6 +36,7 @@ import org.gcube.portal.databook.shared.RangeFeeds;
import org.gcube.portal.databook.shared.ShowUserStatisticAction;
import org.gcube.portal.databook.shared.UserInfo;
import org.gcube.portal.databook.shared.ex.ColumnNameNotFoundException;
import org.gcube.portal.databook.shared.ex.CommentIDNotFoundException;
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException;
import org.gcube.portal.databook.shared.ex.FeedTypeNotFoundException;
import org.gcube.portal.databook.shared.ex.LikeIDNotFoundException;
@ -106,7 +107,6 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
public void init() {
store = new DBCassandraAstyanaxImpl();
try {
escl = new ElasticSearchClientImpl(null);
_log.info("Elasticsearch connection created");
@ -347,10 +347,8 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
// TODO : check this error better
if(escl == null){
_log.warn("There is no connection to elasticsearch, sorry.");
return null;
}
PortalContext pContext = PortalContext.getConfiguration();
@ -616,6 +614,11 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
String escapedCommentText = Utils.escapeHtmlAndTransformUrl(commentText);
// get hashtags
List<String> hashtags = Utils.getHashTags(escapedCommentText);
if (hashtags != null && !hashtags.isEmpty())
escapedCommentText = Utils.convertHashtagsAnchorHTML(escapedCommentText, hashtags);
//copy the set into a list
ArrayList<String> mentionedUserFullNamesList = new ArrayList<String>();
mentionedUserFullNamesList.addAll(mentionedUserFullNames);
@ -636,6 +639,17 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
e.printStackTrace();
return new OperationResult(false, "Related post not found for this comment", comment);
}
try {
if (hashtags != null && !hashtags.isEmpty())
store.saveHashTagsComment(comment.getKey(), store.readFeed(comment.getFeedid()).getVreid(), hashtags);
} catch (CommentIDNotFoundException
| PrivacyLevelTypeNotFoundException
| FeedTypeNotFoundException | FeedIDNotFoundException
| ColumnNameNotFoundException e1) {
_log.error("Unable to save hashtags for this comment " + e1.getMessage());
}
//if the comment was correctly delivered && is not an app feed notify users involved
if (commentCommitResult && isWithinPortal()) {
PortalContext pContext = PortalContext.getConfiguration();
@ -715,17 +729,38 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
@Override
public OperationResult editComment(Comment toEdit) {
UserInfo user = getUserSettings().getUserInfo();
if (user.getUsername().compareTo(NewsConstants.TEST_USER) == 0) {
return new OperationResult(false, "Session Expired", null);
}
String escapedCommentText = Utils.escapeHtmlAndTransformUrl(toEdit.getText());
Comment edited = new Comment(toEdit.getKey(), toEdit.getUserid(),
toEdit.getTime(), toEdit.getFeedid(), escapedCommentText, user.getFullName(), user.getAvatarId(), true, new Date());
Comment edited = null;
try {
UserInfo user = getUserSettings().getUserInfo();
if (user.getUsername().compareTo(NewsConstants.TEST_USER) == 0) {
return new OperationResult(false, "Session Expired", null);
}
String vreIdFeed = store.readFeed(toEdit.getFeedid()).getVreid();
// get old hashtags and delete them
String oldText = store.readCommentById(toEdit.getKey()).getText();
_log.debug("Old text for this comment is " + oldText);
List<String> oldHashtags = Utils.getHashTags(Utils.removeHTMLFromText(oldText));
if (oldHashtags != null && !oldHashtags.isEmpty()) {
_log.debug("The comment has hashtags, attempting to delete them ... " + oldHashtags.toString());
boolean deletedHashtag = store.deleteHashTagsComment(toEdit.getKey(), vreIdFeed, oldHashtags);
_log.debug("deletedHashtag? " + deletedHashtag);
}
// get new hashtags
String escapedCommentText = Utils.escapeHtmlAndTransformUrl(toEdit.getText());
List<String> newHashtags = Utils.getHashTags(escapedCommentText);
if (newHashtags != null && !newHashtags.isEmpty())
escapedCommentText = Utils.convertHashtagsAnchorHTML(escapedCommentText, newHashtags);
edited = new Comment(toEdit.getKey(), toEdit.getUserid(),
toEdit.getTime(), toEdit.getFeedid(), escapedCommentText, user.getFullName(), user.getAvatarId(), true, new Date());
store.editComment(edited);
if (newHashtags != null && !newHashtags.isEmpty())
store.saveHashTagsComment(edited.getKey(), vreIdFeed, newHashtags);
} catch (Exception e) {
e.printStackTrace();
return new OperationResult(false, "Exception on the server: " + e.getMessage(), null);
@ -869,6 +904,16 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
public boolean deleteComment(String commentid, String feedid) {
_log.trace("Attempting to delete comment " + commentid);
try {
// get hashtags, if any, and delete them
Comment toDelete = store.readCommentById(commentid);
String vreIdFeed = store.readFeed(toDelete.getFeedid()).getVreid();
List<String> hashtags = Utils.getHashTags(Utils.removeHTMLFromText(toDelete.getText()));
if (hashtags != null && !hashtags.isEmpty()) {
_log.debug("The comment has hashtags, attempting to delete them ... " + hashtags.toString());
boolean deletedHashtag = store.deleteHashTagsComment(toDelete.getKey(), vreIdFeed, hashtags);
_log.debug("deletedHashtag? " + deletedHashtag);
}
_log.debug("Attempting to delete comment " + commentid);
return store.deleteComment(commentid, feedid);
} catch (Exception e) {
e.printStackTrace();
@ -877,16 +922,28 @@ public class NewsServiceImpl extends RemoteServiceServlet implements NewsService
}
@Override
public boolean deleteFeed(String feedid) {
_log.trace("Called delete feed " + feedid);
_log.debug("Called delete feed " + feedid);
try {
Feed toDelete = store.readFeed(feedid);
List<String> hashtags = Utils.getHashTags(toDelete.getDescription());
if (hashtags != null && !hashtags.isEmpty()) {
_log.trace("The feed has hashtags, attempting to delete them ... " + hashtags.toString());
boolean deletedHashtag = store.deleteHashTags(feedid, toDelete.getVreid(), hashtags);
_log.trace("deletedHashtag? " + deletedHashtag);
// delete comments and hastags as well
boolean hasComments = Integer.parseInt(toDelete.getCommentsNo()) > 0;
if(hasComments){
_log.debug("Deleting feed's comments and their hashtags");
List<Comment> comments = store.getAllCommentByFeed(feedid);
for (Comment comment : comments) {
deleteComment(comment.getKey(), feedid);
}
}
_log.trace("Attempting to delete feed " + feedid);
List<String> hashtags = Utils.getHashTags(Utils.removeHTMLFromText(toDelete.getDescription()));
if (hashtags != null && !hashtags.isEmpty()) {
_log.debug("The feed has hashtags, attempting to delete them ... " + hashtags.toString());
boolean deletedHashtag = store.deleteHashTags(feedid, toDelete.getVreid(), hashtags);
_log.debug("deletedHashtag? " + deletedHashtag);
}
_log.debug("Attempting to delete feed " + feedid);
return store.deleteFeed(feedid);
} catch (Exception e) {