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.action;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.plugins.mobileapp.PostConstants;
028import org.ametys.plugins.mobileapp.UserPreferencesHelper;
029
030/**
031 * Returns the list of feeds for a user
032 */
033public class SetNotificationsPrefsAction extends AbstractLoggedAction
034{
035    /** Parameter containing the notification switch */
036    protected static final String _NOTIF_ENABLED = "enabled";
037
038    /** Parameter containing a marker for all feeds for notification */
039    protected static final String _NOTIF_ALL_FEEDS = "allFeeds";
040
041    /** Parameter containing the requested feeds for notification */
042    protected static final String _NOTIF_FEEDS = "feeds";
043
044    /** Parameter containing the requested projects for notification */
045    protected static final String _NOTIF_WORKSPACES = "workspaces";
046
047    /** Parameter containing a marker for all projects names for notification */
048    protected static final String _NOTIF_ALL_PROJECTS = "allProjects";
049    
050    /** Parameter containing the requested projects names for notification */
051    protected static final String _NOTIF_PROJECTS = "projects";
052
053    /** Parameter containing a marker for all types for notification */
054    protected static final String _NOTIF_ALL_TYPES = "allTypes";
055
056    /** Parameter containing the requested types for notification */
057    protected static final String _NOTIF_TYPES = "types";
058
059    /** User Preferences Helper */
060    protected UserPreferencesHelper _userPreferencesHelper;
061
062    @Override
063    public void service(ServiceManager smanager) throws ServiceException
064    {
065        super.service(smanager);
066        _userPreferencesHelper = (UserPreferencesHelper) smanager.lookup(UserPreferencesHelper.ROLE);
067    }
068
069    @Override
070    @SuppressWarnings("unchecked")
071    protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams)
072    {
073        Map<String, Object> result = new HashMap<>();
074        String lang = (String) getParameter(PostConstants.LANG, jsonParams, request);
075
076        String pushToken = (String) getParameter(PostConstants.NOTIF_TOKEN, jsonParams, request);
077
078        List<String> feeds = (List<String>) getParameter(_NOTIF_FEEDS, jsonParams, request);
079
080        Map<String, Object> workspaces = (Map<String, Object>) getParameter(_NOTIF_WORKSPACES, jsonParams, request);
081        List<String> projects = workspaces != null ? (List<String>) workspaces.get(_NOTIF_PROJECTS) : null;
082        List<String> types = workspaces != null ? (List<String>) workspaces.get(_NOTIF_TYPES) : null;
083        
084        // App Mobile < 1.5.0 only send feeds, projects and types.
085        // For compatibility purposes, if theses arrays are present, we assume the all* fields are not set
086        boolean defaultAllFeedsValue = feeds == null;
087        boolean defaultAllProjectsValue = projects == null;
088        boolean defaultAllTypesValue = types == null;
089        
090        // if all three arrays are present and empty, we assume the default value is to disable notifications
091        boolean defaultDisabled = feeds != null && feeds.isEmpty() && projects != null && projects.isEmpty() && types != null && types.isEmpty();
092        
093        boolean enabled = _getValueOrDefault((Boolean) getParameter(_NOTIF_ENABLED, jsonParams, request), !defaultDisabled);
094        
095        boolean allFeeds = _getValueOrDefault((Boolean) getParameter(_NOTIF_ALL_FEEDS, jsonParams, request), defaultAllFeedsValue);
096        boolean allProjects = _getValueOrDefault(workspaces != null ? (Boolean) workspaces.get(_NOTIF_ALL_PROJECTS) : null, defaultAllProjectsValue);
097        boolean allTypes = _getValueOrDefault(workspaces != null ? (Boolean) workspaces.get(_NOTIF_ALL_TYPES) : null, defaultAllTypesValue);
098
099        // Remove null items
100        feeds = feeds != null ? feeds.stream().filter(StringUtils::isNotBlank).toList() : null;
101        types = types != null ? types.stream().filter(StringUtils::isNotBlank).toList() : null;
102        projects = projects != null ? projects.stream().filter(StringUtils::isNotBlank).toList() : null;
103
104        _userPreferencesHelper.setNotificationSettings(pushToken, enabled, allFeeds, feeds, allProjects, projects, allTypes, types, lang);
105
106        return result;
107    }
108    
109    private boolean _getValueOrDefault(Boolean value, boolean defaultValue)
110    {
111        return value != null ? value : defaultValue;
112    }
113}