Fixing zulip interaction

This commit is contained in:
Luca Frosini 2021-11-22 16:38:56 +01:00
parent e769f7ef2b
commit d7c6257978
2 changed files with 161 additions and 116 deletions

View File

@ -543,13 +543,18 @@ public class CKANPackage extends CKAN implements Moderated {
return result.get(AUTHOR_EMAIL_KEY).asText().compareTo(ckanUser.getPortalUser().getEMail())==0; return result.get(AUTHOR_EMAIL_KEY).asText().compareTo(ckanUser.getPortalUser().getEMail())==0;
} }
protected void readItem() throws Exception {
String ret = super.read();
result = mapper.readTree(ret);
result = cleanResult(result);
this.itemID = result.get(ID_KEY).asText();
}
@Override @Override
public String read() { public String read() {
try { try {
String ret = super.read(); readItem();
result = mapper.readTree(ret);
result = cleanResult(result);
checkModerationRead(); checkModerationRead();
@ -582,10 +587,7 @@ public class CKANPackage extends CKAN implements Moderated {
JsonNode jsonNode = validateJson(json); JsonNode jsonNode = validateJson(json);
boolean moderationEnabled = isModerationEnabled(); setItemToPending(jsonNode);
if(moderationEnabled) {
setToPending(jsonNode);
}
ArrayNode resourcesToBeCreated = mapper.createArrayNode(); ArrayNode resourcesToBeCreated = mapper.createArrayNode();
if(jsonNode.has(RESOURCES_KEY)) { if(jsonNode.has(RESOURCES_KEY)) {
@ -611,10 +613,7 @@ public class CKANPackage extends CKAN implements Moderated {
sendSocialPost(title, catalogueItemURL); sendSocialPost(title, catalogueItemURL);
} }
if(moderationEnabled) { createZulipStream();
createNewStream();
postItemCreatedToStream();
}
result = cleanResult(result); result = cleanResult(result);
@ -634,8 +633,7 @@ public class CKANPackage extends CKAN implements Moderated {
try { try {
JsonNode jsonNode = validateJson(json); JsonNode jsonNode = validateJson(json);
read(); readItem();
this.itemID = result.get(ID_KEY).asText();
jsonNode = checkModerationUpdate(jsonNode); jsonNode = checkModerationUpdate(jsonNode);
@ -687,6 +685,8 @@ public class CKANPackage extends CKAN implements Moderated {
result = cleanResult(result); result = cleanResult(result);
postItemUpdatedToStream();
return getAsString(result); return getAsString(result);
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
rollbackManagedResources(); rollbackManagedResources();
@ -701,11 +701,9 @@ public class CKANPackage extends CKAN implements Moderated {
@Override @Override
public String patch(String json) { public String patch(String json) {
try { try {
read(); readItem();
JsonNode jsonNode = checkBaseInformation(json, true); JsonNode jsonNode = checkBaseInformation(json, true);
this.itemID = result.get(ID_KEY).asText();
((ObjectNode)jsonNode).put(ID_KEY, this.itemID); ((ObjectNode)jsonNode).put(ID_KEY, this.itemID);
jsonNode = checkModerationUpdate(jsonNode); jsonNode = checkModerationUpdate(jsonNode);
@ -758,6 +756,8 @@ public class CKANPackage extends CKAN implements Moderated {
result = cleanResult(result); result = cleanResult(result);
postItemUpdatedToStream();
return getAsString(result); return getAsString(result);
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
rollbackManagedResources(); rollbackManagedResources();
@ -787,8 +787,9 @@ public class CKANPackage extends CKAN implements Moderated {
} }
} }
try {
setApiKey(CKANUtility.getSysAdminAPI()); setApiKey(CKANUtility.getSysAdminAPI());
read(); readItem();
if(ckanUser.getRole()!=Role.ADMIN && !isItemCreator()) { if(ckanUser.getRole()!=Role.ADMIN && !isItemCreator()) {
throw new ForbiddenException("Only " + Role.ADMIN.getPortalRole() + "s and item creator are entitled to purge an the item"); throw new ForbiddenException("Only " + Role.ADMIN.getPortalRole() + "s and item creator are entitled to purge an the item");
@ -804,6 +805,11 @@ public class CKANPackage extends CKAN implements Moderated {
} }
} }
super.purge(); super.purge();
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
} }
/** /**
@ -872,6 +878,7 @@ public class CKANPackage extends CKAN implements Moderated {
boolean moderationEnabled = ckanInstance.isModerationEnabled(); boolean moderationEnabled = ckanInstance.isModerationEnabled();
if(moderationEnabled && zulipStream==null) { if(moderationEnabled && zulipStream==null) {
zulipStream = new ZulipStream(); zulipStream = new ZulipStream();
zulipStream.setCKANUser(ckanUser);
} }
return moderationEnabled; return moderationEnabled;
} }
@ -997,7 +1004,7 @@ public class CKANPackage extends CKAN implements Moderated {
} }
if(setToPending) { if(setToPending) {
setToPending(jsonNode); setItemToPending(jsonNode);
} }
} }
@ -1006,8 +1013,9 @@ public class CKANPackage extends CKAN implements Moderated {
} }
protected void checkModerationDelete() { protected void checkModerationDelete() {
try {
if(isModerationEnabled()) { if(isModerationEnabled()) {
read(); readItem();
PortalUser portalUser = ckanUser.getPortalUser(); PortalUser portalUser = ckanUser.getPortalUser();
@ -1047,13 +1055,19 @@ public class CKANPackage extends CKAN implements Moderated {
break; break;
} }
} }
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
} }
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());
} }
protected void setToPending(JsonNode jsonNode) { protected void setItemToPending(JsonNode jsonNode) {
if(isModerationEnabled()) {
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.PENDING.getValue()); addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.PENDING.getValue());
CMItemVisibility cmItemVisibility = CMItemVisibility.PUBLIC; CMItemVisibility cmItemVisibility = CMItemVisibility.PUBLIC;
@ -1069,6 +1083,7 @@ public class CKANPackage extends CKAN implements Moderated {
((ObjectNode) jsonNode).put(PRIVATE_KEY, true); ((ObjectNode) jsonNode).put(PRIVATE_KEY, true);
((ObjectNode) jsonNode).put(SEARCHABLE_KEY, false); ((ObjectNode) jsonNode).put(SEARCHABLE_KEY, false);
} }
}
protected void setToApproved(JsonNode jsonNode) { protected void setToApproved(JsonNode jsonNode) {
ArrayNode extras = (ArrayNode) jsonNode.get(EXTRAS_KEY); ArrayNode extras = (ArrayNode) jsonNode.get(EXTRAS_KEY);
@ -1101,21 +1116,37 @@ public class CKANPackage extends CKAN implements Moderated {
((ObjectNode) jsonNode).put(SEARCHABLE_KEY, true); ((ObjectNode) jsonNode).put(SEARCHABLE_KEY, true);
} }
private void createNewStream() throws Exception { private void createZulipStream() throws Exception {
try {
if(isModerationEnabled()) {
zulipStream.setItemCoordinates(itemID, name); zulipStream.setItemCoordinates(itemID, name);
zulipStream.setCKANUser(ckanUser);
zulipStream.create(); zulipStream.create();
} }
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
}
private void postItemCreatedToStream() { private void postItemUpdatedToStream() {
zulipStream.postItemCreatedToStream(); try {
if(isModerationEnabled()) {
zulipStream.setItemCoordinates(itemID, name);
zulipStream.postItemUpdated();
}
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
} }
@Override @Override
public String approve(String moderatorMessage) { public String approve(String moderatorMessage) {
try { try {
if(isModerationEnabled()) { if(isModerationEnabled()) {
String ret = read(); readItem();
PortalUser portalUser = ckanUser.getPortalUser(); PortalUser portalUser = ckanUser.getPortalUser();
CMItemStatus cmItemStatus = getCMItemStatus(); CMItemStatus cmItemStatus = getCMItemStatus();
switch (cmItemStatus) { switch (cmItemStatus) {
@ -1131,22 +1162,22 @@ public class CKANPackage extends CKAN implements Moderated {
throw new MethodNotSupportedException("Only catalogue moderator can approve a pending item."); throw new MethodNotSupportedException("Only catalogue moderator can approve a pending item.");
} }
setToApproved(result); setToApproved(result);
ret = sendPostRequest(ITEM_PATCH, getAsString(result)); String ret = sendPostRequest(ITEM_PATCH, getAsString(result));
result = mapper.readTree(ret);
result = cleanResult(result);
zulipStream.setItemCoordinates(itemID, name);
zulipStream.postItemApproved();
if(moderatorMessage!=null && moderatorMessage.compareTo("")!=0) {
zulipStream.postMessageToStream(CMItemStatus.PENDING, moderatorMessage);
}
break; break;
default: default:
break; break;
} }
result = mapper.readTree(ret);
result = cleanResult(result);
ZulipStream zulipStream = new ZulipStream();
zulipStream.setItemCoordinates(itemID, name);
zulipStream.postItemCreatedToStream();
if(moderatorMessage!=null && moderatorMessage.compareTo("")!=0) {
zulipStream.postMessageToStream(CMItemStatus.PENDING, moderatorMessage);
}
return getAsString(result); return getAsString(result);
} }
@ -1162,8 +1193,7 @@ public class CKANPackage extends CKAN implements Moderated {
public String reject(String moderatorMessage) { public String reject(String moderatorMessage) {
try { try {
if(isModerationEnabled()) { if(isModerationEnabled()) {
String ret = read(); readItem();
PortalUser portalUser = ckanUser.getPortalUser();
CMItemStatus cmItemStatus = getCMItemStatus(); CMItemStatus cmItemStatus = getCMItemStatus();
switch (cmItemStatus) { switch (cmItemStatus) {
case APPROVED: case APPROVED:
@ -1174,26 +1204,28 @@ public class CKANPackage extends CKAN implements Moderated {
break; break;
case PENDING: case PENDING:
PortalUser portalUser = ckanUser.getPortalUser();
if(!portalUser.isCatalogueModerator()) { if(!portalUser.isCatalogueModerator()) {
throw new MethodNotSupportedException("Only catalogue moderator can reject a pending item."); throw new MethodNotSupportedException("Only catalogue moderator can reject a pending item.");
} }
setToRejected(result); setToRejected(result);
ret = sendPostRequest(ITEM_PATCH, getAsString(result)); String ret = sendPostRequest(ITEM_PATCH, getAsString(result));
result = mapper.readTree(ret);
result = cleanResult(result);
zulipStream.setItemCoordinates(itemID, name);
zulipStream.postItemRejected();
if(moderatorMessage!=null && moderatorMessage.compareTo("")!=0) {
zulipStream.postMessageToStream(CMItemStatus.REJECTED, moderatorMessage);
}
break; break;
default: default:
break; break;
} }
result = mapper.readTree(ret);
result = cleanResult(result);
ZulipStream zulipStream = new ZulipStream();
zulipStream.setItemCoordinates(itemID, name);
zulipStream.postItemRejectedToStream();
if(moderatorMessage!=null && moderatorMessage.compareTo("")!=0) {
zulipStream.postMessageToStream(CMItemStatus.REJECTED, moderatorMessage);
}
return getAsString(result); return getAsString(result);
} }
@ -1213,7 +1245,7 @@ public class CKANPackage extends CKAN implements Moderated {
return; return;
} }
read(); readItem();
// Catalogue Moderators are allowed to post message to the dedicated Stream // Catalogue Moderators are allowed to post message to the dedicated Stream
if(!ckanUser.getPortalUser().isCatalogueModerator()) { if(!ckanUser.getPortalUser().isCatalogueModerator()) {
@ -1223,9 +1255,9 @@ public class CKANPackage extends CKAN implements Moderated {
} }
} }
ZulipStream zulipStream = new ZulipStream();
zulipStream.setItemCoordinates(itemID, name);
CMItemStatus cmItemStatus = getCMItemStatus(); CMItemStatus cmItemStatus = getCMItemStatus();
zulipStream.setItemCoordinates(itemID, name);
zulipStream.postMessageToStream(cmItemStatus, message); zulipStream.postMessageToStream(cmItemStatus, message);
} }
throw new MethodNotSupportedException("The message operation is available only in moderation mode"); throw new MethodNotSupportedException("The message operation is available only in moderation mode");

View File

@ -121,6 +121,8 @@ public class ZulipStream {
executeZulipCall(postCreateStream); executeZulipCall(postCreateStream);
postItemCreated();
} }
protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, CMItemStatus cmItemStatus, String message) { protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, CMItemStatus cmItemStatus, String message) {
@ -133,14 +135,25 @@ public class ZulipStream {
postMessageToStream(getUserZulipRestExecutor(), cmItemStatus, message); postMessageToStream(getUserZulipRestExecutor(), cmItemStatus, message);
} }
public void postItemCreatedToStream(){ public void postItemCreated(){
String message = ""; String message = "";
postMessageToStream(gCatZulipRestExecutor, CMItemStatus.PENDING, message); postMessageToStream(gCatZulipRestExecutor, CMItemStatus.PENDING, message);
} }
public void postItemRejectedToStream() { public void postItemUpdated(){
String message = "";
postMessageToStream(gCatZulipRestExecutor, CMItemStatus.PENDING, message);
}
public void postItemRejected() {
String message = ""; String message = "";
postMessageToStream(gCatZulipRestExecutor, CMItemStatus.REJECTED, message); postMessageToStream(gCatZulipRestExecutor, CMItemStatus.REJECTED, message);
} }
public void postItemApproved(){
String message = "";
postMessageToStream(gCatZulipRestExecutor, CMItemStatus.PENDING, message);
}
} }