gcat/src/main/java/org/gcube/gcat/social/ZulipStream.java

109 lines
3.3 KiB
Java

package org.gcube.gcat.social;
import java.util.HashSet;
import java.util.Set;
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.persistence.ckan.CKANUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.taliox.zulip.ZulipRestExecutor;
import io.taliox.zulip.calls.messages.PostMessage;
import io.taliox.zulip.calls.streams.PostCreateStream;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ZulipStream {
private static final Logger logger = LoggerFactory.getLogger(ZulipStream.class);
protected String itemID;
protected String itemName;
protected String streamName;
protected String streamDescription;
protected CKANUser ckanUser;
protected Set<String> moderators;
protected ObjectMapper objectMapper;
protected ZulipRestExecutor zulipRestExecutor;
public ZulipStream(String email, String password) {
this.zulipRestExecutor = new ZulipRestExecutor(email, password,"https://zulip.example.com/");
this.objectMapper = new ObjectMapper();
this.moderators = new HashSet<>();
}
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 '{}' with id '{}' moderation", itemName, itemID);
}
return streamName;
}
protected String getStreamDescription() {
if(streamDescription==null) {
streamDescription = String.format("This stream is used to discuss about the moderation of the item '{}' with id '{}'", itemName, itemID);
}
return streamDescription;
}
public void create() {
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);
// Going to add the catalogue moderators
for(String moderator : moderators) {
principalsArrayNode.add(moderator);
}
PostCreateStream postCreateStream = new PostCreateStream(streamsArrayNode.toString());
postCreateStream.setPrincipals(principalsArrayNode.toString());
postCreateStream.setInvite_only(true);
postCreateStream.setAnnounce(false);
String response = zulipRestExecutor.executeCall(postCreateStream);
logger.trace(response);
}
private void postMessageToStream(String topic, String message) {
PostMessage postMessage = new PostMessage(getStreamName(), topic, message);
String response = zulipRestExecutor.executeCall(postMessage);
logger.trace(response);
}
public void postItemCreatedToStream(){
String message = "";
String topic = "Item Creation";
postMessageToStream(topic, message);
}
public void postItemRejectedToStream() {
String message = "";
String topic = "Item Rejected";
postMessageToStream(topic, message);
}
}