001/*
002 *  Copyright 2020 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.mobileapp.observer;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Map.Entry;
023import java.util.Set;
024import java.util.function.Function;
025import java.util.stream.Collectors;
026
027import org.apache.avalon.framework.context.Context;
028import org.apache.avalon.framework.context.ContextException;
029import org.apache.avalon.framework.context.Contextualizable;
030import org.apache.avalon.framework.service.ServiceException;
031import org.apache.avalon.framework.service.ServiceManager;
032import org.apache.avalon.framework.service.Serviceable;
033import org.apache.cocoon.components.ContextHelper;
034import org.apache.cocoon.environment.Request;
035
036import org.ametys.cms.ObservationConstants;
037import org.ametys.cms.data.RichText;
038import org.ametys.cms.data.RichTextHelper;
039import org.ametys.cms.data.type.ModelItemTypeConstants;
040import org.ametys.cms.repository.Content;
041import org.ametys.core.observation.AsyncObserver;
042import org.ametys.core.observation.Event;
043import org.ametys.core.right.AllowedUsers;
044import org.ametys.core.right.RightManager;
045import org.ametys.core.user.User;
046import org.ametys.core.user.UserIdentity;
047import org.ametys.core.user.UserManager;
048import org.ametys.core.user.population.UserPopulationDAO;
049import org.ametys.plugins.mobileapp.PushNotificationManager;
050import org.ametys.plugins.mobileapp.QueriesHelper;
051import org.ametys.plugins.mobileapp.UserPreferencesHelper;
052import org.ametys.plugins.queriesdirectory.Query;
053import org.ametys.plugins.repository.AmetysObjectResolver;
054import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
055import org.ametys.runtime.config.Config;
056import org.ametys.runtime.plugin.component.AbstractLogEnabled;
057import org.ametys.web.WebConstants;
058import org.ametys.web.repository.content.WebContent;
059import org.ametys.web.repository.site.Site;
060
061/**
062 * On validation, test each query to notify impacted users
063 */
064public class ContentValidatedObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable, Contextualizable
065{
066    private static final String __DESCRIPTION_MAX_SIZE_CONF_ID = "plugin.mobileapp.push.description.richtext.max";
067    
068    private Context _context;
069
070    private QueriesHelper _queryHelper;
071    private UserPreferencesHelper _userPreferencesHelper;
072    private PushNotificationManager _pushNotificationManager;
073    private UserManager _userManager;
074    private UserPopulationDAO _userPopulationDAO;
075    private RightManager _rightManager;
076    private RichTextHelper _richTextHelper;
077    private AmetysObjectResolver _resolver;
078
079    @Override
080    public void contextualize(Context context) throws ContextException
081    {
082        _context = context;
083    }
084    
085    @Override
086    public void service(ServiceManager manager) throws ServiceException
087    {
088        _queryHelper = (QueriesHelper) manager.lookup(QueriesHelper.ROLE);
089        _userPreferencesHelper = (UserPreferencesHelper) manager.lookup(UserPreferencesHelper.ROLE);
090        _pushNotificationManager = (PushNotificationManager) manager.lookup(PushNotificationManager.ROLE);
091        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
092        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
093        _userPopulationDAO = (UserPopulationDAO) manager.lookup(UserPopulationDAO.ROLE);
094        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
095        _richTextHelper = (RichTextHelper) manager.lookup(RichTextHelper.ROLE);
096    }
097
098    public boolean supports(Event event)
099    {
100        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED) 
101            || event.getId().equals(ObservationConstants.EVENT_CONTENT_TAGGED);
102    }
103
104    public int getPriority()
105    {
106        return MIN_PRIORITY;
107    }
108
109    public void observe(Event event, Map<String, Object> transientVars) throws Exception
110    {
111        // FIXME : Temporary fix to let time to the content to be visible from Solr queries
112        Thread.sleep(5000);
113        
114        String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
115        
116        Request request = ContextHelper.getRequest(_context);
117
118        // Retrieve current workspace
119        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
120        
121        try
122        {
123            // Use live workspace
124            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
125            
126            if (!_resolver.hasAmetysObjectForId(contentId))
127            {
128                getLogger().debug("Content with id {} does not exists in workspace live, no push notification will be sent.", contentId);
129                return;
130            }
131        
132            Content content = _resolver.resolveById(contentId);
133    
134            if (!(content instanceof WebContent webContent))
135            {
136                getLogger().debug("We currently do not support push notifications for off-site content {}", content.getId());
137                return;
138            }
139            
140            // First find the queries that whose results contain the content
141            Site site = webContent.getSite();
142            Set<Query> queries = _queryHelper.getQueriesForResult(content.getId(), site);
143    
144            getLogger().info("{} queries found for content {}", queries.size(), content.getId());
145            
146            if (queries.isEmpty())
147            {
148                return;
149            }
150    
151            // Then collect users that have read access to the content
152            Set<UserIdentity> users;
153            AllowedUsers readAccessAllowedUsers = _rightManager.getReadAccessAllowedUsers(content);
154            if (readAccessAllowedUsers.isAnonymousAllowed() || readAccessAllowedUsers.isAnyConnectedUserAllowed())
155            {
156                List<String> userPopulationsIds = _userPopulationDAO.getUserPopulationsIds();
157                Collection<User> allUsers = _userManager.getUsersByPopulationIds(userPopulationsIds);
158                users = allUsers.stream().map(User::getIdentity).collect(Collectors.toSet());
159            }
160            else
161            {
162                users = readAccessAllowedUsers.resolveAllowedUsers(true);
163            }
164    
165            Map<String, Query> queryMap = queries.stream().collect(Collectors.toMap(Query::getId, Function.identity()));
166            Set<String> feedIds = queryMap.keySet();
167    
168            // Then find the users that have subscribed to the queries
169            Map<String, Map<UserIdentity, Set<String>>> notificationsNeeded = new HashMap<>();
170            for (UserIdentity user : users)
171            {
172                Map<String, Set<String>> tokensForUser = _userPreferencesHelper.getUserImpactedTokens(user, feedIds, site);
173                for (Entry<String, Set<String>> tokensByFeed : tokensForUser.entrySet())
174                {
175                    Query query = queryMap.get(tokensByFeed.getKey());
176                    
177                    if (_rightManager.hasReadAccess(user, query))
178                    {
179                        Map<UserIdentity, Set<String>> tokens = notificationsNeeded.computeIfAbsent(tokensByFeed.getKey(), __ -> new HashMap<>());
180                        tokens.put(user, tokensByFeed.getValue());
181                    }
182                }
183            }
184    
185            Map<String, String> data = _queryHelper.getDataForContent(content);
186            Map<String, String> sorts = queries.stream().collect(Collectors.toMap(Query::getId, q -> _queryHelper.getSortProperty(q, true).get(0).sortField()));
187    
188            // Finally send the notifications
189            for (Entry<String, Map<UserIdentity, Set<String>>> entry : notificationsNeeded.entrySet())
190            {
191                Map<String, Object> notificationData = new HashMap<>();
192                notificationData.putAll(data);
193                String feedId = entry.getKey();
194                notificationData.put("feed_id", feedId);
195                if (queryMap.containsKey(feedId))
196                {
197                    notificationData.put("category_name", queryMap.get(feedId).getTitle());
198                }
199    
200                String sortField = null;
201                if (sorts.containsKey(feedId))
202                {
203                    sortField = sorts.get(feedId);
204                }
205                String isoDate = _queryHelper.getContentFormattedDate(content, sortField);
206                notificationData.put("date", isoDate);
207    
208                String description = _getContentDescription(content);
209    
210                _pushNotificationManager.pushNotifications(content.getTitle(), description, entry.getValue(), notificationData);
211            }
212        }
213        finally
214        {
215            // Restore context
216            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
217        }
218    }
219
220    /**
221     * Get a description for the notification.
222     * It will first try to read a "abstract" value in the content, and if not available, will try to read a rich-text stored in "content" (and cut it down).
223     * If none is available, an empty String is returned.
224     * @param content The content to read
225     * @return a description for this content
226     */
227    protected String _getContentDescription(Content content)
228    {
229        Long maxSize = Config.getInstance().getValue(__DESCRIPTION_MAX_SIZE_CONF_ID);
230        String result = "";
231
232        if (content.hasValue("abstract") && org.ametys.runtime.model.type.ModelItemTypeConstants.STRING_TYPE_ID.equals(content.getDefinition("abstract").getType().getId()))
233        {
234            result = content.getValue("abstract", false, "");
235        }
236        else if (content.hasValue("content") && ModelItemTypeConstants.RICH_TEXT_ELEMENT_TYPE_ID.equals(content.getDefinition("content").getType().getId()))
237        {
238            RichText richText = content.getValue("content");
239            result = _richTextHelper.richTextToString(richText, maxSize.intValue());
240        }
241
242        return result;
243    }
244}