UNPKG

mattermost-redux

Version:

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

681 lines (680 loc) 25.4 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertAnalyticsRowsToStats = convertAnalyticsRowsToStats; const redux_1 = require("redux"); const action_types_1 = require("mattermost-redux/action_types"); const constants_1 = require("mattermost-redux/constants"); const plugins_1 = __importDefault(require("mattermost-redux/constants/plugins")); function logs(state = [], action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_LOGS: { return action.data; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return []; default: return state; } } function plainLogs(state = [], action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_PLAIN_LOGS: { return action.data; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return []; default: return state; } } function audits(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_AUDITS: { const nextState = { ...state }; for (const audit of action.data) { nextState[audit.id] = audit; } return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function config(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_CONFIG: { return action.data; } case action_types_1.AdminTypes.ENABLED_PLUGIN: { const nextPluginSettings = { ...state.PluginSettings }; const nextPluginStates = { ...nextPluginSettings.PluginStates }; nextPluginStates[action.data] = { Enable: true }; nextPluginSettings.PluginStates = nextPluginStates; return { ...state, PluginSettings: nextPluginSettings }; } case action_types_1.AdminTypes.DISABLED_PLUGIN: { const nextPluginSettings = { ...state.PluginSettings }; const nextPluginStates = { ...nextPluginSettings.PluginStates }; nextPluginStates[action.data] = { Enable: false }; nextPluginSettings.PluginStates = nextPluginStates; return { ...state, PluginSettings: nextPluginSettings }; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function prevTrialLicense(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.PREV_TRIAL_LICENSE_SUCCESS: { return action.data; } default: return state; } } function environmentConfig(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_ENVIRONMENT_CONFIG: { return action.data; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function complianceReports(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_COMPLIANCE_REPORT: { const nextState = { ...state }; nextState[action.data.id] = action.data; return nextState; } case action_types_1.AdminTypes.RECEIVED_COMPLIANCE_REPORTS: { const nextState = { ...state }; for (const report of action.data) { nextState[report.id] = report; } return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function clusterInfo(state = [], action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_CLUSTER_STATUS: { return action.data; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return []; default: return state; } } function samlCertStatus(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_SAML_CERT_STATUS: { return action.data; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function convertAnalyticsRowsToStats(data, name) { const stats = {}; const clonedData = [...data]; if (name === 'post_counts_day') { clonedData.reverse(); stats[constants_1.Stats.POST_PER_DAY] = clonedData; return stats; } if (name === 'bot_post_counts_day') { clonedData.reverse(); stats[constants_1.Stats.BOT_POST_PER_DAY] = clonedData; return stats; } if (name === 'user_counts_with_posts_day') { clonedData.reverse(); stats[constants_1.Stats.USERS_WITH_POSTS_PER_DAY] = clonedData; return stats; } clonedData.forEach((row) => { let key; switch (row.name) { case 'channel_open_count': key = constants_1.Stats.TOTAL_PUBLIC_CHANNELS; break; case 'channel_private_count': key = constants_1.Stats.TOTAL_PRIVATE_GROUPS; break; case 'post_count': key = constants_1.Stats.TOTAL_POSTS; break; case 'unique_user_count': key = constants_1.Stats.TOTAL_USERS; break; case 'inactive_user_count': key = constants_1.Stats.TOTAL_INACTIVE_USERS; break; case 'team_count': key = constants_1.Stats.TOTAL_TEAMS; break; case 'total_websocket_connections': key = constants_1.Stats.TOTAL_WEBSOCKET_CONNECTIONS; break; case 'total_master_db_connections': key = constants_1.Stats.TOTAL_MASTER_DB_CONNECTIONS; break; case 'total_read_db_connections': key = constants_1.Stats.TOTAL_READ_DB_CONNECTIONS; break; case 'daily_active_users': key = constants_1.Stats.DAILY_ACTIVE_USERS; break; case 'monthly_active_users': key = constants_1.Stats.MONTHLY_ACTIVE_USERS; break; case 'incoming_webhook_count': key = constants_1.Stats.TOTAL_IHOOKS; break; case 'outgoing_webhook_count': key = constants_1.Stats.TOTAL_OHOOKS; break; case 'command_count': key = constants_1.Stats.TOTAL_COMMANDS; break; case 'session_count': key = constants_1.Stats.TOTAL_SESSIONS; break; case 'registered_users': key = constants_1.Stats.REGISTERED_USERS; break; case 'total_file_count': key = constants_1.Stats.TOTAL_FILE_COUNT; break; case 'total_file_size': key = constants_1.Stats.TOTAL_FILE_SIZE; break; case 'single_channel_guest_count': key = constants_1.Stats.SINGLE_CHANNEL_GUESTS; break; } if (key) { stats[key] = row.value; } }); return stats; } function analytics(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_SYSTEM_ANALYTICS: { const stats = convertAnalyticsRowsToStats(action.data, action.name); return { ...state, ...stats }; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function teamAnalytics(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_TEAM_ANALYTICS: { const nextState = { ...state }; const stats = convertAnalyticsRowsToStats(action.data, action.name); const analyticsForTeam = { ...(nextState[action.teamId] || {}), ...stats }; nextState[action.teamId] = analyticsForTeam; return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function userAccessTokens(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_USER_ACCESS_TOKEN: { return { ...state, [action.data.id]: action.data }; } case action_types_1.AdminTypes.RECEIVED_USER_ACCESS_TOKENS_FOR_USER: { const nextState = {}; for (const uat of action.data) { nextState[uat.id] = uat; } return { ...state, ...nextState }; } case action_types_1.UserTypes.REVOKED_USER_ACCESS_TOKEN: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data); return { ...nextState }; } case action_types_1.UserTypes.ENABLED_USER_ACCESS_TOKEN: { const token = { ...state[action.data], is_active: true }; return { ...state, [action.data]: token }; } case action_types_1.UserTypes.DISABLED_USER_ACCESS_TOKEN: { const token = { ...state[action.data], is_active: false }; return { ...state, [action.data]: token }; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function userAccessTokensByUser(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_USER_ACCESS_TOKEN: { // UserAccessToken const nextUserState = { ...(state[action.data.user_id] || {}) }; nextUserState[action.data.id] = action.data; return { ...state, [action.data.user_id]: nextUserState }; } case action_types_1.AdminTypes.RECEIVED_USER_ACCESS_TOKENS_FOR_USER: { // UserAccessToken[] const nextUserState = { ...(state[action.userId] || {}) }; for (const uat of action.data) { nextUserState[uat.id] = uat; } return { ...state, [action.userId]: nextUserState }; } case action_types_1.UserTypes.REVOKED_USER_ACCESS_TOKEN: { const userIds = Object.keys(state); for (let i = 0; i < userIds.length; i++) { const userId = userIds[i]; if (state[userId] && state[userId][action.data]) { const nextUserState = { ...state[userId] }; Reflect.deleteProperty(nextUserState, action.data); return { ...state, [userId]: nextUserState }; } } return state; } case action_types_1.UserTypes.ENABLED_USER_ACCESS_TOKEN: { const userIds = Object.keys(state); for (let i = 0; i < userIds.length; i++) { const userId = userIds[i]; if (state[userId] && state[userId][action.data]) { const nextUserState = { ...state[userId] }; const token = { ...nextUserState[action.data], is_active: true }; nextUserState[token.id] = token; return { ...state, [userId]: nextUserState }; } } return state; } case action_types_1.UserTypes.DISABLED_USER_ACCESS_TOKEN: { const userIds = Object.keys(state); for (let i = 0; i < userIds.length; i++) { const userId = userIds[i]; if (state[userId] && state[userId][action.data]) { const nextUserState = { ...state[userId] }; const token = { ...nextUserState[action.data], is_active: false }; nextUserState[token.id] = token; return { ...state, [userId]: nextUserState }; } } return state; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function plugins(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_PLUGINS: { const nextState = { ...state }; const activePlugins = action.data.active; for (const plugin of activePlugins) { nextState[plugin.id] = { ...plugin, active: true }; } const inactivePlugins = action.data.inactive; for (const plugin of inactivePlugins) { nextState[plugin.id] = { ...plugin, active: false }; } return nextState; } case action_types_1.AdminTypes.REMOVED_PLUGIN: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data); return nextState; } case action_types_1.AdminTypes.ENABLED_PLUGIN: { const nextState = { ...state }; const plugin = nextState[action.data]; if (plugin && !plugin.active) { nextState[action.data] = { ...plugin, active: true }; return nextState; } return state; } case action_types_1.AdminTypes.DISABLED_PLUGIN: { const nextState = { ...state }; const plugin = nextState[action.data]; if (plugin && plugin.active) { nextState[action.data] = { ...plugin, active: false }; return nextState; } return state; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function pluginStatuses(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_PLUGIN_STATUSES: { const nextState = {}; for (const plugin of (action.data || [])) { const id = plugin.plugin_id; // The plugin may be in different states across the cluster. Pick the highest one to // surface an error. const pluginState = Math.max((nextState[id] && nextState[id].state) || 0, plugin.state); const instances = [ ...((nextState[id] && nextState[id].instances) || []), { cluster_id: plugin.cluster_id, version: plugin.version, state: plugin.state, }, ]; nextState[id] = { id, name: (nextState[id] && nextState[id].name) || plugin.name, description: (nextState[id] && nextState[id].description) || plugin.description, version: (nextState[id] && nextState[id].version) || plugin.version, active: pluginState > 0, state: pluginState, error: plugin.error, instances, }; } return nextState; } case action_types_1.AdminTypes.ENABLE_PLUGIN_REQUEST: { const pluginId = action.data; if (!state[pluginId]) { return state; } return { ...state, [pluginId]: { ...state[pluginId], state: plugins_1.default.PLUGIN_STATE_STARTING, }, }; } case action_types_1.AdminTypes.ENABLE_PLUGIN_FAILURE: { const pluginId = action.data; if (!state[pluginId]) { return state; } return { ...state, [pluginId]: { ...state[pluginId], state: plugins_1.default.PLUGIN_STATE_NOT_RUNNING, }, }; } case action_types_1.AdminTypes.DISABLE_PLUGIN_REQUEST: { const pluginId = action.data; if (!state[pluginId]) { return state; } return { ...state, [pluginId]: { ...state[pluginId], state: plugins_1.default.PLUGIN_STATE_STOPPING, }, }; } case action_types_1.AdminTypes.REMOVED_PLUGIN: { const pluginId = action.data; if (!state[pluginId]) { return state; } const nextState = { ...state }; Reflect.deleteProperty(nextState, pluginId); return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function ldapGroupsCount(state = 0, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_LDAP_GROUPS: return action.data.count; case action_types_1.UserTypes.LOGOUT_SUCCESS: return 0; default: return state; } } function ldapGroups(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_LDAP_GROUPS: { const nextState = {}; for (const group of action.data.groups) { nextState[group.primary_key] = group; } return nextState; } case action_types_1.AdminTypes.LINKED_LDAP_GROUP: { const nextState = { ...state }; if (nextState[action.data.primary_key]) { nextState[action.data.primary_key] = action.data; } return nextState; } case action_types_1.AdminTypes.UNLINKED_LDAP_GROUP: { const nextState = { ...state }; if (nextState[action.data]) { nextState[action.data] = { ...nextState[action.data], mattermost_group_id: undefined, has_syncables: undefined, failed: false, }; } return nextState; } case action_types_1.AdminTypes.LINK_LDAP_GROUP_FAILURE: { const nextState = { ...state }; if (nextState[action.data]) { nextState[action.data] = { ...nextState[action.data], failed: true, }; } return nextState; } case action_types_1.AdminTypes.UNLINK_LDAP_GROUP_FAILURE: { const nextState = { ...state }; if (nextState[action.data]) { nextState[action.data] = { ...nextState[action.data], failed: true, }; } return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function samlMetadataResponse(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_SAML_METADATA_RESPONSE: { return action.data; } default: return state; } } function dataRetentionCustomPolicies(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.CREATE_DATA_RETENTION_CUSTOM_POLICY_SUCCESS: case action_types_1.AdminTypes.RECEIVED_DATA_RETENTION_CUSTOM_POLICY: case action_types_1.AdminTypes.UPDATE_DATA_RETENTION_CUSTOM_POLICY_SUCCESS: { return { ...state, [action.data.id]: action.data, }; } case action_types_1.AdminTypes.RECEIVED_DATA_RETENTION_CUSTOM_POLICIES: { const nextState = { ...state }; if (action.data.policies) { for (const dataRetention of action.data.policies) { nextState[dataRetention.id] = dataRetention; } } return nextState; } case action_types_1.AdminTypes.DELETE_DATA_RETENTION_CUSTOM_POLICY_SUCCESS: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function dataRetentionCustomPoliciesCount(state = 0, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_DATA_RETENTION_CUSTOM_POLICIES: return action.data.total_count; case action_types_1.UserTypes.LOGOUT_SUCCESS: return 0; default: return state; } } function accessControlPolicies(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.CREATE_ACCESS_CONTROL_POLICY_SUCCESS: case action_types_1.AdminTypes.RECEIVED_ACCESS_CONTROL_POLICY: return { ...state, [action.data.id]: action.data, }; case action_types_1.AdminTypes.RECEIVED_ACCESS_CONTROL_POLICIES: { const nextState = {}; for (const policy of action.data) { nextState[policy.id] = policy; } return nextState; } case action_types_1.AdminTypes.DELETE_ACCESS_CONTROL_POLICY_SUCCESS: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.AdminTypes.RECEIVED_ACCESS_CONTROL_POLICIES_SEARCH: { const nextState = { ...state }; for (const policy of action.data) { nextState[policy.id] = policy; } return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function channelsForAccessControlPolicy(state = {}, action) { switch (action.type) { case action_types_1.AdminTypes.RECEIVED_ACCESS_CONTROL_CHILD_POLICIES: if (action.data) { return { ...state, ...action.data }; } return state; case action_types_1.AdminTypes.ASSIGN_CHANNELS_TO_ACCESS_CONTROL_POLICY_SUCCESS: return { ...state, [action.data.policyId]: action.data.channelIds, }; case action_types_1.AdminTypes.UNASSIGN_CHANNELS_FROM_ACCESS_CONTROL_POLICY_SUCCESS: return { ...state, [action.data.policyId]: action.data.channelIds, }; case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } exports.default = (0, redux_1.combineReducers)({ // array of LogObjects each representing a log entry (JSON) logs, // array of strings each representing a log entry (legacy) with pagination plainLogs, // object where every key is an audit id and has an object with audit details audits, // object representing the server configuration config, // object representing which fields of the server configuration were set through the environment config environmentConfig, // object where every key is a report id and has an object with report details complianceReports, // array of cluster status data clusterInfo, // object with certificate type as keys and boolean statuses as values samlCertStatus, // object with analytic categories as types and numbers as values analytics, // object with team ids as keys and analytics objects as values teamAnalytics, // object with user ids as keys and objects, with token ids as keys, and // user access tokens as values without actual token userAccessTokensByUser, // object with token ids as keys, and user access tokens as values without actual token userAccessTokens, // object with plugin ids as keys and objects representing plugin manifests as values plugins, // object with plugin ids as keys and objects representing plugin statuses across the cluster pluginStatuses, // object representing the ldap groups ldapGroups, // total ldap groups ldapGroupsCount, // object representing the metadata response obtained from the IdP samlMetadataResponse, // object representing the custom data retention policies dataRetentionCustomPolicies, // total custom retention policies dataRetentionCustomPoliciesCount, // the last trial license the server used. prevTrialLicense, // object with policy ids as keys and objects representing the policies as values accessControlPolicies, // object with policy ids as keys and arrays of channel ids as values channelsForAccessControlPolicy, });