Skip to content
Snippets Groups Projects
Commit 3efa0de9 authored by Martin Staehr's avatar Martin Staehr
Browse files

#63 feat: implement channel creation of group chat

parent 508e415c
No related branches found
No related tags found
No related merge requests found
# Rocket Chat Configuration
## Admin Account
In `GFRocketChatConfig.java` add admin user account data.
## Personal Access Token for API
### Create non expiring access token
Go to `Administration -> Allgemeines -> REST API -> Enable Personal Access Tokens to REST API`
\ No newline at end of file
Go to `Administration -> Allgemeines -> REST API -> Enable Personal Access Tokens to REST API`
### Manual Personal Access Token
1. Click on your profile picture -> `Mein Konto` -> `Personal Access Token`
1. add a new personal access token
1. add it to configuration class `GFRocketChatConfig`
\ No newline at end of file
......@@ -33,6 +33,10 @@ public class User {
"", "", isStudent);
}
public User(String name, String password, String email, String rocketChatUsername, String rocketChatPersonalAccessToken, String rocketChatUserId) {
this(name, password, email, "", rocketChatUsername, "", rocketChatPersonalAccessToken, rocketChatUserId, false);
}
public User(String name, String password, String email, String token, String rocketChatUsername,
String rocketChatAuthToken, String rocketChatPersonalAccessToken, String rocketChatUserId,
Boolean isStudent) {
......
package unipotsdam.gf.interfaces;
import unipotsdam.gf.core.management.group.Group;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.states.model.ConstraintsMessages;
......@@ -32,12 +33,20 @@ public interface ICommunication {
* endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/create/
* creates chatroom
*
* @param name chat room name
* @param userList member of chat by id
* @param name chat room name
* @return chat room id
*/
String createChatRoom(String name, List<User> userList);
String createChatRoom(String name, boolean readOnly, List<User> users);
/**
* creates chatRoom with name group.projectId - group.id and set chatroomId for group
*
* @param group Object for information
* @return true if chatRoom was created, otherwise false
*/
boolean createChatRoom(Group group, boolean readOnly);
String createEmptyChatRoom(String name, boolean readOnly);
/**
* endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/invite/
......
package unipotsdam.gf.modules.communication.service;
import io.github.openunirest.http.HttpResponse;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import unipotsdam.gf.assignments.Assignee;
import unipotsdam.gf.assignments.NotImplementedLogger;
import unipotsdam.gf.core.management.group.Group;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserDAO;
......@@ -26,8 +30,8 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static unipotsdam.gf.config.GFRocketChatConfig.ADMIN_USER;
import static unipotsdam.gf.config.GFRocketChatConfig.ROCKET_CHAT_API_LINK;
@Resource
......@@ -35,6 +39,8 @@ import static unipotsdam.gf.config.GFRocketChatConfig.ROCKET_CHAT_API_LINK;
@Singleton
public class CommunicationDummyService implements ICommunication {
private Logger log = LoggerFactory.getLogger(CommunicationDummyService.class);
private UnirestService unirestService;
private UserDAO userDAO;
......@@ -62,12 +68,57 @@ public class CommunicationDummyService implements ICommunication {
}
@Override
public String createChatRoom(String name, List<User> userList) {
if (Objects.isNull(userList)) {
return "2";
public String createEmptyChatRoom(String name, boolean readOnly) {
return createChatRoom(name, readOnly, new ArrayList<>());
}
@Override
public String createChatRoom(String name, boolean readOnly, List<User> users) {
HashMap<String, String> headerMap = new HashMap<>();
headerMap.put("X-Auth-Token", ADMIN_USER.getRocketChatPersonalAccessToken());
headerMap.put("X-User-Id", ADMIN_USER.getRocketChatUserId());
ArrayList<String> usernameList = new ArrayList<>();
if (users.isEmpty()) {
usernameList.add(ADMIN_USER.getRocketChatUsername());
}
HashMap<String, Object> bodyMap = new HashMap<>();
bodyMap.put("name", name);
bodyMap.put("readOnly", readOnly);
if (!users.isEmpty()) {
bodyMap.put("members", usernameList);
}
HttpResponse<Map> response =
unirestService
.post(ROCKET_CHAT_API_LINK + "groups.create")
.headers(headerMap)
.body(bodyMap)
.asObject(Map.class);
if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) {
return Strings.EMPTY;
}
Map responseMap = response.getBody();
if (responseMap.get("success").equals("false") || responseMap.containsKey("status")) {
return Strings.EMPTY;
}
Map groupMap = (Map) responseMap.get("group");
return groupMap.get("_id").toString();
}
@Override
public boolean createChatRoom(Group group, boolean readOnly) {
// chatRoom name: projectId - GroupId
String chatRoomName = String.join(" - ", group.getProjectId(), String.valueOf(group.getId()));
String chatRoomId = createChatRoom(chatRoomName, readOnly, group.getMembers());
if (chatRoomId.isEmpty()) {
group.setChatRoomId(chatRoomId);
}
return "1";
return true;
}
@Override
......@@ -154,7 +205,7 @@ public class CommunicationDummyService implements ICommunication {
user.setRocketChatUserId(registerResponse.getUserId());
if (!loginUser(user)) {
// TODO: eventually consider roleback because of error or something
// TODO: eventually consider rollback because of error or something
return false;
}
......
......@@ -2,6 +2,7 @@ package unipotsdam.gf.modules.communication.view;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import unipotsdam.gf.config.GFRocketChatConfig;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.model.Message;
......@@ -20,6 +21,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static java.util.Objects.isNull;
......@@ -163,7 +166,7 @@ public class CommunicationView {
if (isNull(name)) {
return Response.status(Response.Status.BAD_REQUEST).entity("must provide name as queryParam").build();
}
String chatId = communicationService.createChatRoom(name, users);
String chatId = communicationService.createChatRoom(name, false, users);
if (isNull(chatId)) {
log.error("error while creating chatRoom for: name: {}, users: {}", name, users);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
......@@ -182,5 +185,32 @@ public class CommunicationView {
return Response.ok(user).build();
}
@GET
@Path("/auth")
@Produces(MediaType.APPLICATION_JSON)
public Response getAuth() {
User user = new User("Test", "password", "email", false);
HashMap<String, String> authentificationMap = new HashMap<>();
authentificationMap.put("user", user.getEmail());
authentificationMap.put("password", user.getPassword());
return Response.ok(authentificationMap).build();
}
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test() {
List<String> usernameList = new ArrayList<>();
usernameList.add(GFRocketChatConfig.ADMIN_USER.getRocketChatUsername());
usernameList.add(GFRocketChatConfig.TEST_USER.getRocketChatUsername());
HashMap<String, Object> bodyMap = new HashMap<>();
bodyMap.put("name", "Test");
bodyMap.put("readOnly", false);
if (!usernameList.isEmpty()) {
bodyMap.put("members", usernameList);
}
return Response.ok(bodyMap).build();
}
}
......@@ -58,7 +58,7 @@ public class DummyProjectCreationService {
List<Group> groupsWithId = groupDAO.getGroupsByProjectId(project.getId());
groupsWithId.forEach(group -> {
String chatRoomName = String.join(" - ", project.getId(), String.valueOf(group.getId()));
group.setChatRoomId(communicationService.createChatRoom(chatRoomName, group.getMembers()));
group.setChatRoomId(communicationService.createChatRoom(chatRoomName, false, group.getMembers()));
management.update(group);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment