Adding Zulip interaction

This commit is contained in:
Luca Frosini 2021-11-17 17:47:58 +01:00
parent 8a1a7738bc
commit 89b98f625f
3 changed files with 144 additions and 4 deletions

View File

@ -150,6 +150,11 @@
<version>[1.0.0,2.0.0-SNAPSHOT)</version> <version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<dependency>
<groupId>io.taliox</groupId>
<artifactId>zulip-java-rest</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Test libraries --> <!-- Test libraries -->
<dependency> <dependency>

View File

@ -31,6 +31,7 @@ import org.gcube.gcat.oldutils.Validator;
import org.gcube.gcat.profile.MetadataUtility; import org.gcube.gcat.profile.MetadataUtility;
import org.gcube.gcat.social.PortalUser; import org.gcube.gcat.social.PortalUser;
import org.gcube.gcat.social.SocialPost; import org.gcube.gcat.social.SocialPost;
import org.gcube.gcat.social.ZulipStream;
import org.gcube.gcat.utils.URIResolver; import org.gcube.gcat.utils.URIResolver;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -102,7 +103,6 @@ public class CKANPackage extends CKAN {
protected static final String SEARCHABLE_KEY = "searchable"; protected static final String SEARCHABLE_KEY = "searchable";
protected static final String CAPACITY_KEY = "capacity"; protected static final String CAPACITY_KEY = "capacity";
protected static final String CM_STATUS_QUERY_FILTER_KEY = "extras_systemcm_item_status"; protected static final String CM_STATUS_QUERY_FILTER_KEY = "extras_systemcm_item_status";
protected static final String INCLUDE_PRIVATE_KEY = "include_private"; protected static final String INCLUDE_PRIVATE_KEY = "include_private";
@ -121,6 +121,7 @@ public class CKANPackage extends CKAN {
protected final Set<String> supportedOrganizations; protected final Set<String> supportedOrganizations;
protected String moderationMessage; protected String moderationMessage;
protected ZulipStream zulipStream;
public CKANPackage() { public CKANPackage() {
super(); super();
@ -647,7 +648,11 @@ public class CKANPackage extends CKAN {
} }
protected boolean isModerationEnabled() { protected boolean isModerationEnabled() {
return ckanInstance.isModerationEnabled(); boolean moderationEnabled = ckanInstance.isModerationEnabled();
if(moderationEnabled && zulipStream==null) {
zulipStream = new ZulipStream("user@zulip.com", "apikey");
}
return moderationEnabled;
} }
@Override @Override
@ -755,6 +760,11 @@ public class CKANPackage extends CKAN {
sendSocialPost(title, catalogueItemURL); sendSocialPost(title, catalogueItemURL);
} }
if(isModerationEnabled()) {
createNewStream();
postItemCreatedToStream();
}
result = cleanResult(result); result = cleanResult(result);
return getAsString(result); return getAsString(result);
@ -949,7 +959,22 @@ public class CKANPackage extends CKAN {
// super.purge(); // super.purge();
} }
// Moderation Related functions /*
* ----------------------------------------------------------------------------------------
* Moderation Related functions
* ----------------------------------------------------------------------------------------
*
*/
private void createNewStream() {
String itemName = result.get(name).asText();
zulipStream.setItemCoordinates(itemID, itemName);
zulipStream.setCKANUser(ckanUser);
zulipStream.create();
}
private void postItemCreatedToStream() {
zulipStream.postItemCreatedToStream();
}
protected JsonNode checkModerationUpdate(JsonNode jsonNode) throws Exception{ protected JsonNode checkModerationUpdate(JsonNode jsonNode) throws Exception{
PortalUser portalUser = ckanUser.getPortalUser(); PortalUser portalUser = ckanUser.getPortalUser();
@ -1006,6 +1031,8 @@ public class CKANPackage extends CKAN {
protected void setToRejected(JsonNode jsonNode) { protected void setToRejected(JsonNode jsonNode) {
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.REJECTED.getValue()); addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.REJECTED.getValue());
} }

View File

@ -0,0 +1,108 @@
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);
}
}