001/* 002 * Copyright 2022 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.workspaces.chat; 017 018import java.io.IOException; 019import java.util.Map; 020import java.util.Set; 021import java.util.stream.Collectors; 022 023import org.apache.avalon.framework.logger.AbstractLogEnabled; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.core.group.Group; 029import org.ametys.core.group.GroupIdentity; 030import org.ametys.core.group.GroupManager; 031import org.ametys.core.observation.Event; 032import org.ametys.core.observation.Observer; 033import org.ametys.core.user.UserIdentity; 034import org.ametys.plugins.workspaces.ObservationConstants; 035import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType; 036import org.ametys.plugins.workspaces.members.ProjectMemberManager; 037import org.ametys.plugins.workspaces.members.ProjectMemberManager.ProjectMember; 038import org.ametys.plugins.workspaces.project.objects.Project; 039import org.ametys.runtime.config.Config; 040 041/** 042 * Kick a Chat user from a room when it is removed from a project 043 */ 044public class MemberRemovedObserver extends AbstractLogEnabled implements Observer, Serviceable 045{ 046 /** Chat helper */ 047 protected ChatHelper _chatHelper; 048 private GroupManager _groupManager; 049 private ProjectMemberManager _projectMemberManager; 050 051 public void service(ServiceManager manager) throws ServiceException 052 { 053 _chatHelper = (ChatHelper) manager.lookup(ChatHelper.ROLE); 054 _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE); 055 _projectMemberManager = (ProjectMemberManager) manager.lookup(ProjectMemberManager.ROLE); 056 } 057 058 public boolean supports(Event event) 059 { 060 return ObservationConstants.EVENT_MEMBER_DELETED.equals(event.getId()) && Config.getInstance().<Boolean>getValue("workspaces.chat.active"); 061 } 062 063 public int getPriority(Event event) 064 { 065 return MAX_PRIORITY; // Yes, "MAX PRIORITY" is the lowest priority... 066 } 067 068 public void observe(Event event, Map<String, Object> transientVars) throws Exception 069 { 070 Project project = (Project) event.getArguments().get(ObservationConstants.ARGS_PROJECT); 071 072 String memberIdentity = (String) event.getArguments().get(ObservationConstants.ARGS_MEMBER_IDENTITY); 073 MemberType memberType = (MemberType) event.getArguments().get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE); 074 075 Set<UserIdentity> users; 076 077 if (memberType == MemberType.USER) 078 { 079 users = Set.of(UserIdentity.stringToUserIdentity(memberIdentity)); 080 } 081 else 082 { 083 GroupIdentity groupIdentity = GroupIdentity.stringToGroupIdentity(memberIdentity); 084 Group group = _groupManager.getGroup(groupIdentity); 085 users = group.getUsers(); 086 } 087 088 Set<ProjectMember> projectMembers = _projectMemberManager.getProjectMembers(project, true); 089 Set<UserIdentity> stillMembers = projectMembers.stream().map(m -> m.getUser().getIdentity()).collect(Collectors.toSet()); 090 091 for (UserIdentity user : users) 092 { 093 // A user can be still member via another group or directly... 094 if (!stillMembers.contains(user)) 095 { 096 try 097 { 098 _chatHelper.removeUserFromRoom(user, project.getName()); 099 } 100 catch (IOException e) 101 { 102 getLogger().error("An error occurred that prevented the user " + UserIdentity.userIdentityToString(user) + " to be removed from the room " + project.getName(), e); 103 } 104 } 105 } 106 } 107 108}