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.apache.avalon.framework.context.Context; 022import org.apache.avalon.framework.context.ContextException; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.components.ContextHelper; 026import org.apache.cocoon.environment.Request; 027 028import org.ametys.cms.repository.comment.RichTextComment; 029import org.ametys.core.observation.Event; 030import org.ametys.core.user.UserIdentity; 031import org.ametys.plugins.workspaces.ObservationConstants; 032import org.ametys.plugins.workspaces.forum.ForumWorkspaceModule; 033import org.ametys.plugins.workspaces.forum.Thread; 034import org.ametys.plugins.workspaces.forum.json.ThreadJSONHelper; 035import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint; 036import org.ametys.plugins.workspaces.project.objects.Project; 037import org.ametys.runtime.i18n.I18nizableText; 038import org.ametys.web.WebHelper; 039 040/** 041 * Observer to send mails to mentioned users in contributor comments of {@link Thread} 042 */ 043public class NotifyThreadCommentMentionsObserver extends AbstractNotifyWorkspacesCommentMentionsObserver<Thread> 044{ 045 046 /** The forum module */ 047 protected ForumWorkspaceModule _forumModule; 048 049 /** The project tags DAO */ 050 protected ThreadJSONHelper _threadJSONHelper; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 super.service(manager); 056 WorkspaceModuleExtensionPoint moduleManagerEP = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE); 057 _forumModule = moduleManagerEP.getModule(ForumWorkspaceModule.FORUM_MODULE_ID); 058 _threadJSONHelper = (ThreadJSONHelper) manager.lookup(ThreadJSONHelper.ROLE); 059 } 060 061 @Override 062 public void contextualize(Context context) throws ContextException 063 { 064 _context = context; 065 } 066 067 public boolean supports(Event event) 068 { 069 return event.getId().equals(ObservationConstants.EVENT_THREAD_COMMENTED); 070 } 071 072 @Override 073 protected String _getAmetysObjectTitle(Thread thread) 074 { 075 return thread.getTitle(); 076 } 077 078 @Override 079 protected I18nizableText _getMailMessage(MentionableObject mentionableObject) 080 { 081 List<String> i18nParams = _getCommonMailI18nParameters(mentionableObject); 082 return new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_THREAD_COMMENT_MENTION_MAIL_MESSAGE", i18nParams); 083 } 084 085 private String _getContent(RichTextComment comment, Project project) throws Exception 086 { 087 Request request = ContextHelper.getRequest(_context); 088 String siteName = WebHelper.getSiteName(request); 089 if (siteName == null) 090 { 091 request.setAttribute("site", project.getSite().getName()); 092 } 093 return _threadJSONHelper.richTextToRendering(comment.getRichTextContent()); 094 } 095 096 @Override 097 protected String _transformSyntaxTextToReadableTextWithColors(String content, UserIdentity recipient) 098 { 099 return _mentionUtils.transformRichTextToReadableText(content, recipient); 100 } 101 102 @Override 103 protected MentionableObject _getMentionableObjectFromArguments(Map<String, Object> arguments) throws Exception 104 { 105 Thread thread = _getAmetysObjectFromArguments(arguments); 106 RichTextComment comment = (RichTextComment) arguments.get(ObservationConstants.ARGS_THREAD_COMMENT); 107 comment = thread.getComment(comment.getId()); 108 Project project = _projectManager.getParentProject(thread); 109 String url = _getAbsoluteUrl(thread, project); 110 String content = _getContent(comment, project); 111 112 return new MentionableObject( 113 _userManager.getUser(comment.getAuthor()), 114 content, 115 _mentionUtils.extractMentionedUsersFromRichText(content), 116 comment.getCreationDate(), 117 thread, 118 new LinkToAmetysObject(url, new I18nizableText("plugin.workspaces", "PROJECT_MAIL_NOTIFICATION_BODY_FORUMTHREAD_BUTTON_TEXT")), 119 _getLanguage(project) 120 ); 121 } 122 123 @Override 124 protected I18nizableText _getMailMessageType() 125 { 126 return new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_THREAD_MENTION_MAIL_THREAD_COMMENT_TITLE"); 127 } 128 129 @Override 130 protected String _getModuleId() 131 { 132 return ForumWorkspaceModule.FORUM_MODULE_ID; 133 } 134 135 @Override 136 protected String getUrl(Thread thread, Project project) 137 { 138 return _forumModule.getThreadUri(project, thread.getId()); 139 } 140}