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