001/*
002 *  Copyright 2023 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.pagesubscription.notifier;
017
018import java.io.IOException;
019import java.util.List;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.commons.lang.StringUtils;
025
026import org.ametys.core.user.User;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.core.user.UserManager;
029import org.ametys.core.util.I18nUtils;
030import org.ametys.plugins.pagesubscription.PageSubscriptionDAO;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.activities.Activity;
033import org.ametys.plugins.repository.activities.ActivityType;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.runtime.plugin.component.AbstractLogEnabled;
036import org.ametys.web.activities.AbstractPageActivityType;
037import org.ametys.web.activities.notify.ActivityNotifier;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.repository.page.jcr.DefaultPage;
040import org.ametys.web.repository.site.Site;
041
042/**
043 * Abstract notifier for page activity
044 */
045public abstract class AbstractPageSubscriptionNotifier extends AbstractLogEnabled implements ActivityNotifier, Serviceable
046{
047    /** The ametys object resolver */
048    protected AmetysObjectResolver _resolver;
049    
050    /** The i18n utils */
051    protected I18nUtils _i18nUtils;
052
053    /** The user manager */
054    protected UserManager _userManager;
055    
056    /** The subscription dao */
057    protected PageSubscriptionDAO _subscriptionDAO;
058    
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
062        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
063        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
064        _subscriptionDAO = (PageSubscriptionDAO) manager.lookup(PageSubscriptionDAO.ROLE);
065    }
066    
067    public List<String> getUsersEmailToNotify(Activity activity)
068    {
069        ActivityType activityType = activity.getActivityType();
070        if (activityType instanceof AbstractPageActivityType pageActivityType)
071        {
072            User user = _userManager.getUser(activity.getAuthor());
073            String activityAuthorEmail = user != null ? user.getEmail() : null;
074            String pageId = activity.getValue(AbstractPageActivityType.PAGE_ID);
075            Page page = _resolver.resolveById(pageId);
076            
077            if (page instanceof DefaultPage && isNotificationEnabled(page))
078            {
079                return _subscriptionDAO.getSubscribers(page)
080                            .stream()
081                            .filter(StringUtils::isNotBlank)
082                            .filter(mail -> !mail.equals(activityAuthorEmail))
083                            .toList();
084            }
085        }
086        
087        return List.of();
088    }
089    
090    /**
091     * Determines if the notification is currently enabled
092     * @param page The page
093     * @return true if the notification is enabled
094     */
095    protected boolean isNotificationEnabled(Page page)
096    {
097        Site site = page.getSite();
098        return site.getValue(getSiteParameterId(), true, false);
099    }
100    
101    /**
102     * Id of the site parameter to enable/disable the notification
103     * @return The id of site
104     */
105    protected abstract String getSiteParameterId();
106    
107    public String getMailSubject(Activity activity)
108    {
109        String pageId = activity.getValue(AbstractPageActivityType.PAGE_ID);
110        Page page = _resolver.resolveById(pageId);
111        
112        I18nizableText i18nSubject = _getMailSubject(activity, page);
113        
114        return _i18nUtils.translate(i18nSubject, page.getSitemapName());
115    }
116    
117    /**
118     * Get the mail subject
119     * @param activity the activity
120     * @param page the page from activity
121     * @return the mail subject
122     */
123    protected abstract I18nizableText _getMailSubject(Activity activity, Page page);
124
125    public String getMailTextBody(Activity activity)
126    {
127        // No text body
128        return null;
129    }
130    
131    public String getMailHtmlBody(Activity activity)
132    {
133        String pageId = activity.getValue(AbstractPageActivityType.PAGE_ID);
134        Page page = _resolver.resolveById(pageId);
135      
136        UserIdentity author = activity.getAuthor();
137        User user = _userManager.getUser(author);
138        
139        if (user != null)
140        {
141            try
142            {
143                return _getMailHtmlBody(activity, page, user);
144            }
145            catch (Exception e)
146            {
147                getLogger().error("Can't get mail body for activity from event '{}' and type '{}'", activity.getEventType(), activity.getActivityType().getId(), e);
148            }
149        }
150        return null;
151    }
152    
153    /**
154     * Get the mail body
155     * @param activity the activity
156     * @param page the page from activity
157     * @param author the author of the activity
158     * @return the mail body
159     * @throws IOException if an error occurred
160     */
161    protected abstract String _getMailHtmlBody(Activity activity, Page page, User author) throws IOException;
162
163    /**
164     * Get the absolute URL of a page
165     * @param page The page to retrieve the url
166     * @return The absolute url of the page
167     */
168    protected String _getAbsolutePageUrl(Page page)
169    {
170        return new StringBuilder().append(page.getSite().getUrl())
171                .append("/").append(page.getSitemapName())
172                .append("/").append(page.getPathInSitemap())
173                .append(".html").toString();
174    }
175    
176}