gcat/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThre...

200 lines
6.5 KiB
Java
Raw Normal View History

package org.gcube.gcat.moderation.thread.social;
2022-03-25 18:38:26 +01:00
import java.io.StringWriter;
import java.util.Set;
2022-03-29 15:17:30 +02:00
import org.gcube.common.authorization.utils.manager.SecretManager;
2022-03-25 18:38:26 +01:00
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
2022-03-29 15:17:30 +02:00
import org.gcube.common.authorization.utils.secret.Secret;
2022-03-25 18:38:26 +01:00
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.moderation.thread.ModerationThread;
2022-03-25 18:38:26 +01:00
import org.gcube.gcat.social.Message;
2022-03-29 15:17:30 +02:00
import org.gcube.gcat.social.SocialMessage;
2022-03-25 18:38:26 +01:00
import org.gcube.gcat.social.SocialUsers;
2022-03-29 15:17:30 +02:00
import org.gcube.gcat.utils.Constants;
2022-03-25 18:38:26 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SocialMessageModerationThread extends ModerationThread {
2022-03-25 18:38:26 +01:00
private static final Logger logger = LoggerFactory.getLogger(SocialMessageModerationThread.class);
2022-03-25 18:38:26 +01:00
2022-04-07 16:24:46 +02:00
protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) {
2022-04-06 17:15:46 +02:00
stringBuffer.append("Status: ");
stringBuffer.append(cmItemStatus.getFancyValue());
stringBuffer.append("\n");
stringBuffer.append("Title: ");
stringBuffer.append(itemTitle);
stringBuffer.append("\n");
stringBuffer.append("Name: ");
stringBuffer.append(itemName);
stringBuffer.append("\n");
stringBuffer.append("ID: ");
stringBuffer.append(itemID);
stringBuffer.append("\n");
stringBuffer.append("URL: ");
stringBuffer.append(itemURL);
stringBuffer.append("\n\n");
return stringBuffer;
}
2022-04-07 16:24:46 +02:00
/**
* Create the message for an item that is created/updated
*/
public void postItemToBeManaged() throws Exception {
/*
* An example of created message is:
*
* [mister x] created / updated the following item
*
* Title: RESTful Transaction Model
* Name: my_first_restful_transaction_model
* ID: e31a6ba8-66ef-47b8-b61f-99a1366b4a69
* URL: https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model
*
* You are kindly requested to review it and decide either to APPROVE or REJECT it at <url of the item>
*
*/
cmItemStatus = CMItemStatus.PENDING;
2022-04-06 17:15:46 +02:00
String fullName = ckanUser.getNameSurname();
2022-04-07 16:24:46 +02:00
StringBuffer stringBuffer = new StringBuffer();
2022-04-06 17:15:46 +02:00
stringBuffer.append(fullName);
2022-04-07 16:24:46 +02:00
stringBuffer.append(create ? " created " : " updated ");
stringBuffer.append("the following item\n\n");
stringBuffer = getMainItemInfo(stringBuffer);
stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it.");
// stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT at");
// String manageURL = getManageURL();
// stringBuffer.append(manageURL);
postMessage(stringBuffer.toString());
}
public void postItemCreated() throws Exception {
create = true;
cmItemStatus = CMItemStatus.PENDING;
postItemToBeManaged();
2022-04-06 17:15:46 +02:00
}
public void postItemUpdated() throws Exception {
2022-04-07 16:24:46 +02:00
create = false;
cmItemStatus = CMItemStatus.PENDING;
postItemToBeManaged();
2022-04-06 17:15:46 +02:00
}
protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) {
stringBuffer.append(fullName);
stringBuffer.append(" [");
stringBuffer.append(role);
stringBuffer.append("] ");
return stringBuffer;
}
2022-04-07 16:24:46 +02:00
public void postItemManaged(String userMessage) throws Exception {
create = false;
2022-04-06 17:15:46 +02:00
String fullName = ckanUser.getNameSurname();
2022-04-07 16:24:46 +02:00
StringBuffer stringBuffer = new StringBuffer();
2022-04-06 17:15:46 +02:00
stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer);
2022-04-07 16:24:46 +02:00
stringBuffer.append(cmItemStatus.getValue());
stringBuffer.append(" the following item");
2022-04-06 17:15:46 +02:00
if(userMessage!=null && userMessage.length()>0) {
2022-04-07 16:24:46 +02:00
stringBuffer.append(" with this accompanying message\n\n");
stringBuffer = getMainItemInfo(stringBuffer);
2022-04-06 17:15:46 +02:00
stringBuffer.append(userMessage);
2022-04-07 16:24:46 +02:00
}else {
2022-04-06 17:15:46 +02:00
stringBuffer.append("\n\n");
2022-04-07 16:24:46 +02:00
stringBuffer = getMainItemInfo(stringBuffer);
2022-04-06 17:15:46 +02:00
}
2022-04-07 16:24:46 +02:00
postMessage(stringBuffer.toString());
}
@Override
public void postItemRejected(String userMessage) throws Exception {
create = false;
cmItemStatus = CMItemStatus.REJECTED;
postItemManaged(userMessage);
2022-04-06 17:15:46 +02:00
}
2022-04-07 16:24:46 +02:00
@Override
public void postItemApproved(String userMessage) throws Exception {
create = false;
cmItemStatus = CMItemStatus.APPROVED;
postItemManaged(userMessage);
}
2022-04-06 17:15:46 +02:00
2022-04-07 16:24:46 +02:00
protected String getSubject() {
2022-03-25 18:38:26 +01:00
StringWriter stringWriter = new StringWriter();
2022-04-07 16:24:46 +02:00
if(!create) {
logger.trace("It's a reply");
stringWriter.append("Re: [Catalogue Service] ");
}
2022-04-06 17:15:46 +02:00
stringWriter.append("[Catalogue Service] ");
stringWriter.append(itemTitle);
2022-03-25 18:38:26 +01:00
return stringWriter.toString();
}
2022-04-07 16:24:46 +02:00
protected Message getMessage(String messageString) throws Exception {
2022-03-25 18:38:26 +01:00
Message message = new Message();
message.setMessage(messageString);
2022-04-07 16:24:46 +02:00
message.setSubject(getSubject());
2022-03-25 18:38:26 +01:00
Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
message.setUsers(moderators);
return message;
}
@Override
2022-04-07 16:24:46 +02:00
protected void postMessage(String messageString) throws Exception {
2022-03-29 15:17:30 +02:00
SecretManager secretManager = SecretManagerProvider.instance.get();
String username = secretManager.getUser().getUsername();
2022-04-07 16:24:46 +02:00
Message message = getMessage(messageString);
2022-03-29 15:17:30 +02:00
message.addUser(username);
Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret);
2022-03-31 15:49:22 +02:00
try {
sendMessage(message);
}finally {
secretManager.endSession();
}
2022-03-25 18:38:26 +01:00
}
@Override
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {
2022-04-07 16:24:46 +02:00
this.create = false;
this.cmItemStatus = cmItemStatus;
2022-04-06 17:15:46 +02:00
String fullName = ckanUser.getNameSurname();
2022-04-07 16:24:46 +02:00
StringBuffer stringBuffer = new StringBuffer();
stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer);
stringBuffer.append("sent a message regarding the following item\n\n");
stringBuffer = getMainItemInfo(stringBuffer);
2022-04-06 17:15:46 +02:00
stringBuffer.append(userMessage);
2022-04-07 16:24:46 +02:00
Message message = getMessage(stringBuffer.toString());
2022-04-06 17:15:46 +02:00
SecretManager secretManager = SecretManagerProvider.instance.get();
String username = secretManager.getUser().getUsername();
message.addUser(username);
Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret);
try {
sendMessage(message);
}finally {
secretManager.endSession();
}
2022-03-25 18:38:26 +01:00
}
2022-03-29 15:17:30 +02:00
protected void sendMessage(Message message) throws Exception {
SocialMessage socialMessage = new SocialMessage();
socialMessage.setMessage(message);
socialMessage.start();
}
2022-04-07 16:24:46 +02:00
2022-03-25 18:38:26 +01:00
@Override
protected void createModerationThread() throws Exception {
2022-04-07 16:24:46 +02:00
create = true;
cmItemStatus = CMItemStatus.PENDING;
2022-03-25 18:38:26 +01:00
}
2022-04-07 16:24:46 +02:00
2022-03-25 18:38:26 +01:00
}