From 01d2e527a140db7f8a9eca822eaedf732da07416 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 27 May 2022 17:20:52 +0200 Subject: [PATCH] Switch to notification completed --- .../moderation/thread/ModerationThread.java | 5 +- .../SocialNotificationModerationThread.java | 157 +++++++++++------- .../gcat/moderation/ModerationThreadTest.java | 35 +++- src/test/resources/logback-test.xml | 3 +- 4 files changed, 131 insertions(+), 69 deletions(-) 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 795c9f8..e66881e 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java @@ -8,7 +8,7 @@ import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.api.moderation.CMItemStatus; -import org.gcube.gcat.moderation.thread.social.messages.SocialMessageModerationThread; +import org.gcube.gcat.moderation.thread.social.notifications.SocialNotificationModerationThread; import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP; @@ -35,7 +35,8 @@ public abstract class ModerationThread { public static ModerationThread getDefaultInstance() { // return new FakeModerationThread(); - return new SocialMessageModerationThread(); + // return new SocialMessageModerationThread(); + return new SocialNotificationModerationThread(); } public ModerationThread() { diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java index bc98e1c..b406b65 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java @@ -1,6 +1,5 @@ package org.gcube.gcat.moderation.thread.social.notifications; -import java.io.StringWriter; import java.net.URL; import java.util.Set; @@ -26,29 +25,20 @@ public class SocialNotificationModerationThread extends ModerationThread { private static final Logger logger = LoggerFactory.getLogger(SocialNotificationModerationThread.class); - protected CatalogueEventType catalogueEventType; + public static final String AUTHOR = "Author"; - protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) { - stringBuffer.append("Status: "); - stringBuffer.append(cmItemStatus.getFancyValue()); - stringBuffer.append("\n"); - stringBuffer.append("Title: "); - stringBuffer.append(itemTitle); - stringBuffer.append("\n"); - stringBuffer.append("Name: "); - stringBuffer.append(itemName); - stringBuffer.append("\n"); - stringBuffer.append("ID: "); - stringBuffer.append(itemID); - stringBuffer.append("\n"); - stringBuffer.append("URL: "); - if(cmItemStatus == CMItemStatus.APPROVED) { - stringBuffer.append(itemURL); - }else { - stringBuffer.append(getModerationURL()); - } - stringBuffer.append("\n\n"); - return stringBuffer; + protected CatalogueEventType catalogueEventType; + protected boolean comment; + + protected static final boolean notificationSentByGCat; + + static { + notificationSentByGCat = false; + } + + public SocialNotificationModerationThread() { + super(); + this.comment = false; } /** @@ -58,16 +48,18 @@ public class SocialNotificationModerationThread extends ModerationThread { /* * An example of created message is: * - * [mister x] created /updated the item "[TITLE]". You are kindly requested to review it and decide either to APPROVE or REJECT it. [Go to catalogue] + * [mister x] created/updated the item "[TITLE]". You are kindly requested to review it and decide either to APPROVE or REJECT it. [Go to catalogue] * */ String fullName = ckanUser.getNameSurname(); StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append(fullName); + if(notificationSentByGCat) { + stringBuffer.append(fullName); + } stringBuffer.append(create ? " created " : " updated "); stringBuffer.append("the item "); - stringBuffer.append(itemTitle); - stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it. "); + stringBuffer = addQuotedTitle(stringBuffer); + stringBuffer.append(". You are kindly requested to review it and decide either to APPROVE or REJECT it. "); postMessage(stringBuffer.toString()); } @@ -86,14 +78,22 @@ public class SocialNotificationModerationThread extends ModerationThread { notifyItemToBeManaged(); } - protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { - stringBuffer.append(fullName); - stringBuffer.append(" ["); - stringBuffer.append(role); - stringBuffer.append("] "); + protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer, boolean addUserFullName) { + if(addUserFullName) { + stringBuffer.append(fullName); + } + if(role!=null) { + stringBuffer.append(" ["); + stringBuffer.append(role); + stringBuffer.append("] "); + } return stringBuffer; } + protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { + return addUserWithRole(fullName, role, stringBuffer, notificationSentByGCat); + } + public void postItemManaged(String userMessage) throws Exception { /* * [mister x] rejected the item "[TITLE]" with this accompanying message "[MESSAGE]". To resubmit it [Go to catalogue] @@ -105,13 +105,12 @@ public class SocialNotificationModerationThread extends ModerationThread { StringBuffer stringBuffer = new StringBuffer(); stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); stringBuffer.append(cmItemStatus.getValue()); - stringBuffer.append(" the item '"); - stringBuffer.append(itemTitle); - stringBuffer.append("'"); + stringBuffer.append(" the item "); + stringBuffer = addQuotedTitle(stringBuffer); if(userMessage!=null && userMessage.length()>0) { - stringBuffer.append(" with this accompanying message '"); + stringBuffer.append(" with this accompanying message \""); stringBuffer.append(userMessage); - stringBuffer.append("'"); + stringBuffer.append("\""); } stringBuffer.append("."); @@ -137,37 +136,54 @@ public class SocialNotificationModerationThread extends ModerationThread { postItemManaged(userMessage); } + protected StringBuffer addQuotedTitle(StringBuffer stringBuffer, String quotingCharacter) { + stringBuffer.append(quotingCharacter); + stringBuffer.append(itemTitle); + stringBuffer.append(quotingCharacter); + return stringBuffer; + } + + protected StringBuffer addQuotedTitle(StringBuffer stringBuffer) { + return addQuotedTitle(stringBuffer, "\""); + } + protected String getSubject() { - StringWriter stringWriter = new StringWriter(); - if(catalogueEventType!=null) { + StringBuffer stringBuffer = new StringBuffer(); + String fullName = ckanUser.getNameSurname(); + if(!comment) { switch (catalogueEventType) { case ITEM_SUBMITTED: - stringWriter.append("Submitted item '"); + stringBuffer.append(fullName); + stringBuffer.append(" created the item "); break; case ITEM_UPDATED: - stringWriter.append("Updated item '"); + stringBuffer.append(fullName); + stringBuffer.append(" updated the item "); break; case ITEM_REJECTED: case ITEM_PUBLISHED: - stringWriter.append(cmItemStatus.getFancyValue()); - stringWriter.append(" item '"); + addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer, true); + stringBuffer.append(cmItemStatus.getValue()); + stringBuffer.append(" the item "); break; default: break; } }else { - stringWriter.append("Message for item '"); + addUserWithRole(fullName, itemAuthor ? SocialNotificationModerationThread.AUTHOR : Moderated.CATALOGUE_MODERATOR, stringBuffer, true); + stringBuffer.append("commented on the item "); } - stringWriter.append(itemTitle); - stringWriter.append("'"); - return stringWriter.toString(); + stringBuffer = addQuotedTitle(stringBuffer); + return stringBuffer.toString(); } protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception { CatalogueEvent catalogueEvent = new CatalogueEvent(); + catalogueEvent.setType(catalogueEventType); + catalogueEvent.setNotifyText(messageString); catalogueEvent.setItemId(getSubject()); if(cmItemStatus == CMItemStatus.APPROVED) { catalogueEvent.setItemURL(new URL(itemURL)); @@ -175,16 +191,13 @@ public class SocialNotificationModerationThread extends ModerationThread { catalogueEvent.setItemURL(new URL(getModerationURL())); } - catalogueEvent.setNotifyText(messageString); - Set users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR); + users.add("luca.frosini"); SecretManager secretManager = SecretManagerProvider.instance.get(); String username = secretManager.getUser().getUsername(); - // Adding the user is generating the event users.add(username); - // Adding item creator users.add(CKANUser.getUsernameFromCKANUsername(ckanUser.getName())); @@ -199,18 +212,26 @@ public class SocialNotificationModerationThread extends ModerationThread { CatalogueEvent catalogueEvent = getCatalogueEvent(messageString); SecretManager secretManager = SecretManagerProvider.instance.get(); Secret secret = Constants.getCatalogueSecret(); - secretManager.startSession(secret); + if(notificationSentByGCat) { + secretManager.startSession(secret); + } try { sendNotification(catalogueEvent); }finally { - secretManager.endSession(); + if(notificationSentByGCat) { + secretManager.endSession(); + } } } @Override public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { + /* + * [mister x] ([Role]) commented on the item "[TITLE]" as follows "[MESSAGE]". [Go to catalogue] + */ this.create = false; this.cmItemStatus = cmItemStatus; + this.comment = true; switch (cmItemStatus) { case PENDING: @@ -231,24 +252,40 @@ public class SocialNotificationModerationThread extends ModerationThread { String fullName = ckanUser.getNameSurname(); 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 = addUserWithRole(fullName, itemAuthor ? SocialNotificationModerationThread.AUTHOR : Moderated.CATALOGUE_MODERATOR, stringBuffer); + stringBuffer.append("commented on the item "); + stringBuffer = addQuotedTitle(stringBuffer); + stringBuffer.append(" as follows \""); stringBuffer.append(userMessage); + stringBuffer.append("\"."); CatalogueEvent catalogueEvent = getCatalogueEvent(stringBuffer.toString()); SecretManager secretManager = SecretManagerProvider.instance.get(); Secret secret = Constants.getCatalogueSecret(); - secretManager.startSession(secret); + if(notificationSentByGCat) { + secretManager.startSession(secret); + } try { sendNotification(catalogueEvent); }finally { - secretManager.endSession(); + if(notificationSentByGCat) { + secretManager.endSession(); + } } } protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception { - NotificationClient nc = new NotificationClient(); - nc.sendCatalogueEvent(catalogueEvent); + Thread thread = new Thread() { + public void run() { + try { + NotificationClient nc = new NotificationClient(); + nc.sendCatalogueEvent(catalogueEvent); + } catch(Exception e) { + logger.error("Error while sending notification.", e); + } + } + }; + thread.run(); + // thread.start(); } @Override diff --git a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java index 1b95f88..ba5ea18 100644 --- a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java +++ b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java @@ -4,7 +4,6 @@ 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,23 +11,47 @@ import org.junit.Test; */ public class ModerationThreadTest extends ContextTest { - @Test - @Ignore - public void test() throws Exception { + protected ModerationThread getModerationThread(CKANUser ckanUser) { 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"); + moderationThread.setItemCoordinates("b1040e70-774f-47b6-95e9-f24efca50caf", "my_first_restful_transaction_model", "RESTful Transaction Model", "https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model"); + moderationThread.setCKANUser(ckanUser); + return moderationThread; + } + + @Test + // @Ignore + public void testModerationThread() throws Exception { + ContextTest.setContextByName("pasquale.pagano_/gcube/devsec/devVRE"); + CKANUser ckanUser = new CKANUser(); ckanUser.setName(CKANUser.getCKANUsername()); ckanUser.read(); - moderationThread.setCKANUser(ckanUser); + + ModerationThread moderationThread = getModerationThread(ckanUser); moderationThread.postItemCreated(); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemUpdated(); + + moderationThread = getModerationThread(ckanUser); + moderationThread.postUserMessage(CMItemStatus.PENDING, "Pensaci Bene"); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemRejected(null); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemRejected("reject con messaggio: Non mi garba"); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemApproved(null); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemApproved("approve con messaggio: Ora mi garba"); + + moderationThread = getModerationThread(ckanUser); moderationThread.setItemAuthor(true); moderationThread.postUserMessage(CMItemStatus.APPROVED, "Grazie"); + Thread.sleep(1000); } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 2e2f160..4c2b21d 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -10,8 +10,9 @@ - + +