UNPKG

mattermost-redux

Version:

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

1,091 lines 88.5 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.updateUserRoles = exports.patchUser = exports.updateMe = exports.stopPeriodicStatusUpdates = exports.startPeriodicStatusUpdates = exports.searchProfiles = exports.autocompleteUsers = exports.getUserAudits = exports.loadProfilesForDirect = exports.revokeSessionsForAllUsers = exports.revokeAllSessionsForUser = exports.revokeSession = exports.getSessions = exports.removeRecentCustomStatus = exports.unsetCustomStatus = exports.setCustomStatus = exports.setStatus = exports.getStatus = exports.getStatusesByIds = exports.getStatusesByIdsBatchedDebounced = exports.getUserByEmail = exports.getUserByUsername = exports.getUser = exports.createTermsOfService = exports.demoteUserToGuest = exports.promoteGuestToUser = exports.getTermsOfService = exports.getProfilesInGroup = exports.updateMyTermsOfServiceStatus = exports.getMe = exports.getProfilesNotInChannel = exports.getProfilesInGroupChannels = exports.getProfilesInChannel = exports.getProfilesWithoutTeam = exports.getProfilesNotInTeam = exports.getProfilesInTeam = exports.getProfilesByUsernames = exports.getProfilesByIds = exports.getMissingProfilesByUsernames = exports.getMissingProfilesByIds = exports.getProfiles = exports.getFilteredUsersStats = exports.getTotalUsersStats = exports.logout = exports.loadMe = exports.loginById = exports.login = exports.createUser = exports.generateMfaSecret = exports.checkMfa = void 0; exports.checkForModifiedUsers = exports.clearUserAccessTokens = exports.getKnownUsers = exports.enableUserAccessToken = exports.disableUserAccessToken = exports.revokeUserAccessToken = exports.getUserAccessTokensForUser = exports.getUserAccessTokens = exports.getUserAccessToken = exports.createUserAccessToken = exports.switchLdapToEmail = exports.switchEmailToLdap = exports.switchOAuthToEmail = exports.switchEmailToOAuth = exports.uploadProfileImage = exports.setDefaultProfileImage = exports.sendPasswordResetEmail = exports.resetUserPassword = exports.sendVerificationEmail = exports.verifyUserEmail = exports.updateUserActive = exports.updateUserPassword = exports.updateUserMfa = void 0; var tslib_1 = require("tslib"); var actions_1 = require("../types/actions"); var client_1 = require("../client"); var constants_1 = require("../constants"); var action_types_1 = require("../action_types"); var emojis_1 = require("./emojis"); var general_1 = require("./general"); var teams_1 = require("./teams"); var roles_1 = require("./roles"); var channel_utils_1 = require("../utils/channel_utils"); var user_utils_1 = require("../utils/user_utils"); var helpers_1 = require("../utils/helpers"); var general_2 = require("../selectors/entities/general"); var users_1 = require("../selectors/entities/users"); var errors_1 = require("./errors"); var helpers_2 = require("./helpers"); var preferences_1 = require("./preferences"); function checkMfa(loginId) { var _this = this; return function (dispatch) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var data, error_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: dispatch({ type: action_types_1.UserTypes.CHECK_MFA_REQUEST, data: null }); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.checkUserMfa(loginId)]; case 2: data = _a.sent(); dispatch({ type: action_types_1.UserTypes.CHECK_MFA_SUCCESS, data: null }); return [2 /*return*/, { data: data.mfa_required }]; case 3: error_1 = _a.sent(); dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.CHECK_MFA_FAILURE, error: error_1 }, errors_1.logError(error_1), ])); return [2 /*return*/, { error: error_1 }]; case 4: return [2 /*return*/]; } }); }); }; } exports.checkMfa = checkMfa; function generateMfaSecret(userId) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.generateMfaSecret, params: [ userId, ], }); } exports.generateMfaSecret = generateMfaSecret; function createUser(user, token, inviteId, redirect) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var created, error_2, profiles; var _a; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); return [4 /*yield*/, client_1.Client4.createUser(user, token, inviteId, redirect)]; case 1: created = _b.sent(); return [3 /*break*/, 3]; case 2: error_2 = _b.sent(); helpers_2.forceLogoutIfNecessary(error_2, dispatch, getState); dispatch(errors_1.logError(error_2)); return [2 /*return*/, { error: error_2 }]; case 3: profiles = (_a = {}, _a[created.id] = created, _a); dispatch({ type: action_types_1.UserTypes.RECEIVED_PROFILES, data: profiles }); return [2 /*return*/, { data: created }]; } }); }); }; } exports.createUser = createUser; function login(loginId, password, mfaToken, ldapOnly) { var _this = this; if (mfaToken === void 0) { mfaToken = ''; } if (ldapOnly === void 0) { ldapOnly = false; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var deviceId, data, error_3; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: dispatch({ type: action_types_1.UserTypes.LOGIN_REQUEST, data: null }); deviceId = getState().entities.general.deviceToken; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.login(loginId, password, mfaToken, deviceId, ldapOnly)]; case 2: data = _a.sent(); return [3 /*break*/, 4]; case 3: error_3 = _a.sent(); dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.LOGIN_FAILURE, error: error_3, }, errors_1.logError(error_3), ])); return [2 /*return*/, { error: error_3 }]; case 4: return [2 /*return*/, completeLogin(data)(dispatch, getState)]; } }); }); }; } exports.login = login; function loginById(id, password, mfaToken) { var _this = this; if (mfaToken === void 0) { mfaToken = ''; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var deviceId, data, error_4; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: dispatch({ type: action_types_1.UserTypes.LOGIN_REQUEST, data: null }); deviceId = getState().entities.general.deviceToken; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.loginById(id, password, mfaToken, deviceId)]; case 2: data = _a.sent(); return [3 /*break*/, 4]; case 3: error_4 = _a.sent(); dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.LOGIN_FAILURE, error: error_4, }, errors_1.logError(error_4), ])); return [2 /*return*/, { error: error_4 }]; case 4: return [2 /*return*/, completeLogin(data)(dispatch, getState)]; } }); }); }; } exports.loginById = loginById; function completeLogin(data) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var teamMembers, membersRequest, unreadsRequest, teamUnreads, _loop_1, teamUnreads_1, teamUnreads_1_1, u, error_5, promises, serverVersion, error_6, roles, _a, _b, role, teamMembers_1, teamMembers_1_1, teamMember, _c, _d, role; var e_1, _e, e_2, _f, e_3, _g, e_4, _h; return tslib_1.__generator(this, function (_j) { switch (_j.label) { case 0: dispatch({ type: action_types_1.UserTypes.RECEIVED_ME, data: data, }); client_1.Client4.setUserId(data.id); client_1.Client4.setUserRoles(data.roles); _j.label = 1; case 1: _j.trys.push([1, 4, , 5]); membersRequest = client_1.Client4.getMyTeamMembers(); unreadsRequest = client_1.Client4.getMyTeamUnreads(); return [4 /*yield*/, membersRequest]; case 2: teamMembers = _j.sent(); return [4 /*yield*/, unreadsRequest]; case 3: teamUnreads = _j.sent(); if (teamUnreads) { _loop_1 = function (u) { var index = teamMembers.findIndex(function (m) { return m.team_id === u.team_id; }); var member = teamMembers[index]; member.mention_count = u.mention_count; member.msg_count = u.msg_count; }; try { for (teamUnreads_1 = tslib_1.__values(teamUnreads), teamUnreads_1_1 = teamUnreads_1.next(); !teamUnreads_1_1.done; teamUnreads_1_1 = teamUnreads_1.next()) { u = teamUnreads_1_1.value; _loop_1(u); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (teamUnreads_1_1 && !teamUnreads_1_1.done && (_e = teamUnreads_1.return)) _e.call(teamUnreads_1); } finally { if (e_1) throw e_1.error; } } } return [3 /*break*/, 5]; case 4: error_5 = _j.sent(); dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.LOGIN_FAILURE, error: error_5 }, errors_1.logError(error_5), ])); return [2 /*return*/, { error: error_5 }]; case 5: promises = [ dispatch(preferences_1.getMyPreferences()), dispatch(teams_1.getMyTeams()), dispatch(general_1.getClientConfig()), ]; serverVersion = client_1.Client4.getServerVersion(); dispatch(general_1.setServerVersion(serverVersion)); if (!helpers_1.isMinimumServerVersion(serverVersion, 4, 7) && general_2.getConfig(getState()).EnableCustomEmoji === 'true') { dispatch(emojis_1.getAllCustomEmojis()); } _j.label = 6; case 6: _j.trys.push([6, 8, , 9]); return [4 /*yield*/, Promise.all(promises)]; case 7: _j.sent(); return [3 /*break*/, 9]; case 8: error_6 = _j.sent(); dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.LOGIN_FAILURE, error: error_6 }, errors_1.logError(error_6), ])); return [2 /*return*/, { error: error_6 }]; case 9: dispatch(actions_1.batchActions([ { type: action_types_1.TeamTypes.RECEIVED_MY_TEAM_MEMBERS, data: teamMembers, }, { type: action_types_1.UserTypes.LOGIN_SUCCESS, }, ])); roles = new Set(); try { for (_a = tslib_1.__values(data.roles.split(' ')), _b = _a.next(); !_b.done; _b = _a.next()) { role = _b.value; roles.add(role); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_b && !_b.done && (_f = _a.return)) _f.call(_a); } finally { if (e_2) throw e_2.error; } } try { for (teamMembers_1 = tslib_1.__values(teamMembers), teamMembers_1_1 = teamMembers_1.next(); !teamMembers_1_1.done; teamMembers_1_1 = teamMembers_1.next()) { teamMember = teamMembers_1_1.value; try { for (_c = (e_4 = void 0, tslib_1.__values(teamMember.roles.split(' '))), _d = _c.next(); !_d.done; _d = _c.next()) { role = _d.value; roles.add(role); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_d && !_d.done && (_h = _c.return)) _h.call(_c); } finally { if (e_4) throw e_4.error; } } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (teamMembers_1_1 && !teamMembers_1_1.done && (_g = teamMembers_1.return)) _g.call(teamMembers_1); } finally { if (e_3) throw e_3.error; } } if (roles.size > 0) { dispatch(roles_1.loadRolesIfNeeded(roles)); } return [2 /*return*/, { data: true }]; } }); }); }; } function loadMe() { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var state, config, deviceId, promises, serverVersion, currentUserId, user; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: state = getState(); config = general_2.getConfig(state); deviceId = state.entities.general.deviceToken; if (deviceId) { client_1.Client4.attachDevice(deviceId); } promises = [ dispatch(getMe()), dispatch(preferences_1.getMyPreferences()), dispatch(teams_1.getMyTeams()), dispatch(teams_1.getMyTeamMembers()), dispatch(teams_1.getMyTeamUnreads()), ]; serverVersion = client_1.Client4.getServerVersion() || getState().entities.general.serverVersion; dispatch(general_1.setServerVersion(serverVersion)); if (!helpers_1.isMinimumServerVersion(serverVersion, 4, 7) && config.EnableCustomEmoji === 'true') { dispatch(emojis_1.getAllCustomEmojis()); } return [4 /*yield*/, Promise.all(promises)]; case 1: _a.sent(); currentUserId = getState().entities.users.currentUserId; user = getState().entities.users.profiles[currentUserId]; if (currentUserId) { client_1.Client4.setUserId(currentUserId); } if (user) { client_1.Client4.setUserRoles(user.roles); } return [2 /*return*/, { data: true }]; } }); }); }; } exports.loadMe = loadMe; function logout() { var _this = this; return function (dispatch) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var error_7; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: dispatch({ type: action_types_1.UserTypes.LOGOUT_REQUEST, data: null }); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.logout()]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: error_7 = _a.sent(); return [3 /*break*/, 4]; case 4: dispatch({ type: action_types_1.UserTypes.LOGOUT_SUCCESS, data: null }); return [2 /*return*/, { data: true }]; } }); }); }; } exports.logout = logout; function getTotalUsersStats() { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getTotalUsersStats, onSuccess: action_types_1.UserTypes.RECEIVED_USER_STATS, }); } exports.getTotalUsersStats = getTotalUsersStats; function getFilteredUsersStats(options) { var _this = this; if (options === void 0) { options = {}; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var stats, error_8; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, client_1.Client4.getFilteredUsersStats(options)]; case 1: stats = _a.sent(); return [3 /*break*/, 3]; case 2: error_8 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_8, dispatch, getState); dispatch(errors_1.logError(error_8)); return [2 /*return*/, { error: error_8 }]; case 3: dispatch({ type: action_types_1.UserTypes.RECEIVED_FILTERED_USER_STATS, data: stats, }); return [2 /*return*/, { data: stats }]; } }); }); }; } exports.getFilteredUsersStats = getFilteredUsersStats; function getProfiles(page, perPage, options) { var _this = this; if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } if (options === void 0) { options = {}; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_9; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfiles(page, perPage, options)]; case 2: profiles = _a.sent(); user_utils_1.removeUserFromList(currentUserId, profiles); return [3 /*break*/, 4]; case 3: error_9 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_9, dispatch, getState); dispatch(errors_1.logError(error_9)); return [2 /*return*/, { error: error_9 }]; case 4: dispatch({ type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: profiles, }); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfiles = getProfiles; function getMissingProfilesByIds(userIds) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var profiles, missingIds; return tslib_1.__generator(this, function (_a) { profiles = getState().entities.users.profiles; missingIds = []; userIds.forEach(function (id) { if (!profiles[id]) { missingIds.push(id); } }); if (missingIds.length > 0) { getStatusesByIds(missingIds)(dispatch, getState); return [2 /*return*/, getProfilesByIds(missingIds)(dispatch, getState)]; } return [2 /*return*/, { data: [] }]; }); }); }; } exports.getMissingProfilesByIds = getMissingProfilesByIds; function getMissingProfilesByUsernames(usernames) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var profiles, usernameProfiles, missingUsernames; return tslib_1.__generator(this, function (_a) { profiles = getState().entities.users.profiles; usernameProfiles = Object.values(profiles).reduce(function (acc, profile) { acc[profile.username] = profile; return acc; }, {}); missingUsernames = []; usernames.forEach(function (username) { if (!usernameProfiles[username]) { missingUsernames.push(username); } }); if (missingUsernames.length > 0) { return [2 /*return*/, getProfilesByUsernames(missingUsernames)(dispatch, getState)]; } return [2 /*return*/, { data: [] }]; }); }); }; } exports.getMissingProfilesByUsernames = getMissingProfilesByUsernames; function getProfilesByIds(userIds, options) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_10; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesByIds(userIds, options)]; case 2: profiles = _a.sent(); user_utils_1.removeUserFromList(currentUserId, profiles); return [3 /*break*/, 4]; case 3: error_10 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_10, dispatch, getState); dispatch(errors_1.logError(error_10)); return [2 /*return*/, { error: error_10 }]; case 4: dispatch({ type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: profiles, }); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesByIds = getProfilesByIds; function getProfilesByUsernames(usernames) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_11; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesByUsernames(usernames)]; case 2: profiles = _a.sent(); user_utils_1.removeUserFromList(currentUserId, profiles); return [3 /*break*/, 4]; case 3: error_11 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_11, dispatch, getState); dispatch(errors_1.logError(error_11)); return [2 /*return*/, { error: error_11 }]; case 4: dispatch({ type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: profiles, }); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesByUsernames = getProfilesByUsernames; function getProfilesInTeam(teamId, page, perPage, sort, options) { var _this = this; if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } if (sort === void 0) { sort = ''; } if (options === void 0) { options = {}; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_12; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesInTeam(teamId, page, perPage, sort, options)]; case 2: profiles = _a.sent(); return [3 /*break*/, 4]; case 3: error_12 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_12, dispatch, getState); dispatch(errors_1.logError(error_12)); return [2 /*return*/, { error: error_12 }]; case 4: dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST_IN_TEAM, data: profiles, id: teamId, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: user_utils_1.removeUserFromList(currentUserId, tslib_1.__spread(profiles)), }, ])); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesInTeam = getProfilesInTeam; function getProfilesNotInTeam(teamId, groupConstrained, page, perPage) { var _this = this; if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var profiles, error_13, receivedProfilesListActionType; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, client_1.Client4.getProfilesNotInTeam(teamId, groupConstrained, page, perPage)]; case 1: profiles = _a.sent(); return [3 /*break*/, 3]; case 2: error_13 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_13, dispatch, getState); dispatch(errors_1.logError(error_13)); return [2 /*return*/, { error: error_13 }]; case 3: receivedProfilesListActionType = groupConstrained ? action_types_1.UserTypes.RECEIVED_PROFILES_LIST_NOT_IN_TEAM_AND_REPLACE : action_types_1.UserTypes.RECEIVED_PROFILES_LIST_NOT_IN_TEAM; dispatch(actions_1.batchActions([ { type: receivedProfilesListActionType, data: profiles, id: teamId, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: profiles, }, ])); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesNotInTeam = getProfilesNotInTeam; function getProfilesWithoutTeam(page, perPage, options) { var _this = this; if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } if (options === void 0) { options = {}; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var profiles, error_14; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: profiles = null; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesWithoutTeam(page, perPage, options)]; case 2: profiles = _a.sent(); return [3 /*break*/, 4]; case 3: error_14 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_14, dispatch, getState); dispatch(errors_1.logError(error_14)); return [2 /*return*/, { error: error_14 }]; case 4: dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST_WITHOUT_TEAM, data: profiles, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: profiles, }, ])); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesWithoutTeam = getProfilesWithoutTeam; function getProfilesInChannel(channelId, page, perPage, sort, options) { var _this = this; if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } if (sort === void 0) { sort = ''; } if (options === void 0) { options = {}; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_15; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesInChannel(channelId, page, perPage, sort, options)]; case 2: profiles = _a.sent(); return [3 /*break*/, 4]; case 3: error_15 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_15, dispatch, getState); dispatch(errors_1.logError(error_15)); return [2 /*return*/, { error: error_15 }]; case 4: dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST_IN_CHANNEL, data: profiles, id: channelId, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: user_utils_1.removeUserFromList(currentUserId, tslib_1.__spread(profiles)), }, ])); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesInChannel = getProfilesInChannel; function getProfilesInGroupChannels(channelsIds) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, channelProfiles, error_16, actions, channelId, profiles; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesInGroupChannels(channelsIds.slice(0, constants_1.General.MAX_GROUP_CHANNELS_FOR_PROFILES))]; case 2: channelProfiles = _a.sent(); return [3 /*break*/, 4]; case 3: error_16 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_16, dispatch, getState); dispatch(errors_1.logError(error_16)); return [2 /*return*/, { error: error_16 }]; case 4: actions = []; for (channelId in channelProfiles) { if (channelProfiles.hasOwnProperty(channelId)) { profiles = channelProfiles[channelId]; actions.push({ type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST_IN_CHANNEL, data: profiles, id: channelId, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: user_utils_1.removeUserFromList(currentUserId, tslib_1.__spread(profiles)), }); } } dispatch(actions_1.batchActions(actions)); return [2 /*return*/, { data: channelProfiles }]; } }); }); }; } exports.getProfilesInGroupChannels = getProfilesInGroupChannels; function getProfilesNotInChannel(teamId, channelId, groupConstrained, page, perPage) { var _this = this; if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_17, receivedProfilesListActionType; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesNotInChannel(teamId, channelId, groupConstrained, page, perPage)]; case 2: profiles = _a.sent(); return [3 /*break*/, 4]; case 3: error_17 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_17, dispatch, getState); dispatch(errors_1.logError(error_17)); return [2 /*return*/, { error: error_17 }]; case 4: receivedProfilesListActionType = groupConstrained ? action_types_1.UserTypes.RECEIVED_PROFILES_LIST_NOT_IN_CHANNEL_AND_REPLACE : action_types_1.UserTypes.RECEIVED_PROFILES_LIST_NOT_IN_CHANNEL; dispatch(actions_1.batchActions([ { type: receivedProfilesListActionType, data: profiles, id: channelId, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: user_utils_1.removeUserFromList(currentUserId, tslib_1.__spread(profiles)), }, ])); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesNotInChannel = getProfilesNotInChannel; function getMe() { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var getMeFunc, me; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: getMeFunc = helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getMe, onSuccess: action_types_1.UserTypes.RECEIVED_ME, }); return [4 /*yield*/, getMeFunc(dispatch, getState)]; case 1: me = _a.sent(); if ('error' in me) { return [2 /*return*/, me]; } if ('data' in me) { dispatch(roles_1.loadRolesIfNeeded(me.data.roles.split(' '))); } return [2 /*return*/, me]; } }); }); }; } exports.getMe = getMe; function updateMyTermsOfServiceStatus(termsOfServiceId, accepted) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var response; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch(helpers_2.bindClientFunc({ clientFunc: client_1.Client4.updateMyTermsOfServiceStatus, params: [ termsOfServiceId, accepted, ], }))]; case 1: response = _a.sent(); if ('data' in response) { if (accepted) { dispatch({ type: action_types_1.UserTypes.RECEIVED_TERMS_OF_SERVICE_STATUS, data: { terms_of_service_create_at: new Date().getTime(), terms_of_service_id: accepted ? termsOfServiceId : null, user_id: users_1.getCurrentUserId(getState()), }, }); } return [2 /*return*/, { data: response.data, }]; } return [2 /*return*/, { error: response.error, }]; } }); }); }; } exports.updateMyTermsOfServiceStatus = updateMyTermsOfServiceStatus; function getProfilesInGroup(groupId, page, perPage) { var _this = this; if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = constants_1.General.PROFILE_CHUNK_SIZE; } return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var currentUserId, profiles, error_18; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: currentUserId = getState().entities.users.currentUserId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, client_1.Client4.getProfilesInGroup(groupId, page, perPage)]; case 2: profiles = _a.sent(); return [3 /*break*/, 4]; case 3: error_18 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_18, dispatch, getState); dispatch(errors_1.logError(error_18)); return [2 /*return*/, { error: error_18 }]; case 4: dispatch(actions_1.batchActions([ { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST_IN_GROUP, data: profiles, id: groupId, }, { type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST, data: user_utils_1.removeUserFromList(currentUserId, tslib_1.__spread(profiles)), }, ])); return [2 /*return*/, { data: profiles }]; } }); }); }; } exports.getProfilesInGroup = getProfilesInGroup; function getTermsOfService() { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getTermsOfService, }); } exports.getTermsOfService = getTermsOfService; function promoteGuestToUser(userId) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.promoteGuestToUser, params: [userId], }); } exports.promoteGuestToUser = promoteGuestToUser; function demoteUserToGuest(userId) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.demoteUserToGuest, params: [userId], }); } exports.demoteUserToGuest = demoteUserToGuest; function createTermsOfService(text) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.createTermsOfService, params: [ text, ], }); } exports.createTermsOfService = createTermsOfService; function getUser(id) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getUser, onSuccess: action_types_1.UserTypes.RECEIVED_PROFILE, params: [ id, ], }); } exports.getUser = getUser; function getUserByUsername(username) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getUserByUsername, onSuccess: action_types_1.UserTypes.RECEIVED_PROFILE, params: [ username, ], }); } exports.getUserByUsername = getUserByUsername; function getUserByEmail(email) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getUserByEmail, onSuccess: action_types_1.UserTypes.RECEIVED_PROFILE, params: [ email, ], }); } exports.getUserByEmail = getUserByEmail; // We create an array to hold the id's that we want to get a status for. We build our // debounced function that will get called after a set period of idle time in which // the array of id's will be passed to the getStatusesByIds with a cb that clears out // the array. Helps with performance because instead of making 75 different calls for // statuses, we are only making one call for 75 ids. // We could maybe clean it up somewhat by storing the array of ids in redux state possbily? var ids = []; var debouncedGetStatusesByIds = helpers_2.debounce(function (dispatch, getState) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { getStatusesByIds(tslib_1.__spread(new Set(ids)))(dispatch, getState); return [2 /*return*/]; }); }); }, 20, false, function () { ids = []; }); function getStatusesByIdsBatchedDebounced(id) { ids = tslib_1.__spread(ids, [id]); return debouncedGetStatusesByIds; } exports.getStatusesByIdsBatchedDebounced = getStatusesByIdsBatchedDebounced; function getStatusesByIds(userIds) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getStatusesByIds, onSuccess: action_types_1.UserTypes.RECEIVED_STATUSES, params: [ userIds, ], }); } exports.getStatusesByIds = getStatusesByIds; function getStatus(userId) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getStatus, onSuccess: action_types_1.UserTypes.RECEIVED_STATUS, params: [ userId, ], }); } exports.getStatus = getStatus; function setStatus(status) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var error_19; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, client_1.Client4.updateStatus(status)]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: error_19 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_19, dispatch, getState); dispatch(errors_1.logError(error_19)); return [2 /*return*/, { error: error_19 }]; case 3: dispatch({ type: action_types_1.UserTypes.RECEIVED_STATUS, data: status, }); return [2 /*return*/, { data: status }]; } }); }); }; } exports.setStatus = setStatus; function setCustomStatus(customStatus) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.updateCustomStatus, params: [ customStatus, ], }); } exports.setCustomStatus = setCustomStatus; function unsetCustomStatus() { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.unsetCustomStatus, }); } exports.unsetCustomStatus = unsetCustomStatus; function removeRecentCustomStatus(customStatus) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.removeRecentCustomStatus, params: [ customStatus, ], }); } exports.removeRecentCustomStatus = removeRecentCustomStatus; function getSessions(userId) { return helpers_2.bindClientFunc({ clientFunc: client_1.Client4.getSessions, onSuccess: action_types_1.UserTypes.RECEIVED_SESSIONS, params: [ userId, ], }); } exports.getSessions = getSessions; function revokeSession(userId, sessionId) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var error_20; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, client_1.Client4.revokeSession(userId, sessionId)]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: error_20 = _a.sent(); helpers_2.forceLogoutIfNecessary(error_20, dispatch, getState); dispatch(errors_1.logError(error_20)); return [2 /*return*/, { error: error_20 }]; case 3: dispatch({ type: action_types_1.UserTypes.RECEIVED_REVOKED_SESSION, sessionId: sessionId, data: null, }); return [2 /*return*/, { data: true }]; } }); }); }; } exports.revokeSession = revokeSession; function revokeAllSessionsForUser(userId) { var _this = this; return function (dispatch, getState) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var error_21, data; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, client_1.Client4.revokeAllSessionsForUser(userId)]; case 1: _a.sent(); return [3 /*bre