UNPKG

mattermost-redux

Version:

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

510 lines (509 loc) 17.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.createIncomingHook = createIncomingHook; exports.getIncomingHook = getIncomingHook; exports.getIncomingHooks = getIncomingHooks; exports.isIncomingWebhooksWithCount = isIncomingWebhooksWithCount; exports.removeIncomingHook = removeIncomingHook; exports.updateIncomingHook = updateIncomingHook; exports.createOutgoingHook = createOutgoingHook; exports.getOutgoingHook = getOutgoingHook; exports.getOutgoingHooks = getOutgoingHooks; exports.removeOutgoingHook = removeOutgoingHook; exports.updateOutgoingHook = updateOutgoingHook; exports.regenOutgoingHookToken = regenOutgoingHookToken; exports.getCommands = getCommands; exports.getAutocompleteCommands = getAutocompleteCommands; exports.getCustomTeamCommands = getCustomTeamCommands; exports.addCommand = addCommand; exports.editCommand = editCommand; exports.executeCommand = executeCommand; exports.regenCommandToken = regenCommandToken; exports.deleteCommand = deleteCommand; exports.addOAuthApp = addOAuthApp; exports.editOAuthApp = editOAuthApp; exports.getOAuthApps = getOAuthApps; exports.getOutgoingOAuthConnections = getOutgoingOAuthConnections; exports.getOutgoingOAuthConnectionsForAudience = getOutgoingOAuthConnectionsForAudience; exports.addOutgoingOAuthConnection = addOutgoingOAuthConnection; exports.editOutgoingOAuthConnection = editOutgoingOAuthConnection; exports.getOutgoingOAuthConnection = getOutgoingOAuthConnection; exports.validateOutgoingOAuthConnection = validateOutgoingOAuthConnection; exports.getAppsOAuthAppIDs = getAppsOAuthAppIDs; exports.getAppsBotIDs = getAppsBotIDs; exports.getOAuthApp = getOAuthApp; exports.getAuthorizedOAuthApps = getAuthorizedOAuthApps; exports.deauthorizeOAuthApp = deauthorizeOAuthApp; exports.deleteOAuthApp = deleteOAuthApp; exports.regenOAuthAppSecret = regenOAuthAppSecret; exports.deleteOutgoingOAuthConnection = deleteOutgoingOAuthConnection; exports.submitInteractiveDialog = submitInteractiveDialog; exports.lookupInteractiveDialog = lookupInteractiveDialog; const redux_batched_actions_1 = require("redux-batched-actions"); const action_types_1 = require("mattermost-redux/action_types"); const client_1 = require("mattermost-redux/client"); const channels_1 = require("mattermost-redux/selectors/entities/channels"); const teams_1 = require("mattermost-redux/selectors/entities/teams"); const users_1 = require("mattermost-redux/selectors/entities/users"); const errors_1 = require("./errors"); const helpers_1 = require("./helpers"); const constants_1 = require("../constants"); function createIncomingHook(hook) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.createIncomingWebhook, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOK], params: [ hook, ], }); } function getIncomingHook(hookId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getIncomingWebhook, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOK], params: [ hookId, ], }); } function getIncomingHooks(teamId = '', page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT, includeTotalCount = false) { return async (dispatch, getState) => { let data; try { data = await client_1.Client4.getIncomingWebhooks(teamId, page, perPage, includeTotalCount); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } const isWebhooksWithCount = isIncomingWebhooksWithCount(data); const actions = [{ type: action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOKS, data: isWebhooksWithCount ? data.incoming_webhooks : data, }]; if (isWebhooksWithCount) { actions.push({ type: action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOKS_TOTAL_COUNT, data: data.total_count, }); } dispatch((0, redux_batched_actions_1.batchActions)(actions)); return { data }; }; } function isIncomingWebhooksWithCount(data) { return typeof data.incoming_webhooks !== 'undefined' && Array.isArray(data.incoming_webhooks) && typeof data.total_count === 'number'; } function removeIncomingHook(hookId) { return async (dispatch, getState) => { try { await client_1.Client4.removeIncomingWebhook(hookId); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } dispatch((0, redux_batched_actions_1.batchActions)([ { type: action_types_1.IntegrationTypes.DELETED_INCOMING_HOOK, data: { id: hookId }, }, ])); return { data: true }; }; } function updateIncomingHook(hook) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.updateIncomingWebhook, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOK], params: [ hook, ], }); } function createOutgoingHook(hook) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.createOutgoingWebhook, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOK], params: [ hook, ], }); } function getOutgoingHook(hookId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOutgoingWebhook, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOK], params: [ hookId, ], }); } function getOutgoingHooks(channelId = '', teamId = '', page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOutgoingWebhooks, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOKS], params: [ channelId, teamId, page, perPage, ], }); } function removeOutgoingHook(hookId) { return async (dispatch, getState) => { try { await client_1.Client4.removeOutgoingWebhook(hookId); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } dispatch((0, redux_batched_actions_1.batchActions)([ { type: action_types_1.IntegrationTypes.DELETED_OUTGOING_HOOK, data: { id: hookId }, }, ])); return { data: true }; }; } function updateOutgoingHook(hook) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.updateOutgoingWebhook, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOK], params: [ hook, ], }); } function regenOutgoingHookToken(hookId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.regenOutgoingHookToken, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOK], params: [ hookId, ], }); } function getCommands(teamId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getCommandsList, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_COMMANDS], params: [ teamId, ], }); } function getAutocompleteCommands(teamId, page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getAutocompleteCommandsList, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_COMMANDS], params: [ teamId, page, perPage, ], }); } function getCustomTeamCommands(teamId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getCustomTeamCommands, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_CUSTOM_TEAM_COMMANDS], params: [ teamId, ], }); } function addCommand(command) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.addCommand, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_COMMAND], params: [ command, ], }); } function editCommand(command) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.editCommand, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_COMMAND], params: [ command, ], }); } function executeCommand(command, args) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.executeCommand, params: [ command, args, ], }); } function regenCommandToken(id) { return async (dispatch, getState) => { let res; try { res = await client_1.Client4.regenCommandToken(id); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } dispatch((0, redux_batched_actions_1.batchActions)([ { type: action_types_1.IntegrationTypes.RECEIVED_COMMAND_TOKEN, data: { id, token: res.token, }, }, ])); return { data: true }; }; } function deleteCommand(id) { return async (dispatch, getState) => { try { await client_1.Client4.deleteCommand(id); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } dispatch((0, redux_batched_actions_1.batchActions)([ { type: action_types_1.IntegrationTypes.DELETED_COMMAND, data: { id }, }, ])); return { data: true }; }; } function addOAuthApp(app) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.createOAuthApp, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OAUTH_APP], params: [ app, ], }); } function editOAuthApp(app) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.editOAuthApp, onSuccess: action_types_1.IntegrationTypes.RECEIVED_OAUTH_APP, params: [ app, ], }); } function getOAuthApps(page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOAuthApps, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OAUTH_APPS], params: [ page, perPage, ], }); } function getOutgoingOAuthConnections(teamId, page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOutgoingOAuthConnections, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTIONS], params: [ teamId, page, perPage, ], }); } function getOutgoingOAuthConnectionsForAudience(teamId, audience, page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOutgoingOAuthConnectionsForAudience, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTIONS], params: [ teamId, audience, page, perPage, ], }); } function addOutgoingOAuthConnection(teamId, connection) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.createOutgoingOAuthConnection, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTION], params: [ teamId, connection, ], }); } function editOutgoingOAuthConnection(teamId, connection) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.editOutgoingOAuthConnection, onSuccess: action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTION, params: [ teamId, connection, ], }); } function getOutgoingOAuthConnection(teamId, connectionId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOutgoingOAuthConnection, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTION], params: [ teamId, connectionId, ], }); } function validateOutgoingOAuthConnection(teamId, connection) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.validateOutgoingOAuthConnection, params: [ teamId, connection, ], }); } function getAppsOAuthAppIDs() { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getAppsOAuthAppIDs, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_APPS_OAUTH_APP_IDS], }); } function getAppsBotIDs() { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getAppsBotIDs, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_APPS_BOT_IDS], }); } function getOAuthApp(appId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getOAuthApp, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OAUTH_APP], params: [ appId, ], }); } function getAuthorizedOAuthApps() { return async (dispatch, getState) => { const state = getState(); const currentUserId = (0, users_1.getCurrentUserId)(state); let data; try { data = await client_1.Client4.getAuthorizedOAuthApps(currentUserId); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } return { data }; }; } function deauthorizeOAuthApp(clientId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.deauthorizeOAuthApp, params: [clientId], }); } function deleteOAuthApp(id) { return async (dispatch, getState) => { try { await client_1.Client4.deleteOAuthApp(id); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } dispatch((0, redux_batched_actions_1.batchActions)([ { type: action_types_1.IntegrationTypes.DELETED_OAUTH_APP, data: { id }, }, ])); return { data: true }; }; } function regenOAuthAppSecret(appId) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.regenOAuthAppSecret, onSuccess: [action_types_1.IntegrationTypes.RECEIVED_OAUTH_APP], params: [ appId, ], }); } function deleteOutgoingOAuthConnection(id) { return async (dispatch, getState) => { try { await client_1.Client4.deleteOutgoingOAuthConnection(id); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } dispatch({ type: action_types_1.IntegrationTypes.DELETED_OUTGOING_OAUTH_CONNECTION, data: { id }, }); return { data: true }; }; } function submitInteractiveDialog(submission) { return async (dispatch, getState) => { const state = getState(); // Use the current channel as fallback submission.channel_id ||= (0, channels_1.getCurrentChannelId)(state); submission.team_id = (0, teams_1.getCurrentTeamId)(state); submission.user_id = (0, users_1.getCurrentUserId)(state); let data; try { data = await client_1.Client4.submitInteractiveDialog(submission); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } return { data }; }; } function lookupInteractiveDialog(submission) { return async (dispatch, getState) => { const state = getState(); submission.channel_id = (0, channels_1.getCurrentChannelId)(state); submission.team_id = (0, teams_1.getCurrentTeamId)(state); submission.user_id = (0, users_1.getCurrentUserId)(state); let data; try { data = await client_1.Client4.lookupInteractiveDialog(submission); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); dispatch((0, errors_1.logError)(error)); return { error }; } return { data }; }; }