From e2763d68045e8ceda15487086daa1f3bc4846cae Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 7 Apr 2022 11:41:58 +0200 Subject: [PATCH 1/5] Fixed required role to create/update Profile --- .settings/org.eclipse.core.resources.prefs | 1 - src/main/java/org/gcube/gcat/profile/ISProfile.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 29abf99..cdfe4f1 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,6 +1,5 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 -encoding//src/main/resources=UTF-8 encoding//src/test/java=UTF-8 encoding//src/test/resources=UTF-8 encoding/=UTF-8 diff --git a/src/main/java/org/gcube/gcat/profile/ISProfile.java b/src/main/java/org/gcube/gcat/profile/ISProfile.java index 3108869..40f29a8 100644 --- a/src/main/java/org/gcube/gcat/profile/ISProfile.java +++ b/src/main/java/org/gcube/gcat/profile/ISProfile.java @@ -172,7 +172,7 @@ public class ISProfile { public boolean createOrUpdate(String name, String xml) throws SAXException { try { CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser(); - if(ckanUser.getRole().ordinal() Date: Thu, 7 Apr 2022 11:44:04 +0200 Subject: [PATCH 2/5] Removed IDE specific file --- .settings/.gitignore | 1 + .settings/org.eclipse.core.resources.prefs | 5 ----- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/.gitignore b/.settings/.gitignore index 3b1537c..57d3934 100644 --- a/.settings/.gitignore +++ b/.settings/.gitignore @@ -1 +1,2 @@ /org.eclipse.jdt.core.prefs +/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index cdfe4f1..0000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,5 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding//src/test/java=UTF-8 -encoding//src/test/resources=UTF-8 -encoding/=UTF-8 From 0f86ce828d386dffb65077bec857b1a59b419682 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 7 Apr 2022 16:24:46 +0200 Subject: [PATCH 3/5] Fixed messaging --- .../thread/FakeModerationThread.java | 6 +- .../moderation/thread/ModerationThread.java | 74 ++++---- .../social/SocialMessageModerationThread.java | 173 +++++++++--------- .../moderation/thread/zulip/ZulipStream.java | 9 +- .../gcat/moderation/ModerationThreadTest.java | 1 - 5 files changed, 133 insertions(+), 130 deletions(-) diff --git a/src/main/java/org/gcube/gcat/moderation/thread/FakeModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/FakeModerationThread.java index e7922e2..20198cb 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/FakeModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/FakeModerationThread.java @@ -13,15 +13,13 @@ public class FakeModerationThread extends ModerationThread { private static final Logger logger = LoggerFactory.getLogger(FakeModerationThread.class); @Override - protected void postMessage(CMItemStatus cmItemStatus, String message) throws Exception { - + protected void postMessage(String message) throws Exception { logger.info("gCat is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"", ModerationThread.class.getSimpleName(), itemName, itemID, cmItemStatus, message); } @Override public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { - logger.info("{} is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"", SecretManagerProvider.instance.get().getUser().getUsername(), ModerationThread.class.getSimpleName(), itemName, itemID, cmItemStatus, userMessage); @@ -29,8 +27,8 @@ public class FakeModerationThread extends ModerationThread { @Override protected void createModerationThread() throws Exception { - logger.info("Creating {} for item '{}' (id={})", ModerationThread.class.getSimpleName(), itemName, itemID); } + } diff --git a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java index ef3f621..49d211b 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java @@ -9,37 +9,37 @@ import org.gcube.gcat.persistence.ckan.CKANUser; * @author Luca Frosini (ISTI - CNR) */ public abstract class ModerationThread { - + protected String itemID; protected String itemName; protected String itemTitle; protected String itemURL; - + + protected boolean create; + protected CMItemStatus cmItemStatus; protected boolean itemAuthor; - + protected CKANUser ckanUser; protected ObjectMapper objectMapper; - + public static ModerationThread getDefaultInstance() { // return new FakeModerationThread(); return new SocialMessageModerationThread(); } - + public ModerationThread() { this.objectMapper = new ObjectMapper(); - itemAuthor = false; + this.itemAuthor = false; + this.create = false; + cmItemStatus = CMItemStatus.PENDING; } - + public void setItemCoordinates(String itemID, String itemName, String itemTitle, String itemURL) { this.itemID = itemID; this.itemName = itemName; this.itemTitle = itemTitle; this.itemURL = itemURL; } - - public boolean isItemAuthor() { - return itemAuthor; - } public void setItemAuthor(boolean itemAuthor) { this.itemAuthor = itemAuthor; @@ -48,57 +48,65 @@ public abstract class ModerationThread { public void setCKANUser(CKANUser ckanUser) { this.ckanUser = ckanUser; } - + /** * The message is sent as gCat + * * @param cmItemStatus * @param message * @throws Exception */ - protected abstract void postMessage(CMItemStatus cmItemStatus, String message) throws Exception; - + protected abstract void postMessage(String message) throws Exception; + /** * The message is sent as User + * * @param cmItemStatus * @param userMessage * @throws Exception */ public abstract void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception; - - protected abstract void createModerationThread() throws Exception ; - - public void postItemCreated() throws Exception{ + + protected abstract void createModerationThread() throws Exception; + + public void postItemCreated() throws Exception { createModerationThread(); + create = true; + cmItemStatus = CMItemStatus.PENDING; String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.PENDING; - String message = String.format("@**%s** created the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", + String message = String.format( + "@**%s** created the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", fullName, itemName, itemID, cmItemStatus.getFancyValue()); - postMessage(cmItemStatus, message); + postMessage(message); } public void postItemUpdated() throws Exception { + cmItemStatus = CMItemStatus.PENDING; String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.PENDING; - String message = String.format("@**%s** updated the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", + String message = String.format( + "@**%s** updated the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", fullName, itemName, itemID, cmItemStatus.getFancyValue()); - postMessage(cmItemStatus, message); + postMessage(message); } - + public void postItemRejected(String userMessage) throws Exception { + cmItemStatus = CMItemStatus.REJECTED; String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.REJECTED; - String message = String.format("@**%s** **%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.", + String message = String.format( + "@**%s** **%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.", fullName, cmItemStatus.getFancyValue(), itemName, itemID); - postMessage(cmItemStatus, message); + postMessage(message); postUserMessage(cmItemStatus, userMessage); } - - public void postItemApproved(String userMessage) throws Exception{ + + public void postItemApproved(String userMessage) throws Exception { + cmItemStatus = CMItemStatus.APPROVED; String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.APPROVED; - String message = String.format("@**%s** **%s** the item with name '%s' (id='%s'). The item is now available in the catalogue. The item is available at %s", + String message = String.format( + "@**%s** **%s** the item with name '%s' (id='%s'). The item is now available in the catalogue. The item is available at %s", fullName, cmItemStatus.getFancyValue(), itemName, itemID, itemURL); - postMessage(cmItemStatus, message); + postMessage(message); postUserMessage(cmItemStatus, userMessage); } + } diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java index 44b3e99..2250fd7 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java @@ -23,8 +23,7 @@ public class SocialMessageModerationThread extends ModerationThread { private static final Logger logger = LoggerFactory.getLogger(SocialMessageModerationThread.class); - protected StringBuffer getMainItemInfo(CMItemStatus cmItemStatus) { - StringBuffer stringBuffer = new StringBuffer(); + protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) { stringBuffer.append("Status: "); stringBuffer.append(cmItemStatus.getFancyValue()); stringBuffer.append("\n"); @@ -43,37 +42,48 @@ public class SocialMessageModerationThread extends ModerationThread { return stringBuffer; } - public void postItemCreated() throws Exception{ + /** + * Create the message for an item that is created/updated + */ + public void postItemToBeManaged() throws Exception { + /* + * An example of created message is: + * + * [mister x] created / updated the following item + * + * Title: RESTful Transaction Model + * Name: my_first_restful_transaction_model + * ID: e31a6ba8-66ef-47b8-b61f-99a1366b4a69 + * URL: https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model + * + * You are kindly requested to review it and decide either to APPROVE or REJECT it at + * + */ + cmItemStatus = CMItemStatus.PENDING; String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.PENDING; - StringBuffer stringBuffer = getMainItemInfo(cmItemStatus); - - stringBuffer.append("Message:\n"); + StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(fullName); - stringBuffer.append(" created '"); - stringBuffer.append(itemTitle); - stringBuffer.append("'. It is now in "); - stringBuffer.append(cmItemStatus.getFancyValue()); - stringBuffer.append(" state and must be moderated."); - - postMessage(cmItemStatus, stringBuffer.toString()); - + stringBuffer.append(create ? " created " : " updated "); + stringBuffer.append("the following item\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it."); + // stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT at"); + // String manageURL = getManageURL(); + // stringBuffer.append(manageURL); + postMessage(stringBuffer.toString()); + } + + + public void postItemCreated() throws Exception { + create = true; + cmItemStatus = CMItemStatus.PENDING; + postItemToBeManaged(); } public void postItemUpdated() throws Exception { - String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.PENDING; - StringBuffer stringBuffer = getMainItemInfo(cmItemStatus); - - stringBuffer.append("Message:\n"); - stringBuffer.append(fullName); - stringBuffer.append(" updated '"); - stringBuffer.append(itemTitle); - stringBuffer.append("'. It is now in "); - stringBuffer.append(cmItemStatus.getFancyValue()); - stringBuffer.append(" state and must be moderated."); - - postMessage(cmItemStatus, stringBuffer.toString()); + create = false; + cmItemStatus = CMItemStatus.PENDING; + postItemToBeManaged(); } protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { @@ -84,80 +94,63 @@ public class SocialMessageModerationThread extends ModerationThread { return stringBuffer; } + public void postItemManaged(String userMessage) throws Exception { + create = false; + String fullName = ckanUser.getNameSurname(); + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); + stringBuffer.append(cmItemStatus.getValue()); + stringBuffer.append(" the following item"); + if(userMessage!=null && userMessage.length()>0) { + stringBuffer.append(" with this accompanying message\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append(userMessage); + }else { + stringBuffer.append("\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + } + postMessage(stringBuffer.toString()); + } + + @Override public void postItemRejected(String userMessage) throws Exception { - String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.REJECTED; - - StringBuffer stringBuffer = getMainItemInfo(cmItemStatus); - - stringBuffer.append("Message:\n"); - stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); - stringBuffer.append("rejected '"); - stringBuffer.append(itemTitle); - stringBuffer.append("'."); - stringBuffer.append(" The author can delete or update it."); - - if(userMessage!=null && userMessage.length()>0) { - stringBuffer.append("\n\n"); - stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); - stringBuffer.append("added the following comment:\n"); - stringBuffer.append(userMessage); - } - - postMessage(cmItemStatus, stringBuffer.toString()); + create = false; + cmItemStatus = CMItemStatus.REJECTED; + postItemManaged(userMessage); } - public void postItemApproved(String userMessage) throws Exception{ - String fullName = ckanUser.getNameSurname(); - CMItemStatus cmItemStatus = CMItemStatus.APPROVED; - - StringBuffer stringBuffer = getMainItemInfo(cmItemStatus); - - stringBuffer.append("Message:\n"); - stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); - stringBuffer.append("approved the '"); - stringBuffer.append(itemTitle); - stringBuffer.append("'."); - stringBuffer.append(" It is now available in the catalogue at "); - stringBuffer.append(itemURL); - - if(userMessage!=null && userMessage.length()>0) { - stringBuffer.append("\n\n"); - stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); - stringBuffer.append("added the following comment:\n"); - stringBuffer.append(userMessage); - } - - postMessage(cmItemStatus, stringBuffer.toString()); + @Override + public void postItemApproved(String userMessage) throws Exception { + create = false; + cmItemStatus = CMItemStatus.APPROVED; + postItemManaged(userMessage); } - - protected String getSubject(CMItemStatus cmItemStatus) { + protected String getSubject() { StringWriter stringWriter = new StringWriter(); + if(!create) { + logger.trace("It's a reply"); + stringWriter.append("Re: [Catalogue Service] "); + } stringWriter.append("[Catalogue Service] "); stringWriter.append(itemTitle); - stringWriter.append(" (name:"); - stringWriter.append(itemName); - stringWriter.append(" - id:"); - stringWriter.append(itemID); - stringWriter.append(")"); return stringWriter.toString(); } - protected Message getMessage(CMItemStatus cmItemStatus, String messageString) throws Exception { + protected Message getMessage(String messageString) throws Exception { Message message = new Message(); message.setMessage(messageString); - message.setSubject(getSubject(cmItemStatus)); + message.setSubject(getSubject()); Set moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR); message.setUsers(moderators); return message; } @Override - protected void postMessage(CMItemStatus cmItemStatus, String messageString) throws Exception { + protected void postMessage(String messageString) throws Exception { SecretManager secretManager = SecretManagerProvider.instance.get(); String username = secretManager.getUser().getUsername(); - Message message = getMessage(cmItemStatus, messageString); + Message message = getMessage(messageString); message.addUser(username); Secret secret = Constants.getCatalogueSecret(); secretManager.startSession(secret); @@ -170,12 +163,15 @@ public class SocialMessageModerationThread extends ModerationThread { @Override public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { + this.create = false; + this.cmItemStatus = cmItemStatus; String fullName = ckanUser.getNameSurname(); - StringBuffer stringBuffer = getMainItemInfo(cmItemStatus); - stringBuffer = addUserWithRole(fullName, isItemAuthor() ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer); - stringBuffer.append("sent the following comment:\n"); + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer); + stringBuffer.append("sent a message regarding the following item\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); stringBuffer.append(userMessage); - Message message = getMessage(cmItemStatus, stringBuffer.toString()); + Message message = getMessage(stringBuffer.toString()); SecretManager secretManager = SecretManagerProvider.instance.get(); String username = secretManager.getUser().getUsername(); message.addUser(username); @@ -193,10 +189,11 @@ public class SocialMessageModerationThread extends ModerationThread { socialMessage.setMessage(message); socialMessage.start(); } - + @Override protected void createModerationThread() throws Exception { - // Nothing to do + create = true; + cmItemStatus = CMItemStatus.PENDING; } - + } diff --git a/src/main/java/org/gcube/gcat/moderation/thread/zulip/ZulipStream.java b/src/main/java/org/gcube/gcat/moderation/thread/zulip/ZulipStream.java index e951d02..a180035 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/zulip/ZulipStream.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/zulip/ZulipStream.java @@ -133,20 +133,21 @@ public class ZulipStream extends ModerationThread { executeZulipCall(gCatZulipRestExecutor, postCreateStream); } - protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, CMItemStatus cmItemStatus, String message) throws Exception { + protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, String message) throws Exception { PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message); logger.debug("Going to send the following message: {}", message); executeZulipCall(zulipRestExecutor, postMessage); } @Override - protected void postMessage(CMItemStatus cmItemStatus, String message) throws Exception { - postMessageToStream(getGCatZulipRestExecutor(), cmItemStatus, message); + protected void postMessage(String message) throws Exception { + postMessageToStream(getGCatZulipRestExecutor(), message); } @Override public void postUserMessage(CMItemStatus cmItemStatus, String message) throws Exception { - postMessageToStream(getUserZulipRestExecutor(), cmItemStatus, message); + this.cmItemStatus = cmItemStatus; + postMessageToStream(getUserZulipRestExecutor(), message); } } diff --git a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java index c8f064c..7d8d737 100644 --- a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java +++ b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java @@ -1,6 +1,5 @@ package org.gcube.gcat.moderation; -import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore; import org.gcube.gcat.ContextTest; import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.moderation.thread.ModerationThread; From d1deb9837756832ded3b37274aeb29c5a1bf1b92 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 7 Apr 2022 16:57:35 +0200 Subject: [PATCH 4/5] fixed test --- .../org/gcube/gcat/moderation/ModerationThreadTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java index 7d8d737..d20b7dd 100644 --- a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java +++ b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java @@ -22,8 +22,10 @@ public class ModerationThreadTest extends ContextTest { moderationThread.setCKANUser(ckanUser); moderationThread.postItemCreated(); moderationThread.postItemUpdated(); - moderationThread.postItemRejected("Non mi garba"); - moderationThread.postItemApproved("Ora mi garba"); + moderationThread.postItemRejected(null); + moderationThread.postItemRejected("reject con messaggio: Non mi garba"); + moderationThread.postItemApproved(null); + moderationThread.postItemApproved("approve con messaggio: Ora mi garba"); moderationThread.setItemAuthor(true); moderationThread.postUserMessage(CMItemStatus.APPROVED, "Grazie"); Thread.sleep(1000); From b53c51b46f55282f9063cce914e430bf7b3b26a2 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 7 Apr 2022 17:00:36 +0200 Subject: [PATCH 5/5] Added @Ignore to test --- .../java/org/gcube/gcat/moderation/ModerationThreadTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java index d20b7dd..1b95f88 100644 --- a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java +++ b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java @@ -4,6 +4,7 @@ import org.gcube.gcat.ContextTest; import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.moderation.thread.ModerationThread; import org.gcube.gcat.persistence.ckan.CKANUser; +import org.junit.Ignore; import org.junit.Test; /** @@ -12,7 +13,7 @@ import org.junit.Test; public class ModerationThreadTest extends ContextTest { @Test - // @JsonIgnore + @Ignore public void test() throws Exception { ModerationThread moderationThread = ModerationThread.getDefaultInstance(); moderationThread.setItemCoordinates("e31a6ba8-66ef-47b8-b61f-99a1366b4a69", "my_first_restful_transaction_model", "RESTful Transaction Model", "https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model");