diff --git a/services/src/main/java/io/meeds/chat/listeners/MatrixSpaceListener.java b/services/src/main/java/io/meeds/chat/listeners/MatrixSpaceListener.java index 40d3b095..e6fce00b 100644 --- a/services/src/main/java/io/meeds/chat/listeners/MatrixSpaceListener.java +++ b/services/src/main/java/io/meeds/chat/listeners/MatrixSpaceListener.java @@ -90,6 +90,9 @@ public void spaceCreated(SpaceLifeCycleEvent event) { } } } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } LOG.error("Matrix integration: Could not create a room for space {}", space.getDisplayName(), e); } } @@ -106,6 +109,9 @@ public void spaceRenamed(SpaceLifeCycleEvent event) { try { matrixService.renameRoom(room.getRoomId(), spaceDisplayName); } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } LOG.error("Could not rename the room linked to the space {}", space.getDisplayName(), e); } } @@ -121,23 +127,26 @@ public void joined(SpaceLifeCycleEvent event) { String restrictedGroupOfUsers = PropertyManager.getProperty(MATRIX_RESTRICTED_USERS_GROUP); String matrixUserAdmin = PropertyManager.getProperty(MATRIX_ADMIN_USERNAME); String matrixIdOfUser = matrixService.getMatrixIdForUser(userId); - if (StringUtils.isBlank(matrixIdOfUser) && StringUtils.isNotBlank(restrictedGroupOfUsers) - && restrictedGroupOfUsers.equals(space.getGroupId()) && !userId.equals(matrixUserAdmin)) { - Identity user; - try { + try { + if (StringUtils.isBlank(matrixIdOfUser) + && (StringUtils.isBlank(restrictedGroupOfUsers) || (StringUtils.isNotBlank(restrictedGroupOfUsers) + && this.matrixService.isUserMemberOfGroup(userId, restrictedGroupOfUsers))) + && !userId.equals(matrixUserAdmin)) { + Identity user; user = identityManager.getOrCreateUserIdentity(userId); matrixIdOfUser = matrixService.saveUserAccount(user, true); - } catch (Exception e) { - LOG.error("Could not retrieve the user {}", userId, e); } - } - Room room = matrixService.getRoomBySpace(space); - if (room != null && StringUtils.isNotBlank(room.getRoomId()) && StringUtils.isNotBlank(matrixIdOfUser)) { - try { + + Room room = matrixService.getRoomBySpace(space); + if (room != null && StringUtils.isNotBlank(room.getRoomId()) && StringUtils.isNotBlank(matrixIdOfUser)) { matrixService.joinUserToRoom(room.getRoomId(), matrixIdOfUser); - } catch (Exception e) { - LOG.error("Could not join the user {} to the room of the space {} on Matrix", userId, space.getDisplayName(), e); + + } + } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); } + LOG.error("Could not join the user {} to the room of the space {} on Matrix", userId, space.getDisplayName(), e); } } @@ -156,6 +165,9 @@ public void left(SpaceLifeCycleEvent event) { matrixIdOfUser, MESSAGE_USER_KICKED_SPACE.formatted(space.getDisplayName())); } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } LOG.error("Could not kick the user {] from the room of the space {}", userId, space.getDisplayName(), e); } } @@ -187,6 +199,14 @@ public void revokedLead(SpaceLifeCycleEvent event) { } } + /** + * Updates the user role based on his role in the space + * + * @param space the space + * @param matrixIdOfUser the matrix ID of the user + * @param userRole the user role "0" for simple user, "50" for the manager + * @return true if the operation is successful + */ private boolean updateMemberRoleInSpace(Space space, String matrixIdOfUser, String userRole) { Room room = matrixService.getRoomBySpace(space); if (room != null && StringUtils.isNotBlank(room.getRoomId())) { @@ -211,6 +231,9 @@ private boolean updateMemberRoleInSpace(Space space, String matrixIdOfUser, Stri } return matrixService.updateRoomSettings(room.getRoomId(), matrixRoomPermissions); } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } LOG.error("Could not update member roles in the space {}", space.getDisplayName(), e); } } @@ -246,6 +269,9 @@ public void spaceDescriptionEdited(SpaceLifeCycleEvent event) { matrixService.updateRoomDescription(room.getRoomId(), space.getDescription()); } } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } LOG.error("Could not save the description of space {} ", space.getDisplayName(), e); } } @@ -261,6 +287,9 @@ public void spaceRemoved(SpaceLifeCycleEvent event) { try { matrixService.deleteRoom(room.getRoomId()); } catch (Exception e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } LOG.error("Could not delete the room {} linked to the space {}", room.getRoomId(), space.getDisplayName()); } } diff --git a/services/src/test/java/io/meeds/chat/listeners/MatrixListenerTest.java b/services/src/test/java/io/meeds/chat/listeners/MatrixListenerTest.java index 19bc94b7..e155770e 100644 --- a/services/src/test/java/io/meeds/chat/listeners/MatrixListenerTest.java +++ b/services/src/test/java/io/meeds/chat/listeners/MatrixListenerTest.java @@ -6,12 +6,14 @@ import org.exoplatform.services.organization.User; import org.exoplatform.services.security.ConversationState; import org.exoplatform.services.security.Identity; +import org.exoplatform.social.core.identity.model.Profile; import org.exoplatform.social.core.manager.IdentityManager; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import static io.meeds.chat.service.utils.MatrixConstants.USER_MATRIX_ID; import static org.mockito.ArgumentMatchers.*; @SpringJUnitConfig(MatrixBaseTest.class) @@ -23,7 +25,7 @@ class MatrixListenerTest extends MatrixBaseTest { ListenerService listenerService; @Autowired - IdentityManager identityManager; + IdentityManager identityManager; @Test void testUserListener() throws Exception { @@ -38,16 +40,20 @@ void testUserListener() throws Exception { // Check enabling user organizationService.getUserHandler().setEnabled("raul", true, true); Mockito.verify(matrixHttpClient, Mockito.times(2)) - .saveUserAccount(any(), anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean()); + .saveUserAccount(any(), anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean()); } @Test void testUserLoginListener() { Identity identity = new Identity("ghost"); ConversationState state = new ConversationState(identity); + org.exoplatform.social.core.identity.model.Identity ghostIdentity = identityManager.getOrCreateUserIdentity("ghost"); + Profile profile = ghostIdentity.getProfile(); + profile.setProperty(USER_MATRIX_ID, ""); + identityManager.updateProfile(profile); listenerService.broadcast("exo.core.security.ConversationRegistry.register", this, state); Mockito.verify(matrixHttpClient, Mockito.times(1)) - .saveUserAccount(any(), anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean()); + .saveUserAccount(any(), anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean()); } } diff --git a/services/src/test/java/io/meeds/chat/service/MatrixServiceTest.java b/services/src/test/java/io/meeds/chat/service/MatrixServiceTest.java index 2be4b027..581b1cca 100644 --- a/services/src/test/java/io/meeds/chat/service/MatrixServiceTest.java +++ b/services/src/test/java/io/meeds/chat/service/MatrixServiceTest.java @@ -20,6 +20,7 @@ import io.meeds.chat.MatrixBaseTest; import io.meeds.chat.entity.RoomStatus; +import io.meeds.chat.model.MatrixRoomPermissions; import io.meeds.chat.model.Room; import io.meeds.chat.rest.model.LastMessage; import io.meeds.chat.rest.model.RoomEntity; @@ -31,6 +32,7 @@ import org.exoplatform.social.core.model.AvatarAttachment; import org.exoplatform.social.core.space.model.Space; import org.exoplatform.ws.frameworks.json.impl.JsonException; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -294,13 +296,49 @@ void invalidateAccessToken() throws IOException, InterruptedException { assertFalse(result); } - @org.junit.Test - public void testCleanMatrixUsername() { + @Test + void testCleanMatrixUsername() { String[] usernames = new String[] { "Samueâl", "fre@d", "Shazia", "gorkef/", "²&é\"'(-è_çà)=²1234567890°+'azertyuiopqsdfghjklmù*^$wxcvbn,;:!?./§%µ¨£<>²&~#{[|`\\^@]}" }; for (String username : usernames) { String result = matrixService.cleanMatrixUsername(username); - assertNotNull(result); + Assertions.assertNotNull(result); } } + + @Test + void testLeftSpace() { + Space space = getSpaceInstance(1); + spaceService.removeMember(space, "dragon"); + verify(matrixHttpClient, times(1)).kickUserFromRoom(anyString(), anyString(), anyString(), anyString()); + } + + @Test + void testRenameSpace() { + Space space = getSpaceInstance(1); + spaceService.renameSpace(space, "New Space Name"); + verify(matrixHttpClient, times(1)).renameRoom(anyString(), anyString(), anyString()); + } + + @Test + void testPromoteAndRevokeLead() throws JsonException, IOException, InterruptedException { + Space space = getSpaceInstance(1); + spaceService.setManager(space, "dragon", true); + // function already + verify(matrixHttpClient, times(4)).updateRoomSettings(anyString(), any(MatrixRoomPermissions.class), anyString()); + + //revoke lead + spaceService.setManager(space, "dragon", false); + + verify(matrixHttpClient, times(5)).updateRoomSettings(anyString(), any(MatrixRoomPermissions.class), anyString()); + } + + @Test + void testSpaceDescriptionEdited() { + Space space = getSpaceInstance(1); + space.setDescription("New space description"); + spaceService.updateSpace(space); + verify(matrixHttpClient, times(1)).updateRoomDescription(anyString(), anyString(), anyString()); + } + }