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

View File

@ -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

View File

@ -118,6 +118,7 @@ public class CKANPackage extends CKAN implements Moderated {
protected final List<CKANResource> 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;
}

View File

@ -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<String> users;
public String getMessage() {
@ -27,18 +54,30 @@ public class Message {
this.subject = subject;
}
@JsonIgnore
public Collection<String> getUsers() {
return users;
}
@JsonIgnore
public void setUsers(Collection<String> users) {
this.users = users;
}
@JsonIgnore
public void addUser(String 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
public String toString() {
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.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);

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