-
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.
Merge pull request #30 from rwth-acis/develop
RocketChat channel connection
- Loading branch information
Showing
12 changed files
with
353 additions
and
122 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
8 changes: 8 additions & 0 deletions
8
project_service/src/main/java/i5/las2peer/services/projectService/chat/ChatConfig.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,8 @@ | ||
package i5.las2peer.services.projectService.chat; | ||
|
||
/** | ||
* Super class for all chat configurations. | ||
* Currently, only RocketChat config is implemented. | ||
*/ | ||
public abstract class ChatConfig { | ||
} |
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); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
project_service/src/main/java/i5/las2peer/services/projectService/chat/RocketChatConfig.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,49 @@ | ||
package i5.las2peer.services.projectService.chat; | ||
|
||
import org.json.simple.JSONObject; | ||
|
||
/** | ||
* RocketChat instance configuration. | ||
*/ | ||
public class RocketChatConfig extends ChatConfig { | ||
|
||
/** | ||
* Public url of RocketChat instance. | ||
*/ | ||
private String url; | ||
|
||
/** | ||
* Auth token of bot account that is used to make API calls. | ||
*/ | ||
private String botAuthToken; | ||
|
||
/** | ||
* User ID of bot account that is used to make API calls. | ||
*/ | ||
private String botUserId; | ||
|
||
public RocketChatConfig(String url, String botAuthToken, String botUserId) { | ||
this.url = url; | ||
this.botAuthToken = botAuthToken; | ||
this.botUserId = botUserId; | ||
} | ||
|
||
public static RocketChatConfig fromJSON(JSONObject config) { | ||
String url = (String) config.get("url"); | ||
String botAuthToken = (String) config.get("botAuthToken"); | ||
String botUserId = (String) config.get("botUserId"); | ||
return new RocketChatConfig(url, botAuthToken, botUserId); | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public String getBotAuthToken() { | ||
return botAuthToken; | ||
} | ||
|
||
public String getBotUserId() { | ||
return botUserId; | ||
} | ||
} |
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; | ||
} | ||
} |
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
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
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
26 changes: 26 additions & 0 deletions
26
..._service/src/test/java/i5/las2peer/services/projectService/chat/RocketChatConfigTest.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,26 @@ | ||
package i5.las2peer.services.projectService.chat; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class RocketChatConfigTest { | ||
|
||
private static String url = "http://url.com"; | ||
private static String botAuthToken = "token"; | ||
private static String botUserId = "id"; | ||
|
||
@Test | ||
public void fromJSONTest() { | ||
JSONObject configJSON = new JSONObject(); | ||
configJSON.put("url", url); | ||
configJSON.put("botAuthToken", botAuthToken); | ||
configJSON.put("botUserId", botUserId); | ||
|
||
RocketChatConfig chatConfig = RocketChatConfig.fromJSON(configJSON); | ||
assertEquals(chatConfig.getUrl(), url); | ||
assertEquals(chatConfig.getBotAuthToken(), botAuthToken); | ||
assertEquals(chatConfig.getBotUserId(), botUserId); | ||
} | ||
} |