From 36977e1c16cf576b2e7ce5ef035d489ed2468ff3 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 31 Mar 2022 15:49:22 +0200 Subject: [PATCH] Fixing Social Message delivery --- .../moderation/thread/ModerationThread.java | 36 ++++++++++------- .../social/SocialMessageModerationThread.java | 7 +++- .../gcat/persistence/ckan/CKANPackage.java | 31 ++++++++------- .../java/org/gcube/gcat/social/Message.java | 39 +++++++++++++++++++ .../org/gcube/gcat/social/SocialMessage.java | 8 ++-- .../gcat/moderation/ModerationThreadTest.java | 32 +++++++++++++++ 6 files changed, 120 insertions(+), 33 deletions(-) create mode 100644 src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java 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 34eab12..a3a5949 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java @@ -12,6 +12,7 @@ public abstract class ModerationThread { protected String itemID; protected String itemName; + protected String itemURL; protected CKANUser ckanUser; protected ObjectMapper objectMapper; @@ -25,9 +26,10 @@ public abstract class ModerationThread { this.objectMapper = new ObjectMapper(); } - public void setItemCoordinates(String itemID, String itemName) { + public void setItemCoordinates(String itemID, String itemName, String itemURL) { this.itemID = itemID; this.itemName = itemName; + this.itemURL = itemURL; } public void setCKANUser(CKANUser ckanUser) { @@ -54,35 +56,43 @@ public abstract class ModerationThread { public void postItemCreated() throws Exception{ createModerationThread(); - String username = ckanUser.getNameSurname(); + String fullName = ckanUser.getNameSurname(); CMItemStatus cmItemStatus = CMItemStatus.PENDING; - String message = String.format("@**%s** has created the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", - username, itemName, itemID, cmItemStatus.getFancyValue()); +// String message = String.format("@**%s** has created the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", +// username, itemName, itemID, cmItemStatus.getFancyValue()); + String message = String.format("%s has 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); } public void postItemUpdated() throws Exception { - String username = ckanUser.getNameSurname(); + String fullName = ckanUser.getNameSurname(); CMItemStatus cmItemStatus = CMItemStatus.PENDING; - String message = String.format("@**%s** has updated the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", - username, itemName, itemID, cmItemStatus.getFancyValue()); +// String message = String.format("@**%s** has updated the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.", +// username, itemName, itemID, cmItemStatus.getFancyValue()); + String message = String.format("%s has updated the item with name '%s' (id='%s'). The item is now in %s state and must be moderated.", + fullName, itemName, itemID, cmItemStatus.getFancyValue(), itemURL); postMessage(cmItemStatus, message); } public void postItemRejected(String userMessage) throws Exception { - String username = ckanUser.getNameSurname(); + String fullName = ckanUser.getNameSurname(); CMItemStatus cmItemStatus = CMItemStatus.REJECTED; - String message = String.format("@**%s** has **%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.", - username, cmItemStatus.getFancyValue(), itemName, itemID); +// String message = String.format("@**%s** has **%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.", +// username, cmItemStatus.getFancyValue(), itemName, itemID,); + String message = String.format("%s has %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); postUserMessage(cmItemStatus, userMessage); } public void postItemApproved(String userMessage) throws Exception{ - String username = ckanUser.getNameSurname(); + String fullName = ckanUser.getNameSurname(); CMItemStatus cmItemStatus = CMItemStatus.APPROVED; - String message = String.format("@**%s** has **%s** the item with name '%s' (id='%s'). The item is now available in the catalogue.", - username, cmItemStatus.getFancyValue(), itemName, itemID); +// String message = String.format("@**%s** has **%s** the item with name '%s' (id='%s'). The item is now available in the catalogue. The item is available at %s", +// username, cmItemStatus.getFancyValue(), itemName, itemID, itemURL); + String message = String.format("%s has %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); 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 c1e00d8..8234e9c 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 @@ -50,8 +50,11 @@ public class SocialMessageModerationThread extends ModerationThread { message.addUser(username); Secret secret = Constants.getCatalogueSecret(); secretManager.startSession(secret); - sendMessage(message); - secretManager.endSession(); + try { + sendMessage(message); + }finally { + secretManager.endSession(); + } } @Override diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index 0822748..83292b0 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -118,6 +118,7 @@ public class CKANPackage extends CKAN implements Moderated { protected final List managedResources; protected String itemID; + protected String itemURL; protected final CKANUser ckanUser; @@ -662,12 +663,12 @@ public class CKANPackage extends CKAN implements Moderated { protected String addItemURLViaResolver(JsonNode jsonNode) { // Adding Item URL via Resolver - String catalogueItemURL = URIResolver.getCatalogueItemURL(name); - addExtraField(jsonNode, EXTRAS_ITEM_URL_KEY, catalogueItemURL); - return catalogueItemURL; + itemURL = URIResolver.getCatalogueItemURL(name); + addExtraField(jsonNode, EXTRAS_ITEM_URL_KEY, itemURL); + return itemURL; } - protected void sendSocialPost(String title, String catalogueItemURL) { + protected void sendSocialPost(String title) { try { boolean makePost = false; try { @@ -683,7 +684,7 @@ public class CKANPackage extends CKAN implements Moderated { ArrayNode arrayNode = (ArrayNode) result.get(TAGS_KEY); SocialPost socialPost = new SocialPost(); socialPost.setItemID(itemID); - socialPost.setItemURL(catalogueItemURL); + socialPost.setItemURL(itemURL); socialPost.setTags(arrayNode); socialPost.setItemTitle(title); @@ -720,6 +721,7 @@ public class CKANPackage extends CKAN implements Moderated { if(this.itemID == null) { this.itemID = result.get(ID_KEY).asText(); } + itemURL = getExtraField(result, EXTRAS_ITEM_URL_KEY).asText(); } @@ -763,9 +765,8 @@ public class CKANPackage extends CKAN implements Moderated { ((ObjectNode) jsonNode).remove(RESOURCES_KEY); } - String catalogueItemURL = ""; if(configuration.getScopeBean().is(Type.VRE)) { - catalogueItemURL = addItemURLViaResolver(jsonNode); + addItemURLViaResolver(jsonNode); } super.create(getAsString(jsonNode)); @@ -780,7 +781,7 @@ public class CKANPackage extends CKAN implements Moderated { if(configuration.getScopeBean().is(Type.VRE)) { // Actions performed after a package has been correctly created on ckan. String title = result.get(TITLE_KEY).asText(); - sendSocialPost(title, catalogueItemURL); + sendSocialPost(title); } } @@ -1291,7 +1292,7 @@ public class CKANPackage extends CKAN implements Moderated { private void postItemCreated() throws Exception { try { if(isModerationEnabled()) { - moderationThread.setItemCoordinates(itemID, name); + moderationThread.setItemCoordinates(itemID, name, itemURL); moderationThread.postItemCreated(); } } catch(WebApplicationException e) { @@ -1304,7 +1305,7 @@ public class CKANPackage extends CKAN implements Moderated { private void postItemUpdated() { try { if(isModerationEnabled()) { - moderationThread.setItemCoordinates(itemID, name); + moderationThread.setItemCoordinates(itemID, name, itemURL); moderationThread.postItemUpdated(); } } catch(WebApplicationException e) { @@ -1336,14 +1337,14 @@ public class CKANPackage extends CKAN implements Moderated { String ret = sendPostRequest(ITEM_UPDATE, getAsString(result)); result = mapper.readTree(ret); - moderationThread.setItemCoordinates(itemID, name); + itemURL = getExtraField(result, EXTRAS_ITEM_URL_KEY).asText(); + moderationThread.setItemCoordinates(itemID, name, itemURL); moderationThread.postItemApproved(moderatorMessage); if(configuration.getScopeBean().is(Type.VRE)) { // Actions performed after a package has been correctly created on ckan. String title = result.get(TITLE_KEY).asText(); - String catalogueItemURL = getExtraField(result, EXTRAS_ITEM_URL_KEY).asText(); - sendSocialPost(title, catalogueItemURL); + sendSocialPost(title); } break; @@ -1384,7 +1385,7 @@ public class CKANPackage extends CKAN implements Moderated { String ret = sendPostRequest(ITEM_PATCH, getAsString(result)); result = mapper.readTree(ret); - moderationThread.setItemCoordinates(itemID, name); + moderationThread.setItemCoordinates(itemID, name, itemURL); moderationThread.postItemRejected(moderatorMessage); break; @@ -1420,7 +1421,7 @@ public class CKANPackage extends CKAN implements Moderated { } CMItemStatus cmItemStatus = getCMItemStatus(); - moderationThread.setItemCoordinates(itemID, name); + moderationThread.setItemCoordinates(itemID, name, itemURL); moderationThread.postUserMessage(cmItemStatus, message); return; } diff --git a/src/main/java/org/gcube/gcat/social/Message.java b/src/main/java/org/gcube/gcat/social/Message.java index c747963..cb4cf20 100644 --- a/src/main/java/org/gcube/gcat/social/Message.java +++ b/src/main/java/org/gcube/gcat/social/Message.java @@ -1,14 +1,41 @@ package org.gcube.gcat.social; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; + +import org.gcube.com.fasterxml.jackson.annotation.JsonGetter; +import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore; +import org.gcube.com.fasterxml.jackson.annotation.JsonProperty; /** * @author Luca Frosini (ISTI-CNR) */ public class Message { + public class Recipient { + + String id; + + public Recipient(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + } + + @JsonProperty(value = "body") protected String message; + @JsonProperty(value = "subject") protected String subject; + @JsonIgnore protected Collection users; public String getMessage() { @@ -27,18 +54,30 @@ public class Message { this.subject = subject; } + @JsonIgnore public Collection getUsers() { return users; } + @JsonIgnore public void setUsers(Collection users) { this.users = users; } + @JsonIgnore public void addUser(String user) { this.users.add(user); } + @JsonGetter(value = "recipients") + public List getRecipients() { + List recipients = new ArrayList<>(); + for(String username : users) { + recipients.add(new Recipient(username)); + } + return recipients; + } + @Override public String toString() { return "Message [message=" + message + ", subject=" + subject + ", users=" + users + "]"; diff --git a/src/main/java/org/gcube/gcat/social/SocialMessage.java b/src/main/java/org/gcube/gcat/social/SocialMessage.java index a1c57a8..eaa7c4a 100644 --- a/src/main/java/org/gcube/gcat/social/SocialMessage.java +++ b/src/main/java/org/gcube/gcat/social/SocialMessage.java @@ -7,6 +7,8 @@ import javax.ws.rs.core.MediaType; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.common.authorization.utils.socialservice.SocialService; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.gcat.utils.Constants; @@ -68,14 +70,14 @@ public class SocialMessage extends Thread { } basePath = basePath.endsWith("/") ? basePath : basePath + "/"; - logger.debug("The message that is going to be send is\n{}", message); - String messageString = objectMapper.writeValueAsString(message); + logger.debug("The message that is going to be send is\n{}", messageString); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath); gxhttpStringRequest.from(Constants.CATALOGUE_NAME); gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); - gxhttpStringRequest.setSecurityToken(Constants.getCatalogueSecret().getToken()); + SecretManager secretManager = SecretManagerProvider.instance.get(); + gxhttpStringRequest.setSecurityToken(secretManager.getCurrentSecretHolder().getSecrets().first().getToken()); gxhttpStringRequest.path(SOCIAL_SERVICE_SEND_MESSAGE_PATH); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(messageString); diff --git a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java new file mode 100644 index 0000000..efcc105 --- /dev/null +++ b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java @@ -0,0 +1,32 @@ +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; +import org.gcube.gcat.persistence.ckan.CKANUser; +import org.junit.Test; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ModerationThreadTest extends ContextTest { + + @Test + @JsonIgnore + public void test() throws Exception { + ModerationThread moderationThread = ModerationThread.getDefaultInstance(); + moderationThread.setItemCoordinates("my_first_restful_transaction_model", "RESTful Transaction Model", "https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model"); + CKANUser ckanUser = new CKANUser(); + ckanUser.setName(CKANUser.getCKANUsername()); + ckanUser.read(); + moderationThread.setCKANUser(ckanUser); + moderationThread.postItemCreated(); + moderationThread.postItemUpdated(); + moderationThread.postItemRejected("Non mi garba"); + moderationThread.postItemApproved("Ora mi garba"); + moderationThread.postUserMessage(CMItemStatus.APPROVED, "Well Done"); + Thread.sleep(1000); + } + +}