-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
project_service/src/main/java/i5/las2peer/services/projectService/chat/ChatManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ect_service/src/main/java/i5/las2peer/services/projectService/chat/RocketChatManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |