UNPKG

mattermost-redux

Version:

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

808 lines 121 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientError = exports.DEFAULT_LIMIT_AFTER = exports.DEFAULT_LIMIT_BEFORE = exports.HEADER_X_VERSION_ID = void 0; var tslib_1 = require("tslib"); // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. var constants_1 = require("../constants"); var helpers_1 = require("../utils/helpers"); var sentry_1 = require("../utils/sentry"); var user_utils_1 = require("../utils/user_utils"); var fetch_etag_1 = tslib_1.__importDefault(require("./fetch_etag")); var FormData = require('form-data'); var HEADER_AUTH = 'Authorization'; var HEADER_BEARER = 'BEARER'; var HEADER_REQUESTED_WITH = 'X-Requested-With'; var HEADER_USER_AGENT = 'User-Agent'; var HEADER_X_CLUSTER_ID = 'X-Cluster-Id'; var HEADER_X_CSRF_TOKEN = 'X-CSRF-Token'; exports.HEADER_X_VERSION_ID = 'X-Version-Id'; var PER_PAGE_DEFAULT = 60; var LOGS_PER_PAGE_DEFAULT = 10000; exports.DEFAULT_LIMIT_BEFORE = 30; exports.DEFAULT_LIMIT_AFTER = 30; /* eslint-disable no-throw-literal */ var Client4 = /** @class */ (function () { function Client4() { var _this = this; this.logToConsole = false; this.serverVersion = ''; this.clusterId = ''; this.token = ''; this.csrf = ''; this.url = ''; this.urlVersion = '/api/v4'; this.userAgent = null; this.enableLogging = false; this.defaultHeaders = {}; this.userId = ''; this.diagnosticId = ''; this.includeCookies = true; this.translations = { connectionError: 'There appears to be a problem with your internet connection.', unknownError: 'We received an unexpected status code from the server.', }; // User Routes this.createUser = function (user, token, inviteId, redirect) { _this.trackEvent('api', 'api_users_create'); var queryParams = {}; if (token) { queryParams.t = token; } if (inviteId) { queryParams.iid = inviteId; } if (redirect) { queryParams.r = redirect; } return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(queryParams), { method: 'post', body: JSON.stringify(user) }); }; this.patchMe = function (userPatch) { return _this.doFetch(_this.getUserRoute('me') + "/patch", { method: 'put', body: JSON.stringify(userPatch) }); }; this.patchUser = function (userPatch) { _this.trackEvent('api', 'api_users_patch'); return _this.doFetch(_this.getUserRoute(userPatch.id) + "/patch", { method: 'put', body: JSON.stringify(userPatch) }); }; this.updateUser = function (user) { _this.trackEvent('api', 'api_users_update'); return _this.doFetch("" + _this.getUserRoute(user.id), { method: 'put', body: JSON.stringify(user) }); }; this.promoteGuestToUser = function (userId) { _this.trackEvent('api', 'api_users_promote_guest_to_user'); return _this.doFetch(_this.getUserRoute(userId) + "/promote", { method: 'post' }); }; this.demoteUserToGuest = function (userId) { _this.trackEvent('api', 'api_users_demote_user_to_guest'); return _this.doFetch(_this.getUserRoute(userId) + "/demote", { method: 'post' }); }; this.updateUserRoles = function (userId, roles) { _this.trackEvent('api', 'api_users_update_roles'); return _this.doFetch(_this.getUserRoute(userId) + "/roles", { method: 'put', body: JSON.stringify({ roles: roles }) }); }; this.updateUserMfa = function (userId, activate, code) { var body = { activate: activate, }; if (activate) { body.code = code; } return _this.doFetch(_this.getUserRoute(userId) + "/mfa", { method: 'put', body: JSON.stringify(body) }); }; this.updateUserPassword = function (userId, currentPassword, newPassword) { _this.trackEvent('api', 'api_users_newpassword'); return _this.doFetch(_this.getUserRoute(userId) + "/password", { method: 'put', body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }) }); }; this.resetUserPassword = function (token, newPassword) { _this.trackEvent('api', 'api_users_reset_password'); return _this.doFetch(_this.getUsersRoute() + "/password/reset", { method: 'post', body: JSON.stringify({ token: token, new_password: newPassword }) }); }; this.getKnownUsers = function () { _this.trackEvent('api', 'api_get_known_users'); return _this.doFetch(_this.getUsersRoute() + "/known", { method: 'get' }); }; this.sendPasswordResetEmail = function (email) { _this.trackEvent('api', 'api_users_send_password_reset'); return _this.doFetch(_this.getUsersRoute() + "/password/reset/send", { method: 'post', body: JSON.stringify({ email: email }) }); }; this.updateUserActive = function (userId, active) { _this.trackEvent('api', 'api_users_update_active'); return _this.doFetch(_this.getUserRoute(userId) + "/active", { method: 'put', body: JSON.stringify({ active: active }) }); }; this.uploadProfileImage = function (userId, imageData) { _this.trackEvent('api', 'api_users_update_profile_picture'); var formData = new FormData(); formData.append('image', imageData); var request = { method: 'post', body: formData, }; if (formData.getBoundary) { request.headers = { 'Content-Type': "multipart/form-data; boundary=" + formData.getBoundary(), }; } return _this.doFetch(_this.getUserRoute(userId) + "/image", request); }; this.setDefaultProfileImage = function (userId) { _this.trackEvent('api', 'api_users_set_default_profile_picture'); return _this.doFetch(_this.getUserRoute(userId) + "/image", { method: 'delete' }); }; this.verifyUserEmail = function (token) { return _this.doFetch(_this.getUsersRoute() + "/email/verify", { method: 'post', body: JSON.stringify({ token: token }) }); }; this.updateMyTermsOfServiceStatus = function (termsOfServiceId, accepted) { return _this.doFetch(_this.getUserRoute('me') + "/terms_of_service", { method: 'post', body: JSON.stringify({ termsOfServiceId: termsOfServiceId, accepted: accepted }) }); }; this.getTermsOfService = function () { return _this.doFetch(_this.getBaseRoute() + "/terms_of_service", { method: 'get' }); }; this.createTermsOfService = function (text) { return _this.doFetch(_this.getBaseRoute() + "/terms_of_service", { method: 'post', body: JSON.stringify({ text: text }) }); }; this.sendVerificationEmail = function (email) { return _this.doFetch(_this.getUsersRoute() + "/email/verify/send", { method: 'post', body: JSON.stringify({ email: email }) }); }; this.login = function (loginId, password, token, deviceId, ldapOnly) { if (token === void 0) { token = ''; } if (deviceId === void 0) { deviceId = ''; } if (ldapOnly === void 0) { ldapOnly = false; } _this.trackEvent('api', 'api_users_login'); if (ldapOnly) { _this.trackEvent('api', 'api_users_login_ldap'); } var body = { device_id: deviceId, login_id: loginId, password: password, token: token, }; if (ldapOnly) { body.ldap_only = 'true'; } return _this.doFetch(_this.getUsersRoute() + "/login", { method: 'post', body: JSON.stringify(body) }); }; this.loginById = function (id, password, token, deviceId) { if (token === void 0) { token = ''; } if (deviceId === void 0) { deviceId = ''; } _this.trackEvent('api', 'api_users_login'); var body = { device_id: deviceId, id: id, password: password, token: token, }; return _this.doFetch(_this.getUsersRoute() + "/login", { method: 'post', body: JSON.stringify(body) }); }; this.logout = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var response; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.trackEvent('api', 'api_users_logout'); return [4 /*yield*/, this.doFetchWithResponse(this.getUsersRoute() + "/logout", { method: 'post' })]; case 1: response = (_a.sent()).response; if (response.ok) { this.token = ''; } this.serverVersion = ''; return [2 /*return*/, response]; } }); }); }; this.getProfiles = function (page, perPage, options) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } if (options === void 0) { options = {}; } _this.trackEvent('api', 'api_profiles_get'); return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(tslib_1.__assign({ page: page, per_page: perPage }, options)), { method: 'get' }); }; this.getProfilesByIds = function (userIds, options) { if (options === void 0) { options = {}; } _this.trackEvent('api', 'api_profiles_get_by_ids'); return _this.doFetch(_this.getUsersRoute() + "/ids" + helpers_1.buildQueryString(options), { method: 'post', body: JSON.stringify(userIds) }); }; this.getProfilesByUsernames = function (usernames) { _this.trackEvent('api', 'api_profiles_get_by_usernames'); return _this.doFetch(_this.getUsersRoute() + "/usernames", { method: 'post', body: JSON.stringify(usernames) }); }; this.getProfilesInTeam = function (teamId, page, perPage, sort, options) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } if (sort === void 0) { sort = ''; } if (options === void 0) { options = {}; } _this.trackEvent('api', 'api_profiles_get_in_team', { team_id: teamId, sort: sort }); return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(tslib_1.__assign(tslib_1.__assign({}, options), { in_team: teamId, page: page, per_page: perPage, sort: sort })), { method: 'get' }); }; this.getProfilesNotInTeam = function (teamId, groupConstrained, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } _this.trackEvent('api', 'api_profiles_get_not_in_team', { team_id: teamId, group_constrained: groupConstrained }); var queryStringObj = { not_in_team: teamId, page: page, per_page: perPage }; if (groupConstrained) { queryStringObj.group_constrained = true; } return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(queryStringObj), { method: 'get' }); }; this.getProfilesWithoutTeam = function (page, perPage, options) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } if (options === void 0) { options = {}; } _this.trackEvent('api', 'api_profiles_get_without_team'); return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(tslib_1.__assign(tslib_1.__assign({}, options), { without_team: 1, page: page, per_page: perPage })), { method: 'get' }); }; this.getProfilesInChannel = function (channelId, page, perPage, sort, options) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } if (sort === void 0) { sort = ''; } if (options === void 0) { options = {}; } _this.trackEvent('api', 'api_profiles_get_in_channel', { channel_id: channelId }); var serverVersion = _this.getServerVersion(); var queryStringObj; if (helpers_1.isMinimumServerVersion(serverVersion, 4, 7)) { queryStringObj = { in_channel: channelId, page: page, per_page: perPage, sort: sort }; } else { queryStringObj = { in_channel: channelId, page: page, per_page: perPage }; } return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(tslib_1.__assign(tslib_1.__assign({}, queryStringObj), options)), { method: 'get' }); }; this.getProfilesInGroupChannels = function (channelsIds) { _this.trackEvent('api', 'api_profiles_get_in_group_channels', { channelsIds: channelsIds }); return _this.doFetch(_this.getUsersRoute() + "/group_channels", { method: 'post', body: JSON.stringify(channelsIds) }); }; this.getProfilesNotInChannel = function (teamId, channelId, groupConstrained, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } _this.trackEvent('api', 'api_profiles_get_not_in_channel', { team_id: teamId, channel_id: channelId, group_constrained: groupConstrained }); var queryStringObj = { in_team: teamId, not_in_channel: channelId, page: page, per_page: perPage }; if (groupConstrained) { queryStringObj.group_constrained = true; } return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString(queryStringObj), { method: 'get' }); }; this.getProfilesInGroup = function (groupId, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch("" + _this.getUsersRoute() + helpers_1.buildQueryString({ in_group: groupId, page: page, per_page: perPage }), { method: 'get' }); }; this.getMe = function () { return _this.doFetch("" + _this.getUserRoute('me'), { method: 'get' }); }; this.getUser = function (userId) { return _this.doFetch("" + _this.getUserRoute(userId), { method: 'get' }); }; this.getUserByUsername = function (username) { return _this.doFetch(_this.getUsersRoute() + "/username/" + username, { method: 'get' }); }; this.getUserByEmail = function (email) { return _this.doFetch(_this.getUsersRoute() + "/email/" + email, { method: 'get' }); }; this.getProfilePictureUrl = function (userId, lastPictureUpdate) { var params = {}; if (lastPictureUpdate) { params._ = lastPictureUpdate; } return _this.getUserRoute(userId) + "/image" + helpers_1.buildQueryString(params); }; this.getDefaultProfilePictureUrl = function (userId) { return _this.getUserRoute(userId) + "/image/default"; }; this.autocompleteUsers = function (name, teamId, channelId, options) { if (options === void 0) { options = { limit: constants_1.General.AUTOCOMPLETE_LIMIT_DEFAULT, }; } return _this.doFetch(_this.getUsersRoute() + "/autocomplete" + helpers_1.buildQueryString({ in_team: teamId, in_channel: channelId, name: name, limit: options.limit, }), { method: 'get', }); }; this.getSessions = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/sessions", { method: 'get' }); }; this.revokeSession = function (userId, sessionId) { return _this.doFetch(_this.getUserRoute(userId) + "/sessions/revoke", { method: 'post', body: JSON.stringify({ session_id: sessionId }) }); }; this.revokeAllSessionsForUser = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/sessions/revoke/all", { method: 'post' }); }; this.revokeSessionsForAllUsers = function () { return _this.doFetch(_this.getUsersRoute() + "/sessions/revoke/all", { method: 'post' }); }; this.getUserAudits = function (userId, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch(_this.getUserRoute(userId) + "/audits" + helpers_1.buildQueryString({ page: page, per_page: perPage }), { method: 'get' }); }; this.checkUserMfa = function (loginId) { return _this.doFetch(_this.getUsersRoute() + "/mfa", { method: 'post', body: JSON.stringify({ login_id: loginId }) }); }; this.generateMfaSecret = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/mfa/generate", { method: 'post' }); }; this.attachDevice = function (deviceId) { return _this.doFetch(_this.getUsersRoute() + "/sessions/device", { method: 'put', body: JSON.stringify({ device_id: deviceId }) }); }; this.searchUsers = function (term, options) { _this.trackEvent('api', 'api_search_users'); return _this.doFetch(_this.getUsersRoute() + "/search", { method: 'post', body: JSON.stringify(tslib_1.__assign({ term: term }, options)) }); }; this.getStatusesByIds = function (userIds) { return _this.doFetch(_this.getUsersRoute() + "/status/ids", { method: 'post', body: JSON.stringify(userIds) }); }; this.getStatus = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/status", { method: 'get' }); }; this.updateStatus = function (status) { return _this.doFetch(_this.getUserRoute(status.user_id) + "/status", { method: 'put', body: JSON.stringify(status) }); }; this.updateCustomStatus = function (customStatus) { return _this.doFetch(_this.getUserRoute('me') + "/status/custom", { method: 'put', body: JSON.stringify(customStatus) }); }; this.unsetCustomStatus = function () { return _this.doFetch(_this.getUserRoute('me') + "/status/custom", { method: 'delete' }); }; this.removeRecentCustomStatus = function (customStatus) { return _this.doFetch(_this.getUserRoute('me') + "/status/custom/recent", { method: 'delete', body: JSON.stringify(customStatus) }); }; this.switchEmailToOAuth = function (service, email, password, mfaCode) { if (mfaCode === void 0) { mfaCode = ''; } _this.trackEvent('api', 'api_users_email_to_oauth'); return _this.doFetch(_this.getUsersRoute() + "/login/switch", { method: 'post', body: JSON.stringify({ current_service: 'email', new_service: service, email: email, password: password, mfa_code: mfaCode }) }); }; this.switchOAuthToEmail = function (currentService, email, password) { _this.trackEvent('api', 'api_users_oauth_to_email'); return _this.doFetch(_this.getUsersRoute() + "/login/switch", { method: 'post', body: JSON.stringify({ current_service: currentService, new_service: 'email', email: email, new_password: password }) }); }; this.switchEmailToLdap = function (email, emailPassword, ldapId, ldapPassword, mfaCode) { if (mfaCode === void 0) { mfaCode = ''; } _this.trackEvent('api', 'api_users_email_to_ldap'); return _this.doFetch(_this.getUsersRoute() + "/login/switch", { method: 'post', body: JSON.stringify({ current_service: 'email', new_service: 'ldap', email: email, password: emailPassword, ldap_id: ldapId, new_password: ldapPassword, mfa_code: mfaCode }) }); }; this.switchLdapToEmail = function (ldapPassword, email, emailPassword, mfaCode) { if (mfaCode === void 0) { mfaCode = ''; } _this.trackEvent('api', 'api_users_ldap_to_email'); return _this.doFetch(_this.getUsersRoute() + "/login/switch", { method: 'post', body: JSON.stringify({ current_service: 'ldap', new_service: 'email', email: email, password: ldapPassword, new_password: emailPassword, mfa_code: mfaCode }) }); }; this.getAuthorizedOAuthApps = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/oauth/apps/authorized", { method: 'get' }); }; this.authorizeOAuthApp = function (responseType, clientId, redirectUri, state, scope) { return _this.doFetch(_this.url + "/oauth/authorize", { method: 'post', body: JSON.stringify({ client_id: clientId, response_type: responseType, redirect_uri: redirectUri, state: state, scope: scope }) }); }; this.deauthorizeOAuthApp = function (clientId) { return _this.doFetch(_this.url + "/oauth/deauthorize", { method: 'post', body: JSON.stringify({ client_id: clientId }) }); }; this.createUserAccessToken = function (userId, description) { _this.trackEvent('api', 'api_users_create_access_token'); return _this.doFetch(_this.getUserRoute(userId) + "/tokens", { method: 'post', body: JSON.stringify({ description: description }) }); }; this.getUserAccessToken = function (tokenId) { return _this.doFetch(_this.getUsersRoute() + "/tokens/" + tokenId, { method: 'get' }); }; this.getUserAccessTokensForUser = function (userId, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch(_this.getUserRoute(userId) + "/tokens" + helpers_1.buildQueryString({ page: page, per_page: perPage }), { method: 'get' }); }; this.getUserAccessTokens = function (page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch(_this.getUsersRoute() + "/tokens" + helpers_1.buildQueryString({ page: page, per_page: perPage }), { method: 'get' }); }; this.revokeUserAccessToken = function (tokenId) { _this.trackEvent('api', 'api_users_revoke_access_token'); return _this.doFetch(_this.getUsersRoute() + "/tokens/revoke", { method: 'post', body: JSON.stringify({ token_id: tokenId }) }); }; this.disableUserAccessToken = function (tokenId) { return _this.doFetch(_this.getUsersRoute() + "/tokens/disable", { method: 'post', body: JSON.stringify({ token_id: tokenId }) }); }; this.enableUserAccessToken = function (tokenId) { return _this.doFetch(_this.getUsersRoute() + "/tokens/enable", { method: 'post', body: JSON.stringify({ token_id: tokenId }) }); }; // Team Routes this.createTeam = function (team) { _this.trackEvent('api', 'api_teams_create'); return _this.doFetch("" + _this.getTeamsRoute(), { method: 'post', body: JSON.stringify(team) }); }; this.deleteTeam = function (teamId) { _this.trackEvent('api', 'api_teams_delete'); return _this.doFetch("" + _this.getTeamRoute(teamId), { method: 'delete' }); }; this.updateTeam = function (team) { _this.trackEvent('api', 'api_teams_update_name', { team_id: team.id }); return _this.doFetch("" + _this.getTeamRoute(team.id), { method: 'put', body: JSON.stringify(team) }); }; this.patchTeam = function (team) { _this.trackEvent('api', 'api_teams_patch_name', { team_id: team.id }); return _this.doFetch(_this.getTeamRoute(team.id) + "/patch", { method: 'put', body: JSON.stringify(team) }); }; this.regenerateTeamInviteId = function (teamId) { _this.trackEvent('api', 'api_teams_regenerate_invite_id', { team_id: teamId }); return _this.doFetch(_this.getTeamRoute(teamId) + "/regenerate_invite_id", { method: 'post' }); }; this.updateTeamScheme = function (teamId, schemeId) { var patch = { scheme_id: schemeId }; _this.trackEvent('api', 'api_teams_update_scheme', tslib_1.__assign({ team_id: teamId }, patch)); return _this.doFetch("" + _this.getTeamSchemeRoute(teamId), { method: 'put', body: JSON.stringify(patch) }); }; this.checkIfTeamExists = function (teamName) { return _this.doFetch(_this.getTeamNameRoute(teamName) + "/exists", { method: 'get' }); }; this.getTeams = function (page, perPage, includeTotalCount) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } if (includeTotalCount === void 0) { includeTotalCount = false; } return _this.doFetch("" + _this.getTeamsRoute() + helpers_1.buildQueryString({ page: page, per_page: perPage, include_total_count: includeTotalCount }), { method: 'get' }); }; this.searchTeams = function (term, opts) { _this.trackEvent('api', 'api_search_teams'); return _this.doFetch(_this.getTeamsRoute() + "/search", { method: 'post', body: JSON.stringify(tslib_1.__assign({ term: term }, opts)) }); }; this.getTeam = function (teamId) { return _this.doFetch(_this.getTeamRoute(teamId), { method: 'get' }); }; this.getTeamByName = function (teamName) { _this.trackEvent('api', 'api_teams_get_team_by_name'); return _this.doFetch(_this.getTeamNameRoute(teamName), { method: 'get' }); }; this.getMyTeams = function () { return _this.doFetch(_this.getUserRoute('me') + "/teams", { method: 'get' }); }; this.getTeamsForUser = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/teams", { method: 'get' }); }; this.getMyTeamMembers = function () { return _this.doFetch(_this.getUserRoute('me') + "/teams/members", { method: 'get' }); }; this.getMyTeamUnreads = function () { return _this.doFetch(_this.getUserRoute('me') + "/teams/unread", { method: 'get' }); }; this.getTeamMembers = function (teamId, page, perPage, options) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch("" + _this.getTeamMembersRoute(teamId) + helpers_1.buildQueryString(tslib_1.__assign({ page: page, per_page: perPage }, options)), { method: 'get' }); }; this.getTeamMembersForUser = function (userId) { return _this.doFetch(_this.getUserRoute(userId) + "/teams/members", { method: 'get' }); }; this.getTeamMember = function (teamId, userId) { return _this.doFetch("" + _this.getTeamMemberRoute(teamId, userId), { method: 'get' }); }; this.getTeamMembersByIds = function (teamId, userIds) { return _this.doFetch(_this.getTeamMembersRoute(teamId) + "/ids", { method: 'post', body: JSON.stringify(userIds) }); }; this.addToTeam = function (teamId, userId) { _this.trackEvent('api', 'api_teams_invite_members', { team_id: teamId }); var member = { user_id: userId, team_id: teamId }; return _this.doFetch("" + _this.getTeamMembersRoute(teamId), { method: 'post', body: JSON.stringify(member) }); }; this.addToTeamFromInvite = function (token, inviteId) { if (token === void 0) { token = ''; } if (inviteId === void 0) { inviteId = ''; } _this.trackEvent('api', 'api_teams_invite_members'); var query = helpers_1.buildQueryString({ token: token, invite_id: inviteId }); return _this.doFetch(_this.getTeamsRoute() + "/members/invite" + query, { method: 'post' }); }; this.addUsersToTeam = function (teamId, userIds) { _this.trackEvent('api', 'api_teams_batch_add_members', { team_id: teamId, count: userIds.length }); var members = []; userIds.forEach(function (id) { return members.push({ team_id: teamId, user_id: id }); }); return _this.doFetch(_this.getTeamMembersRoute(teamId) + "/batch", { method: 'post', body: JSON.stringify(members) }); }; this.addUsersToTeamGracefully = function (teamId, userIds) { _this.trackEvent('api', 'api_teams_batch_add_members', { team_id: teamId, count: userIds.length }); var members = []; userIds.forEach(function (id) { return members.push({ team_id: teamId, user_id: id }); }); return _this.doFetch(_this.getTeamMembersRoute(teamId) + "/batch?graceful=true", { method: 'post', body: JSON.stringify(members) }); }; this.joinTeam = function (inviteId) { var query = helpers_1.buildQueryString({ invite_id: inviteId }); return _this.doFetch(_this.getTeamsRoute() + "/members/invite" + query, { method: 'post' }); }; this.removeFromTeam = function (teamId, userId) { _this.trackEvent('api', 'api_teams_remove_members', { team_id: teamId }); return _this.doFetch("" + _this.getTeamMemberRoute(teamId, userId), { method: 'delete' }); }; this.getTeamStats = function (teamId) { return _this.doFetch(_this.getTeamRoute(teamId) + "/stats", { method: 'get' }); }; this.getTotalUsersStats = function () { return _this.doFetch(_this.getUsersRoute() + "/stats", { method: 'get' }); }; this.getFilteredUsersStats = function (options) { return _this.doFetch(_this.getUsersRoute() + "/stats/filtered" + helpers_1.buildQueryString(options), { method: 'get' }); }; this.invalidateAllEmailInvites = function () { return _this.doFetch(_this.getTeamsRoute() + "/invites/email", { method: 'delete' }); }; this.getTeamInviteInfo = function (inviteId) { return _this.doFetch(_this.getTeamsRoute() + "/invite/" + inviteId, { method: 'get' }); }; this.updateTeamMemberRoles = function (teamId, userId, roles) { _this.trackEvent('api', 'api_teams_update_member_roles', { team_id: teamId }); return _this.doFetch(_this.getTeamMemberRoute(teamId, userId) + "/roles", { method: 'put', body: JSON.stringify({ roles: roles }) }); }; this.sendEmailInvitesToTeam = function (teamId, emails) { _this.trackEvent('api', 'api_teams_invite_members', { team_id: teamId }); return _this.doFetch(_this.getTeamRoute(teamId) + "/invite/email", { method: 'post', body: JSON.stringify(emails) }); }; this.sendEmailGuestInvitesToChannels = function (teamId, channelIds, emails, message) { _this.trackEvent('api', 'api_teams_invite_guests', { team_id: teamId, channel_ids: channelIds }); return _this.doFetch(_this.getTeamRoute(teamId) + "/invite-guests/email", { method: 'post', body: JSON.stringify({ emails: emails, channels: channelIds, message: message }) }); }; this.sendEmailInvitesToTeamGracefully = function (teamId, emails) { _this.trackEvent('api', 'api_teams_invite_members', { team_id: teamId }); return _this.doFetch(_this.getTeamRoute(teamId) + "/invite/email?graceful=true", { method: 'post', body: JSON.stringify(emails) }); }; this.sendEmailGuestInvitesToChannelsGracefully = function (teamId, channelIds, emails, message) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { this.trackEvent('api', 'api_teams_invite_guests', { team_id: teamId, channel_ids: channelIds }); return [2 /*return*/, this.doFetch(this.getTeamRoute(teamId) + "/invite-guests/email?graceful=true", { method: 'post', body: JSON.stringify({ emails: emails, channels: channelIds, message: message }) })]; }); }); }; this.importTeam = function (teamId, file, importFrom) { var formData = new FormData(); formData.append('file', file, file.name); formData.append('filesize', file.size); formData.append('importFrom', importFrom); var request = { method: 'post', body: formData, }; if (formData.getBoundary) { request.headers = { 'Content-Type': "multipart/form-data; boundary=" + formData.getBoundary(), }; } return _this.doFetch(_this.getTeamRoute(teamId) + "/import", request); }; this.getTeamIconUrl = function (teamId, lastTeamIconUpdate) { var params = {}; if (lastTeamIconUpdate) { params._ = lastTeamIconUpdate; } return _this.getTeamRoute(teamId) + "/image" + helpers_1.buildQueryString(params); }; this.setTeamIcon = function (teamId, imageData) { _this.trackEvent('api', 'api_team_set_team_icon'); var formData = new FormData(); formData.append('image', imageData); var request = { method: 'post', body: formData, }; if (formData.getBoundary) { request.headers = { 'Content-Type': "multipart/form-data; boundary=" + formData.getBoundary(), }; } return _this.doFetch(_this.getTeamRoute(teamId) + "/image", request); }; this.removeTeamIcon = function (teamId) { _this.trackEvent('api', 'api_team_remove_team_icon'); return _this.doFetch(_this.getTeamRoute(teamId) + "/image", { method: 'delete' }); }; this.updateTeamMemberSchemeRoles = function (teamId, userId, isSchemeUser, isSchemeAdmin) { var body = { scheme_user: isSchemeUser, scheme_admin: isSchemeAdmin }; return _this.doFetch(_this.getTeamRoute(teamId) + "/members/" + userId + "/schemeRoles", { method: 'put', body: JSON.stringify(body) }); }; // Channel Routes this.getAllChannels = function (page, perPage, notAssociatedToGroup, excludeDefaultChannels, includeTotalCount, includeDeleted) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } if (notAssociatedToGroup === void 0) { notAssociatedToGroup = ''; } if (excludeDefaultChannels === void 0) { excludeDefaultChannels = false; } if (includeTotalCount === void 0) { includeTotalCount = false; } if (includeDeleted === void 0) { includeDeleted = false; } var queryData = { page: page, per_page: perPage, not_associated_to_group: notAssociatedToGroup, exclude_default_channels: excludeDefaultChannels, include_total_count: includeTotalCount, include_deleted: includeDeleted, }; return _this.doFetch("" + _this.getChannelsRoute() + helpers_1.buildQueryString(queryData), { method: 'get' }); }; this.createChannel = function (channel) { _this.trackEvent('api', 'api_channels_create', { team_id: channel.team_id }); return _this.doFetch("" + _this.getChannelsRoute(), { method: 'post', body: JSON.stringify(channel) }); }; this.createDirectChannel = function (userIds) { _this.trackEvent('api', 'api_channels_create_direct'); return _this.doFetch(_this.getChannelsRoute() + "/direct", { method: 'post', body: JSON.stringify(userIds) }); }; this.createGroupChannel = function (userIds) { _this.trackEvent('api', 'api_channels_create_group'); return _this.doFetch(_this.getChannelsRoute() + "/group", { method: 'post', body: JSON.stringify(userIds) }); }; this.deleteChannel = function (channelId) { _this.trackEvent('api', 'api_channels_delete', { channel_id: channelId }); return _this.doFetch("" + _this.getChannelRoute(channelId), { method: 'delete' }); }; this.unarchiveChannel = function (channelId) { _this.trackEvent('api', 'api_channels_unarchive', { channel_id: channelId }); return _this.doFetch(_this.getChannelRoute(channelId) + "/restore", { method: 'post' }); }; this.updateChannel = function (channel) { _this.trackEvent('api', 'api_channels_update', { channel_id: channel.id }); return _this.doFetch("" + _this.getChannelRoute(channel.id), { method: 'put', body: JSON.stringify(channel) }); }; this.convertChannelToPrivate = function (channelId) { _this.trackEvent('api', 'api_channels_convert_to_private', { channel_id: channelId }); return _this.doFetch(_this.getChannelRoute(channelId) + "/convert", { method: 'post' }); }; this.updateChannelPrivacy = function (channelId, privacy) { _this.trackEvent('api', 'api_channels_update_privacy', { channel_id: channelId, privacy: privacy }); return _this.doFetch(_this.getChannelRoute(channelId) + "/privacy", { method: 'put', body: JSON.stringify({ privacy: privacy }) }); }; this.patchChannel = function (channelId, channelPatch) { _this.trackEvent('api', 'api_channels_patch', { channel_id: channelId }); return _this.doFetch(_this.getChannelRoute(channelId) + "/patch", { method: 'put', body: JSON.stringify(channelPatch) }); }; this.updateChannelNotifyProps = function (props) { _this.trackEvent('api', 'api_users_update_channel_notifications', { channel_id: props.channel_id }); return _this.doFetch(_this.getChannelMemberRoute(props.channel_id, props.user_id) + "/notify_props", { method: 'put', body: JSON.stringify(props) }); }; this.updateChannelScheme = function (channelId, schemeId) { var patch = { scheme_id: schemeId }; _this.trackEvent('api', 'api_channels_update_scheme', tslib_1.__assign({ channel_id: channelId }, patch)); return _this.doFetch("" + _this.getChannelSchemeRoute(channelId), { method: 'put', body: JSON.stringify(patch) }); }; this.getChannel = function (channelId) { _this.trackEvent('api', 'api_channel_get', { channel_id: channelId }); return _this.doFetch("" + _this.getChannelRoute(channelId), { method: 'get' }); }; this.getChannelByName = function (teamId, channelName, includeDeleted) { if (includeDeleted === void 0) { includeDeleted = false; } return _this.doFetch(_this.getTeamRoute(teamId) + "/channels/name/" + channelName + "?include_deleted=" + includeDeleted, { method: 'get' }); }; this.getChannelByNameAndTeamName = function (teamName, channelName, includeDeleted) { if (includeDeleted === void 0) { includeDeleted = false; } _this.trackEvent('api', 'api_channel_get_by_name_and_teamName', { include_deleted: includeDeleted }); return _this.doFetch(_this.getTeamNameRoute(teamName) + "/channels/name/" + channelName + "?include_deleted=" + includeDeleted, { method: 'get' }); }; this.getChannels = function (teamId, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch(_this.getTeamRoute(teamId) + "/channels" + helpers_1.buildQueryString({ page: page, per_page: perPage }), { method: 'get' }); }; this.getArchivedChannels = function (teamId, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch(_this.getTeamRoute(teamId) + "/channels/deleted" + helpers_1.buildQueryString({ page: page, per_page: perPage }), { method: 'get' }); }; this.getMyChannels = function (teamId, includeDeleted) { if (includeDeleted === void 0) { includeDeleted = false; } return _this.doFetch(_this.getUserRoute('me') + "/teams/" + teamId + "/channels" + helpers_1.buildQueryString({ include_deleted: includeDeleted }), { method: 'get' }); }; this.getMyChannelMember = function (channelId) { return _this.doFetch("" + _this.getChannelMemberRoute(channelId, 'me'), { method: 'get' }); }; this.getMyChannelMembers = function (teamId) { return _this.doFetch(_this.getUserRoute('me') + "/teams/" + teamId + "/channels/members", { method: 'get' }); }; this.getChannelMembers = function (channelId, page, perPage) { if (page === void 0) { page = 0; } if (perPage === void 0) { perPage = PER_PAGE_DEFAULT; } return _this.doFetch("" + _this.getChannelMembersRoute(channelId) + helpers_1.buildQueryString({ page: page, per_page: perPage }), { method: 'get' }); }; this.getChannelTimezones = function (channelId) { return _this.doFetch(_this.getChannelRoute(channelId) + "/timezones", { method: 'get' }); }; this.getChannelMember = function (channelId, userId) { return _this.doFetch("" + _this.getChannelMemberRoute(channelId, userId), { method: 'get' }); }; this.getChannelMembersByIds = function (channelId, userIds) { return _this.doFetch(_this.getChannelMembersRoute(channelId) + "/ids", { method: 'post', body: JSON.stringify(userIds) }); }; this.addToChannel = function (userId, channelId, postRootId) { if (postRootId === void 0) { postRootId = ''; } _this.trackEvent('api', 'api_channels_add_member', { channel_id: channelId }); var member = { user_id: userId, channel_id: channelId, post_root_id: postRootId }; return _this.doFetch("" + _this.getChannelMembersRoute(channelId), { method: 'post', body: JSON.stringify(member) }); }; this.removeFromChannel = function (userId, channelId) { _this.trackEvent('api', 'api_channels_remove_member', { channel_id: channelId }); return _this.doFetch("" + _this.getChannelMemberRoute(channelId, userId), { method: 'delete' }); }; this.updateChannelMemberRoles = function (channelId, userId, roles) { return _this.doFetch(_this.getChannelMemberRoute(channelId, userId) + "/roles", { method: 'put', body: JSON.stringify({ roles: roles }) }); }; this.getChannelStats = function (channelId) { return _this.doFetch(_this.getChannelRoute(channelId) + "/stats", { method: 'get' }); }; this.getChannelModerations = function (channelId) { return _this.doFetch(_this.getChannelRoute(channelId) + "/moderations", { method: 'get' }); }; this.patchChannelModerations = function (channelId, channelModerationsPatch) { return _this.doFetch(_this.getChannelRoute(channelId) + "/moderations/patch", { method: 'put', body: JSON.stringify(channelModerationsPatch) }); }; this.getChannelMemberCountsByGroup = function (channelId, includeTimezones) { return _this.doFetch(_this.getChannelRoute(channelId) + "/member_counts_by_group?include_timezones=" + includeTimezones, { method: 'get' }); }; this.viewMyChannel = function (channelId, prevChannelId) { var data = { channel_id: channelId, prev_channel_id: prevChannelId }; return _this.doFetch(_this.getChannelsRoute() + "/members/me/view", { method: 'post', body: JSON.stringify(data) }); }; this.autocompleteChannels = function (teamId, name) { return _this.doFetch(_this.getTeamRoute(teamId) + "/channels/autocomplete" + helpers_1.buildQueryString({ name: name }), { method: 'get' }); }; this.autocompleteChannelsForSearch = function (teamId, name) { return _this.doFetch(_this.getTeamRoute(teamId) + "/channels/search_autocomplete" + helpers_1.buildQueryString({ name: name }), { method: 'get' }); }; this.searchChannels = function (teamId, term) { return _this.doFetch(_this.getTeamRoute(teamId) + "/channels/search", { method: 'post', body: JSON.stringify({ term: term }) }); }; this.searchArchivedChannels = function (teamId, term) { return _this.doFetch(_this.getTeamRoute(teamId) + "/channels/search_archived", { method: 'post', body: JSON.stringify({ term: term }) }); }; this.searchAllChannels = function (term, opts) { if (opts === void 0) { opts = {}; } var body = tslib_1.__assign({ term: term }, opts); var includeDeleted = Boolean(opts.include_deleted); return _this.doFetch(_this.getChannelsRoute() + "/search?include_deleted=" + includeDeleted, { method: 'post', body: JSON.stringify(body) }); }; this.searchGroupChannels = function (term) { return _this.doFetch(_this.getChannelsRoute() + "/group/search", { method: 'post', body: JSON.stringify({ term: term }) }); }; this.updateChannelMemberSchemeRoles = function (channelId, userId, isSchemeUser, isSchemeAdmin) { var body = { scheme_user: isSchemeUser, scheme_admin: isSchemeAdmin }; return _this.doFetch(_this.getChannelRoute(channelId) + "/members/" + userId + "/schemeRoles", { method: 'put', body: JSON.stringify(body) }); }; // Channel Category Routes this.getChannelCategories = function (userId, teamId) { return _this.doFetch("" + _this.getChannelCategoriesRoute(userId, teamId), { method: 'get' }); }; this.createChannelCategory = function (userId, teamId, category) { return _this.doFetch("" + _this.getChannelCategoriesRoute(userId, teamId), { method: 'post', body: JSON.stringify(category) }); }; this.updateChannelCategories = function (userId, teamId, categories) { return _this.doFetch("" + _this.getChannelCategoriesRoute(userId, teamId), { method: 'put', body: JSON.stringify(categories) }); }; this.getChannelCategoryOrder = function (userId, teamId) { return _this.doFetch(_this.getChannelCategoriesRoute(userId, teamId) + "/order", { method: 'get' }); }; this.updateChannelCategoryOrder = function (userId, teamId, categoryOrder) { return _this.doFetch(_this.getChannelCategoriesRoute(userId, teamId) + "/order", { method: 'put', body: JSON.stringify(categoryOrder) }); }; this.getChannelCategory = function (userId, teamId, categoryId) { return _this.doFetch(_this.getChannelCategoriesRoute(userId, teamId) + "/" + categoryId, { method: 'get' }); }; this.updateChannelCategory = function (userId, teamId, category) { return _this.doFetch(_this.getChannelCategoriesRoute(userId, teamId) + "/" + category.id, { method: 'put', body: JSON.stringify(category) }); }; this.deleteChannelCategory = function (userId, teamId, categoryId) { return _this.doFetch(_this.getChannelCategoriesRoute(userId, teamId) + "/" + categoryId, { method: 'delete' }); }; // Post Routes this.createPost = function (post) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var result, analyticsData; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.doFetch("" + this.getPostsRoute(), { method: 'post', body: JSON.stringify(post) })]; case 1: result = _a.sent(); analyticsData = { channel_id: result.channel_id, post_id: result.id, user_actual_id: result.user_id, root_id: result.root_id }; this.trackEvent('api', 'api_posts_create', analyticsData); if (result.root_id != null && result.root_id !== '') { this.trackEvent('api', 'api_posts_replied', analyticsData); } return [2 /*return*/, result]; } }); }); }; this.updatePost = function (post) { _this.trackEvent('api', 'api_posts_update', { channel_id: post.channel_id, post_id: post.id }); return _this.doFetch("" + _this.getPostRoute(post.id), { method: 'put', body: JSON.stringify(post) }); }; this.getPost = function (postId) { return _this.doFetch("" + _this.getPostRoute(postId), { method: 'get' }); }; this.patchPost