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.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.gcube.gcat.api.moderation.CMItemStatus; 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.gcat.persistence.ckan.CKANUser;
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP; import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP;
@ -35,7 +35,8 @@ public abstract class ModerationThread {
public static ModerationThread getDefaultInstance() { public static ModerationThread getDefaultInstance() {
// return new FakeModerationThread(); // return new FakeModerationThread();
return new SocialMessageModerationThread(); // return new SocialMessageModerationThread();
return new SocialNotificationModerationThread();
} }
public ModerationThread() { public ModerationThread() {

View File

@ -1,6 +1,5 @@
package org.gcube.gcat.moderation.thread.social.notifications; package org.gcube.gcat.moderation.thread.social.notifications;
import java.io.StringWriter;
import java.net.URL; import java.net.URL;
import java.util.Set; import java.util.Set;
@ -26,29 +25,20 @@ public class SocialNotificationModerationThread extends ModerationThread {
private static final Logger logger = LoggerFactory.getLogger(SocialNotificationModerationThread.class); private static final Logger logger = LoggerFactory.getLogger(SocialNotificationModerationThread.class);
protected CatalogueEventType catalogueEventType; public static final String AUTHOR = "Author";
protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) { protected CatalogueEventType catalogueEventType;
stringBuffer.append("Status: "); protected boolean comment;
stringBuffer.append(cmItemStatus.getFancyValue());
stringBuffer.append("\n"); protected static final boolean notificationSentByGCat;
stringBuffer.append("Title: ");
stringBuffer.append(itemTitle); static {
stringBuffer.append("\n"); notificationSentByGCat = false;
stringBuffer.append("Name: "); }
stringBuffer.append(itemName);
stringBuffer.append("\n"); public SocialNotificationModerationThread() {
stringBuffer.append("ID: "); super();
stringBuffer.append(itemID); this.comment = false;
stringBuffer.append("\n");
stringBuffer.append("URL: ");
if(cmItemStatus == CMItemStatus.APPROVED) {
stringBuffer.append(itemURL);
}else {
stringBuffer.append(getModerationURL());
}
stringBuffer.append("\n\n");
return stringBuffer;
} }
/** /**
@ -58,16 +48,18 @@ public class SocialNotificationModerationThread extends ModerationThread {
/* /*
* An example of created message is: * 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(); String fullName = ckanUser.getNameSurname();
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(fullName); if(notificationSentByGCat) {
stringBuffer.append(fullName);
}
stringBuffer.append(create ? " created " : " updated "); stringBuffer.append(create ? " created " : " updated ");
stringBuffer.append("the item "); stringBuffer.append("the item ");
stringBuffer.append(itemTitle); stringBuffer = addQuotedTitle(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 it. ");
postMessage(stringBuffer.toString()); postMessage(stringBuffer.toString());
} }
@ -86,14 +78,22 @@ public class SocialNotificationModerationThread extends ModerationThread {
notifyItemToBeManaged(); notifyItemToBeManaged();
} }
protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer, boolean addUserFullName) {
stringBuffer.append(fullName); if(addUserFullName) {
stringBuffer.append(" ["); stringBuffer.append(fullName);
stringBuffer.append(role); }
stringBuffer.append("] "); if(role!=null) {
stringBuffer.append(" [");
stringBuffer.append(role);
stringBuffer.append("] ");
}
return stringBuffer; return stringBuffer;
} }
protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) {
return addUserWithRole(fullName, role, stringBuffer, notificationSentByGCat);
}
public void postItemManaged(String userMessage) throws Exception { 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] 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 stringBuffer = new StringBuffer();
stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer);
stringBuffer.append(cmItemStatus.getValue()); stringBuffer.append(cmItemStatus.getValue());
stringBuffer.append(" the item '"); stringBuffer.append(" the item ");
stringBuffer.append(itemTitle); stringBuffer = addQuotedTitle(stringBuffer);
stringBuffer.append("'");
if(userMessage!=null && userMessage.length()>0) { if(userMessage!=null && userMessage.length()>0) {
stringBuffer.append(" with this accompanying message '"); stringBuffer.append(" with this accompanying message \"");
stringBuffer.append(userMessage); stringBuffer.append(userMessage);
stringBuffer.append("'"); stringBuffer.append("\"");
} }
stringBuffer.append("."); stringBuffer.append(".");
@ -137,37 +136,54 @@ public class SocialNotificationModerationThread extends ModerationThread {
postItemManaged(userMessage); 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() { protected String getSubject() {
StringWriter stringWriter = new StringWriter(); StringBuffer stringBuffer = new StringBuffer();
if(catalogueEventType!=null) { String fullName = ckanUser.getNameSurname();
if(!comment) {
switch (catalogueEventType) { switch (catalogueEventType) {
case ITEM_SUBMITTED: case ITEM_SUBMITTED:
stringWriter.append("Submitted item '"); stringBuffer.append(fullName);
stringBuffer.append(" created the item ");
break; break;
case ITEM_UPDATED: case ITEM_UPDATED:
stringWriter.append("Updated item '"); stringBuffer.append(fullName);
stringBuffer.append(" updated the item ");
break; break;
case ITEM_REJECTED: case ITEM_REJECTED:
case ITEM_PUBLISHED: case ITEM_PUBLISHED:
stringWriter.append(cmItemStatus.getFancyValue()); addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer, true);
stringWriter.append(" item '"); stringBuffer.append(cmItemStatus.getValue());
stringBuffer.append(" the item ");
break; break;
default: default:
break; break;
} }
}else { }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); stringBuffer = addQuotedTitle(stringBuffer);
stringWriter.append("'"); return stringBuffer.toString();
return stringWriter.toString();
} }
protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception { protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception {
CatalogueEvent catalogueEvent = new CatalogueEvent(); CatalogueEvent catalogueEvent = new CatalogueEvent();
catalogueEvent.setType(catalogueEventType);
catalogueEvent.setNotifyText(messageString);
catalogueEvent.setItemId(getSubject()); catalogueEvent.setItemId(getSubject());
if(cmItemStatus == CMItemStatus.APPROVED) { if(cmItemStatus == CMItemStatus.APPROVED) {
catalogueEvent.setItemURL(new URL(itemURL)); catalogueEvent.setItemURL(new URL(itemURL));
@ -175,16 +191,13 @@ public class SocialNotificationModerationThread extends ModerationThread {
catalogueEvent.setItemURL(new URL(getModerationURL())); catalogueEvent.setItemURL(new URL(getModerationURL()));
} }
catalogueEvent.setNotifyText(messageString);
Set<String> users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR); Set<String> users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
users.add("luca.frosini");
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
String username = secretManager.getUser().getUsername(); String username = secretManager.getUser().getUsername();
// Adding the user is generating the event // Adding the user is generating the event
users.add(username); users.add(username);
// Adding item creator // Adding item creator
users.add(CKANUser.getUsernameFromCKANUsername(ckanUser.getName())); users.add(CKANUser.getUsernameFromCKANUsername(ckanUser.getName()));
@ -199,18 +212,26 @@ public class SocialNotificationModerationThread extends ModerationThread {
CatalogueEvent catalogueEvent = getCatalogueEvent(messageString); CatalogueEvent catalogueEvent = getCatalogueEvent(messageString);
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret(); Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret); if(notificationSentByGCat) {
secretManager.startSession(secret);
}
try { try {
sendNotification(catalogueEvent); sendNotification(catalogueEvent);
}finally { }finally {
secretManager.endSession(); if(notificationSentByGCat) {
secretManager.endSession();
}
} }
} }
@Override @Override
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { 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.create = false;
this.cmItemStatus = cmItemStatus; this.cmItemStatus = cmItemStatus;
this.comment = true;
switch (cmItemStatus) { switch (cmItemStatus) {
case PENDING: case PENDING:
@ -231,24 +252,40 @@ public class SocialNotificationModerationThread extends ModerationThread {
String fullName = ckanUser.getNameSurname(); String fullName = ckanUser.getNameSurname();
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer); stringBuffer = addUserWithRole(fullName, itemAuthor ? SocialNotificationModerationThread.AUTHOR : Moderated.CATALOGUE_MODERATOR, stringBuffer);
stringBuffer.append("sent a message regarding the following item\n\n"); stringBuffer.append("commented on the item ");
stringBuffer = getMainItemInfo(stringBuffer); stringBuffer = addQuotedTitle(stringBuffer);
stringBuffer.append(" as follows \"");
stringBuffer.append(userMessage); stringBuffer.append(userMessage);
stringBuffer.append("\".");
CatalogueEvent catalogueEvent = getCatalogueEvent(stringBuffer.toString()); CatalogueEvent catalogueEvent = getCatalogueEvent(stringBuffer.toString());
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret(); Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret); if(notificationSentByGCat) {
secretManager.startSession(secret);
}
try { try {
sendNotification(catalogueEvent); sendNotification(catalogueEvent);
}finally { }finally {
secretManager.endSession(); if(notificationSentByGCat) {
secretManager.endSession();
}
} }
} }
protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception { protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception {
NotificationClient nc = new NotificationClient(); Thread thread = new Thread() {
nc.sendCatalogueEvent(catalogueEvent); 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 @Override

View File

@ -4,7 +4,6 @@ import org.gcube.gcat.ContextTest;
import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.moderation.thread.ModerationThread; import org.gcube.gcat.moderation.thread.ModerationThread;
import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUser;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
/** /**
@ -12,23 +11,47 @@ import org.junit.Test;
*/ */
public class ModerationThreadTest extends ContextTest { public class ModerationThreadTest extends ContextTest {
@Test protected ModerationThread getModerationThread(CKANUser ckanUser) {
@Ignore
public void test() throws Exception {
ModerationThread moderationThread = ModerationThread.getDefaultInstance(); 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 ckanUser = new CKANUser();
ckanUser.setName(CKANUser.getCKANUsername()); ckanUser.setName(CKANUser.getCKANUsername());
ckanUser.read(); ckanUser.read();
moderationThread.setCKANUser(ckanUser);
ModerationThread moderationThread = getModerationThread(ckanUser);
moderationThread.postItemCreated(); moderationThread.postItemCreated();
moderationThread = getModerationThread(ckanUser);
moderationThread.postItemUpdated(); moderationThread.postItemUpdated();
moderationThread = getModerationThread(ckanUser);
moderationThread.postUserMessage(CMItemStatus.PENDING, "Pensaci Bene");
moderationThread = getModerationThread(ckanUser);
moderationThread.postItemRejected(null); moderationThread.postItemRejected(null);
moderationThread = getModerationThread(ckanUser);
moderationThread.postItemRejected("reject con messaggio: Non mi garba"); moderationThread.postItemRejected("reject con messaggio: Non mi garba");
moderationThread = getModerationThread(ckanUser);
moderationThread.postItemApproved(null); moderationThread.postItemApproved(null);
moderationThread = getModerationThread(ckanUser);
moderationThread.postItemApproved("approve con messaggio: Ora mi garba"); moderationThread.postItemApproved("approve con messaggio: Ora mi garba");
moderationThread = getModerationThread(ckanUser);
moderationThread.setItemAuthor(true); moderationThread.setItemAuthor(true);
moderationThread.postUserMessage(CMItemStatus.APPROVED, "Grazie"); moderationThread.postUserMessage(CMItemStatus.APPROVED, "Grazie");
Thread.sleep(1000); Thread.sleep(1000);
} }

View File

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