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            // Content has already been notified so we ignore it
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            Set<Query> queries = _queryHelper.getQueriesForResult(content.getId(), site);
156    
157            getLogger().info("{} queries found for content {}", queries.size(), content.getId());
158            
159            if (queries.isEmpty())
160            {
161                return;
162            }
163    
164            // Then collect users that have read access to the content
165            Set<UserIdentity> users;
166            AllowedUsers readAccessAllowedUsers = _rightManager.getReadAccessAllowedUsers(content);
167            if (readAccessAllowedUsers.isAnonymousAllowed() || readAccessAllowedUsers.isAnyConnectedUserAllowed())
168            {
169                List<String> userPopulationsIds = _userPopulationDAO.getUserPopulationsIds();
170                Collection<User> allUsers = _userManager.getUsersByPopulationIds(userPopulationsIds);
171                users = allUsers.stream().map(User::getIdentity).collect(Collectors.toSet());
172            }
173            else
174            {
175                users = readAccessAllowedUsers.resolveAllowedUsers(true);
176            }
177    
178            Map<String, Query> queryMap = queries.stream().collect(Collectors.toMap(Query::getId, Function.identity()));
179            Set<String> feedIds = queryMap.keySet();
180    
181            // Then find the users that have subscribed to the queries
182            Map<String, Map<UserIdentity, Set<String>>> notificationsNeeded = new HashMap<>();
183            for (UserIdentity user : users)
184            {
185                Map<String, Set<String>> tokensForUser = _userPreferencesHelper.getUserImpactedTokens(user, feedIds, site);
186                for (Entry<String, Set<String>> tokensByFeed : tokensForUser.entrySet())
187                {
188                    Query query = queryMap.get(tokensByFeed.getKey());
189                    
190                    if (_rightManager.hasReadAccess(user, query))
191                    {
192                        Map<UserIdentity, Set<String>> tokens = notificationsNeeded.computeIfAbsent(tokensByFeed.getKey(), __ -> new HashMap<>());
193                        tokens.put(user, tokensByFeed.getValue());
194                    }
195                }
196            }
197    
198            Map<String, String> data = _queryHelper.getDataForContent(content);
199            Map<String, String> sorts = queries.stream().collect(Collectors.toMap(Query::getId, q -> _queryHelper.getSortProperty(q, true).get(0).sortField()));
200    
201            // Finally send the notifications
202            for (Entry<String, Map<UserIdentity, Set<String>>> entry : notificationsNeeded.entrySet())
203            {
204                Map<String, Object> notificationData = new HashMap<>();
205                notificationData.putAll(data);
206                String feedId = entry.getKey();
207                notificationData.put("feed_id", feedId);
208                if (queryMap.containsKey(feedId))
209                {
210                    notificationData.put("category_name", queryMap.get(feedId).getTitle());
211                }
212    
213                String sortField = null;
214                if (sorts.containsKey(feedId))
215                {
216                    sortField = sorts.get(feedId);
217                }
218                String isoDate = _queryHelper.getContentFormattedDate(content, sortField);
219                notificationData.put("date", isoDate);
220    
221                String description = _getContentDescription(content);
222    
223                _pushNotificationManager.pushNotifications(content.getTitle(), description, entry.getValue(), notificationData);
224            }
225        }
226        finally
227        {
228            // Restore context
229            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
230        }
231        
232        // Mark the content to be able to remember that it was notified
233        if (defaultContent instanceof ModifiableDataAwareVersionableAmetysObject versionable)
234        {
235            versionable.getUnversionedDataHolder().setValue(NOTIFICATION_PUSHED_METADATA_NAME, true);
236            versionable.saveChanges();
237        }
238    }
239
240    /**
241     * Get a description for the notification.
242     * 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).
243     * If none is available, an empty String is returned.
244     * @param content The content to read
245     * @return a description for this content
246     */
247    protected String _getContentDescription(Content content)
248    {
249        Long maxSize = Config.getInstance().getValue(__DESCRIPTION_MAX_SIZE_CONF_ID);
250        String result = "";
251
252        if (content.hasValue("abstract") && org.ametys.runtime.model.type.ModelItemTypeConstants.STRING_TYPE_ID.equals(content.getDefinition("abstract").getType().getId()))
253        {
254            result = content.getValue("abstract", false, "");
255        }
256        else if (content.hasValue("content") && ModelItemTypeConstants.RICH_TEXT_ELEMENT_TYPE_ID.equals(content.getDefinition("content").getType().getId()))
257        {
258            RichText richText = content.getValue("content");
259            result = _richTextHelper.richTextToString(richText, maxSize.intValue());
260        }
261
262        return result;
263    }
264}