commit comment
This commit is contained in:
parent
db5834fcef
commit
9f696b7f54
|
@ -1,12 +1,17 @@
|
||||||
package org.gcube.portal.social.networking.ws.methods.v2;
|
package org.gcube.portal.social.networking.ws.methods.v2;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
import javax.validation.ValidationException;
|
import javax.validation.ValidationException;
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
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.authorization.library.utils.Caller;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.portal.databook.shared.Comment;
|
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.outputs.ResponseBean;
|
||||||
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
|
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.ErrorMessages;
|
||||||
import org.gcube.portal.social.networking.ws.utils.Filters;
|
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 org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
||||||
|
@ -153,4 +167,85 @@ public class Comments {
|
||||||
|
|
||||||
return Response.status(status).entity(responseBean).build();
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -236,7 +236,7 @@ public class Posts {
|
||||||
|
|
||||||
// try to share
|
// try to share
|
||||||
logger.debug("Trying to share user post...");
|
logger.debug("Trying to share user post...");
|
||||||
Feed res = SocialUtils.shareUserUpdate(
|
Post res = SocialUtils.shareUserUpdate(
|
||||||
username,
|
username,
|
||||||
postText,
|
postText,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -26,6 +26,8 @@ import org.gcube.common.scope.impl.ScopeBean;
|
||||||
import org.gcube.portal.databook.shared.ApplicationProfile;
|
import org.gcube.portal.databook.shared.ApplicationProfile;
|
||||||
import org.gcube.portal.databook.shared.Feed;
|
import org.gcube.portal.databook.shared.Feed;
|
||||||
import org.gcube.portal.databook.shared.FeedType;
|
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.PrivacyLevel;
|
||||||
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException;
|
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException;
|
||||||
import org.gcube.portal.notifications.bean.GenericItemBean;
|
import org.gcube.portal.notifications.bean.GenericItemBean;
|
||||||
|
@ -372,7 +374,7 @@ public class SocialUtils {
|
||||||
* @param notifyGroup
|
* @param notifyGroup
|
||||||
* @return The written Feed
|
* @return The written Feed
|
||||||
*/
|
*/
|
||||||
public static Feed shareUserUpdate(
|
public static Post shareUserUpdate(
|
||||||
String userId,
|
String userId,
|
||||||
String postText,
|
String postText,
|
||||||
String vreId,
|
String vreId,
|
||||||
|
@ -423,12 +425,12 @@ public class SocialUtils {
|
||||||
textToPost = escapedPostText;
|
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);
|
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);
|
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) {
|
if(vreId != null && vreId.compareTo("") != 0 && result) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue