Switching moderation communication to notification

This commit is contained in:
Luca Frosini 2022-05-27 12:14:07 +02:00
parent 41a8011afb
commit b88f72a6d2
3 changed files with 92 additions and 32 deletions

View File

@ -10,6 +10,7 @@ import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.moderation.thread.ModerationThread;
import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.social.SocialUsers;
import org.gcube.gcat.utils.Constants;
import org.gcube.social_networking.social_networking_client_library.NotificationClient;
@ -53,31 +54,20 @@ public class SocialNotificationModerationThread extends ModerationThread {
/**
* Create the message for an item that is created/updated
*/
public void notifyItemToBeManaged() throws Exception {
protected void notifyItemToBeManaged() 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 <url of the item>
* [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]
*
*/
cmItemStatus = CMItemStatus.PENDING;
String fullName = ckanUser.getNameSurname();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(fullName);
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);
stringBuffer.append("the item ");
stringBuffer.append(itemTitle);
stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it. ");
postMessage(stringBuffer.toString());
}
@ -105,19 +95,28 @@ public class SocialNotificationModerationThread extends ModerationThread {
}
public void postItemManaged(String userMessage) throws Exception {
/*
* [mister x] rejected the item "[TITLE]" with this accompanying message "[MESSAGE]". To resubmit it [Go to catalogue]
*
* [mister x] approved the item "[TITLE]" with this accompanying message "[MESSAGE]". [Go to catalogue]
*/
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");
stringBuffer.append(" the item '");
stringBuffer.append(itemTitle);
stringBuffer.append("'");
if(userMessage!=null && userMessage.length()>0) {
stringBuffer.append(" with this accompanying message\n\n");
stringBuffer = getMainItemInfo(stringBuffer);
stringBuffer.append(" with this accompanying message '");
stringBuffer.append(userMessage);
}else {
stringBuffer.append("\n\n");
stringBuffer = getMainItemInfo(stringBuffer);
stringBuffer.append("'");
}
stringBuffer.append(".");
if(cmItemStatus == CMItemStatus.REJECTED) {
stringBuffer.append(" To resubmit it ");
}
postMessage(stringBuffer.toString());
}
@ -126,6 +125,7 @@ public class SocialNotificationModerationThread extends ModerationThread {
public void postItemRejected(String userMessage) throws Exception {
create = false;
cmItemStatus = CMItemStatus.REJECTED;
catalogueEventType = CatalogueEventType.ITEM_REJECTED;
postItemManaged(userMessage);
}
@ -133,23 +133,42 @@ public class SocialNotificationModerationThread extends ModerationThread {
public void postItemApproved(String userMessage) throws Exception {
create = false;
cmItemStatus = CMItemStatus.APPROVED;
catalogueEventType = CatalogueEventType.ITEM_PUBLISHED;
postItemManaged(userMessage);
}
protected String getSubject() {
StringWriter stringWriter = new StringWriter();
if(!create) {
logger.trace("It's a reply");
stringWriter.append("Re: ");
if(catalogueEventType!=null) {
switch (catalogueEventType) {
case ITEM_SUBMITTED:
stringWriter.append("Submitted item '");
break;
case ITEM_UPDATED:
stringWriter.append("Updated item '");
break;
case ITEM_REJECTED:
case ITEM_PUBLISHED:
stringWriter.append(cmItemStatus.getFancyValue());
stringWriter.append(" item '");
break;
default:
break;
}
}else {
stringWriter.append("Message for item '");
}
stringWriter.append("[Catalogue Service] ");
stringWriter.append(itemTitle);
stringWriter.append("'");
return stringWriter.toString();
}
protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception {
CatalogueEvent catalogueEvent = new CatalogueEvent();
catalogueEvent.setItemId(itemID);
catalogueEvent.setItemId(getSubject());
if(cmItemStatus == CMItemStatus.APPROVED) {
catalogueEvent.setItemURL(new URL(itemURL));
}else {
@ -159,15 +178,18 @@ public class SocialNotificationModerationThread extends ModerationThread {
catalogueEvent.setNotifyText(messageString);
Set<String> users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
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()));
catalogueEvent.setIdsToNotify(users.toArray(new String[users.size()]));
catalogueEvent.setIdsAsGroup(false);
return catalogueEvent;
}
@ -189,6 +211,24 @@ public class SocialNotificationModerationThread extends ModerationThread {
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {
this.create = false;
this.cmItemStatus = cmItemStatus;
switch (cmItemStatus) {
case PENDING:
catalogueEventType = CatalogueEventType.ITEM_UPDATED;
break;
case APPROVED:
catalogueEventType = CatalogueEventType.ITEM_PUBLISHED;
break;
case REJECTED:
catalogueEventType = CatalogueEventType.ITEM_REJECTED;
break;
default:
break;
}
String fullName = ckanUser.getNameSurname();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer);

View File

@ -199,11 +199,20 @@ public class CKANUser extends CKAN {
}
protected static String getCKANUsername(String username) {
if(username == null)
if(username == null) {
return null;
}
return username.trim().replaceAll("\\.", "_");
}
public static String getUsernameFromCKANUsername(String ckanUsername) {
if(ckanUsername == null) {
return null;
}
return ckanUsername.trim().replaceAll("_", ".");
}
public static String getCKANUsername() {
String username = SecretManagerProvider.instance.get().getUser().getUsername();
return getCKANUsername(username);

View File

@ -1,6 +1,7 @@
package org.gcube.gcat.persistence.ckan;
import org.gcube.gcat.ContextTest;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,6 +23,16 @@ public class CKANUserTest extends ContextTest {
return user;
}
@Test
public void getUsernameFromCkanUsername() {
String username = "luca.frosini";
String ckanUsername = CKANUser.getCKANUsername(username);
Assert.assertTrue(ckanUsername.compareTo("luca_frosini")==0);
String gotUsername = CKANUser.getUsernameFromCKANUsername(ckanUsername);
Assert.assertTrue(gotUsername.compareTo(username)==0);
}
@Test
public void list() throws Exception {
CKANUser ckanUser = getCKANUser();