200 lines
7.5 KiB
Java
200 lines
7.5 KiB
Java
package org.gcube.gcat.zulip;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import org.gcube.gcat.api.CMItemStatus;
|
|
import org.gcube.gcat.persistence.ckan.CKANUser;
|
|
import org.gcube.gcat.utils.Constants;
|
|
import org.gcube.storagehub.ApplicationMode;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import io.taliox.zulip.ZulipRestExecutor;
|
|
import io.taliox.zulip.calls.ZulipRestAPICall;
|
|
import io.taliox.zulip.calls.messages.PostMessage;
|
|
import io.taliox.zulip.calls.streams.GetStreamID;
|
|
import io.taliox.zulip.calls.streams.PostCreateStream;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class ZulipStream {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ZulipStream.class);
|
|
|
|
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;
|
|
protected ZulipRestExecutor userZulipRestExecutor;
|
|
|
|
protected String itemID;
|
|
protected String itemName;
|
|
|
|
protected String streamName;
|
|
protected String streamDescription;
|
|
|
|
protected CKANUser ckanUser;
|
|
|
|
protected ObjectMapper objectMapper;
|
|
|
|
protected ZulipRestExecutor getZulipRestExecutor() {
|
|
// ZulipAuth zulipAuth = new ZulipAuth(ContextUtility.getUsername());
|
|
// return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite());
|
|
return null;
|
|
}
|
|
|
|
public ZulipStream() {
|
|
this.objectMapper = new ObjectMapper();
|
|
}
|
|
|
|
public ZulipRestExecutor getGCatZulipRestExecutor() {
|
|
if(gCatZulipRestExecutor==null) {
|
|
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
|
|
applicationMode.start();
|
|
gCatZulipRestExecutor = getZulipRestExecutor();
|
|
applicationMode.end();
|
|
}
|
|
return gCatZulipRestExecutor;
|
|
}
|
|
|
|
public ZulipRestExecutor getUserZulipRestExecutor() {
|
|
if(userZulipRestExecutor==null) {
|
|
userZulipRestExecutor = getZulipRestExecutor();
|
|
}
|
|
return userZulipRestExecutor;
|
|
}
|
|
|
|
public void setItemCoordinates(String itemID, String itemName) {
|
|
this.itemID = itemID;
|
|
this.itemName = itemName;
|
|
}
|
|
|
|
public void setCKANUser(CKANUser ckanUser) {
|
|
this.ckanUser = ckanUser;
|
|
}
|
|
|
|
protected String getStreamName() {
|
|
if(streamName==null) {
|
|
streamName = String.format("Item '%s' moderation", itemID);
|
|
}
|
|
return streamName;
|
|
}
|
|
|
|
protected Integer getStreamID() throws Exception {
|
|
GetStreamID getStreamID = new GetStreamID(getStreamName());
|
|
ZulipResponse zulipResponse = executeZulipCall(gCatZulipRestExecutor, getStreamID);
|
|
JsonNode response = zulipResponse.getResponse();
|
|
return response.get("stream_id").asInt();
|
|
}
|
|
|
|
protected String getStreamDescription() {
|
|
if(streamDescription==null) {
|
|
streamDescription = String.format("This stream is used to discuss about the moderation of the item '%s' with id '%s'", itemName, itemID);
|
|
}
|
|
return streamDescription;
|
|
}
|
|
|
|
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;
|
|
return null;
|
|
}
|
|
|
|
public void create() throws Exception {
|
|
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.getPortalUser().getEMail();
|
|
principalsArrayNode.add(itemCreatorEmail);
|
|
|
|
getGCatZulipRestExecutor();
|
|
|
|
principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName());
|
|
// Going to add the catalogue moderators
|
|
Set<String> moderators = getCatalogueModerators();
|
|
for(String moderator : moderators) {
|
|
principalsArrayNode.add(moderator);
|
|
}
|
|
|
|
PostCreateStream postCreateStream = new PostCreateStream(streamsArrayNode.toString());
|
|
postCreateStream.setPrincipals(principalsArrayNode.toString());
|
|
postCreateStream.setInvite_only(true);
|
|
postCreateStream.setAnnounce(false);
|
|
|
|
executeZulipCall(gCatZulipRestExecutor, postCreateStream);
|
|
|
|
postItemCreated();
|
|
}
|
|
|
|
private Set<String> getCatalogueModerators() {
|
|
Set<String> moderators = new HashSet<>();
|
|
// moderators.add("pasquale.pagano@isti.cnr.it");
|
|
// moderators.add("leonardo.candela@isti.cnr.it");
|
|
return moderators;
|
|
}
|
|
|
|
protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, CMItemStatus cmItemStatus, String message) throws Exception {
|
|
PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message);
|
|
logger.debug("Going to send the following message: {}", message);
|
|
executeZulipCall(zulipRestExecutor, postMessage);
|
|
}
|
|
|
|
public void postUserMessageToStream(CMItemStatus cmItemStatus, String message) throws Exception {
|
|
postMessageToStream(getUserZulipRestExecutor(), cmItemStatus, message);
|
|
}
|
|
|
|
private void postItemCreated() throws Exception{
|
|
String username = ckanUser.getPortalUser().getNameSurname();
|
|
CMItemStatus cmItemStatus = CMItemStatus.PENDING;
|
|
String message = String.format("@**%s** has created the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.",
|
|
username, itemName, itemID, cmItemStatus.getFancyValue());
|
|
postMessageToStream(getGCatZulipRestExecutor(), cmItemStatus, message);
|
|
}
|
|
|
|
public void postItemUpdated() throws Exception{
|
|
String username = ckanUser.getPortalUser().getNameSurname();
|
|
CMItemStatus cmItemStatus = CMItemStatus.PENDING;
|
|
String message = String.format("@**%s** has updated the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.",
|
|
username, itemName, itemID, cmItemStatus.getFancyValue());
|
|
postMessageToStream(getGCatZulipRestExecutor(), cmItemStatus, message);
|
|
}
|
|
|
|
public void postItemRejected(String userMessage) throws Exception {
|
|
String username = ckanUser.getPortalUser().getNameSurname();
|
|
CMItemStatus cmItemStatus = CMItemStatus.REJECTED;
|
|
String message = String.format("@**%s** has **%s** the item with name '%s' (id='%s'). The author can delete the item or update it to try to meet moderators requests if any.",
|
|
username, cmItemStatus.getFancyValue(), itemName, itemID);
|
|
postMessageToStream(getGCatZulipRestExecutor(), cmItemStatus, message);
|
|
postUserMessageToStream(cmItemStatus, userMessage);
|
|
}
|
|
|
|
public void postItemApproved(String userMessage) throws Exception{
|
|
String username = ckanUser.getPortalUser().getNameSurname();
|
|
CMItemStatus cmItemStatus = CMItemStatus.APPROVED;
|
|
String message = String.format("@**%s** has **%s** the item with name '%s' (id='%s'). The item is now available in the catalogue.",
|
|
username, cmItemStatus.getFancyValue(), itemName, itemID);
|
|
postMessageToStream(getGCatZulipRestExecutor(), cmItemStatus, message);
|
|
postUserMessageToStream(cmItemStatus, userMessage);
|
|
}
|
|
|
|
}
|