2022-10-25 18:38:20 +02:00
|
|
|
package org.gcube.portal.social.networking.ws.methods.v2;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2023-02-09 12:47:01 +01:00
|
|
|
import javax.validation.Valid;
|
2022-10-25 18:38:20 +02:00
|
|
|
import javax.validation.ValidationException;
|
|
|
|
import javax.validation.constraints.NotNull;
|
2023-02-09 12:47:01 +01:00
|
|
|
import javax.ws.rs.Consumes;
|
2022-10-25 18:38:20 +02:00
|
|
|
import javax.ws.rs.GET;
|
2023-02-09 12:47:01 +01:00
|
|
|
import javax.ws.rs.POST;
|
2022-10-25 18:38:20 +02:00
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response.Status;
|
|
|
|
|
|
|
|
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.Like;
|
2023-02-09 15:31:11 +01:00
|
|
|
import org.gcube.portal.social.networking.ws.inputs.LikeInputBean;
|
2023-02-09 15:09:38 +01:00
|
|
|
import org.gcube.portal.social.networking.ws.inputs.PostId;
|
2022-10-25 18:38:20 +02:00
|
|
|
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;
|
2023-02-09 12:47:01 +01:00
|
|
|
import org.gcube.portal.social.networking.ws.utils.SocialUtils;
|
2022-10-25 18:38:20 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
|
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
|
|
|
|
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
|
|
|
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST interface for the social networking library (likes).
|
2023-11-24 11:53:20 +01:00
|
|
|
* @author Ahmed Ibrahim ISTI-CNR
|
2022-10-25 18:38:20 +02:00
|
|
|
*/
|
|
|
|
@Path("2/likes")
|
|
|
|
@RequestHeaders ({
|
2023-11-22 19:52:25 +01:00
|
|
|
@RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
|
|
|
|
@RequestHeader( name = "Content-Type", description = "application/json")
|
2023-02-09 12:47:01 +01:00
|
|
|
})
|
2022-10-25 18:38:20 +02:00
|
|
|
public class Likes {
|
|
|
|
|
|
|
|
// Logger
|
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Likes.class);
|
2023-02-09 12:47:01 +01:00
|
|
|
|
2022-10-25 18:38:20 +02:00
|
|
|
/*
|
|
|
|
* Retrieve the list of likes belonging to the post id (key) of the token in the related context
|
2023-11-22 19:52:25 +01:00
|
|
|
* @param key the key as in the POST JSON representation
|
2022-10-25 18:38:20 +02:00
|
|
|
* @pathExample /get-likes-by-post-id?key=9ea137e9-6606-45ff-a1a2-94d4e8760583
|
2023-11-22 19:52:25 +01:00
|
|
|
* @return the list of likes belonging to the post identified by the key in the context identified by the token
|
2022-10-25 18:38:20 +02:00
|
|
|
*/
|
|
|
|
@GET
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Path("get-likes-by-post-id")
|
|
|
|
@StatusCodes ({
|
2023-11-22 19:52:25 +01:00
|
|
|
@ResponseCode ( code = 200, condition = "The list of likes is put into the 'result' field"),
|
|
|
|
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
|
2022-10-25 18:38:20 +02:00
|
|
|
})
|
|
|
|
public Response getAllLikesByPostId(
|
|
|
|
@NotNull
|
2023-11-22 19:52:25 +01:00
|
|
|
@QueryParam("key")
|
2022-10-25 18:38:20 +02:00
|
|
|
String key) {
|
|
|
|
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
|
|
Status status = Status.OK;
|
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
List<Like> likes = null;
|
|
|
|
|
|
|
|
try{
|
2024-04-22 11:19:17 +02:00
|
|
|
logger.debug("Retrieving likes for user id " + username);
|
2022-10-25 18:38:20 +02:00
|
|
|
likes = CassandraConnection.getInstance().getDatabookStore().getAllLikesByPost(key);
|
|
|
|
responseBean.setResult(likes);
|
|
|
|
responseBean.setSuccess(true);
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Unable to retrieve such likes.", e);
|
|
|
|
responseBean.setMessage(e.getMessage());
|
|
|
|
responseBean.setSuccess(false);
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2023-02-09 12:47:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new like to a post in the context of the token
|
2023-11-22 19:52:25 +01:00
|
|
|
* @param postid The post id to be liked
|
|
|
|
* @return true if everything is OK
|
2023-02-09 12:47:01 +01:00
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
|
|
|
@POST
|
|
|
|
@Path("like-post")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@StatusCodes ({
|
2023-11-22 19:52:25 +01:00
|
|
|
@ResponseCode ( code = 201, condition = "Successful created, the like operation result is reported in the 'result' field of the returned object"),
|
|
|
|
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
|
2023-02-09 12:47:01 +01:00
|
|
|
})
|
|
|
|
public Response like(
|
2023-11-22 19:52:25 +01:00
|
|
|
@NotNull(message="Post to like is missing")
|
|
|
|
@Valid
|
2023-02-09 15:31:11 +01:00
|
|
|
PostId post) throws ValidationException {
|
2023-02-09 12:47:01 +01:00
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
logger.debug("Request of like coming from user " + username);
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
|
|
Status status = Status.OK;
|
2023-02-09 15:31:11 +01:00
|
|
|
boolean likeResultOperation = SocialUtils.like(username, post.getPostId(), context);
|
2023-02-09 12:47:01 +01:00
|
|
|
if (likeResultOperation)
|
2024-04-22 11:19:17 +02:00
|
|
|
logger.debug("Added like OK to postId " + post.getPostId());
|
2023-02-09 12:47:01 +01:00
|
|
|
else {
|
2023-02-09 15:31:11 +01:00
|
|
|
logger.error("Unable to like this post"+ post.getPostId());
|
2023-02-09 12:47:01 +01:00
|
|
|
responseBean.setMessage("Unable to like, something went wrong please see server log");
|
|
|
|
responseBean.setSuccess(false);
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
responseBean.setResult(true);
|
|
|
|
responseBean.setSuccess(true);
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2023-11-22 19:52:25 +01:00
|
|
|
|
2023-02-09 12:47:01 +01:00
|
|
|
/**
|
|
|
|
* Unlike to a post in the context of the token
|
2023-11-22 19:52:25 +01:00
|
|
|
* @param postid The post id to be liked
|
2023-02-09 12:47:01 +01:00
|
|
|
* @return true if everything is OK
|
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
|
|
|
@POST
|
|
|
|
@Path("unlike-post")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@StatusCodes ({
|
2023-11-22 19:52:25 +01:00
|
|
|
@ResponseCode ( code = 201, condition = "The unlike operation result is reported in the 'result' field of the returned object"),
|
|
|
|
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
|
2023-02-09 12:47:01 +01:00
|
|
|
})
|
|
|
|
public Response unlike(
|
2023-11-22 19:52:25 +01:00
|
|
|
@NotNull(message="Post to unlike is missing")
|
|
|
|
@Valid
|
2023-02-09 15:31:11 +01:00
|
|
|
LikeInputBean likeInputBean) throws ValidationException {
|
2023-02-09 12:47:01 +01:00
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
logger.debug("Request of unlike coming from user " + username);
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
|
|
Status status = Status.OK;
|
2023-02-09 15:31:11 +01:00
|
|
|
boolean likeResultOperation = SocialUtils.unlike(username, likeInputBean.getLikeid(), likeInputBean.getPostid());
|
2023-02-09 12:47:01 +01:00
|
|
|
if (likeResultOperation)
|
2024-04-22 11:19:17 +02:00
|
|
|
logger.debug("Unlike OK to postId " + likeInputBean.getPostid());
|
2023-02-09 12:47:01 +01:00
|
|
|
else {
|
2023-02-09 15:31:11 +01:00
|
|
|
logger.error("Unable to unlike this post"+ likeInputBean.getPostid());
|
2023-02-09 12:47:01 +01:00
|
|
|
responseBean.setMessage("Unable to unlike, something went wrong please see server log");
|
|
|
|
responseBean.setSuccess(false);
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
responseBean.setResult(true);
|
|
|
|
responseBean.setSuccess(true);
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2022-10-25 18:38:20 +02:00
|
|
|
}
|