Skip to content

Commit

Permalink
Add ChatManager #28
Browse files Browse the repository at this point in the history
  • Loading branch information
pdolif committed Jul 7, 2022
1 parent 27614d4 commit 83276af
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions project_service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}

0 comments on commit 83276af

Please sign in to comment.