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.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.core.group.Group;
027import org.ametys.core.group.GroupIdentity;
028import org.ametys.core.group.GroupManager;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.workspaces.ObservationConstants;
033import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
034import org.ametys.plugins.workspaces.project.objects.Project;
035import org.ametys.runtime.config.Config;
036
037/**
038 * Invite a Chat user in a room when added in a project
039 * (And create the user if needed)
040 */
041public class MemberAddedObserver extends AbstractLogEnabled implements Observer, Serviceable // Needs to be synchronous otherwise, group.getUsers won't work
042{
043    /** Chat helper */
044    protected ChatHelper _chatHelper;
045    
046    private GroupManager _groupManager;
047
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _chatHelper = (ChatHelper) manager.lookup(ChatHelper.ROLE);
051        _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
052    }
053
054    public boolean supports(Event event)
055    {
056        return ObservationConstants.EVENT_MEMBER_ADDED.equals(event.getId()) && Config.getInstance().<Boolean>getValue("workspaces.chat.active");
057    }
058
059    public int getPriority(Event event)
060    {
061        return MAX_PRIORITY; // Yes, "MAX PRIORITY" is the lowest priority...
062    }
063
064    public void observe(Event event, Map<String, Object> transientVars) throws Exception
065    {
066        Project project = (Project) event.getArguments().get(ObservationConstants.ARGS_PROJECT);
067        
068        String memberIdentity = (String) event.getArguments().get(ObservationConstants.ARGS_MEMBER_IDENTITY);
069        MemberType memberType = (MemberType) event.getArguments().get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE);
070
071        Set<UserIdentity> users;
072        
073        if (memberType == MemberType.USER)
074        {
075            users = Set.of(UserIdentity.stringToUserIdentity(memberIdentity));
076        }
077        else // Group
078        {
079            GroupIdentity groupIdentity = GroupIdentity.stringToGroupIdentity(memberIdentity);
080            Group group = _groupManager.getGroup(groupIdentity);
081            users = group.getUsers();
082        }
083        
084        for (UserIdentity user : users)
085        {
086            try
087            {
088                if (_chatHelper.getUser(user, true) != null)
089                {
090                    _chatHelper.addUserToRoom(user, project.getName());
091                }
092            }
093            catch (Exception e)
094            {
095                getLogger().error("An error occurred that prevented the user " + UserIdentity.userIdentityToString(user) + " to be part of the room " + project.getName(), e);
096            }
097        }
098    }
099
100}