001/*
002 *  Copyright 2024 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.comments;
017
018import java.util.List;
019import java.util.Map;
020
021import org.ametys.cms.repository.comment.Comment;
022import org.ametys.cms.repository.mentions.MentionUtils;
023import org.ametys.core.observation.Event;
024import org.ametys.core.user.User;
025import org.ametys.plugins.workspaces.ObservationConstants;
026import org.ametys.plugins.workspaces.documents.DocumentWorkspaceModule;
027import org.ametys.plugins.workspaces.documents.jcr.File;
028import org.ametys.plugins.workspaces.project.objects.Project;
029import org.ametys.runtime.i18n.I18nizableText;
030/**
031 * Observer to send mails to mentioned users in contributor comments of {@link File}
032 */
033public class NotifyFileCommentMentionsObserver extends AbstractNotifyWorkspacesCommentMentionsObserver<File>
034{
035    @Override
036    public boolean supports(Event event)
037    {
038        return event.getId().equals(ObservationConstants.EVENT_RESOURCE_COMMENTED);
039    }
040    
041    @Override
042    protected MentionableObject _getMentionableObjectFromArguments(Map<String, Object> arguments) throws Exception
043    {
044        File file = _getAmetysObjectFromArguments(arguments);
045        String commentId = (String) arguments.get(ObservationConstants.ARGS_FILE_COMMENT_ID);
046        Comment comment = file.getComment(commentId);
047        Project project = _projectManager.getParentProject(file);
048        String url = _getAbsoluteUrl(file, project);
049        
050        return new MentionableObject(
051            _userManager.getUser(comment.getAuthor()),
052            comment.getContent(),
053            MentionUtils.extractMentionedUsersFromSimpleText(comment.getContent()),
054            comment.getCreationDate(),
055            file,
056            new LinkToAmetysObject(url, new I18nizableText("plugin.workspaces", "PROJECT_MAIL_NOTIFICATION_BODY_DEFAULT_BUTTON_TEXT")),
057            _getLanguage(project)
058        );
059    }
060
061    @Override
062    protected String _getAmetysObjectTitle(File file)
063    {
064        return file.getName();
065    }
066    
067    @Override
068    protected I18nizableText _getMailMessage(MentionableObject mentionableObject)
069    {
070        File file = (File) mentionableObject.ametysObject();
071        Project project = _projectManager.getParentProject(file);
072        User author = mentionableObject.author();
073        List<String> i18nParams = List.of(author.getFullName(), _getAmetysObjectTitle(file), project.getTitle());
074        return new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_RESOURCE_MENTION_MAIL_MESSAGE", i18nParams);
075    }
076
077    @Override
078    protected String _getModuleId()
079    {
080        return DocumentWorkspaceModule.DOCUMENT_MODULE_ID;
081    }
082
083    @Override
084    protected String getUrl(File ametysObject, Project project)
085    {
086        return _documentModule.getModuleUrl(project);
087    }
088}