Switch to notification completed

This commit is contained in:
Luca Frosini 2022-05-27 17:20:52 +02:00
parent b88f72a6d2
commit 01d2e527a1
4 changed files with 131 additions and 69 deletions

View File

@ -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() {

View File

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

View File

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

View File

@ -10,8 +10,9 @@
<logger name="org.gcube" level="ERROR" />
<logger name="org.gcube.gcat" level="ERROR" />
<logger name="org.gcube.gcat" level="TRACE" />
<logger name="org.gcube.gcat.persistence.grsf" level="TRACE" />
<logger name="org.gcube.social_networking.social_networking_client_library.utils" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />