From 9f696b7f54661518f13ad51b121b8d5d6d596061 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Tue, 7 Feb 2023 11:42:49 +0100 Subject: [PATCH] commit comment --- .../networking/ws/methods/v2/Comments.java | 95 +++++++++++++++++++ .../networking/ws/methods/v2/Posts.java | 2 +- .../networking/ws/utils/SocialUtils.java | 8 +- 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java index 81cde77..fbe9552 100644 --- a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java +++ b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Comments.java @@ -1,12 +1,17 @@ package org.gcube.portal.social.networking.ws.methods.v2; +import java.util.Date; import java.util.List; +import java.util.UUID; +import javax.validation.Valid; import javax.validation.ValidationException; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; @@ -18,10 +23,19 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portal.databook.shared.Comment; +import org.gcube.portal.databook.shared.Feed; +import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException; +import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder; +import org.gcube.portal.social.networking.ws.inputs.CommentInputBean; +import org.gcube.portal.social.networking.ws.inputs.PostInputBean; import org.gcube.portal.social.networking.ws.outputs.ResponseBean; import org.gcube.portal.social.networking.ws.utils.CassandraConnection; import org.gcube.portal.social.networking.ws.utils.ErrorMessages; import org.gcube.portal.social.networking.ws.utils.Filters; +import org.gcube.portal.social.networking.ws.utils.SocialUtils; +import org.gcube.socialnetworking.socialtoken.SocialMessageParser; +import org.gcube.vomanagement.usermanagement.UserManager; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.LoggerFactory; import com.webcohesion.enunciate.metadata.rs.RequestHeader; @@ -153,4 +167,85 @@ public class Comments { return Response.status(status).entity(responseBean).build(); } + + /** + * Create a new comment to a post having as owner the auth token's owner + * @param post The post to be written + * @return + * @throws ValidationException + */ + @POST + @Path("comment-post") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @StatusCodes ({ + @ResponseCode ( code = 201, condition = "Successfull created, the new comment is reported in the 'result' field of the returned object"), + @ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT) + }) + public Response writeComment( + @NotNull(message="Comment to write is missing") + @Valid + CommentInputBean comment) throws ValidationException{ + + Caller caller = AuthorizationProvider.instance.get(); + String username = caller.getClient().getId(); + logger.debug("Request of writing a comment coming from user " + username); + String context = ScopeProvider.instance.get(); + ResponseBean responseBean = new ResponseBean(); + Status status = Status.OK; + SocialMessageParser messageParser = new SocialMessageParser(comment.getText()); + String escapedCommentText = messageParser.getParsedMessage(); + // parse + String key = UUID.randomUUID().toString(); + String postId = comment.getPostid(); + String commentText = escapedCommentText; + String userid = username; + Date time = new Date(); + + GCubeUser user; + + // retrieve group information + UserManager uManager = UserManagerWSBuilder.getInstance().getUserManager(); + try { + user = uManager.getUserByUsername(userid); + } catch(Exception e){ + logger.error("Unable to get user informations, comment write fails.", e); + responseBean.setMessage("Unable to get user informations, comment write fails with username " + userid + " that does not exist "); + responseBean.setSuccess(false); + status = Status.INTERNAL_SERVER_ERROR; + return Response.status(status).entity(responseBean).build(); + } + String fullName = user.getFirstName() + " " + user.getLastName(); + String thumbnailURL = user.getUserAvatarURL(); + + Comment theComment = new Comment(key, userid, time, postId, commentText, fullName, thumbnailURL); + boolean result = false; + + try { + result = CassandraConnection.getInstance().getDatabookStore().addComment(theComment); + logger.info("Added comment? " + theComment.toString() + " Result is " +result); + } catch(Exception e){ + e.printStackTrace(); + responseBean.setMessage("Could not reach the DB to write the comment, something went wrong"); + responseBean.setSuccess(false); + status = Status.INTERNAL_SERVER_ERROR; + return Response.status(status).entity(responseBean).build(); + } + + if(result){ + logger.info("Comment correctly written by user " + username); + responseBean.setResult(result); + responseBean.setSuccess(true); + return Response.status(status).entity(responseBean).build(); + + } + + logger.error("Unable to write comment."); + responseBean.setMessage("Unable to write comment"); + responseBean.setSuccess(false); + status = Status.INTERNAL_SERVER_ERROR; + return Response.status(status).entity(responseBean).build(); + + } + } 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 f679897..a0a6d20 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 @@ -236,7 +236,7 @@ public class Posts { // try to share logger.debug("Trying to share user post..."); - Feed res = SocialUtils.shareUserUpdate( + Post res = SocialUtils.shareUserUpdate( username, postText, context, 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 2c5207b..25a58e4 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 @@ -26,6 +26,8 @@ import org.gcube.common.scope.impl.ScopeBean; import org.gcube.portal.databook.shared.ApplicationProfile; import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.FeedType; +import org.gcube.portal.databook.shared.Post; +import org.gcube.portal.databook.shared.PostType; import org.gcube.portal.databook.shared.PrivacyLevel; import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException; import org.gcube.portal.notifications.bean.GenericItemBean; @@ -372,7 +374,7 @@ public class SocialUtils { * @param notifyGroup * @return The written Feed */ - public static Feed shareUserUpdate( + public static Post shareUserUpdate( String userId, String postText, String vreId, @@ -423,12 +425,12 @@ public class SocialUtils { textToPost = escapedPostText; } - Feed toShare = new Feed(UUID.randomUUID().toString(), FeedType.PUBLISH, userId, new Date(), + Post toShare = new Post(UUID.randomUUID().toString(), PostType.PUBLISH, userId, new Date(), vreId, url, urlThumbnail, textToPost, PrivacyLevel.SINGLE_VRE, fullName, email, thumbnailURL, linkTitle, linkDesc, host); logger.info("Attempting to save Post with text: " + textToPost + " Level = " + PrivacyLevel.SINGLE_VRE + " Timeline = " + vreId); - boolean result = CassandraConnection.getInstance().getDatabookStore().saveUserFeed(toShare); + boolean result = CassandraConnection.getInstance().getDatabookStore().saveUserPost(toShare); if(vreId != null && vreId.compareTo("") != 0 && result) {