2021-11-18 16:53:46 +01:00
package org.gcube.gcat.zulip ;
2021-11-17 17:47:58 +01:00
import java.util.Set ;
2021-11-25 17:58:56 +01:00
import javax.ws.rs.InternalServerErrorException ;
2021-11-18 16:53:46 +01:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode ;
2021-11-17 17:47:58 +01:00
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper ;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode ;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode ;
2021-11-18 17:49:24 +01:00
import org.gcube.gcat.api.CMItemStatus ;
2021-11-25 17:50:14 +01:00
import org.gcube.gcat.api.GCatConstants ;
2021-11-17 17:47:58 +01:00
import org.gcube.gcat.persistence.ckan.CKANUser ;
2021-11-25 17:50:14 +01:00
import org.gcube.gcat.social.SocialUsers ;
2021-11-23 17:53:37 +01:00
import org.gcube.gcat.utils.Constants ;
2021-11-25 17:58:56 +01:00
import org.gcube.gcat.utils.ContextUtility ;
import org.gcube.gcat.zulip.ZulipResponse.Result ;
2021-11-23 17:53:37 +01:00
import org.gcube.storagehub.ApplicationMode ;
2021-11-17 17:47:58 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import io.taliox.zulip.ZulipRestExecutor ;
2021-11-18 16:53:46 +01:00
import io.taliox.zulip.calls.ZulipRestAPICall ;
2021-11-17 17:47:58 +01:00
import io.taliox.zulip.calls.messages.PostMessage ;
2021-11-18 16:53:46 +01:00
import io.taliox.zulip.calls.streams.GetStreamID ;
2021-11-17 17:47:58 +01:00
import io.taliox.zulip.calls.streams.PostCreateStream ;
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
public class ZulipStream {
private static final Logger logger = LoggerFactory . getLogger ( ZulipStream . class ) ;
2021-11-23 17:53:37 +01:00
public static final String TOPICS_KEY = " topics " ;
public static final String NAME_KEY = " name " ;
public static final String MAX_ID_KEY = " max_id " ;
public static final String INITIAL_TOPIC_NAME = " hello " ;
protected ZulipRestExecutor gCatZulipRestExecutor ;
2021-11-18 17:49:24 +01:00
protected ZulipRestExecutor userZulipRestExecutor ;
2021-11-17 17:47:58 +01:00
protected String itemID ;
protected String itemName ;
protected String streamName ;
protected String streamDescription ;
protected CKANUser ckanUser ;
protected ObjectMapper objectMapper ;
2021-11-18 16:53:46 +01:00
2021-11-23 17:53:37 +01:00
protected ZulipRestExecutor getZulipRestExecutor ( ) {
2021-11-25 17:58:56 +01:00
ZulipAuth zulipAuth = new ZulipAuth ( ContextUtility . getUsername ( ) ) ;
return new ZulipRestExecutor ( zulipAuth . getEmail ( ) , zulipAuth . getAPIKey ( ) , zulipAuth . getSite ( ) ) ;
2021-11-23 17:53:37 +01:00
}
2021-11-18 17:49:24 +01:00
public ZulipStream ( ) {
2021-11-17 17:47:58 +01:00
this . objectMapper = new ObjectMapper ( ) ;
2021-11-23 17:53:37 +01:00
}
public ZulipRestExecutor getGCatZulipRestExecutor ( ) {
if ( gCatZulipRestExecutor = = null ) {
ApplicationMode applicationMode = new ApplicationMode ( Constants . getCatalogueApplicationToken ( ) ) ;
applicationMode . start ( ) ;
gCatZulipRestExecutor = getZulipRestExecutor ( ) ;
applicationMode . end ( ) ;
}
return gCatZulipRestExecutor ;
2021-11-17 17:47:58 +01:00
}
2021-11-18 17:49:24 +01:00
public ZulipRestExecutor getUserZulipRestExecutor ( ) {
if ( userZulipRestExecutor = = null ) {
2021-11-23 17:53:37 +01:00
userZulipRestExecutor = getZulipRestExecutor ( ) ;
2021-11-18 17:49:24 +01:00
}
return userZulipRestExecutor ;
}
2021-11-17 17:47:58 +01:00
public void setItemCoordinates ( String itemID , String itemName ) {
this . itemID = itemID ;
this . itemName = itemName ;
}
public void setCKANUser ( CKANUser ckanUser ) {
this . ckanUser = ckanUser ;
}
protected String getStreamName ( ) {
if ( streamName = = null ) {
2021-11-23 17:53:37 +01:00
streamName = String . format ( " Item '%s' moderation " , itemID ) ;
2021-11-17 17:47:58 +01:00
}
return streamName ;
}
2021-11-18 16:53:46 +01:00
protected Integer getStreamID ( ) throws Exception {
GetStreamID getStreamID = new GetStreamID ( getStreamName ( ) ) ;
2021-11-23 17:53:37 +01:00
ZulipResponse zulipResponse = executeZulipCall ( gCatZulipRestExecutor , getStreamID ) ;
2021-11-18 16:53:46 +01:00
JsonNode response = zulipResponse . getResponse ( ) ;
return response . get ( " stream_id " ) . asInt ( ) ;
}
2021-11-17 17:47:58 +01:00
protected String getStreamDescription ( ) {
if ( streamDescription = = null ) {
2021-11-23 17:53:37 +01:00
streamDescription = String . format ( " This stream is used to discuss about the moderation of the item '%s' with id '%s' " , itemName , itemID ) ;
2021-11-17 17:47:58 +01:00
}
return streamDescription ;
}
2021-11-23 17:53:37 +01:00
protected ZulipResponse executeZulipCall ( ZulipRestExecutor zulipRestExecutor , ZulipRestAPICall call ) throws Exception {
logger . trace ( " Going to execute {} " , call ) ;
2021-11-25 17:58:56 +01:00
String responseString = zulipRestExecutor . executeCall ( call ) ;
logger . trace ( " Response from {} is {} " , call . getClass ( ) . getSimpleName ( ) , responseString ) ;
ZulipResponse zulipResponse = new ZulipResponse ( responseString ) ;
if ( zulipResponse . getResponseResult ( ) = = Result . error ) {
throw new InternalServerErrorException ( zulipResponse . getResponseMessage ( ) ) ;
}
return zulipResponse ;
2021-11-18 16:53:46 +01:00
}
2021-11-25 14:20:08 +01:00
2021-11-18 16:53:46 +01:00
public void create ( ) throws Exception {
2021-11-17 17:47:58 +01:00
ArrayNode streamsArrayNode = objectMapper . createArrayNode ( ) ;
ObjectNode streamobjectNode = objectMapper . createObjectNode ( ) ;
streamobjectNode . put ( " name " , getStreamName ( ) ) ;
streamobjectNode . put ( " description " , getStreamDescription ( ) ) ;
streamsArrayNode . add ( streamobjectNode ) ;
ArrayNode principalsArrayNode = objectMapper . createArrayNode ( ) ;
// Going to add the item creator
String itemCreatorEmail = ckanUser . getPortalUser ( ) . getEMail ( ) ;
principalsArrayNode . add ( itemCreatorEmail ) ;
2021-11-23 17:53:37 +01:00
getGCatZulipRestExecutor ( ) ;
principalsArrayNode . add ( gCatZulipRestExecutor . httpController . getUserName ( ) ) ;
2021-11-25 17:50:14 +01:00
2021-11-17 17:47:58 +01:00
// Going to add the catalogue moderators
2021-11-25 17:50:14 +01:00
Set < String > moderators = SocialUsers . getUsernamesByRole ( GCatConstants . CATALOGUE_MODERATOR ) ;
2021-11-17 17:47:58 +01:00
for ( String moderator : moderators ) {
principalsArrayNode . add ( moderator ) ;
}
PostCreateStream postCreateStream = new PostCreateStream ( streamsArrayNode . toString ( ) ) ;
postCreateStream . setPrincipals ( principalsArrayNode . toString ( ) ) ;
postCreateStream . setInvite_only ( true ) ;
postCreateStream . setAnnounce ( false ) ;
2021-11-23 17:53:37 +01:00
executeZulipCall ( gCatZulipRestExecutor , postCreateStream ) ;
2021-11-18 16:53:46 +01:00
2021-11-23 17:53:37 +01:00
postItemCreated ( ) ;
2021-11-17 17:47:58 +01:00
}
2021-11-23 17:53:37 +01:00
protected void postMessageToStream ( ZulipRestExecutor zulipRestExecutor , CMItemStatus cmItemStatus , String message ) throws Exception {
2021-11-18 17:49:24 +01:00
PostMessage postMessage = new PostMessage ( getStreamName ( ) , cmItemStatus . getFancyValue ( ) , message ) ;
2021-11-23 17:53:37 +01:00
logger . debug ( " Going to send the following message: {} " , message ) ;
executeZulipCall ( zulipRestExecutor , postMessage ) ;
2021-11-17 17:47:58 +01:00
}
2021-11-23 17:53:37 +01:00
public void postUserMessageToStream ( CMItemStatus cmItemStatus , String message ) throws Exception {
2021-11-18 17:49:24 +01:00
postMessageToStream ( getUserZulipRestExecutor ( ) , cmItemStatus , message ) ;
}
2021-11-23 17:53:37 +01:00
private void postItemCreated ( ) throws Exception {
String username = ckanUser . getPortalUser ( ) . 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 ( ) ) ;
postMessageToStream ( getGCatZulipRestExecutor ( ) , cmItemStatus , message ) ;
2021-11-22 16:38:56 +01:00
}
2021-11-23 17:53:37 +01:00
public void postItemUpdated ( ) throws Exception {
String username = ckanUser . getPortalUser ( ) . 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 ( ) ) ;
postMessageToStream ( getGCatZulipRestExecutor ( ) , cmItemStatus , message ) ;
2021-11-17 17:47:58 +01:00
}
2021-11-23 17:53:37 +01:00
public void postItemRejected ( String userMessage ) throws Exception {
String username = ckanUser . getPortalUser ( ) . 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 ) ;
postMessageToStream ( getGCatZulipRestExecutor ( ) , cmItemStatus , message ) ;
postUserMessageToStream ( cmItemStatus , userMessage ) ;
2021-11-17 17:47:58 +01:00
}
2021-11-23 17:53:37 +01:00
public void postItemApproved ( String userMessage ) throws Exception {
String username = ckanUser . getPortalUser ( ) . 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 ) ;
postMessageToStream ( getGCatZulipRestExecutor ( ) , cmItemStatus , message ) ;
postUserMessageToStream ( cmItemStatus , userMessage ) ;
2021-11-22 16:38:56 +01:00
}
2021-11-17 17:47:58 +01:00
}