diff --git a/project_service/build.gradle b/project_service/build.gradle index 85845c3..fa9d6f6 100644 --- a/project_service/build.gradle +++ b/project_service/build.gradle @@ -31,6 +31,8 @@ dependencies { // las2peer bundle which is not necessary in the runtime path // compileOnly will be moved into the lib dir afterwards implementation "i5:las2peer-bundle:${project.property('core.version')}" + + implementation "com.konghq:unirest-java:3.13.10" } diff --git a/project_service/src/main/java/i5/las2peer/services/projectService/chat/ChatManager.java b/project_service/src/main/java/i5/las2peer/services/projectService/chat/ChatManager.java new file mode 100644 index 0000000..1a5e619 --- /dev/null +++ b/project_service/src/main/java/i5/las2peer/services/projectService/chat/ChatManager.java @@ -0,0 +1,20 @@ +package i5.las2peer.services.projectService.chat; + +import i5.las2peer.services.projectService.project.Project; +import org.json.simple.JSONObject; + +/** + * Super class for all chat managers. + * Currently, only RocketChat is implemented. + */ +public abstract class ChatManager { + + protected ChatConfig config; + + public ChatManager(ChatConfig config) { + this.config = config; + } + + public abstract JSONObject createProjectChannel(Project project, String systemName); + +} diff --git a/project_service/src/main/java/i5/las2peer/services/projectService/chat/RocketChatManager.java b/project_service/src/main/java/i5/las2peer/services/projectService/chat/RocketChatManager.java new file mode 100644 index 0000000..c65a055 --- /dev/null +++ b/project_service/src/main/java/i5/las2peer/services/projectService/chat/RocketChatManager.java @@ -0,0 +1,51 @@ +package i5.las2peer.services.projectService.chat; + +import i5.las2peer.services.projectService.project.Project; +import kong.unirest.HttpResponse; +import kong.unirest.Unirest; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +/** + * ChatManager for RocketChat. Allows to create channels for las2peer projects. + */ +public class RocketChatManager extends ChatManager { + + public RocketChatManager(ChatConfig config) { + super(config); + } + + @Override + public JSONObject createProjectChannel(Project project, String systemName) { + String channelName = project.getName().replaceAll(" ", "") + "_" + systemName; + + JSONObject body = new JSONObject(); + body.put("name", channelName); + + HttpResponse response = Unirest.post(getConfig().getUrl() + "/api/v1/channels.create") + .header("X-Auth-Token", getConfig().getBotAuthToken()) + .header("X-User-Id", getConfig().getBotUserId()) + .header("Content-Type", "application/json") + .body(body.toJSONString()) + .asString(); + + if(!response.isSuccess()) { + System.out.println("RocketChat channel creation failed with status code: " + response.getStatus()); + return null; + } + + JSONObject res = (JSONObject) JSONValue.parse(response.getBody()); + JSONObject resChannel = (JSONObject) res.get("channel"); + + JSONObject channelInfo = new JSONObject(); + channelInfo.put("type", "RocketChat"); + channelInfo.put("url", getConfig().getUrl()); + channelInfo.put("channelId", resChannel.get("_id")); + channelInfo.put("chatUrl", getConfig().getUrl() + "/channel/" + resChannel.get("_id")); + return channelInfo; + } + + private RocketChatConfig getConfig() { + return (RocketChatConfig) this.config; + } +}