improving the zulip integration
This commit is contained in:
parent
f00127a82a
commit
bdbb954ba5
|
@ -13,6 +13,7 @@ import java.util.regex.Pattern;
|
|||
import javax.ws.rs.BadRequestException;
|
||||
import javax.ws.rs.ForbiddenException;
|
||||
import javax.ws.rs.InternalServerErrorException;
|
||||
import javax.ws.rs.NotAllowedException;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
@ -647,6 +648,11 @@ public class CKANPackage extends CKAN {
|
|||
return cmItemStatus;
|
||||
}
|
||||
|
||||
protected boolean isItemCreator() {
|
||||
return result.get(AUTHOR_EMAIL_KEY).asText().compareTo(ckanUser.getPortalUser().getEMail())==0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String read() {
|
||||
try {
|
||||
|
@ -664,7 +670,7 @@ public class CKANPackage extends CKAN {
|
|||
|
||||
PortalUser portalUser = ckanUser.getPortalUser();
|
||||
|
||||
if(result.get(AUTHOR_EMAIL_KEY).asText().compareTo(portalUser.getEMail())==0) {
|
||||
if(isItemCreator()) {
|
||||
// The author is entitled to read its own items independently from the status
|
||||
return getAsString(result);
|
||||
}
|
||||
|
@ -961,14 +967,18 @@ public class CKANPackage extends CKAN {
|
|||
protected boolean isModerationEnabled() {
|
||||
boolean moderationEnabled = ckanInstance.isModerationEnabled();
|
||||
if(moderationEnabled && zulipStream==null) {
|
||||
zulipStream = new ZulipStream("user@zulip.com", "apikey");
|
||||
zulipStream = new ZulipStream();
|
||||
}
|
||||
return moderationEnabled;
|
||||
}
|
||||
|
||||
// private String getItemName() {
|
||||
// String itemName = result.get(name).asText();
|
||||
// return itemName;
|
||||
// }
|
||||
|
||||
private void createNewStream() throws Exception {
|
||||
String itemName = result.get(name).asText();
|
||||
zulipStream.setItemCoordinates(itemID, itemName);
|
||||
zulipStream.setItemCoordinates(itemID, name);
|
||||
zulipStream.setCKANUser(ckanUser);
|
||||
zulipStream.create();
|
||||
}
|
||||
|
@ -1099,8 +1109,14 @@ public class CKANPackage extends CKAN {
|
|||
result = mapper.readTree(ret);
|
||||
result = cleanResult(result);
|
||||
|
||||
return getAsString(result);
|
||||
ZulipStream zulipStream = new ZulipStream();
|
||||
zulipStream.setItemCoordinates(itemID, name);
|
||||
zulipStream.postItemCreatedToStream();
|
||||
if(moderationMessage!=null && moderationMessage.compareTo("")!=0) {
|
||||
zulipStream.postMessageToStream(CMItemStatus.PENDING, moderationMessage);
|
||||
}
|
||||
|
||||
return getAsString(result);
|
||||
}
|
||||
|
||||
throw new MethodNotSupportedException("The approve operation is available only in moderation mode");
|
||||
|
@ -1141,8 +1157,14 @@ public class CKANPackage extends CKAN {
|
|||
result = mapper.readTree(ret);
|
||||
result = cleanResult(result);
|
||||
|
||||
return getAsString(result);
|
||||
ZulipStream zulipStream = new ZulipStream();
|
||||
zulipStream.setItemCoordinates(itemID, name);
|
||||
zulipStream.postItemRejectedToStream();
|
||||
if(moderationMessage!=null && moderationMessage.compareTo("")!=0) {
|
||||
zulipStream.postMessageToStream(CMItemStatus.REJECTED, moderationMessage);
|
||||
}
|
||||
|
||||
return getAsString(result);
|
||||
}
|
||||
|
||||
throw new MethodNotSupportedException("The reject operation is available only in moderation mode");
|
||||
|
@ -1157,7 +1179,26 @@ public class CKANPackage extends CKAN {
|
|||
public void message() {
|
||||
try {
|
||||
if(isModerationEnabled()) {
|
||||
// TODO
|
||||
if(moderationMessage==null || moderationMessage.compareTo("")==0) {
|
||||
return;
|
||||
}
|
||||
|
||||
read();
|
||||
|
||||
// Catalogue Moderators are allowed to post message to the dedicated Stream
|
||||
if(!ckanUser.getPortalUser().isCatalogueModerator()) {
|
||||
// Users that are not
|
||||
if(!isItemCreator()) {
|
||||
throw new NotAllowedException("Only item creator and " + GCatConstants.CATALOGUE_MODERATOR + "s are entitled to partecipate to the moderation discussion thread.");
|
||||
}
|
||||
}
|
||||
|
||||
ZulipStream zulipStream = new ZulipStream();
|
||||
zulipStream.setItemCoordinates(itemID, name);
|
||||
|
||||
CMItemStatus cmItemStatus = getCMItemStatus();
|
||||
|
||||
zulipStream.postMessageToStream(cmItemStatus, moderationMessage);
|
||||
}
|
||||
throw new MethodNotSupportedException("The message operation is available only in moderation mode");
|
||||
}catch(WebApplicationException e) {
|
||||
|
|
|
@ -9,6 +9,7 @@ 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.zulip.ZulipResponse.Result;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -27,6 +28,9 @@ public class ZulipStream {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ZulipStream.class);
|
||||
|
||||
protected final ZulipRestExecutor gCatZulipRestExecutor;
|
||||
protected ZulipRestExecutor userZulipRestExecutor;
|
||||
|
||||
protected String itemID;
|
||||
protected String itemName;
|
||||
|
||||
|
@ -38,14 +42,23 @@ public class ZulipStream {
|
|||
|
||||
protected ObjectMapper objectMapper;
|
||||
|
||||
protected ZulipRestExecutor zulipRestExecutor;
|
||||
|
||||
public ZulipStream(String email, String password) {
|
||||
this.zulipRestExecutor = new ZulipRestExecutor(email, password,"https://zulip.example.com/");
|
||||
public ZulipStream() {
|
||||
// TODO Get gcat email and API key from configuration
|
||||
// TODO Get zulip URL from configuration
|
||||
this.gCatZulipRestExecutor = new ZulipRestExecutor("gcat@d4science.com", "apikey", "https://zulip.example.com/");
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.moderators = new HashSet<>();
|
||||
}
|
||||
|
||||
public ZulipRestExecutor getUserZulipRestExecutor() {
|
||||
if(userZulipRestExecutor==null) {
|
||||
// TODO get API key ??
|
||||
// TODO Get zulip URL from configuration
|
||||
userZulipRestExecutor = new ZulipRestExecutor(ckanUser.getPortalUser().getEMail(), "apikey", "https://zulip.example.com/");
|
||||
}
|
||||
return userZulipRestExecutor;
|
||||
}
|
||||
|
||||
public void setItemCoordinates(String itemID, String itemName) {
|
||||
this.itemID = itemID;
|
||||
this.itemName = itemName;
|
||||
|
@ -77,7 +90,7 @@ public class ZulipStream {
|
|||
}
|
||||
|
||||
protected ZulipResponse executeZulipCall(ZulipRestAPICall call) throws Exception {
|
||||
String responseString = zulipRestExecutor.executeCall(call);
|
||||
String responseString = gCatZulipRestExecutor.executeCall(call);
|
||||
ZulipResponse zulipResponse = new ZulipResponse(responseString);
|
||||
if(zulipResponse.getResponseResult()==Result.error) {
|
||||
throw new InternalServerErrorException(zulipResponse.getResponseMessage());
|
||||
|
@ -110,22 +123,24 @@ public class ZulipStream {
|
|||
|
||||
}
|
||||
|
||||
private void postMessageToStream(String topic, String message) {
|
||||
PostMessage postMessage = new PostMessage(getStreamName(), topic, message);
|
||||
protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, CMItemStatus cmItemStatus, String message) {
|
||||
PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message);
|
||||
String response = zulipRestExecutor.executeCall(postMessage);
|
||||
logger.trace(response);
|
||||
}
|
||||
|
||||
public void postMessageToStream(CMItemStatus cmItemStatus, String message) {
|
||||
postMessageToStream(getUserZulipRestExecutor(), cmItemStatus, message);
|
||||
}
|
||||
|
||||
public void postItemCreatedToStream(){
|
||||
String message = "";
|
||||
String topic = "Item Creation";
|
||||
postMessageToStream(topic, message);
|
||||
postMessageToStream(gCatZulipRestExecutor, CMItemStatus.PENDING, message);
|
||||
}
|
||||
|
||||
public void postItemRejectedToStream() {
|
||||
String message = "";
|
||||
String topic = "Item Rejected";
|
||||
postMessageToStream(topic, message);
|
||||
postMessageToStream(gCatZulipRestExecutor, CMItemStatus.REJECTED, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue