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;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.contenttype.ContentTypesHelper;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.plugins.workspaces.WorkspacesConstants;
032import org.ametys.runtime.config.Config;
033
034/**
035 * When a member content is validated, the chat server need to be updated (email, name, avatar...)
036 */
037public class MemberValidatedObserver extends AbstractLogEnabled implements Observer, Serviceable // Needs to be synchronous otherwise, there is no current site when updating avatar url that will lead to cocoon://null/_contents url
038{
039    /** Rocket.Chat helper */
040    protected ChatHelper _chatHelper;
041    
042    private ContentTypesHelper _contentTypesHelper;
043
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _chatHelper = (ChatHelper) manager.lookup(ChatHelper.ROLE);
047        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
048    }
049
050    public boolean supports(Event event)
051    {
052        if (!ObservationConstants.EVENT_CONTENT_VALIDATED.equals(event.getId()) || !Config.getInstance().<Boolean>getValue("workspaces.chat.active"))
053        {
054            return false;
055        }
056        
057        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
058        return _contentTypesHelper.isInstanceOf(content, WorkspacesConstants.MEMBER_CONTENT_TYPE_ID);
059    }
060
061    public int getPriority(Event event)
062    {
063        return MAX_PRIORITY; // Yes, "MAX PRIORITY" is the lowest priority...
064    }
065
066    public void observe(Event event, Map<String, Object> transientVars) throws Exception
067    {
068        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
069        
070        UserIdentity user = content.getValue("user");
071        if (user != null)
072        {
073            try 
074            {
075                _chatHelper.updateUserInfos(user, false);
076            }
077            catch (Exception e)
078            {
079                // Only debug since user may not exist due to inconsistency between servers
080                getLogger().debug("An error occurred while update userinfo on chat server for " + UserIdentity.userIdentityToString(user), e);
081            }
082        }
083    }
084}