Adding Zulip interaction
This commit is contained in:
parent
89b98f625f
commit
8d1fc7f045
|
@ -0,0 +1,57 @@
|
|||
package org.gcube.gcat.zulip;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ZulipResponse {
|
||||
|
||||
public static final String RESULT_KEY = "result";
|
||||
public static final String MSG_KEY = "msg";
|
||||
|
||||
public enum Result {
|
||||
success,
|
||||
error
|
||||
}
|
||||
|
||||
protected ObjectMapper objectMapper;
|
||||
|
||||
protected String responseString;
|
||||
protected JsonNode response;
|
||||
|
||||
protected Result result;
|
||||
protected String message;
|
||||
|
||||
public ZulipResponse(String responseString) {
|
||||
this.responseString = responseString;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
public Result getResponseResult() throws JsonProcessingException, IOException {
|
||||
if(result==null) {
|
||||
String resultString = getResponse().get(RESULT_KEY).asText();
|
||||
result = Result.valueOf(resultString);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getResponseMessage() throws JsonProcessingException, IOException {
|
||||
if(message==null) {
|
||||
message = getResponse().get(MSG_KEY).asText();
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
public JsonNode getResponse() throws JsonProcessingException, IOException {
|
||||
if(response == null) {
|
||||
response = objectMapper.readTree(responseString);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,17 +1,23 @@
|
|||
package org.gcube.gcat.social;
|
||||
package org.gcube.gcat.zulip;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.ws.rs.InternalServerErrorException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
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;
|
||||
import org.gcube.gcat.persistence.ckan.CKANUser;
|
||||
import org.gcube.gcat.zulip.ZulipResponse.Result;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.taliox.zulip.ZulipRestExecutor;
|
||||
import io.taliox.zulip.calls.ZulipRestAPICall;
|
||||
import io.taliox.zulip.calls.messages.PostMessage;
|
||||
import io.taliox.zulip.calls.streams.GetStreamID;
|
||||
import io.taliox.zulip.calls.streams.PostCreateStream;
|
||||
|
||||
/**
|
||||
|
@ -31,6 +37,7 @@ public class ZulipStream {
|
|||
protected Set<String> moderators;
|
||||
|
||||
protected ObjectMapper objectMapper;
|
||||
|
||||
protected ZulipRestExecutor zulipRestExecutor;
|
||||
|
||||
public ZulipStream(String email, String password) {
|
||||
|
@ -55,6 +62,13 @@ public class ZulipStream {
|
|||
return streamName;
|
||||
}
|
||||
|
||||
protected Integer getStreamID() throws Exception {
|
||||
GetStreamID getStreamID = new GetStreamID(getStreamName());
|
||||
ZulipResponse zulipResponse = executeZulipCall(getStreamID);
|
||||
JsonNode response = zulipResponse.getResponse();
|
||||
return response.get("stream_id").asInt();
|
||||
}
|
||||
|
||||
protected String getStreamDescription() {
|
||||
if(streamDescription==null) {
|
||||
streamDescription = String.format("This stream is used to discuss about the moderation of the item '{}' with id '{}'", itemName, itemID);
|
||||
|
@ -62,7 +76,16 @@ public class ZulipStream {
|
|||
return streamDescription;
|
||||
}
|
||||
|
||||
public void create() {
|
||||
protected ZulipResponse executeZulipCall(ZulipRestAPICall call) throws Exception {
|
||||
String responseString = zulipRestExecutor.executeCall(call);
|
||||
ZulipResponse zulipResponse = new ZulipResponse(responseString);
|
||||
if(zulipResponse.getResponseResult()==Result.error) {
|
||||
throw new InternalServerErrorException(zulipResponse.getResponseMessage());
|
||||
}
|
||||
return zulipResponse;
|
||||
}
|
||||
|
||||
public void create() throws Exception {
|
||||
ArrayNode streamsArrayNode = objectMapper.createArrayNode();
|
||||
ObjectNode streamobjectNode = objectMapper.createObjectNode();
|
||||
streamobjectNode.put("name", getStreamName());
|
||||
|
@ -83,8 +106,8 @@ public class ZulipStream {
|
|||
postCreateStream.setInvite_only(true);
|
||||
postCreateStream.setAnnounce(false);
|
||||
|
||||
String response = zulipRestExecutor.executeCall(postCreateStream);
|
||||
logger.trace(response);
|
||||
executeZulipCall(postCreateStream);
|
||||
|
||||
}
|
||||
|
||||
private void postMessageToStream(String topic, String message) {
|
Loading…
Reference in New Issue