001/*
002 *  Copyright 2018 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.wall;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025import java.util.concurrent.ExecutionException;
026import java.util.stream.Collectors;
027
028import javax.jcr.RepositoryException;
029
030import org.apache.avalon.framework.component.Component;
031import org.apache.avalon.framework.service.ServiceException;
032import org.apache.avalon.framework.service.ServiceManager;
033import org.apache.avalon.framework.service.Serviceable;
034import org.apache.cocoon.servlet.multipart.Part;
035import org.apache.commons.lang3.StringUtils;
036import org.apache.commons.lang3.tuple.Pair;
037import org.apache.excalibur.xml.sax.SAXParser;
038import org.apache.solr.client.solrj.SolrServerException;
039import org.xml.sax.InputSource;
040
041import org.ametys.cms.ObservationConstants;
042import org.ametys.cms.content.RichTextHandler;
043import org.ametys.cms.content.indexing.solr.SolrIndexer;
044import org.ametys.cms.contenttype.ContentType;
045import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
046import org.ametys.cms.data.Binary;
047import org.ametys.cms.data.RichText;
048import org.ametys.cms.data.type.ResourceElementTypeHelper;
049import org.ametys.cms.indexing.IndexingObserver;
050import org.ametys.cms.repository.Content;
051import org.ametys.cms.repository.ContentDAO;
052import org.ametys.cms.repository.ContentDAO.TagMode;
053import org.ametys.cms.repository.comment.ui.CommentsAndReportsTreeComponent;
054import org.ametys.cms.rights.ContentRightAssignmentContext;
055import org.ametys.core.observation.Event;
056import org.ametys.core.observation.ObservationManager;
057import org.ametys.core.observation.ObservationManager.ObserverFuture;
058import org.ametys.core.right.RightManager;
059import org.ametys.core.ui.Callable;
060import org.ametys.core.ui.mail.StandardMailBodyHelper;
061import org.ametys.core.ui.mail.StandardMailBodyHelper.MailBodyBuilder;
062import org.ametys.core.upload.Upload;
063import org.ametys.core.upload.UploadManager;
064import org.ametys.core.user.CurrentUserProvider;
065import org.ametys.core.user.User;
066import org.ametys.core.user.UserIdentity;
067import org.ametys.core.user.UserManager;
068import org.ametys.core.util.I18nUtils;
069import org.ametys.core.util.language.UserLanguagesManager;
070import org.ametys.core.util.mail.SendMailHelper;
071import org.ametys.plugins.explorer.resources.ResourceCollection;
072import org.ametys.plugins.repository.AmetysObjectResolver;
073import org.ametys.plugins.repository.AmetysRepositoryException;
074import org.ametys.plugins.workspaces.WorkspacesConstants;
075import org.ametys.plugins.workspaces.WorkspacesHelper;
076import org.ametys.plugins.workspaces.project.ProjectManager;
077import org.ametys.plugins.workspaces.project.modules.WorkspaceModule;
078import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
079import org.ametys.plugins.workspaces.project.objects.Project;
080import org.ametys.plugins.workspaces.project.rights.ProjectRightHelper;
081import org.ametys.runtime.authentication.AccessDeniedException;
082import org.ametys.runtime.config.Config;
083import org.ametys.runtime.i18n.I18nizableText;
084import org.ametys.runtime.i18n.I18nizableTextParameter;
085import org.ametys.runtime.plugin.component.AbstractLogEnabled;
086import org.ametys.web.content.FOContentCreationHelper;
087import org.ametys.web.repository.site.Site;
088import org.ametys.web.repository.site.SiteManager;
089
090import com.opensymphony.workflow.WorkflowException;
091
092import jakarta.mail.MessagingException;
093
094/**
095 * Helper for wall contents
096 *
097 */
098public class WallContentManager extends AbstractLogEnabled implements Component, Serviceable
099{
100    /** The Avalon role */
101    public static final String ROLE = WallContentManager.class.getName();
102    /** The tag for pin */
103    public static final String WALL_CONTENT_PIN_TAG = "WORKSPACES_CONTENT_PINNED";
104    /** Wall content creation right id */
105    public static final String PUBLISH_WALL_CONTENT_RIGHT_ID = "Plugins_Workspaces_Right_WallContent_Create";
106    /** Wall content pin right id */
107    public static final String PIN_WALL_CONTENT_RIGHT_ID = "Plugins_Workspaces_Right_Pin_WallContent";
108    /** Wall content report right id */
109    public static final String REPORT_WALL_CONTENT_RIGHT_ID = "Plugins_Workspaces_Right_Report_WallContent";
110
111    private static final int __INITIAL_WORKFLOW_ACTION_ID = 1111;
112    private static final String __WORKFLOW_NAME = "wall-content";
113    
114    private FOContentCreationHelper _foContentHelper;
115    private ContentTypeExtensionPoint _cTypeEP;
116    private I18nUtils _i18nUtils;
117    private ContentDAO _contentDAO;
118    private ObservationManager _observationManager;
119    private RightManager _rightManager;
120    private AmetysObjectResolver _resolver;
121    private UserManager _userManager;
122    private CurrentUserProvider _currentUserProvider;
123    private ProjectManager _projectManager;
124    private SiteManager _siteManager;
125    private ServiceManager _smanager;
126    private UploadManager _uploadManager;
127    private SolrIndexer _solrIndexer;
128    private WorkspaceModuleExtensionPoint _moduleEP;
129    private CommentsAndReportsTreeComponent _commentAndReportCmp;
130    private ProjectRightHelper _projectRightHelper;
131    private UserLanguagesManager _userLanguagesManager;
132    private WorkspacesHelper _workspaceHelper;
133    
134    public void service(ServiceManager manager) throws ServiceException
135    {
136        _smanager = manager;
137        _foContentHelper = (FOContentCreationHelper) manager.lookup(FOContentCreationHelper.ROLE);
138        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
139        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
140        _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE);
141        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
142        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
143        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
144        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
145        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
146        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
147        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
148        _uploadManager = (UploadManager) manager.lookup(UploadManager.ROLE);
149        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
150        _moduleEP = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE);
151        _commentAndReportCmp = (CommentsAndReportsTreeComponent) manager.lookup(CommentsAndReportsTreeComponent.ROLE);
152        _projectRightHelper = (ProjectRightHelper) manager.lookup(ProjectRightHelper.ROLE);
153        _userLanguagesManager = (UserLanguagesManager) manager.lookup(UserLanguagesManager.ROLE);
154        _workspaceHelper = (WorkspacesHelper) manager.lookup(WorkspacesHelper.ROLE);
155    }
156    
157    /**
158     * Create and publish a new wall content
159     * @param text the text
160     * @param part the file part for illustration. Can be null.
161     * @param siteName the site name
162     * @param lang the language
163     * @return the results
164     */
165    @Callable(rights = Callable.CHECKED_BY_IMPLEMENTATION)
166    public Map<String, Object> publishContent(String text, Part part, String siteName, String lang)
167    {
168        Map<String, Object> results = new HashMap<>();
169
170        try
171        {
172            if (!_projectRightHelper.hasRightOnModule(PUBLISH_WALL_CONTENT_RIGHT_ID, WallContentModule.WALLCONTENT_MODULE_ID))
173            {
174                throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to publish wall content on non authorized project");
175            }
176            
177            ContentType cType = _cTypeEP.getExtension(WorkspacesConstants.WALL_CONTENT_CONTENT_TYPE_ID);
178            
179            String contentTitle = _i18nUtils.translate(cType.getDefaultTitle(), lang);
180            
181            Map<String, Object> userValues = new HashMap<>();
182            userValues.put(Content.ATTRIBUTE_TITLE, contentTitle);
183            userValues.put("content", text);
184            userValues.put("comment", true); // active comments
185            
186            if (part != null)
187            {
188                try (InputStream is = part.getInputStream())
189                {
190                    Upload upload = _uploadManager.storeUpload(_currentUserProvider.getUser(), part.getFileName(), is);
191                    Binary fileValue = ResourceElementTypeHelper.binaryFromUpload(upload);
192                    
193                    userValues.put("illustration", Map.of("image", fileValue));
194                }
195                catch (IOException e)
196                {
197                    getLogger().error("Failed to store uploaded wall content illustration", e);
198                }
199            }
200            
201            try
202            {
203                results = _foContentHelper.createAndEditContent(__INITIAL_WORKFLOW_ACTION_ID, WorkspacesConstants.WALL_CONTENT_CONTENT_TYPE_ID, siteName, contentTitle, contentTitle, lang, userValues, __WORKFLOW_NAME, null);
204                
205                Content content = (Content) results.get(Content.class.getName());
206                _notifyContentCreation(content);
207                
208                // remove Content from result
209                results.remove(Content.class.getName());
210                
211                results.put("success", true);
212            }
213            finally
214            {
215                _commitAllChanges();
216            }
217        }
218        catch (AmetysRepositoryException | WorkflowException e)
219        {
220            results.put("success", false);
221            getLogger().error("Failed to create wall content for site {} and language {}", siteName, lang, e);
222        }
223        return results;
224    }
225    
226    /**
227     * Commit all changes in solr
228     */
229    protected void _commitAllChanges()
230    {
231        // Before trying to commit, be sure all the async observers of the current request are finished
232        List<ObserverFuture> futuresForRequest = _observationManager.getFuturesForRequest();
233        for (ObserverFuture observerFuture : futuresForRequest)
234        {
235            if (observerFuture.traits().contains(IndexingObserver.INDEXING_OBSERVER))
236            {
237                try
238                {
239                    observerFuture.future().get();
240                }
241                catch (ExecutionException | InterruptedException e)
242                {
243                    getLogger().info("An exception occured when calling #get() on Future result of an observer." , e);
244                }
245            }
246        }
247        
248        // Force commit all uncommited changes
249        try
250        {
251            _solrIndexer.commit();
252        }
253        catch (IOException | SolrServerException e)
254        {
255            getLogger().error("Impossible to commit changes", e);
256        }
257    }
258    
259    private void _notifyContentCreation(Content content)
260    {
261        Map<String, Object> eventParams = new HashMap<>();
262        eventParams.put(ObservationConstants.ARGS_CONTENT, content);
263        eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId());
264        
265        _observationManager.notify(new Event(org.ametys.plugins.workspaces.ObservationConstants.EVENT_WALLCONTENT_ADDED, content.getCreator(), eventParams));
266    }
267    
268    /**
269     * Pin a wall content
270     * @param contentId the content id
271     * @param contextualParameters the contextual parameters
272     * @return the result
273     */
274    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
275    public Map<String, Object> pinContent(String contentId, Map<String, Object> contextualParameters)
276    {
277        if (!_projectRightHelper.hasRightOnModule(PIN_WALL_CONTENT_RIGHT_ID, WallContentModule.WALLCONTENT_MODULE_ID))
278        {
279            throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to pin content '" + contentId + "' without convenient right.");
280        }
281        
282        return _pinOrUnpinContent(contentId, contextualParameters, TagMode.INSERT);
283    }
284    
285    /**
286     * Unpin a wall content
287     * @param contentId the content id
288     * @param contextualParameters the contextual parameters
289     * @return the result
290     */
291    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
292    public Map<String, Object> unpinContent(String contentId, Map<String, Object> contextualParameters)
293    {
294        if (!_projectRightHelper.hasRightOnModule(PIN_WALL_CONTENT_RIGHT_ID, WallContentModule.WALLCONTENT_MODULE_ID))
295        {
296            throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to unpin content '" + contentId + "' without convenient right.");
297        }
298        
299        return _pinOrUnpinContent(contentId, contextualParameters, TagMode.REMOVE);
300    }
301    
302    private Map<String, Object> _pinOrUnpinContent(String contentId,  Map<String, Object> contextualParameters, TagMode mode)
303    {
304        try
305        {
306            Map<String, Object> result = _contentDAO.tag(Collections.singletonList(contentId), Collections.singletonList(WALL_CONTENT_PIN_TAG), mode, contextualParameters, true);
307            return result;
308        }
309        finally
310        {
311            _commitAllChanges();
312        }
313    }
314    
315    /**
316     * Report content to webmasters (user with report notification right on wall contents)
317     * @param siteName the current site name
318     * @param contentId the id of content to report
319     * @return true if the content was successfully reported
320     */
321    @Callable (rights = REPORT_WALL_CONTENT_RIGHT_ID, paramIndex = 1, rightContext = ContentRightAssignmentContext.ID)
322    public boolean reportContent(String siteName, String contentId)
323    {
324        Content content = _resolver.resolveById(contentId);
325        User reporter = _userManager.getUser(_currentUserProvider.getUser());
326        Site site = _siteManager.getSite(siteName);
327        
328        // Add the report to the content
329        _contentDAO.report(content);
330        
331        // Send a mail to the allowed users
332        List<Project> projects = _projectManager.getProjectsForSite(site);
333        if (!projects.isEmpty())
334        {
335            Project project = projects.get(0);
336            
337            Map<String, List<String>> recipientsByLanguage = _getReportsRecipientsByLanguage(project);
338            if (!recipientsByLanguage.isEmpty())
339            {
340                Map<String, I18nizableTextParameter> i18nParams = new HashMap<>();
341                i18nParams.put("projectTitle", new I18nizableText(project.getTitle()));
342                i18nParams.put("siteTitle", new I18nizableText(site.getTitle()));
343                
344                I18nizableText i18nSubject = new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_WALL_CONTENT_REPORTED_SUBJECT", i18nParams);
345                
346                i18nParams.put("projectUrl", new I18nizableText(site.getUrl()));
347                i18nParams.put("reporter", new I18nizableText(reporter.getFullName()));
348                i18nParams.put("content", new I18nizableText(getExcerpt(content, 200)));
349                
350                String from = site.getValue("site-mail-from");
351                
352                MailBodyBuilder bodyBuilder = StandardMailBodyHelper.newHTMLBody()
353                        .withTitle(i18nSubject)
354                        .withMessage(new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_WALL_CONTENT_REPORTED_BODY", i18nParams))
355                        .withDetails(new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_WALL_CONTENT_REPORTED_BODY_EXTRACT"), getExcerpt(content, 200), false)
356                        .withLink(site.getUrl(), new I18nizableText("plugin.workspaces", "PROJECT_MAIL_NOTIFICATION_BODY_DEFAULT_BUTTON_TEXT"));
357                
358                for (String userLanguage : recipientsByLanguage.keySet())
359                {
360                    try
361                    {
362                        List<String> emails = recipientsByLanguage.get(userLanguage);
363                        String subject = _i18nUtils.translate(i18nSubject, userLanguage);
364                        
365                        String htmlBody = bodyBuilder.withLanguage(userLanguage).build();
366                        
367                        SendMailHelper.newMail()
368                            .withSubject(subject)
369                            .withHTMLBody(htmlBody)
370                            .withSender(from)
371                            .withRecipients(emails)
372                            .sendMail();
373                    }
374                    catch (MessagingException | IOException e)
375                    {
376                        getLogger().warn("Could not send a notification mail to {}", recipientsByLanguage.get(userLanguage), e);
377                    }
378                }
379
380                return true;
381            }
382        }
383        
384        return false;
385    }
386    
387    /**
388     * Get the excerpt of content
389     * @param content the content
390     * @param maxLength the max length for content excerpt
391     * @return the excerpt
392     */
393    public String getExcerpt(Content content, int maxLength)
394    {
395        if (content.hasValue("content"))
396        {
397            RichText richText = content.getValue("content");
398            SAXParser saxParser = null;
399            try (InputStream is = richText.getInputStream())
400            {
401                RichTextHandler txtHandler = new RichTextHandler(maxLength);
402                saxParser = (SAXParser) _smanager.lookup(SAXParser.ROLE);
403                saxParser.parse(new InputSource(is), txtHandler);
404                
405                return txtHandler.getValue();
406            }
407            catch (Exception e)
408            {
409                getLogger().error("Cannot extract excerpt from content {}", content.getId(), e);
410            }
411            finally
412            {
413                _smanager.release(saxParser);
414            }
415        }
416     
417        return "";
418    }
419    
420    /**
421     * Retrieves the list of recipients for reports notification sending
422     * @param project the current project
423     * @return the list of recipients for reports notification sending
424     */
425    protected Map<String, List<String>> _getReportsRecipientsByLanguage(Project project)
426    {
427        WorkspaceModule module = _moduleEP.getModule(WallContentModule.WALLCONTENT_MODULE_ID);
428        ResourceCollection moduleRoot = module.getModuleRoot(project, false);
429        
430        Set<UserIdentity> users = _rightManager.getAllowedUsers("Plugins_Workspaces_Right_ReportNotification_WallContent", moduleRoot).resolveAllowedUsers(Config.getInstance().getValue("runtime.mail.massive.sending"));
431        
432        String defaultLanguage = _workspaceHelper.getLang(project, _userLanguagesManager.getDefaultLanguage());
433        
434        return users.stream()
435                .map(_userManager::getUser)
436                .filter(user -> user != null)
437                .map(user -> Pair.of(user, user.getEmail()))
438                .filter(p -> StringUtils.isNotBlank(p.getRight()))
439                .collect(Collectors.groupingBy(
440                        p -> {
441                            return StringUtils.defaultIfBlank(p.getLeft().getLanguage(), defaultLanguage);
442                        },
443                        Collectors.mapping(
444                                Pair::getRight,
445                                Collectors.toList()
446                        )
447                    )
448                );
449    }
450    
451    /**
452     * Add or remove a reaction on a content
453     * @param contentId The content id
454     * @param reactionName the reaction name (ex: LIKE)
455     * @param remove true to remove the reaction, false to add reaction
456     * @return the result with the current actors of this reaction
457     */
458    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
459    public Map<String, Object> react(String contentId, String reactionName, boolean remove)
460    {
461        return _contentDAO.react(contentId, reactionName, remove);
462    }
463    
464    /**
465     * Retrieves the comments and reports of the contents of the given type
466     * Manages only contents that have at least one report (on itself or on a comment)
467     * @param contentTypeId the content type identifier
468     * @return the comments and reports of the contents
469     * @throws RepositoryException if an error occurs while retrieving contents from the repository
470     */
471    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
472    public Map getCommentsAndReportsFromContentTypeId(String contentTypeId) throws RepositoryException
473    {
474        return _commentAndReportCmp.getCommentsAndReportsFromContentTypeId(contentTypeId, "Plugins_Workspaces_Right_See_Reports_WallContent");
475    }
476    
477}