From bffac60eb1bad156e1e5f7818a449253c26b7dcd Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 2 Feb 2023 20:11:52 +0100 Subject: [PATCH] added the comment post method --- CHANGELOG.md | 6 +- pom.xml | 2 +- .../ws/inputs/CommentInputBean.java | 73 ++++++++++++ .../networking/ws/methods/v2/Comments.java | 105 ++++++++++++++++++ .../networking/ws/utils/SocialUtils.java | 7 ++ 5 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/gcube/portal/social/networking/ws/inputs/CommentInputBean.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 634e76d..5bc3052 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## [v2.8.0-SNAPSHOT] - 2022-10-20 +## [v2.9.0-SNAPSHOT] - 2023-02-02 + +- Feature #24456 get a specific post by ID + +## [v2.8.0] - 2022-10-20 - Feature #23891 Refactored following updates social lib - Feature #23847 Social service: temporarily block of notifications for given username(s) diff --git a/pom.xml b/pom.xml index bd6fe3f..790753a 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portal social-networking-library-ws war - 2.8.0-SNAPSHOT + 2.9.0-SNAPSHOT social-networking-library-ws Rest interface for the social networking library. diff --git a/src/main/java/org/gcube/portal/social/networking/ws/inputs/CommentInputBean.java b/src/main/java/org/gcube/portal/social/networking/ws/inputs/CommentInputBean.java new file mode 100644 index 0000000..5ff7a85 --- /dev/null +++ b/src/main/java/org/gcube/portal/social/networking/ws/inputs/CommentInputBean.java @@ -0,0 +1,73 @@ +package org.gcube.portal.social.networking.ws.inputs; + +import java.io.Serializable; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.webcohesion.enunciate.metadata.DocumentationExample; +/** + * Generic input bean for methods that allow to comment posts + */ +@JsonIgnoreProperties(ignoreUnknown = true) // ignore in serialization/deserialization +public class CommentInputBean implements Serializable{ + + private static final long serialVersionUID = 5274608088828232980L; + + @JsonProperty("text") + @NotNull(message="text cannot be null") + @Size(min=1, message="text cannot be empty") + @DocumentationExample("I would like to comment that ...") + /** + * text the text of the comment + */ + private String text; + + @NotNull(message="postid cannot be null") + @JsonProperty("postid") + /** + * postid the postid of the post where you attach the comment + */ + private String postid; + + public CommentInputBean() { + super(); + } + + /** + * @param text + * @param postid + * @param params + */ + public CommentInputBean(String text, String postid) { + super(); + this.text = text; + this.postid = postid; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getPostid() { + return postid; + } + + public void setPostid(String postid) { + this.postid = postid; + } + + @Override + public String toString() { + return "CommentInputBean [text=" + text + ", postid=" + postid + "]"; + } + + + +} 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..cab47ee 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,95 @@ 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 post 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; + //check that the postId exists first + String postId = comment.getPostid(); + try { + CassandraConnection.getInstance().getDatabookStore().readPost(postId); + } catch(Exception e){ + responseBean.setMessage("You're probably trying comment a post with id " + postId + " that does not exist "); + responseBean.setSuccess(false); + status = Status.INTERNAL_SERVER_ERROR; + return Response.status(status).entity(responseBean).build(); + } + + SocialMessageParser messageParser = new SocialMessageParser(comment.getText()); + String escapedCommentText = messageParser.getParsedMessage(); + // parse + String key = UUID.randomUUID().toString(); + 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(); + + Date lastEditTime = null; + + Comment theComment = new Comment(key, userid, lastEditTime, userid, commentText, fullName, thumbnailURL); + boolean result = false; + + try { + result = CassandraConnection.getInstance().getDatabookStore().addComment(theComment); + } catch(Exception e){ + 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.debug("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/utils/SocialUtils.java b/src/main/java/org/gcube/portal/social/networking/ws/utils/SocialUtils.java index 2c5207b..6dbca20 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 @@ -11,6 +11,9 @@ import java.util.List; import java.util.Set; import java.util.UUID; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -19,6 +22,7 @@ import org.gcube.applicationsupportlayer.social.NotificationsManager; import org.gcube.applicationsupportlayer.social.ex.ApplicationProfileNotFoundException; import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite; import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser; +import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.resources.gcore.utils.XPathHelper; import org.gcube.common.scope.api.ScopeProvider; @@ -26,6 +30,7 @@ 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.PrivacyLevel; import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException; import org.gcube.portal.notifications.bean.GenericItemBean; @@ -34,6 +39,7 @@ 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.portal.social.networking.ws.outputs.ResponseBean; 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; @@ -495,4 +501,5 @@ public class SocialUtils { } return toShare; } + }