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.project.objects.Project;
027import org.ametys.plugins.workspaces.tasks.Task;
028import org.ametys.plugins.workspaces.tasks.TasksWorkspaceModule;
029import org.ametys.runtime.i18n.I18nizableText;
030/**
031 * Observer to send mails to mentioned users in contributor comments of {@link Task}
032 */
033public class NotifyTaskCommentMentionsObserver extends AbstractNotifyWorkspacesCommentMentionsObserver<Task>
034{
035    @Override
036    public boolean supports(Event event)
037    {
038        return event.getId().equals(ObservationConstants.EVENT_TASK_COMMENTED);
039    }
040    
041    @Override
042    protected String _getAmetysObjectTitle(Task task)
043    {
044        return task.getLabel();
045    }
046
047    @Override
048    protected MentionableObject _getMentionableObjectFromArguments(Map<String, Object> arguments) throws Exception
049    {
050        Task task = _getAmetysObjectFromArguments(arguments);
051        String commentId = (String) arguments.get(ObservationConstants.ARGS_TASK_COMMENT_ID);
052        Comment comment = task.getComment(commentId);
053        Project project = _projectManager.getParentProject(task);
054        
055        String url = _getAbsoluteUrl(task, project);
056
057        return new MentionableObject(
058            _userManager.getUser(comment.getAuthor()),
059            comment.getContent(),
060            MentionUtils.extractMentionedUsersFromSimpleText(comment.getContent()),
061            comment.getCreationDate(),
062            task,
063            new LinkToAmetysObject(url, new I18nizableText("plugin.workspaces", "PROJECT_MAIL_NOTIFICATION_BODY_TASK_BUTTON_TEXT")),
064            _getLanguage(project)
065        );
066    }
067
068    @Override
069    protected I18nizableText _getMailMessage(MentionableObject mentionableObject)
070    {
071        Task task = (Task) mentionableObject.ametysObject();
072        User author = mentionableObject.author();
073        Project project = _projectManager.getParentProject(task);
074        List<String> i18nParams = List.of(author.getFullName(), _getAmetysObjectTitle(task), project.getTitle());
075        return new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_TASK_MENTION_MAIL_MESSAGE", i18nParams);
076    }
077
078    @Override
079    protected String _getModuleId()
080    {
081        return TasksWorkspaceModule.TASK_MODULE_ID;
082    }
083
084    @Override
085    protected String getUrl(Task task, Project project)
086    {
087        return _taskModule.getTaskUri(project, task.getId());
088    }
089}