Fixing Social Message delivery

This commit is contained in:
Luca Frosini 2022-03-31 15:49:22 +02:00
parent 2bef5f60ec
commit 36977e1c16
6 changed files with 120 additions and 33 deletions

View File

@ -12,6 +12,7 @@ public abstract class ModerationThread {
protected String itemID; protected String itemID;
protected String itemName; protected String itemName;
protected String itemURL;
protected CKANUser ckanUser; protected CKANUser ckanUser;
protected ObjectMapper objectMapper; protected ObjectMapper objectMapper;
@ -25,9 +26,10 @@ public abstract class ModerationThread {
this.objectMapper = new ObjectMapper(); this.objectMapper = new ObjectMapper();
} }
public void setItemCoordinates(String itemID, String itemName) { public void setItemCoordinates(String itemID, String itemName, String itemURL) {
this.itemID = itemID; this.itemID = itemID;
this.itemName = itemName; this.itemName = itemName;
this.itemURL = itemURL;
} }
public void setCKANUser(CKANUser ckanUser) { public void setCKANUser(CKANUser ckanUser) {
@ -54,35 +56,43 @@ public abstract class ModerationThread {
public void postItemCreated() throws Exception{ public void postItemCreated() throws Exception{
createModerationThread(); createModerationThread();
String username = ckanUser.getNameSurname(); String fullName = ckanUser.getNameSurname();
CMItemStatus cmItemStatus = CMItemStatus.PENDING; 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.", // 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()); // 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); postMessage(cmItemStatus, message);
} }
public void postItemUpdated() throws Exception { public void postItemUpdated() throws Exception {
String username = ckanUser.getNameSurname(); String fullName = ckanUser.getNameSurname();
CMItemStatus cmItemStatus = CMItemStatus.PENDING; 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.", // 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()); // 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); postMessage(cmItemStatus, message);
} }
public void postItemRejected(String userMessage) throws Exception { public void postItemRejected(String userMessage) throws Exception {
String username = ckanUser.getNameSurname(); String fullName = ckanUser.getNameSurname();
CMItemStatus cmItemStatus = CMItemStatus.REJECTED; 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.", // 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); // 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); postMessage(cmItemStatus, message);
postUserMessage(cmItemStatus, userMessage); postUserMessage(cmItemStatus, userMessage);
} }
public void postItemApproved(String userMessage) throws Exception{ public void postItemApproved(String userMessage) throws Exception{
String username = ckanUser.getNameSurname(); String fullName = ckanUser.getNameSurname();
CMItemStatus cmItemStatus = CMItemStatus.APPROVED; 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.", // 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); // 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); postMessage(cmItemStatus, message);
postUserMessage(cmItemStatus, userMessage); postUserMessage(cmItemStatus, userMessage);
} }

View File

@ -50,9 +50,12 @@ public class SocialMessageModerationThread extends ModerationThread {
message.addUser(username); message.addUser(username);
Secret secret = Constants.getCatalogueSecret(); Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret); secretManager.startSession(secret);
try {
sendMessage(message); sendMessage(message);
}finally {
secretManager.endSession(); secretManager.endSession();
} }
}
@Override @Override
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {

View File

@ -118,6 +118,7 @@ public class CKANPackage extends CKAN implements Moderated {
protected final List<CKANResource> managedResources; protected final List<CKANResource> managedResources;
protected String itemID; protected String itemID;
protected String itemURL;
protected final CKANUser ckanUser; protected final CKANUser ckanUser;
@ -662,12 +663,12 @@ public class CKANPackage extends CKAN implements Moderated {
protected String addItemURLViaResolver(JsonNode jsonNode) { protected String addItemURLViaResolver(JsonNode jsonNode) {
// Adding Item URL via Resolver // Adding Item URL via Resolver
String catalogueItemURL = URIResolver.getCatalogueItemURL(name); itemURL = URIResolver.getCatalogueItemURL(name);
addExtraField(jsonNode, EXTRAS_ITEM_URL_KEY, catalogueItemURL); addExtraField(jsonNode, EXTRAS_ITEM_URL_KEY, itemURL);
return catalogueItemURL; return itemURL;
} }
protected void sendSocialPost(String title, String catalogueItemURL) { protected void sendSocialPost(String title) {
try { try {
boolean makePost = false; boolean makePost = false;
try { try {
@ -683,7 +684,7 @@ public class CKANPackage extends CKAN implements Moderated {
ArrayNode arrayNode = (ArrayNode) result.get(TAGS_KEY); ArrayNode arrayNode = (ArrayNode) result.get(TAGS_KEY);
SocialPost socialPost = new SocialPost(); SocialPost socialPost = new SocialPost();
socialPost.setItemID(itemID); socialPost.setItemID(itemID);
socialPost.setItemURL(catalogueItemURL); socialPost.setItemURL(itemURL);
socialPost.setTags(arrayNode); socialPost.setTags(arrayNode);
socialPost.setItemTitle(title); socialPost.setItemTitle(title);
@ -720,6 +721,7 @@ public class CKANPackage extends CKAN implements Moderated {
if(this.itemID == null) { if(this.itemID == null) {
this.itemID = result.get(ID_KEY).asText(); 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); ((ObjectNode) jsonNode).remove(RESOURCES_KEY);
} }
String catalogueItemURL = "";
if(configuration.getScopeBean().is(Type.VRE)) { if(configuration.getScopeBean().is(Type.VRE)) {
catalogueItemURL = addItemURLViaResolver(jsonNode); addItemURLViaResolver(jsonNode);
} }
super.create(getAsString(jsonNode)); super.create(getAsString(jsonNode));
@ -780,7 +781,7 @@ public class CKANPackage extends CKAN implements Moderated {
if(configuration.getScopeBean().is(Type.VRE)) { if(configuration.getScopeBean().is(Type.VRE)) {
// Actions performed after a package has been correctly created on ckan. // Actions performed after a package has been correctly created on ckan.
String title = result.get(TITLE_KEY).asText(); 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 { private void postItemCreated() throws Exception {
try { try {
if(isModerationEnabled()) { if(isModerationEnabled()) {
moderationThread.setItemCoordinates(itemID, name); moderationThread.setItemCoordinates(itemID, name, itemURL);
moderationThread.postItemCreated(); moderationThread.postItemCreated();
} }
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
@ -1304,7 +1305,7 @@ public class CKANPackage extends CKAN implements Moderated {
private void postItemUpdated() { private void postItemUpdated() {
try { try {
if(isModerationEnabled()) { if(isModerationEnabled()) {
moderationThread.setItemCoordinates(itemID, name); moderationThread.setItemCoordinates(itemID, name, itemURL);
moderationThread.postItemUpdated(); moderationThread.postItemUpdated();
} }
} catch(WebApplicationException e) { } catch(WebApplicationException e) {
@ -1336,14 +1337,14 @@ public class CKANPackage extends CKAN implements Moderated {
String ret = sendPostRequest(ITEM_UPDATE, getAsString(result)); String ret = sendPostRequest(ITEM_UPDATE, getAsString(result));
result = mapper.readTree(ret); result = mapper.readTree(ret);
moderationThread.setItemCoordinates(itemID, name); itemURL = getExtraField(result, EXTRAS_ITEM_URL_KEY).asText();
moderationThread.setItemCoordinates(itemID, name, itemURL);
moderationThread.postItemApproved(moderatorMessage); moderationThread.postItemApproved(moderatorMessage);
if(configuration.getScopeBean().is(Type.VRE)) { if(configuration.getScopeBean().is(Type.VRE)) {
// Actions performed after a package has been correctly created on ckan. // Actions performed after a package has been correctly created on ckan.
String title = result.get(TITLE_KEY).asText(); String title = result.get(TITLE_KEY).asText();
String catalogueItemURL = getExtraField(result, EXTRAS_ITEM_URL_KEY).asText(); sendSocialPost(title);
sendSocialPost(title, catalogueItemURL);
} }
break; break;
@ -1384,7 +1385,7 @@ public class CKANPackage extends CKAN implements Moderated {
String ret = sendPostRequest(ITEM_PATCH, getAsString(result)); String ret = sendPostRequest(ITEM_PATCH, getAsString(result));
result = mapper.readTree(ret); result = mapper.readTree(ret);
moderationThread.setItemCoordinates(itemID, name); moderationThread.setItemCoordinates(itemID, name, itemURL);
moderationThread.postItemRejected(moderatorMessage); moderationThread.postItemRejected(moderatorMessage);
break; break;
@ -1420,7 +1421,7 @@ public class CKANPackage extends CKAN implements Moderated {
} }
CMItemStatus cmItemStatus = getCMItemStatus(); CMItemStatus cmItemStatus = getCMItemStatus();
moderationThread.setItemCoordinates(itemID, name); moderationThread.setItemCoordinates(itemID, name, itemURL);
moderationThread.postUserMessage(cmItemStatus, message); moderationThread.postUserMessage(cmItemStatus, message);
return; return;
} }

View File

@ -1,14 +1,41 @@
package org.gcube.gcat.social; package org.gcube.gcat.social;
import java.util.ArrayList;
import java.util.Collection; 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) * @author Luca Frosini (ISTI-CNR)
*/ */
public class Message { 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; protected String message;
@JsonProperty(value = "subject")
protected String subject; protected String subject;
@JsonIgnore
protected Collection<String> users; protected Collection<String> users;
public String getMessage() { public String getMessage() {
@ -27,18 +54,30 @@ public class Message {
this.subject = subject; this.subject = subject;
} }
@JsonIgnore
public Collection<String> getUsers() { public Collection<String> getUsers() {
return users; return users;
} }
@JsonIgnore
public void setUsers(Collection<String> users) { public void setUsers(Collection<String> users) {
this.users = users; this.users = users;
} }
@JsonIgnore
public void addUser(String user) { public void addUser(String user) {
this.users.add(user); this.users.add(user);
} }
@JsonGetter(value = "recipients")
public List<Recipient> getRecipients() {
List<Recipient> recipients = new ArrayList<>();
for(String username : users) {
recipients.add(new Recipient(username));
}
return recipients;
}
@Override @Override
public String toString() { public String toString() {
return "Message [message=" + message + ", subject=" + subject + ", users=" + users + "]"; return "Message [message=" + message + ", subject=" + subject + ", users=" + users + "]";

View File

@ -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.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; 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.authorization.utils.socialservice.SocialService;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.Constants;
@ -68,14 +70,14 @@ public class SocialMessage extends Thread {
} }
basePath = basePath.endsWith("/") ? basePath : basePath + "/"; basePath = basePath.endsWith("/") ? basePath : basePath + "/";
logger.debug("The message that is going to be send is\n{}", message);
String messageString = objectMapper.writeValueAsString(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 gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME); gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); 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); gxhttpStringRequest.path(SOCIAL_SERVICE_SEND_MESSAGE_PATH);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(messageString); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(messageString);

View File

@ -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);
}
}