gcat/src/main/java/org/gcube/gcat/moderation/thread/zulip/ZulipStream.java

154 lines
5.6 KiB
Java
Raw Normal View History

2021-11-26 12:00:13 +01:00
package org.gcube.gcat.moderation.thread.zulip;
2021-11-17 17:47:58 +01:00
import java.util.Set;
import javax.ws.rs.InternalServerErrorException;
2021-11-18 16:53:46 +01:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
2021-11-17 17:47:58 +01:00
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.moderation.thread.ModerationThread;
2021-11-26 12:00:13 +01:00
import org.gcube.gcat.moderation.thread.zulip.ZulipResponse.Result;
import org.gcube.gcat.social.SocialUsers;
2021-11-23 17:53:37 +01:00
import org.gcube.gcat.utils.Constants;
2021-11-17 17:47:58 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.taliox.zulip.ZulipRestExecutor;
2021-11-18 16:53:46 +01:00
import io.taliox.zulip.calls.ZulipRestAPICall;
2021-11-17 17:47:58 +01:00
import io.taliox.zulip.calls.messages.PostMessage;
2021-11-18 16:53:46 +01:00
import io.taliox.zulip.calls.streams.GetStreamID;
2021-11-17 17:47:58 +01:00
import io.taliox.zulip.calls.streams.PostCreateStream;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ZulipStream extends ModerationThread {
2021-11-17 17:47:58 +01:00
private static final Logger logger = LoggerFactory.getLogger(ZulipStream.class);
2021-11-23 17:53:37 +01:00
public static final String TOPICS_KEY = "topics";
public static final String NAME_KEY = "name";
public static final String MAX_ID_KEY = "max_id";
public static final String INITIAL_TOPIC_NAME = "hello";
protected ZulipRestExecutor gCatZulipRestExecutor;
2021-11-18 17:49:24 +01:00
protected ZulipRestExecutor userZulipRestExecutor;
2021-11-17 17:47:58 +01:00
protected String streamName;
protected String streamDescription;
public ZulipStream() {
super();
}
2021-11-18 16:53:46 +01:00
2021-11-23 17:53:37 +01:00
protected ZulipRestExecutor getZulipRestExecutor() {
ZulipAuth zulipAuth = new ZulipAuth(SecretManagerProvider.instance.get().getUser().getUsername());
return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite());
2021-11-23 17:53:37 +01:00
}
public ZulipRestExecutor getGCatZulipRestExecutor() throws Exception {
2021-11-23 17:53:37 +01:00
if(gCatZulipRestExecutor==null) {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret);
2021-11-23 17:53:37 +01:00
gCatZulipRestExecutor = getZulipRestExecutor();
secretManager.endSession();
2021-11-23 17:53:37 +01:00
}
return gCatZulipRestExecutor;
2021-11-17 17:47:58 +01:00
}
2021-11-18 17:49:24 +01:00
public ZulipRestExecutor getUserZulipRestExecutor() {
if(userZulipRestExecutor==null) {
2021-11-23 17:53:37 +01:00
userZulipRestExecutor = getZulipRestExecutor();
2021-11-18 17:49:24 +01:00
}
return userZulipRestExecutor;
}
2021-11-17 17:47:58 +01:00
protected String getStreamName() {
if(streamName==null) {
2021-11-23 17:53:37 +01:00
streamName = String.format("Item '%s' moderation", itemID);
2021-11-17 17:47:58 +01:00
}
return streamName;
}
2021-11-18 16:53:46 +01:00
protected Integer getStreamID() throws Exception {
GetStreamID getStreamID = new GetStreamID(getStreamName());
2021-11-23 17:53:37 +01:00
ZulipResponse zulipResponse = executeZulipCall(gCatZulipRestExecutor, getStreamID);
2021-11-18 16:53:46 +01:00
JsonNode response = zulipResponse.getResponse();
return response.get("stream_id").asInt();
}
2021-11-17 17:47:58 +01:00
protected String getStreamDescription() {
if(streamDescription==null) {
2021-11-23 17:53:37 +01:00
streamDescription = String.format("This stream is used to discuss about the moderation of the item '%s' with id '%s'", itemName, itemID);
2021-11-17 17:47:58 +01:00
}
return streamDescription;
}
2021-11-23 17:53:37 +01:00
protected ZulipResponse executeZulipCall(ZulipRestExecutor zulipRestExecutor, ZulipRestAPICall call) throws Exception {
logger.trace("Going to execute {}", call);
String responseString = zulipRestExecutor.executeCall(call);
logger.trace("Response from {} is {}", call.getClass().getSimpleName(), responseString);
ZulipResponse zulipResponse = new ZulipResponse(responseString);
if(zulipResponse.getResponseResult()==Result.error) {
throw new InternalServerErrorException(zulipResponse.getResponseMessage());
}
return zulipResponse;
2021-11-18 16:53:46 +01:00
}
2021-11-25 14:20:08 +01:00
@Override
protected void createModerationThread() throws Exception {
2021-11-17 17:47:58 +01:00
ArrayNode streamsArrayNode = objectMapper.createArrayNode();
ObjectNode streamobjectNode = objectMapper.createObjectNode();
streamobjectNode.put("name", getStreamName());
streamobjectNode.put("description", getStreamDescription());
streamsArrayNode.add(streamobjectNode);
ArrayNode principalsArrayNode = objectMapper.createArrayNode();
// Going to add the item creator
String itemCreatorEmail = ckanUser.getEMail();
2021-11-17 17:47:58 +01:00
principalsArrayNode.add(itemCreatorEmail);
2021-11-23 17:53:37 +01:00
getGCatZulipRestExecutor();
principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName());
2021-11-17 17:47:58 +01:00
// Going to add the catalogue moderators
Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
2021-11-17 17:47:58 +01:00
for(String moderator : moderators) {
principalsArrayNode.add(moderator);
}
PostCreateStream postCreateStream = new PostCreateStream(streamsArrayNode.toString());
postCreateStream.setPrincipals(principalsArrayNode.toString());
postCreateStream.setInvite_only(true);
postCreateStream.setAnnounce(false);
2021-11-23 17:53:37 +01:00
executeZulipCall(gCatZulipRestExecutor, postCreateStream);
2021-11-17 17:47:58 +01:00
}
2022-04-07 16:24:46 +02:00
protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, String message) throws Exception {
2021-11-18 17:49:24 +01:00
PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message);
2021-11-23 17:53:37 +01:00
logger.debug("Going to send the following message: {}", message);
executeZulipCall(zulipRestExecutor, postMessage);
2021-11-17 17:47:58 +01:00
}
@Override
2022-04-07 16:24:46 +02:00
protected void postMessage(String message) throws Exception {
postMessageToStream(getGCatZulipRestExecutor(), message);
2021-11-17 17:47:58 +01:00
}
@Override
public void postUserMessage(CMItemStatus cmItemStatus, String message) throws Exception {
2022-04-07 16:24:46 +02:00
this.cmItemStatus = cmItemStatus;
postMessageToStream(getUserZulipRestExecutor(), message);
2021-11-22 16:38:56 +01:00
}
2021-11-17 17:47:58 +01:00
}