UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

225 lines (224 loc) 13.7 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.getPostHistoryLimitBannerPreferences = exports.getOverageBannerPreferences = exports.shouldShowUnreadsCategory = exports.getTheme = exports.getThemePreferences = exports.getTeammateNameDisplaySetting = exports.getGroupShowPreferences = exports.getDirectShowPreferences = void 0; exports.getMyPreferences = getMyPreferences; exports.getUserPreferences = getUserPreferences; exports.get = get; exports.getFromPreferences = getFromPreferences; exports.getBool = getBool; exports.getInt = getInt; exports.makeGetCategory = makeGetCategory; exports.makeGetUserCategory = makeGetUserCategory; exports.makeGetStyleFromTheme = makeGetStyleFromTheme; exports.shouldShowJoinLeaveMessages = shouldShowJoinLeaveMessages; exports.getUnreadScrollPositionPreference = getUnreadScrollPositionPreference; exports.getCollapsedThreadsPreference = getCollapsedThreadsPreference; exports.getCollapsedThreadsPreferenceFromPreferences = getCollapsedThreadsPreferenceFromPreferences; exports.isCollapsedThreadsAllowed = isCollapsedThreadsAllowed; exports.isCollapsedThreadsEnabled = isCollapsedThreadsEnabled; exports.isCollapsedThreadsEnabledForUser = isCollapsedThreadsEnabledForUser; exports.isGroupChannelManuallyVisible = isGroupChannelManuallyVisible; exports.isCustomGroupsEnabled = isCustomGroupsEnabled; exports.getIsOnboardingFlowEnabled = getIsOnboardingFlowEnabled; exports.getHasDismissedSystemConsoleLimitReached = getHasDismissedSystemConsoleLimitReached; exports.syncedDraftsAreAllowed = syncedDraftsAreAllowed; exports.syncedDraftsAreAllowedAndEnabled = syncedDraftsAreAllowedAndEnabled; exports.getVisibleDmGmLimit = getVisibleDmGmLimit; exports.onboardingTourTipsEnabled = onboardingTourTipsEnabled; exports.moveThreadsEnabled = moveThreadsEnabled; exports.streamlinedMarketplaceEnabled = streamlinedMarketplaceEnabled; const config_1 = require("@mattermost/types/config"); const constants_1 = require("mattermost-redux/constants"); const create_selector_1 = require("mattermost-redux/selectors/create_selector"); const general_1 = require("mattermost-redux/selectors/entities/general"); const helpers_1 = require("mattermost-redux/utils/helpers"); const preference_utils_1 = require("mattermost-redux/utils/preference_utils"); const theme_utils_1 = require("mattermost-redux/utils/theme_utils"); function getMyPreferences(state) { return state.entities.preferences.myPreferences; } function getUserPreferences(state, userID) { return state.entities.preferences.userPreferences[userID]; } function getPreferenceObject(state, category, name) { return getMyPreferences(state)[(0, preference_utils_1.getPreferenceKey)(category, name)]; } function get(state, category, name, defaultValue = '', preferences) { if (preferences) { return getFromPreferences(preferences, category, name, defaultValue); } const pref = getPreferenceObject(state, category, name); return pref ? pref.value : defaultValue; } function getFromPreferences(preferences, category, name, defaultValue = '') { const key = (0, preference_utils_1.getPreferenceKey)(category, name); if (!(key in preferences)) { return defaultValue; } return preferences[key].value; } function getBool(state, category, name, defaultValue = false, userPreferences) { const value = get(state, category, name, String(defaultValue), userPreferences); return value !== 'false'; } function getInt(state, category, name, defaultValue = 0, userPreferences) { const value = get(state, category, name, String(defaultValue), userPreferences); return parseInt(value, 10); } function makeGetCategory(selectorName, category) { return (0, helpers_1.createIdsSelector)(selectorName, getMyPreferences, (preferences) => { const prefix = category + '--'; const prefsInCategory = []; for (const key in preferences) { if (key.startsWith(prefix)) { prefsInCategory.push(preferences[key]); } } return prefsInCategory; }); } function makeGetUserCategory(selectorName, category) { return (0, helpers_1.createIdsSelector)(selectorName, (state, userID) => getUserPreferences(state, userID), (preferences) => { const prefix = category + '--'; const prefsInCategory = []; for (const key in preferences) { if (key.startsWith(prefix)) { prefsInCategory.push(preferences[key]); } } return prefsInCategory; }); } exports.getDirectShowPreferences = makeGetCategory('getDirectShowPreferences', constants_1.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW); exports.getGroupShowPreferences = makeGetCategory('getGroupShowPreferences', constants_1.Preferences.CATEGORY_GROUP_CHANNEL_SHOW); exports.getTeammateNameDisplaySetting = (0, create_selector_1.createSelector)('getTeammateNameDisplaySetting', general_1.getConfig, (state) => getPreferenceObject(state, constants_1.Preferences.CATEGORY_DISPLAY_SETTINGS, constants_1.Preferences.NAME_NAME_FORMAT), general_1.getLicense, (config, teammateNameDisplayPreference, license) => { const useAdminTeammateNameDisplaySetting = (license && license.LockTeammateNameDisplay === 'true') && config.LockTeammateNameDisplay === 'true'; if (teammateNameDisplayPreference && !useAdminTeammateNameDisplaySetting) { return teammateNameDisplayPreference.value || ''; } else if (config.TeammateNameDisplay) { return config.TeammateNameDisplay; } return constants_1.General.TEAMMATE_NAME_DISPLAY.SHOW_USERNAME; }); exports.getThemePreferences = makeGetCategory('getThemePreferences', constants_1.Preferences.CATEGORY_THEME); const getThemePreference = (0, create_selector_1.createSelector)('getThemePreference', getMyPreferences, (state) => state.entities.teams.currentTeamId, (myPreferences, currentTeamId) => { // Prefer the user's current team-specific theme over the user's current global theme let themePreference; if (currentTeamId) { themePreference = myPreferences[(0, preference_utils_1.getPreferenceKey)(constants_1.Preferences.CATEGORY_THEME, currentTeamId)]; } if (!themePreference) { themePreference = myPreferences[(0, preference_utils_1.getPreferenceKey)(constants_1.Preferences.CATEGORY_THEME, '')]; } return themePreference; }); const getDefaultTheme = (0, create_selector_1.createSelector)('getDefaultTheme', general_1.getConfig, (config) => { if (config.DefaultTheme && config.DefaultTheme in constants_1.Preferences.THEMES) { const theme = constants_1.Preferences.THEMES[config.DefaultTheme]; if (theme) { return theme; } } // If no config.DefaultTheme or value doesn't refer to a valid theme name... return constants_1.Preferences.THEMES.denim; }); exports.getTheme = (0, helpers_1.createShallowSelector)('getTheme', getThemePreference, getDefaultTheme, (themePreference, defaultTheme) => { const themeValue = themePreference?.value ?? defaultTheme; // A custom theme will be a JSON-serialized object stored in a preference // At this point, the theme should be a plain object const theme = typeof themeValue === 'string' ? JSON.parse(themeValue) : themeValue; return (0, theme_utils_1.setThemeDefaults)(theme); }); function makeGetStyleFromTheme() { return (0, create_selector_1.createSelector)('makeGetStyleFromTheme', exports.getTheme, (state, getStyleFromTheme) => getStyleFromTheme, (theme, getStyleFromTheme) => { return getStyleFromTheme(theme); }); } function shouldShowJoinLeaveMessages(state) { const config = (0, general_1.getConfig)(state); const enableJoinLeaveMessage = config.EnableJoinLeaveMessageByDefault === 'true'; // This setting is true or not set if join/leave messages are to be displayed return getBool(state, constants_1.Preferences.CATEGORY_ADVANCED_SETTINGS, constants_1.Preferences.ADVANCED_FILTER_JOIN_LEAVE, enableJoinLeaveMessage); } // shouldShowUnreadsCategory returns true if the user has unereads grouped separately with the new sidebar enabled. exports.shouldShowUnreadsCategory = (0, create_selector_1.createSelector)('shouldShowUnreadsCategory', (state, userPreferences) => get(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, constants_1.Preferences.SHOW_UNREAD_SECTION, '', userPreferences), (state, userPreferences) => get(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, '', '', userPreferences), (state) => (0, general_1.getConfig)(state).ExperimentalGroupUnreadChannels, (userPreference, oldUserPreference, serverDefault) => { // Prefer the show_unread_section user preference over the previous version if (userPreference) { return userPreference === 'true'; } if (oldUserPreference) { return JSON.parse(oldUserPreference).unreads_at_top === 'true'; } // The user setting is not set, so use the system default return serverDefault === constants_1.General.DEFAULT_ON; }); function getUnreadScrollPositionPreference(state, userPreferences) { return get(state, constants_1.Preferences.CATEGORY_ADVANCED_SETTINGS, constants_1.Preferences.UNREAD_SCROLL_POSITION, constants_1.Preferences.UNREAD_SCROLL_POSITION_START_FROM_LEFT, userPreferences); } function getCollapsedThreadsPreference(state) { const configValue = (0, general_1.getConfig)(state)?.CollapsedThreads; let preferenceDefault = constants_1.Preferences.COLLAPSED_REPLY_THREADS_OFF; if (configValue === config_1.CollapsedThreads.DEFAULT_ON || configValue === config_1.CollapsedThreads.ALWAYS_ON) { preferenceDefault = constants_1.Preferences.COLLAPSED_REPLY_THREADS_ON; } return get(state, constants_1.Preferences.CATEGORY_DISPLAY_SETTINGS, constants_1.Preferences.COLLAPSED_REPLY_THREADS, preferenceDefault); } function getCollapsedThreadsPreferenceFromPreferences(state, userPreferences) { const configValue = (0, general_1.getConfig)(state)?.CollapsedThreads; let preferenceDefault = constants_1.Preferences.COLLAPSED_REPLY_THREADS_OFF; if (configValue === config_1.CollapsedThreads.DEFAULT_ON || configValue === config_1.CollapsedThreads.ALWAYS_ON) { preferenceDefault = constants_1.Preferences.COLLAPSED_REPLY_THREADS_ON; } return getFromPreferences(userPreferences, constants_1.Preferences.CATEGORY_DISPLAY_SETTINGS, constants_1.Preferences.COLLAPSED_REPLY_THREADS, preferenceDefault); } function isCollapsedThreadsAllowed(state) { return Boolean((0, general_1.getConfig)(state)) && (0, general_1.getConfig)(state).CollapsedThreads !== undefined && (0, general_1.getConfig)(state).CollapsedThreads !== config_1.CollapsedThreads.DISABLED; } function isCollapsedThreadsEnabled(state) { const isAllowed = isCollapsedThreadsAllowed(state); const userPreference = getCollapsedThreadsPreference(state); return isAllowed && (userPreference === constants_1.Preferences.COLLAPSED_REPLY_THREADS_ON || (0, general_1.getConfig)(state).CollapsedThreads === config_1.CollapsedThreads.ALWAYS_ON); } function isCollapsedThreadsEnabledForUser(state, userPreferences) { const isAllowed = isCollapsedThreadsAllowed(state); const userPreference = getCollapsedThreadsPreferenceFromPreferences(state, userPreferences); return isAllowed && (userPreference === constants_1.Preferences.COLLAPSED_REPLY_THREADS_ON || (0, general_1.getConfig)(state).CollapsedThreads === config_1.CollapsedThreads.ALWAYS_ON); } function isGroupChannelManuallyVisible(state, channelId) { return getBool(state, constants_1.Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channelId, false); } function isCustomGroupsEnabled(state) { return (0, general_1.getConfig)(state).EnableCustomGroups === 'true'; } function getIsOnboardingFlowEnabled(state) { return (0, general_1.getConfig)(state).EnableOnboardingFlow === 'true'; } function getHasDismissedSystemConsoleLimitReached(state) { return getBool(state, constants_1.Preferences.CATEGORY_UPGRADE_CLOUD, constants_1.Preferences.SYSTEM_CONSOLE_LIMIT_REACHED, false); } function syncedDraftsAreAllowed(state) { return (0, general_1.getConfig)(state).AllowSyncedDrafts === 'true'; } function syncedDraftsAreAllowedAndEnabled(state) { const isConfiguredForFeature = (0, general_1.getConfig)(state).AllowSyncedDrafts === 'true'; const isConfiguredForUser = getBool(state, constants_1.Preferences.CATEGORY_ADVANCED_SETTINGS, constants_1.Preferences.ADVANCED_SYNC_DRAFTS, true); return isConfiguredForFeature && isConfiguredForUser; } function getVisibleDmGmLimit(state, userPreferences) { const defaultLimit = 40; return getInt(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, constants_1.Preferences.LIMIT_VISIBLE_DMS_GMS, defaultLimit, userPreferences); } function onboardingTourTipsEnabled(state) { return (0, general_1.getFeatureFlagValue)(state, 'OnboardingTourTips') === 'true'; } function moveThreadsEnabled(state) { return (0, general_1.getFeatureFlagValue)(state, 'MoveThreadsEnabled') === 'true' && (0, general_1.getLicense)(state).IsLicensed === 'true'; } function streamlinedMarketplaceEnabled(state) { return (0, general_1.getFeatureFlagValue)(state, 'StreamlinedMarketplace') === 'true'; } exports.getOverageBannerPreferences = makeGetCategory('getOverageBannerPreferences', constants_1.Preferences.CATEGORY_OVERAGE_USERS_BANNER); exports.getPostHistoryLimitBannerPreferences = makeGetCategory('getPostHistoryLimitBannerPreferences', constants_1.Preferences.CATEGORY_POST_HISTORY_LIMIT_BANNER);