From 61203ff9f7da89ff62d7906f3becb52b323a34e7 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Tue, 25 Oct 2022 16:50:26 +0200 Subject: [PATCH] Feature #24022 Add api the get posts with range filter parameters --- CHANGELOG.md | 2 +- pom.xml | 2 +- .../networking/ws/methods/v2/Posts.java | 56 ++++++++++++++++++- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66eca84..7fd83de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [v2.8.0] - 2022-10-20 +## [v2.8.0-SNAPSHOT - 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 880065f..bd6fe3f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portal social-networking-library-ws war - 2.8.0 + 2.8.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/methods/v2/Posts.java b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java index 8df5095..25b4f86 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 @@ -26,6 +26,10 @@ import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.shared.ApplicationProfile; import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.Post; +import org.gcube.portal.databook.shared.RangePosts; +import org.gcube.portal.databook.shared.ex.ColumnNameNotFoundException; +import org.gcube.portal.databook.shared.ex.FeedTypeNotFoundException; +import org.gcube.portal.databook.shared.ex.PrivacyLevelTypeNotFoundException; 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; @@ -44,9 +48,9 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes; */ @Path("2/posts") @RequestHeaders ({ - @RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"), - @RequestHeader( name = "Content-Type", description = "application/json") - }) + @RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"), + @RequestHeader( name = "Content-Type", description = "application/json") +}) public class Posts { // Logger @@ -419,6 +423,52 @@ public class Posts { return Response.status(status).entity(responseBean).build(); } + @GET + @Path("get-recent-posts-vre-by-range/") + @Produces(MediaType.APPLICATION_JSON) + @StatusCodes ({ + @ResponseCode ( code = 201, condition = "Sccessfull retrieved posts, they are reported in the 'result' field of the returned object"), + @ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT) + }) + /** + * return the most recent posts for this vre up to quantity param and the last index of the posts in the timeline + * lastReturnedPostTimelineIndex is useful to know from where to start the range the next time you ask, because there are deletions + * + * @param from the range start (most recent feeds for this vre) has to be greater than 0 + * @param quantity the number of most recent feeds for this vre starting from "from" param + * @return a RangePosts containing of most recent feeds for this vre + * @throws FeedTypeNotFoundException + * @throws PrivacyLevelTypeNotFoundException + * @throws ColumnNameNotFoundException + */ + public Response getRecentPostsByVREAndRange( + @QueryParam("from") + @Min(value=1, message="from cannot be negative") + int from, + @QueryParam("quantity") + @Min(value=1, message="quantity cannot be negative") + int quantity) throws ValidationException { + + String context = ScopeProvider.instance.get(); + Caller caller = AuthorizationProvider.instance.get(); + logger.debug("Retrieving all posts coming from vre = " + context); + ResponseBean responseBean = new ResponseBean(); + Status status = Status.OK; + try{ + RangePosts rangePosts = CassandraConnection.getInstance().getDatabookStore().getRecentPostsByVREAndRange(context, from, quantity); + Filters.hideSensitiveInformation(rangePosts.getPosts(), caller.getClient().getId()); + responseBean.setResult(rangePosts); + responseBean.setSuccess(true); + }catch(Exception e){ + logger.error("Unable to retrieve posts for vre = " + context, e); + status = Status.INTERNAL_SERVER_ERROR; + responseBean.setMessage(e.toString()); + responseBean.setSuccess(false); + } + + return Response.status(status).entity(responseBean).build(); + } + @GET @Path("get-posts-by-hashtag/") @Produces({MediaType.APPLICATION_JSON})