Skip to content

Commit

Permalink
Merge pull request #592 from Onlineberatung/feature-leave-group
Browse files Browse the repository at this point in the history
Feature leave group
  • Loading branch information
koepferd authored Dec 22, 2022
2 parents 8ac9d24 + 1de65b0 commit cfe510e
Show file tree
Hide file tree
Showing 27 changed files with 5,021 additions and 167 deletions.
4,919 changes: 4,888 additions & 31 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupMemberDTO;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatAddUserToGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveUserFromGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLeaveFromGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatUserNotInitializedException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -56,12 +56,12 @@ public void rollbackRemoveUsersFromRocketChatGroup(
}
}

// Remove technical user from Rocket.Chat group
// Leave from Rocket.Chat group as technical user
try {
rocketChatService.removeTechnicalUserFromGroup(groupId);
} catch (RocketChatRemoveUserFromGroupException e) {
rocketChatService.leaveFromGroupAsTechnicalUser(groupId);
} catch (RocketChatLeaveFromGroupException e) {
log.error(
"Internal Server Error: Could not remove technical user from Rocket.Chat group "
"Internal Server Error: Could not leave from Rocket.Chat group as technical user "
+ "id {} during roll back.",
groupId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupDTO;
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupDeleteBodyDTO;
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupDeleteResponseDTO;
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupLeaveBodyDTO;
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupMemberDTO;
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupMemberResponseDTO;
import de.caritas.cob.userservice.api.adapters.rocketchat.dto.group.GroupRemoveUserBodyDTO;
Expand Down Expand Up @@ -46,6 +47,7 @@
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatGetGroupMembersException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatGetGroupsListAllException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatGetUserIdException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLeaveFromGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLoginException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveSystemMessagesException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveUserFromGroupException;
Expand Down Expand Up @@ -92,6 +94,7 @@ public class RocketChatService implements MessageClient {
private static final String ENDPOINT_GROUP_DELETE = "/groups.delete";
private static final String ENDPOINT_GROUP_INVITE = "/groups.invite";
private static final String ENDPOINT_GROUP_KICK = "/groups.kick";
private static final String ENDPOINT_ROOM_LEAVE = "/rooms.leave";
private static final String ENDPOINT_GROUP_MEMBERS = "/groups.members";
private static final String ENDPOINT_GROUP_READ_ONLY = "/groups.setReadOnly";
private static final String ENDPOINT_GROUP_KEY_UPDATE = "/e2e.updateGroupKey";
Expand Down Expand Up @@ -576,6 +579,37 @@ public void addTechnicalUserToGroup(String rcGroupId)
this.addUserToGroup(rcCredentialHelper.getTechnicalUser().getRocketChatUserId(), rcGroupId);
}

/**
* Leave from the Rocket.Chat group with given groupId as the technical user.
*
* @param rcGroupId Rocket.Chat roomId
* @throws RocketChatLeaveFromGroupException on failure
*/
public void leaveFromGroupAsTechnicalUser(String rcGroupId)
throws RocketChatLeaveFromGroupException {

GroupResponseDTO response;
try {
var technicalUser = rcCredentialHelper.getTechnicalUser();
var header = getStandardHttpHeaders(technicalUser);
var body = new GroupLeaveBodyDTO(rcGroupId);
HttpEntity<GroupLeaveBodyDTO> request = new HttpEntity<>(body, header);

var url = rocketChatConfig.getApiUrl(ENDPOINT_ROOM_LEAVE);
response = restTemplate.postForObject(url, request, GroupResponseDTO.class);

} catch (Exception ex) {
throw new RocketChatLeaveFromGroupException(
String.format(
"Could not leave as technical user from Rocket.Chat group with id %s", rcGroupId));
}

if (response != null && !response.isSuccess()) {
var error = "Could not leave as technical user from Rocket.Chat group with id %s";
throw new RocketChatLeaveFromGroupException(String.format(error, rcGroupId));
}
}

/**
* Removes the provided user from the Rocket.Chat group with given groupId.
*
Expand Down Expand Up @@ -612,7 +646,7 @@ public boolean removeUserFromSession(String chatUserId, String chatId) {
try {
addTechnicalUserToGroup(chatId);
removeUserFromGroup(chatUserId, chatId);
removeTechnicalUserFromGroup(chatId);
leaveFromGroupAsTechnicalUser(chatId);

return true;
} catch (Exception exception) {
Expand All @@ -622,17 +656,6 @@ public boolean removeUserFromSession(String chatUserId, String chatId) {
}
}

/**
* Removes the technical user from the given Rocket.Chat group id.
*
* @param rcGroupId the rocket chat group id
*/
public void removeTechnicalUserFromGroup(String rcGroupId)
throws RocketChatRemoveUserFromGroupException, RocketChatUserNotInitializedException {
this.removeUserFromGroup(
rcCredentialHelper.getTechnicalUser().getRocketChatUserId(), rcGroupId);
}

/**
* Get all standard members (all users except system user and technical user) of a rocket chat
* group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import lombok.Getter;
import lombok.Setter;

/**
* Body object for Rocket.Chat API Call for adding a user to a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/invite/
*/
// Body object for Rocket.Chat API Call for adding a user to a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import lombok.Getter;
import lombok.Setter;

/**
* Body object for Rocket.Chat API Call for cleaning up a room (remove specific messages)
* https://rocket.chat/docs/developer-guides/rest-api/rooms/cleanhistory/
*/
// Body object for Rocket.Chat API Call for cleaning up a room (remove specific messages)
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Response object for Rocket.Chat API Call for getting the group counters
* https://rocket.chat/docs/developer-guides/rest-api/groups/counters/
*/
// Response object for Rocket.Chat API Call for getting the group counters
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import lombok.Getter;
import lombok.Setter;

/**
* Body object for Rocket.Chat API Call for creating a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/create/
*/
// Body object for Rocket.Chat API Call for creating a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/*
* Rocket.Chat group object
*/
// Rocket.Chat group object
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Body object for Rocket.Chat API Call for deleting a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/delete/
*/
// Body object for Rocket.Chat API Call for deleting a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Response object for Rocket.Chat API Call for deleting a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/delete/
*/
// Response object for Rocket.Chat API Call for deleting a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.caritas.cob.userservice.api.adapters.rocketchat.dto.group;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

// Body object for Rocket.Chat API Call for leaving a group
@Setter
@Getter
@AllArgsConstructor
public class GroupLeaveBodyDTO {
private String roomId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/** Rocket.Chat group member DTO */
// Rocket.Chat group member DTO
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Response object for Rocket.Chat API Call for getting the members of a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/members/
*/
// Response object for Rocket.Chat API Call for getting the members of a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import lombok.Getter;
import lombok.Setter;

/**
* Body object for Rocket.Chat API Call for removing a user to a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/kick/
*/
// Body object for Rocket.Chat API Call for removing a user to a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Response object for Rocket.Chat API Call for creating a group
* https://rocket.chat/docs/developer-guides/rest-api/groups/create/
*/
// Response object for Rocket.Chat API Call for creating a group
@Setter
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
public class UserUpdateDataDTO {

private String email;
private boolean verified;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ private UserDTO buildValidatedUserDTO(

private UserUpdateRequestDTO buildUserUpdateRequestDTO(
String rcUserId, UpdateAdminConsultantDTO updateConsultantDTO) {
UserUpdateDataDTO userUpdateDataDTO = new UserUpdateDataDTO(updateConsultantDTO.getEmail());
UserUpdateDataDTO userUpdateDataDTO =
new UserUpdateDataDTO(updateConsultantDTO.getEmail(), true);
return new UserUpdateRequestDTO(rcUserId, userUpdateDataDTO);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void removeConsultantsFromRocketChatGroup(
.map(Consultant::getRocketChatId)
.filter(groupMemberList::contains)
.forEach(rcUserId -> rocketChatFacade.removeUserFromGroup(rcUserId, rcGroupId));
rocketChatFacade.removeTechnicalUserFromGroup(rcGroupId);
rocketChatFacade.leaveFromGroupAsTechnicalUser(rcGroupId);
}

private List<String> obtainRocketChatGroupMemberIds(String groupId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.caritas.cob.userservice.api.exception.rocketchat;

public class RocketChatLeaveFromGroupException extends Exception {

private static final long serialVersionUID = 2106829666296656057L;

/**
* Exception, when a Rocket.Chat API call to leave a group fails
*
* @param ex
*/
public RocketChatLeaveFromGroupException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatAddUserToGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatGetGroupMembersException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLeaveFromGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveSystemMessagesException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveUserFromGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatUserNotInitializedException;
Expand All @@ -36,7 +37,7 @@ public void addUserToRocketChatGroup(String rcUserId, String groupId) {
try {
addTechnicalUserToGroup(groupId);
rocketChatService.addUserToGroup(rcUserId, groupId);
removeTechnicalUserFromGroup(groupId);
leaveFromGroupAsTechnicalUser(groupId);
} catch (RocketChatAddUserToGroupException addUserEx) {
var message =
String.format(
Expand All @@ -57,7 +58,7 @@ public void removeSystemMessagesFromRocketChatGroup(String groupId) {
addTechnicalUserToGroup(groupId);
rocketChatService.removeSystemMessages(
groupId, nowInUtc().minusHours(ONE_DAY_IN_HOURS), nowInUtc());
removeTechnicalUserFromGroup(groupId);
leaveFromGroupAsTechnicalUser(groupId);
} catch (RocketChatRemoveSystemMessagesException | RocketChatUserNotInitializedException e) {
var message =
String.format("Could not remove system messages from Rocket.Chat group id %s", groupId);
Expand All @@ -78,7 +79,7 @@ public List<GroupMemberDTO> retrieveRocketChatMembers(String rcGroupId) {
try {
addTechnicalUserToGroup(rcGroupId);
List<GroupMemberDTO> memberList = rocketChatService.getMembersOfGroup(rcGroupId);
removeTechnicalUserFromGroup(rcGroupId);
leaveFromGroupAsTechnicalUser(rcGroupId);
return memberList;
} catch (RocketChatGetGroupMembersException getGroupMembersEx) {
var message =
Expand Down Expand Up @@ -107,16 +108,16 @@ public void addTechnicalUserToGroup(String groupId) {
}

/**
* Removes the technical user from the given Rocket.Chat group id.
* Leaves group from the given Rocket.Chat group id as the technical user.
*
* @param groupId the rocket chat group id
*/
public void removeTechnicalUserFromGroup(String groupId) {
public void leaveFromGroupAsTechnicalUser(String groupId) {
try {
rocketChatService.removeTechnicalUserFromGroup(groupId);
} catch (RocketChatRemoveUserFromGroupException | RocketChatUserNotInitializedException e) {
rocketChatService.leaveFromGroupAsTechnicalUser(groupId);
} catch (RocketChatLeaveFromGroupException e) {
var message =
String.format("Could not remove technical user from Rocket.Chat group id %s", groupId);
String.format("Could not leave from Rocket.Chat group id %s as technical user", groupId);
throw new InternalServerErrorException(message, LogService::logInternalServerError);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void changeUserAccountEmailAddress(Optional<String> optionalEmail) {
}

private void updateConsultantEmail(Consultant consultant, String email) {
UserUpdateDataDTO userUpdateDataDTO = new UserUpdateDataDTO(email);
UserUpdateDataDTO userUpdateDataDTO = new UserUpdateDataDTO(email, true);
UserUpdateRequestDTO requestDTO =
new UserUpdateRequestDTO(consultant.getRocketChatId(), userUpdateDataDTO);
this.rocketChatService.updateUser(requestDTO);
Expand All @@ -119,7 +119,7 @@ private void updateConsultantEmail(Consultant consultant, String email) {
}

private void updateUserEmail(User user, String email) {
UserUpdateDataDTO userUpdateDataDTO = new UserUpdateDataDTO(email);
UserUpdateDataDTO userUpdateDataDTO = new UserUpdateDataDTO(email, true);
UserUpdateRequestDTO requestDTO =
new UserUpdateRequestDTO(user.getRcUserId(), userUpdateDataDTO);
this.rocketChatService.updateUser(requestDTO);
Expand Down
Loading

0 comments on commit cfe510e

Please sign in to comment.