From 2e5617c29be69637acd384e85f795a0b343bec8d Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Thu, 21 Apr 2016 22:37:01 -0700 Subject: PLT-2057 User as a first class object (#2648) * Adding TeamMember to system * Fixing all unit tests on the backend * Fixing merge conflicts * Fixing merge conflict * Adding javascript unit tests * Adding TeamMember to system * Fixing all unit tests on the backend * Fixing merge conflicts * Fixing merge conflict * Adding javascript unit tests * Adding client side unit test * Cleaning up the clint side tests * Fixing msg * Adding more client side unit tests * Adding more using tests * Adding last bit of client side unit tests and adding make cmd * Fixing bad merge * Fixing libraries * Updating to new client side API * Fixing borken unit test * Fixing unit tests * ugg...trying to beat gofmt * ugg...trying to beat gofmt * Cleaning up remainder of the server side routes * Adding inital load api * Increased coverage of webhook unit tests (#2660) * Adding loading ... to root html * Fixing bad merge * Removing explicit content type so superagent will guess corectly (#2685) * Fixing merge and unit tests * Adding create team UI * Fixing signup flows * Adding LDAP unit tests and enterprise unit test helper (#2702) * Add the ability to reset MFA from the commandline (#2706) * Fixing compliance unit tests * Fixing client side tests * Adding open server to system console * Moving websocket connection * Fixing unit test * Fixing unit tests * Fixing unit tests * Adding nickname and more LDAP unit tests (#2717) * Adding join open teams * Cleaning up all TODOs in the code * Fixing web sockets * Removing unused webockets file * PLT-2533 Add the ability to reset a user's MFA from the system console (#2715) * Add the ability to reset a user's MFA from the system console * Add client side unit test for adminResetMfa * Reorganizing authentication to fix LDAP error message (#2723) * Fixing failing unit test * Initial upgrade db code * Adding upgrade script * Fixing upgrade script after running on core * Update OAuth and Claim routes to work with user model changes (#2739) * Fixing perminant deletion. Adding ability to delete all user and the entire database (#2740) * Fixing team invite ldap login call (#2741) * Fixing bluebar and some img stuff * Fix all the different file upload web utils (#2743) * Fixing invalid session redirect (#2744) * Redirect on bad channel name (#2746) * Fixing a bunch of issue and removing dead code * Patch to fix error message on leave channel (#2747) * Setting EnableOpenServer to false by default * Fixing config * Fixing upgrade * Fixing reported bugs * Bug fixes for PLT-2057 * PLT-2563 Redo password recovery to use a database table (#2745) * Redo password recovery to use a database table * Update reset password audits * Split out admin and user reset password APIs to be separate * Delete password recovery when user is permanently deleted * Consolidate password resetting into a single function * Removed private channels as an option for outgoing webhooks (#2752) * PLT-2577/PLT-2552 Fixes for backstage (#2753) * Added URL to incoming webhook list * Fixed client functions for adding/removing integrations * Disallowed slash commands without trigger words * Fixed clientside handling of errors on AddCommand page * Minor auth cleanup (#2758) * Changed EditPostModal to just close if you save without making any changes (#2759) * Renamed client -> Client in async_client.jsx and fixed eslint warnings (#2756) * Fixed url in channel info modal (#2755) * Fixing reported issues * Moving to version 3 of the apis * Fixing command unit tests (#2760) * Adding team admins * Fixing DM issue * Fixing eslint error * Properly set EditPostModal's originalText state in all cases (#2762) * Update client config check to assume features is defined if server is licensed (#2772) * Fixing url link * Fixing issue with websocket crashing when sending messages to different teams --- webapp/utils/async_client.jsx | 324 +++--- webapp/utils/channel_intro_messages.jsx | 3 +- webapp/utils/client.jsx | 1759 ------------------------------- webapp/utils/constants.jsx | 5 + webapp/utils/utils.jsx | 20 +- webapp/utils/web_client.jsx | 67 ++ 6 files changed, 239 insertions(+), 1939 deletions(-) delete mode 100644 webapp/utils/client.jsx create mode 100644 webapp/utils/web_client.jsx (limited to 'webapp/utils') diff --git a/webapp/utils/async_client.jsx b/webapp/utils/async_client.jsx index 80a08dc21..189b159e8 100644 --- a/webapp/utils/async_client.jsx +++ b/webapp/utils/async_client.jsx @@ -2,7 +2,7 @@ // See License.txt for license information. import $ from 'jquery'; -import * as client from './client.jsx'; +import Client from './web_client.jsx'; import * as GlobalActions from 'action_creators/global_actions.jsx'; import AppDispatcher from '../dispatcher/app_dispatcher.jsx'; import BrowserStore from 'stores/browser_store.jsx'; @@ -11,6 +11,7 @@ import PreferenceStore from 'stores/preference_store.jsx'; import PostStore from 'stores/post_store.jsx'; import UserStore from 'stores/user_store.jsx'; import * as utils from './utils.jsx'; +import ErrorStore from 'stores/error_store.jsx'; import Constants from './constants.jsx'; const ActionTypes = Constants.ActionTypes; @@ -19,6 +20,8 @@ const StatTypes = Constants.StatTypes; // Used to track in progress async calls const callTracker = {}; +const ASYNC_CLIENT_TIMEOUT = 5000; + export function dispatchError(err, method) { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_ERROR, @@ -36,7 +39,7 @@ function isCallInProgress(callName) { return false; } - if (utils.getTimestamp() - callTracker[callName] > 5000) { + if (utils.getTimestamp() - callTracker[callName] > ASYNC_CLIENT_TIMEOUT) { //console.log('AsyncClient call ' + callName + ' expired after more than 5 seconds'); return false; } @@ -51,16 +54,12 @@ export function getChannels(checkVersion) { callTracker.getChannels = utils.getTimestamp(); - return client.getChannels( - (data, textStatus, xhr) => { + return Client.getChannels( + (data) => { callTracker.getChannels = 0; - if (xhr.status === 304 || !data) { - return; - } - if (checkVersion) { - var serverVersion = xhr.getResponseHeader('X-Version-ID'); + var serverVersion = Client.getServerVersion(); if (serverVersion !== BrowserStore.getLastServerVersion()) { if (!BrowserStore.getLastServerVersion() || BrowserStore.getLastServerVersion() === '') { @@ -93,14 +92,10 @@ export function getChannel(id) { callTracker['getChannel' + id] = utils.getTimestamp(); - client.getChannel(id, - (data, textStatus, xhr) => { + Client.getChannel(id, + (data) => { callTracker['getChannel' + id] = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_CHANNEL, channel: data.channel, @@ -131,13 +126,17 @@ export function updateLastViewedAt(id) { } callTracker[`updateLastViewed${channelId}`] = utils.getTimestamp(); - client.updateLastViewedAt( + Client.updateLastViewedAt( channelId, () => { callTracker.updateLastViewed = 0; + ErrorStore.clearLastError(); + ErrorStore.emitChange(); }, (err) => { callTracker.updateLastViewed = 0; + var count = ErrorStore.getConnectionErrorCount(); + ErrorStore.setConnectionErrorCount(count + 1); dispatchError(err, 'updateLastViewedAt'); } ); @@ -150,21 +149,17 @@ export function getMoreChannels(force) { if (ChannelStore.getMoreAll().loading || force) { callTracker.getMoreChannels = utils.getTimestamp(); - client.getMoreChannels( - function getMoreChannelsSuccess(data, textStatus, xhr) { + Client.getMoreChannels( + (data) => { callTracker.getMoreChannels = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_MORE_CHANNELS, channels: data.channels, members: data.members }); }, - function getMoreChannelsFailure(err) { + (err) => { callTracker.getMoreChannels = 0; dispatchError(err, 'getMoreChannels'); } @@ -187,16 +182,12 @@ export function getChannelExtraInfo(id, memberLimit) { callTracker['getChannelExtraInfo_' + channelId] = utils.getTimestamp(); - client.getChannelExtraInfo( + Client.getChannelExtraInfo( channelId, memberLimit, - (data, textStatus, xhr) => { + (data) => { callTracker['getChannelExtraInfo_' + channelId] = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_CHANNEL_EXTRA_INFO, extra_info: data @@ -210,53 +201,90 @@ export function getChannelExtraInfo(id, memberLimit) { } } +export function getTeamMembers(teamId) { + if (isCallInProgress('getTeamMembers')) { + return; + } + + callTracker.getTeamMembers = utils.getTimestamp(); + Client.getTeamMembers( + teamId, + (data) => { + callTracker.getTeamMembers = 0; + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_MEMBERS_FOR_TEAM, + team_members: data + }); + }, + (err) => { + callTracker.getTeamMembers = 0; + dispatchError(err, 'getTeamMembers'); + } + ); +} + export function getProfiles() { if (isCallInProgress('getProfiles')) { return; } callTracker.getProfiles = utils.getTimestamp(); - client.getProfiles( - function getProfilesSuccess(data, textStatus, xhr) { + Client.getProfiles( + (data) => { callTracker.getProfiles = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_PROFILES, profiles: data }); }, - function getProfilesFailure(err) { + (err) => { callTracker.getProfiles = 0; dispatchError(err, 'getProfiles'); } ); } +export function getDirectProfiles() { + if (isCallInProgress('getDirectProfiles')) { + return; + } + + callTracker.getDirectProfiles = utils.getTimestamp(); + Client.getDirectProfiles( + (data) => { + callTracker.getDirectProfiles = 0; + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_DIRECT_PROFILES, + profiles: data + }); + }, + (err) => { + callTracker.getDirectProfiles = 0; + dispatchError(err, 'getDirectProfiles'); + } + ); +} + export function getSessions() { if (isCallInProgress('getSessions')) { return; } callTracker.getSessions = utils.getTimestamp(); - client.getSessions( + Client.getSessions( UserStore.getCurrentId(), - function getSessionsSuccess(data, textStatus, xhr) { + (data) => { callTracker.getSessions = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_SESSIONS, sessions: data }); }, - function getSessionsFailure(err) { + (err) => { callTracker.getSessions = 0; dispatchError(err, 'getSessions'); } @@ -269,21 +297,17 @@ export function getAudits() { } callTracker.getAudits = utils.getTimestamp(); - client.getAudits( + Client.getAudits( UserStore.getCurrentId(), - function getAuditsSuccess(data, textStatus, xhr) { + (data) => { callTracker.getAudits = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_AUDITS, audits: data }); }, - function getAuditsFailure(err) { + (err) => { callTracker.getAudits = 0; dispatchError(err, 'getAudits'); } @@ -296,14 +320,10 @@ export function getLogs() { } callTracker.getLogs = utils.getTimestamp(); - client.getLogs( - (data, textStatus, xhr) => { + Client.getLogs( + (data) => { callTracker.getLogs = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_LOGS, logs: data @@ -322,14 +342,10 @@ export function getServerAudits() { } callTracker.getServerAudits = utils.getTimestamp(); - client.getServerAudits( - (data, textStatus, xhr) => { + Client.getServerAudits( + (data) => { callTracker.getServerAudits = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_SERVER_AUDITS, audits: data @@ -348,14 +364,10 @@ export function getComplianceReports() { } callTracker.getComplianceReports = utils.getTimestamp(); - client.getComplianceReports( - (data, textStatus, xhr) => { + Client.getComplianceReports( + (data) => { callTracker.getComplianceReports = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_SERVER_COMPLIANCE_REPORTS, complianceReports: data @@ -374,14 +386,10 @@ export function getConfig() { } callTracker.getConfig = utils.getTimestamp(); - client.getConfig( - (data, textStatus, xhr) => { + Client.getConfig( + (data) => { callTracker.getConfig = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_CONFIG, config: data @@ -400,14 +408,10 @@ export function getAllTeams() { } callTracker.getAllTeams = utils.getTimestamp(); - client.getAllTeams( - (data, textStatus, xhr) => { + Client.getAllTeams( + (data) => { callTracker.getAllTeams = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_ALL_TEAMS, teams: data @@ -420,27 +424,45 @@ export function getAllTeams() { ); } +export function getAllTeamListings() { + if (isCallInProgress('getAllTeamListings')) { + return; + } + + callTracker.getAllTeamListings = utils.getTimestamp(); + Client.getAllTeamListings( + (data) => { + callTracker.getAllTeamListings = 0; + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_ALL_TEAM_LISTINGS, + teams: data + }); + }, + (err) => { + callTracker.getAllTeams = 0; + dispatchError(err, 'getAllTeamListings'); + } + ); +} + export function search(terms) { if (isCallInProgress('search_' + String(terms))) { return; } callTracker['search_' + String(terms)] = utils.getTimestamp(); - client.search( + Client.search( terms, - function searchSuccess(data, textStatus, xhr) { + (data) => { callTracker['search_' + String(terms)] = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_SEARCH, results: data }); }, - function searchFailure(err) { + (err) => { callTracker['search_' + String(terms)] = 0; dispatchError(err, 'search'); } @@ -478,15 +500,11 @@ export function getPostsPage(id, maxPosts) { if (channelId != null) { callTracker['getPostsPage_' + channelId] = utils.getTimestamp(); - client.getPostsPage( + Client.getPostsPage( channelId, 0, numPosts, - (data, textStatus, xhr) => { - if (xhr.status === 304 || !data) { - return; - } - + (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_POSTS, id: channelId, @@ -536,14 +554,10 @@ export function getPosts(id) { callTracker['getPosts_' + channelId] = utils.getTimestamp(); - client.getPosts( + Client.getPosts( channelId, latestPostTime, - (data, textStatus, xhr) => { - if (xhr.status === 304 || !data) { - return; - } - + (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_POSTS, id: channelId, @@ -573,16 +587,12 @@ export function getPostsBefore(postId, offset, numPost) { return; } - client.getPostsBefore( + Client.getPostsBefore( channelId, postId, offset, numPost, - (data, textStatus, xhr) => { - if (xhr.status === 304 || !data) { - return; - } - + (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_POSTS, id: channelId, @@ -612,16 +622,12 @@ export function getPostsAfter(postId, offset, numPost) { return; } - client.getPostsAfter( + Client.getPostsAfter( channelId, postId, offset, numPost, - (data, textStatus, xhr) => { - if (xhr.status === 304 || !data) { - return; - } - + (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_POSTS, id: channelId, @@ -647,14 +653,10 @@ export function getMe() { } callTracker.getMe = utils.getTimestamp(); - return client.getMe( - (data, textStatus, xhr) => { + return Client.getMe( + (data) => { callTracker.getMe = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_ME, me: data @@ -684,14 +686,10 @@ export function getStatuses() { } callTracker.getStatuses = utils.getTimestamp(); - client.getStatuses(teammateIds, - (data, textStatus, xhr) => { + Client.getStatuses(teammateIds, + (data) => { callTracker.getStatuses = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_STATUSES, statuses: data @@ -710,20 +708,16 @@ export function getMyTeam() { } callTracker.getMyTeam = utils.getTimestamp(); - return client.getMyTeam( - function getMyTeamSuccess(data, textStatus, xhr) { + return Client.getMyTeam( + (data) => { callTracker.getMyTeam = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_MY_TEAM, team: data }); }, - function getMyTeamFailure(err) { + (err) => { callTracker.getMyTeam = 0; dispatchError(err, 'getMyTeam'); } @@ -736,14 +730,10 @@ export function getAllPreferences() { } callTracker.getAllPreferences = utils.getTimestamp(); - client.getAllPreferences( - (data, textStatus, xhr) => { + Client.getAllPreferences( + (data) => { callTracker.getAllPreferences = 0; - if (xhr.status === 304 || !data) { - return; - } - AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_PREFERENCES, preferences: data @@ -768,15 +758,13 @@ export function savePreference(category, name, value, success, error) { } export function savePreferences(preferences, success, error) { - client.savePreferences( + Client.savePreferences( preferences, - (data, textStatus, xhr) => { - if (xhr.status !== 304) { - AppDispatcher.handleServerAction({ - type: ActionTypes.RECEIVED_PREFERENCES, - preferences - }); - } + (data) => { + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_PREFERENCES, + preferences + }); if (success) { success(data); @@ -793,12 +781,12 @@ export function savePreferences(preferences, success, error) { } export function getSuggestedCommands(command, suggestionId, component) { - client.listCommands( + Client.listCommands( (data) => { var matches = []; data.forEach((cmd) => { if (('/' + cmd.trigger).indexOf(command) === 0) { - let s = '/' + cmd.trigger; + const s = '/' + cmd.trigger; let hint = ''; if (cmd.auto_complete_hint && cmd.auto_complete_hint.length !== 0) { hint = cmd.auto_complete_hint; @@ -842,7 +830,7 @@ export function getFileInfo(filename) { callTracker[callName] = utils.getTimestamp(); - client.getFileInfo( + Client.getFileInfo( filename, (data) => { callTracker[callName] = 0; @@ -870,7 +858,7 @@ export function getStandardAnalytics(teamId) { callTracker[callName] = utils.getTimestamp(); - client.getAnalytics( + Client.getAnalytics( 'standard', teamId, (data) => { @@ -923,7 +911,7 @@ export function getAdvancedAnalytics(teamId) { callTracker[callName] = utils.getTimestamp(); - client.getAnalytics( + Client.getAnalytics( 'extra_counts', teamId, (data) => { @@ -980,7 +968,7 @@ export function getPostsPerDayAnalytics(teamId) { callTracker[callName] = utils.getTimestamp(); - client.getAnalytics( + Client.getAnalytics( 'post_counts_day', teamId, (data) => { @@ -1014,7 +1002,7 @@ export function getUsersPerDayAnalytics(teamId) { callTracker[callName] = utils.getTimestamp(); - client.getAnalytics( + Client.getAnalytics( 'user_counts_with_posts_day', teamId, (data) => { @@ -1048,7 +1036,7 @@ export function getRecentAndNewUsersAnalytics(teamId) { callTracker[callName] = utils.getTimestamp(); - client.getProfilesForTeam( + Client.getProfilesForTeam( teamId, (users) => { const stats = {}; @@ -1129,7 +1117,7 @@ export function listIncomingHooks() { callTracker.listIncomingHooks = utils.getTimestamp(); - client.listIncomingHooks( + Client.listIncomingHooks( (data) => { callTracker.listIncomingHooks = 0; @@ -1152,7 +1140,7 @@ export function listOutgoingHooks() { callTracker.listOutgoingHooks = utils.getTimestamp(); - client.listOutgoingHooks( + Client.listOutgoingHooks( (data) => { callTracker.listOutgoingHooks = 0; @@ -1169,7 +1157,7 @@ export function listOutgoingHooks() { } export function addIncomingHook(hook, success, error) { - client.addIncomingHook( + Client.addIncomingHook( hook, (data) => { AppDispatcher.handleServerAction({ @@ -1192,7 +1180,7 @@ export function addIncomingHook(hook, success, error) { } export function addOutgoingHook(hook, success, error) { - client.addOutgoingHook( + Client.addOutgoingHook( hook, (data) => { AppDispatcher.handleServerAction({ @@ -1215,8 +1203,8 @@ export function addOutgoingHook(hook, success, error) { } export function deleteIncomingHook(id) { - client.deleteIncomingHook( - {id}, + Client.deleteIncomingHook( + id, () => { AppDispatcher.handleServerAction({ type: ActionTypes.REMOVED_INCOMING_WEBHOOK, @@ -1230,8 +1218,8 @@ export function deleteIncomingHook(id) { } export function deleteOutgoingHook(id) { - client.deleteOutgoingHook( - {id}, + Client.deleteOutgoingHook( + id, () => { AppDispatcher.handleServerAction({ type: ActionTypes.REMOVED_OUTGOING_WEBHOOK, @@ -1245,8 +1233,8 @@ export function deleteOutgoingHook(id) { } export function regenOutgoingHookToken(id) { - client.regenOutgoingHookToken( - {id}, + Client.regenOutgoingHookToken( + id, (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.UPDATED_OUTGOING_WEBHOOK, @@ -1266,7 +1254,7 @@ export function listTeamCommands() { callTracker.listTeamCommands = utils.getTimestamp(); - client.listTeamCommands( + Client.listTeamCommands( (data) => { callTracker.listTeamCommands = 0; @@ -1283,7 +1271,7 @@ export function listTeamCommands() { } export function addCommand(command, success, error) { - client.addCommand( + Client.addCommand( command, (data) => { AppDispatcher.handleServerAction({ @@ -1306,8 +1294,8 @@ export function addCommand(command, success, error) { } export function deleteCommand(id) { - client.deleteCommand( - {id}, + Client.deleteCommand( + id, () => { AppDispatcher.handleServerAction({ type: ActionTypes.REMOVED_COMMAND, @@ -1321,8 +1309,8 @@ export function deleteCommand(id) { } export function regenCommandToken(id) { - client.regenCommandToken( - {id}, + Client.regenCommandToken( + id, (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.UPDATED_COMMAND, diff --git a/webapp/utils/channel_intro_messages.jsx b/webapp/utils/channel_intro_messages.jsx index ddd615581..1d18e26ba 100644 --- a/webapp/utils/channel_intro_messages.jsx +++ b/webapp/utils/channel_intro_messages.jsx @@ -9,6 +9,7 @@ import UserProfile from 'components/user_profile.jsx'; import ChannelStore from 'stores/channel_store.jsx'; import Constants from 'utils/constants.jsx'; import * as GlobalActions from 'action_creators/global_actions.jsx'; +import Client from 'utils/web_client.jsx'; import React from 'react'; import {FormattedMessage, FormattedHTMLMessage, FormattedDate} from 'react-intl'; @@ -40,7 +41,7 @@ export function createDMIntroMessage(channel) {
diff --git a/webapp/utils/client.jsx b/webapp/utils/client.jsx deleted file mode 100644 index 687d47da4..000000000 --- a/webapp/utils/client.jsx +++ /dev/null @@ -1,1759 +0,0 @@ -// See License.txt for license information. - -import BrowserStore from 'stores/browser_store.jsx'; -import $ from 'jquery'; - -import {browserHistory} from 'react-router'; - -let translations = { - connectionError: 'There appears to be a problem with your internet connection.', - unknownError: 'We received an unexpected status code from the server.' -}; - -export function setTranslations(messages) { - translations = messages; -} - -export function track(category, action, label, property, value) { - global.window.analytics.track(action, {category, label, property, value}); -} - -export function trackPage() { - global.window.analytics.page(); -} - -function handleError(methodName, xhr, status, err) { - var e = null; - try { - e = JSON.parse(xhr.responseText); - } catch (parseError) { - e = null; - } - - var msg = ''; - - if (e) { - msg = 'method=' + methodName + ' msg=' + e.message + ' detail=' + e.detailed_error + ' rid=' + e.request_id; - } else { - msg = 'method=' + methodName + ' status=' + status + ' statusCode=' + xhr.status + ' err=' + err; - - if (xhr.status === 0) { - e = {message: translations.connectionError}; - } else { - e = {message: translations.unknownError + ' (' + xhr.status + ')'}; - } - } - - console.error(msg); //eslint-disable-line no-console - console.error(e); //eslint-disable-line no-console - - track('api', 'api_weberror', methodName, 'message', msg); - - if (xhr.status === 401) { - const team = window.location.pathname.split('/')[1]; - browserHistory.push('/' + team + '/login?extra=expired&redirect=' + encodeURIComponent(window.location.pathname + window.location.search)); - } - - return e; -} - -export function getTranslations(url, success, error) { - $.ajax({ - url: url, - dataType: 'json', - success, - error: function onError(xhr, status, err) { - var e = handleError('getTranslations', xhr, status, err); - error(e); - } - }); -} - -export function createTeamFromSignup(teamSignup, success, error) { - $.ajax({ - url: '/api/v1/teams/create_from_signup', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(teamSignup), - success, - error: function onError(xhr, status, err) { - var e = handleError('createTeamFromSignup', xhr, status, err); - error(e); - } - }); -} - -export function createTeamWithLdap(teamSignup, success, error) { - $.ajax({ - url: '/api/v1/teams/create_with_ldap', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(teamSignup), - success, - error: function onError(xhr, status, err) { - var e = handleError('createTeamFromSignup', xhr, status, err); - error(e); - } - }); -} - -export function createTeamWithSSO(team, service, success, error) { - $.ajax({ - url: '/api/v1/teams/create_with_sso/' + service, - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(team), - success, - error: function onError(xhr, status, err) { - var e = handleError('createTeamWithSSO', xhr, status, err); - error(e); - } - }); -} - -export function createUser(user, data, emailHash, success, error) { - $.ajax({ - url: '/api/v1/users/create?d=' + encodeURIComponent(data) + '&h=' + encodeURIComponent(emailHash), - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(user), - success, - error: function onError(xhr, status, err) { - var e = handleError('createUser', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_create', user.team_id, 'email', user.email); -} - -export function updateUser(user, success, error) { - $.ajax({ - url: '/api/v1/users/update', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(user), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateUser', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_update'); -} - -export function updatePassword(data, success, error) { - $.ajax({ - url: '/api/v1/users/newpassword', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('newPassword', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_newpassword'); -} - -export function updateUserNotifyProps(data, success, error) { - $.ajax({ - url: '/api/v1/users/update_notify', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateUserNotifyProps', xhr, status, err); - error(e); - } - }); -} - -export function updateRoles(data, success, error) { - $.ajax({ - url: '/api/v1/users/update_roles', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateRoles', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_update_roles'); -} - -export function updateActive(userId, active, success, error) { - var data = {}; - data.user_id = userId; - data.active = '' + active; - - $.ajax({ - url: '/api/v1/users/update_active', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateActive', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_update_roles'); -} - -export function sendPasswordReset(data, success, error) { - $.ajax({ - url: '/api/v1/users/send_password_reset', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('sendPasswordReset', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_send_password_reset'); -} - -export function resetPassword(data, success, error) { - $.ajax({ - url: '/api/v1/users/reset_password', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('resetPassword', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_reset_password'); -} - -export function emailToOAuth(data, success, error) { - $.ajax({ - url: '/api/v1/users/claim/email_to_oauth', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('emailToOAuth', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_email_to_oauth'); -} - -export function oauthToEmail(data, success, error) { - $.ajax({ - url: '/api/v1/users/claim/oauth_to_email', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('oauthToEmail', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_oauth_to_email'); -} - -export function emailToLDAP(data, success, error) { - $.ajax({ - url: '/api/v1/users/claim/email_to_ldap', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('emailToLDAP', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_email_to_ldap'); -} - -export function ldapToEmail(data, success, error) { - $.ajax({ - url: '/api/v1/users/claim/ldap_to_email', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('ldapToEmail', xhr, status, err); - error(e); - } - }); - - track('api', 'api_users_ldap_to_email'); -} - -export function logout(success, error) { - track('api', 'api_users_logout'); - $.ajax({ - url: '/api/v1/users/logout', - type: 'POST', - success, - error: function onError(xhr, status, err) { - var e = handleError('logout', xhr, status, err); - error(e); - } - }); -} - -export function checkMfa(method, team, loginId, success, error) { - $.ajax({ - url: '/api/v1/users/mfa', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({method, team_name: team, login_id: loginId}), - success, - error: function onError(xhr, status, err) { - var e = handleError('checkMfa', xhr, status, err); - error(e); - } - }); -} - -export function loginByEmail(name, email, password, token, success, error) { - $.ajax({ - url: '/api/v1/users/login', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({name, email, password, token}), - success: function onSuccess(data, textStatus, xhr) { - track('api', 'api_users_login_success', data.team_id, 'email', data.email); - sessionStorage.removeItem(data.id + '_last_error'); - BrowserStore.signalLogin(); - success(data, textStatus, xhr); - }, - error: function onError(xhr, status, err) { - track('api', 'api_users_login_fail', name, 'email', email); - - var e = handleError('loginByEmail', xhr, status, err); - error(e); - } - }); -} - -export function loginByUsername(name, username, password, success, error) { - $.ajax({ - url: '/api/v1/users/login', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({name, username, password}), - success: function onSuccess(data, textStatus, xhr) { - track('api', 'api_users_login_success', data.team_id, 'username', data.username); - sessionStorage.removeItem(data.id + '_last_error'); - BrowserStore.signalLogin(); - success(data, textStatus, xhr); - }, - error: function onError(xhr, status, err) { - track('api', 'api_users_login_fail', name, 'username', username); - - var e = handleError('loginByUsername', xhr, status, err); - error(e); - } - }); -} - -export function loginByLdap(teamName, id, password, token, success, error) { - $.ajax({ - url: '/api/v1/users/login_ldap', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({teamName, id, password, token}), - success: function onSuccess(data, textStatus, xhr) { - track('api', 'api_users_loginLdap_success', data.team_id, 'id', id); - sessionStorage.removeItem(data.id + '_last_error'); - BrowserStore.signalLogin(); - success(data, textStatus, xhr); - }, - error: function onError(xhr, status, err) { - track('api', 'api_users_loginLdap_fail', teamName, 'id', id); - - var e = handleError('loginByLdap', xhr, status, err); - error(e); - } - }); -} - -export function revokeSession(altId, success, error) { - $.ajax({ - url: '/api/v1/users/revoke_session', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({id: altId}), - success, - error: function onError(xhr, status, err) { - var e = handleError('revokeSession', xhr, status, err); - error(e); - } - }); -} - -export function getSessions(userId, success, error) { - $.ajax({ - cache: false, - url: '/api/v1/users/' + userId + '/sessions', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getSessions', xhr, status, err); - error(e); - } - }); -} - -export function getAudits(userId, success, error) { - $.ajax({ - url: '/api/v1/users/' + userId + '/audits', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getAudits', xhr, status, err); - error(e); - } - }); -} - -export function getComplianceReports(success, error) { - $.ajax({ - url: '/api/v1/admin/compliance_reports', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getComplianceReports', xhr, status, err); - error(e); - } - }); -} - -export function saveComplianceReports(job, success, error) { - $.ajax({ - url: '/api/v1/admin/save_compliance_report', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(job), - success, - error: (xhr, status, err) => { - var e = handleError('saveComplianceReports', xhr, status, err); - error(e); - } - }); -} - -export function getLogs(success, error) { - $.ajax({ - url: '/api/v1/admin/logs', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getLogs', xhr, status, err); - error(e); - } - }); -} - -export function getServerAudits(success, error) { - $.ajax({ - url: '/api/v1/admin/audits', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getServerAudits', xhr, status, err); - error(e); - } - }); -} - -export function getConfig(success, error) { - return $.ajax({ - url: '/api/v1/admin/config', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getConfig', xhr, status, err); - error(e); - } - }); -} - -export function getAnalytics(name, teamId, success, error) { - let url = '/api/v1/admin/analytics/'; - if (teamId == null) { - url += name; - } else { - url += teamId + '/' + name; - } - $.ajax({ - url, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('getSystemAnalytics', xhr, status, err); - error(e); - } - }); -} - -export function getClientConfig(success, error) { - return $.ajax({ - url: '/api/v1/admin/client_props', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getClientConfig', xhr, status, err); - error(e); - } - }); -} - -export function getTeamAnalytics(teamId, name, success, error) { - $.ajax({ - url: '/api/v1/admin/analytics/' + teamId + '/' + name, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('getTeamAnalytics', xhr, status, err); - error(e); - } - }); -} - -export function saveConfig(config, success, error) { - $.ajax({ - url: '/api/v1/admin/save_config', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(config), - success, - error: function onError(xhr, status, err) { - var e = handleError('saveConfig', xhr, status, err); - error(e); - } - }); -} - -export function logClientError(msg) { - var l = {}; - l.level = 'ERROR'; - l.message = msg; - - $.ajax({ - url: '/api/v1/admin/log_client', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(l) - }); -} - -export function testEmail(config, success, error) { - $.ajax({ - url: '/api/v1/admin/test_email', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(config), - success, - error: function onError(xhr, status, err) { - var e = handleError('testEmail', xhr, status, err); - error(e); - } - }); -} - -export function getAllTeams(success, error) { - $.ajax({ - url: '/api/v1/teams/all', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getAllTeams', xhr, status, err); - error(e); - } - }); -} - -export function getMeLoggedIn(success, error) { - return $.ajax({ - cache: false, - url: '/api/v1/users/me_logged_in', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getMeLoggedIn', xhr, status, err); - error(e); - } - }); -} - -export function getMe(success, error) { - var currentUser = null; - $.ajax({ - cache: false, - url: '/api/v1/users/me', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success: function gotUser(data, textStatus, xhr) { - currentUser = data; - if (success) { - success(data, textStatus, xhr); - } - }, - error: function onError(xhr, status, err) { - if (error) { - var e = handleError('getMe', xhr, status, err); - error(e); - } - } - }); - - return currentUser; -} - -export function inviteMembers(data, success, error) { - $.ajax({ - url: '/api/v1/teams/invite_members', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('inviteMembers', xhr, status, err); - error(e); - } - }); - - track('api', 'api_teams_invite_members'); -} - -export function updateTeam(team, success, error) { - $.ajax({ - url: '/api/v1/teams/update', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(team), - success, - error: (xhr, status, err) => { - var e = handleError('updateTeam', xhr, status, err); - error(e); - } - }); - - track('api', 'api_teams_update_name'); -} - -export function signupTeam(email, success, error) { - $.ajax({ - url: '/api/v1/teams/signup', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({email: email}), - success, - error: function onError(xhr, status, err) { - var e = handleError('singupTeam', xhr, status, err); - error(e); - } - }); - - track('api', 'api_teams_signup'); -} - -export function createTeam(team, success, error) { - $.ajax({ - url: '/api/v1/teams/create', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(team), - success, - error: function onError(xhr, status, err) { - var e = handleError('createTeam', xhr, status, err); - error(e); - } - }); -} - -export function findTeamByName(teamName, success, error) { - $.ajax({ - url: '/api/v1/teams/find_team_by_name', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({name: teamName}), - success, - error: function onError(xhr, status, err) { - var e = handleError('findTeamByName', xhr, status, err); - error(e); - } - }); -} - -export function createChannel(channel, success, error) { - $.ajax({ - url: '/api/v1/channels/create', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(channel), - success, - error: function onError(xhr, status, err) { - var e = handleError('createChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_create'); -} - -export function createDirectChannel(channel, userId, success, error) { - $.ajax({ - url: '/api/v1/channels/create_direct', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({user_id: userId}), - success, - error: function onError(xhr, status, err) { - var e = handleError('createDirectChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_create_direct'); -} - -export function updateChannel(channel, success, error) { - $.ajax({ - url: '/api/v1/channels/update', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(channel), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_update'); -} - -export function updateChannelHeader(channelId, header, success, error) { - const data = { - channel_id: channelId, - channel_header: header - }; - - $.ajax({ - url: '/api/v1/channels/update_header', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateChannelHeader', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_header'); -} - -export function updateChannelPurpose(data, success, error) { - $.ajax({ - url: '/api/v1/channels/update_purpose', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateChannelPurpose', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_purpose'); -} - -export function updateNotifyProps(data, success, error) { - $.ajax({ - url: '/api/v1/channels/update_notify_props', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('updateNotifyProps', xhr, status, err); - error(e); - } - }); -} - -export function joinChannel(id, success, error) { - $.ajax({ - url: '/api/v1/channels/' + id + '/join', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - success, - error: function onError(xhr, status, err) { - var e = handleError('joinChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_join'); -} - -export function leaveChannel(id, success, error) { - $.ajax({ - url: '/api/v1/channels/' + id + '/leave', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - success, - error: function onError(xhr, status, err) { - var e = handleError('leaveChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_leave'); -} - -export function deleteChannel(id, success, error) { - $.ajax({ - url: '/api/v1/channels/' + id + '/delete', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - success, - error: function onError(xhr, status, err) { - var e = handleError('deleteChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_delete'); -} - -export function updateLastViewedAt(channelId, success, error) { - $.ajax({ - url: '/api/v1/channels/' + channelId + '/update_last_viewed_at', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - success, - error: function onError(xhr, status, err) { - var e = handleError('updateLastViewedAt', xhr, status, err); - error(e); - } - }); -} - -export function getChannels(success, error) { - return $.ajax({ - cache: false, - url: '/api/v1/channels/', - dataType: 'json', - type: 'GET', - success, - ifModified: true, - error: function onError(xhr, status, err) { - var e = handleError('getChannels', xhr, status, err); - error(e); - } - }); -} - -export function getChannel(id, success, error) { - $.ajax({ - cache: false, - url: '/api/v1/channels/' + id + '/', - dataType: 'json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getChannel', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channel_get'); -} - -export function getMoreChannels(success, error) { - $.ajax({ - url: '/api/v1/channels/more', - dataType: 'json', - type: 'GET', - success, - ifModified: true, - error: function onError(xhr, status, err) { - var e = handleError('getMoreChannels', xhr, status, err); - error(e); - } - }); -} - -export function getChannelCounts(success, error) { - $.ajax({ - cache: false, - url: '/api/v1/channels/counts', - dataType: 'json', - type: 'GET', - success, - ifModified: true, - error: function onError(xhr, status, err) { - var e = handleError('getChannelCounts', xhr, status, err); - error(e); - } - }); -} - -export function getChannelExtraInfo(id, memberLimit, success, error) { - let url = '/api/v1/channels/' + id + '/extra_info'; - - if (memberLimit) { - url += '/' + memberLimit; - } - - return $.ajax({ - url, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getChannelExtraInfo', xhr, status, err); - error(e); - } - }); -} - -export function executeCommand(channelId, command, suggest, success, error) { - $.ajax({ - url: '/api/v1/commands/execute', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify({channelId, command, suggest: '' + suggest}), - success, - error: function onError(xhr, status, err) { - var e = handleError('executeCommand', xhr, status, err); - error(e); - } - }); -} - -export function addCommand(cmd, success, error) { - $.ajax({ - url: '/api/v1/commands/create', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(cmd), - success, - error: (xhr, status, err) => { - var e = handleError('addCommand', xhr, status, err); - error(e); - } - }); -} - -export function deleteCommand(data, success, error) { - $.ajax({ - url: '/api/v1/commands/delete', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: (xhr, status, err) => { - var e = handleError('deleteCommand', xhr, status, err); - error(e); - } - }); -} - -export function listTeamCommands(success, error) { - $.ajax({ - url: '/api/v1/commands/list_team_commands', - dataType: 'json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('listTeamCommands', xhr, status, err); - error(e); - } - }); -} - -export function regenCommandToken(data, success, error) { - $.ajax({ - url: '/api/v1/commands/regen_token', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: (xhr, status, err) => { - var e = handleError('regenCommandToken', xhr, status, err); - error(e); - } - }); -} - -export function listCommands(success, error) { - $.ajax({ - url: '/api/v1/commands/list', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('listCommands', xhr, status, err); - error(e); - } - }); -} - -export function getPostsPage(channelId, offset, limit, success, error, complete) { - $.ajax({ - cache: false, - url: '/api/v1/channels/' + channelId + '/posts/' + offset + '/' + limit, - dataType: 'json', - type: 'GET', - ifModified: true, - success, - error: function onError(xhr, status, err) { - var e = handleError('getPosts', xhr, status, err); - error(e); - }, - complete: complete - }); -} - -export function getPosts(channelId, since, success, error, complete) { - return $.ajax({ - url: '/api/v1/channels/' + channelId + '/posts/' + since, - dataType: 'json', - type: 'GET', - ifModified: true, - success, - error: function onError(xhr, status, err) { - var e = handleError('getPosts', xhr, status, err); - error(e); - }, - complete: complete - }); -} - -export function getPostsBefore(channelId, post, offset, numPost, success, error, complete) { - $.ajax({ - url: '/api/v1/channels/' + channelId + '/post/' + post + '/before/' + offset + '/' + numPost, - dataType: 'json', - type: 'GET', - ifModified: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('getPostsBefore', xhr, status, err); - error(e); - }, - complete: complete - }); -} - -export function getPostsAfter(channelId, post, offset, numPost, success, error, complete) { - $.ajax({ - url: '/api/v1/channels/' + channelId + '/post/' + post + '/after/' + offset + '/' + numPost, - dataType: 'json', - type: 'GET', - ifModified: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('getPostsAfter', xhr, status, err); - error(e); - }, - complete: complete - }); -} - -export function getPost(channelId, postId, success, error, complete) { - $.ajax({ - cache: false, - url: '/api/v1/channels/' + channelId + '/post/' + postId, - dataType: 'json', - type: 'GET', - ifModified: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('getPost', xhr, status, err); - error(e); - }, - complete - }); -} - -export function getPostById(postId, success, error, complete) { - $.ajax({ - cache: false, - url: '/api/v1/posts/' + postId, - dataType: 'json', - type: 'GET', - ifModified: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('getPostById', xhr, status, err); - error(e); - }, - complete - }); -} - -export function search(terms, success, error) { - $.ajax({ - url: '/api/v1/posts/search', - dataType: 'json', - type: 'GET', - data: {terms: terms}, - success, - error: function onError(xhr, status, err) { - var e = handleError('search', xhr, status, err); - error(e); - } - }); - - track('api', 'api_posts_search'); -} - -export function deletePost(channelId, id, success, error) { - $.ajax({ - url: '/api/v1/channels/' + channelId + '/post/' + id + '/delete', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - success, - error: function onError(xhr, status, err) { - var e = handleError('deletePost', xhr, status, err); - error(e); - } - }); - - track('api', 'api_posts_delete'); -} - -export function createPost(post, channel, success, error) { - $.ajax({ - url: '/api/v1/channels/' + post.channel_id + '/create', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(post), - success, - error: function onError(xhr, status, err) { - var e = handleError('createPost', xhr, status, err); - error(e); - } - }); - - track('api', 'api_posts_create', channel.name, 'length', post.message.length); - - // global.window.analytics.track('api_posts_create', { - // category: 'api', - // channel_name: channel.name, - // channel_type: channel.type, - // length: post.message.length, - // files: (post.filenames || []).length, - // mentions: (post.message.match('//g') || []).length - // }); -} - -export function updatePost(post, success, error) { - $.ajax({ - url: '/api/v1/channels/' + post.channel_id + '/update', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(post), - success, - error: function onError(xhr, status, err) { - var e = handleError('updatePost', xhr, status, err); - error(e); - } - }); - - track('api', 'api_posts_update'); -} - -export function addChannelMember(id, data, success, error) { - $.ajax({ - url: '/api/v1/channels/' + id + '/add', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('addChannelMember', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_add_member'); -} - -export function removeChannelMember(id, data, success, error) { - $.ajax({ - url: '/api/v1/channels/' + id + '/remove', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('removeChannelMember', xhr, status, err); - error(e); - } - }); - - track('api', 'api_channels_remove_member'); -} - -export function getProfiles(success, error) { - $.ajax({ - cache: false, - url: '/api/v1/users/profiles', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - ifModified: true, - error: function onError(xhr, status, err) { - var e = handleError('getProfiles', xhr, status, err); - error(e); - } - }); -} - -export function getProfilesForTeam(teamId, success, error) { - $.ajax({ - cache: false, - url: '/api/v1/users/profiles/' + teamId, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getProfilesForTeam', xhr, status, err); - error(e); - } - }); -} - -export function uploadFile(formData, success, error) { - var request = $.ajax({ - url: '/api/v1/files/upload', - type: 'POST', - data: formData, - cache: false, - contentType: false, - processData: false, - success, - error: function onError(xhr, status, err) { - if (err !== 'abort') { - var e = handleError('uploadFile', xhr, status, err); - error(e); - } - } - }); - - track('api', 'api_files_upload'); - - return request; -} - -export function getFileInfo(filename, success, error) { - $.ajax({ - url: '/api/v1/files/get_info' + filename, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success: (data) => { - success(data); - }, - error: function onError(xhr, status, err) { - var e = handleError('getFileInfo', xhr, status, err); - error(e); - } - }); -} - -export function getPublicLink(data, success, error) { - $.ajax({ - url: '/api/v1/files/get_public_link', - dataType: 'json', - type: 'POST', - data: JSON.stringify(data), - success, - error: function onError(xhr, status, err) { - var e = handleError('getPublicLink', xhr, status, err); - error(e); - } - }); -} - -export function uploadProfileImage(imageData, success, error) { - $.ajax({ - url: '/api/v1/users/newimage', - type: 'POST', - data: imageData, - cache: false, - contentType: false, - processData: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('uploadProfileImage', xhr, status, err); - error(e); - } - }); -} - -export function importSlack(fileData, success, error) { - $.ajax({ - url: '/api/v1/teams/import_team', - type: 'POST', - data: fileData, - cache: false, - contentType: false, - processData: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('importTeam', xhr, status, err); - error(e); - } - }); -} - -export function exportTeam(success, error) { - $.ajax({ - url: '/api/v1/teams/export_team', - type: 'GET', - dataType: 'json', - success, - error: function onError(xhr, status, err) { - var e = handleError('exportTeam', xhr, status, err); - error(e); - } - }); -} - -export function getStatuses(ids, success, error) { - $.ajax({ - url: '/api/v1/users/status', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(ids), - success, - error: function onError(xhr, status, err) { - var e = handleError('getStatuses', xhr, status, err); - error(e); - } - }); -} - -export function getMyTeam(success, error) { - return $.ajax({ - url: '/api/v1/teams/me', - dataType: 'json', - type: 'GET', - success, - ifModified: true, - error: function onError(xhr, status, err) { - var e = handleError('getMyTeam', xhr, status, err); - error(e); - } - }); -} - -export function registerOAuthApp(app, success, error) { - $.ajax({ - url: '/api/v1/oauth/register', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(app), - success: success, - error: (xhr, status, err) => { - const e = handleError('registerApp', xhr, status, err); - error(e); - } - }); - - module.exports.track('api', 'api_apps_register'); -} - -export function allowOAuth2(responseType, clientId, redirectUri, state, scope, success, error) { - $.ajax({ - url: '/api/v1/oauth/allow?response_type=' + responseType + '&client_id=' + clientId + '&redirect_uri=' + redirectUri + '&scope=' + scope + '&state=' + state, - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: (xhr, status, err) => { - const e = handleError('allowOAuth2', xhr, status, err); - error(e); - } - }); - - module.exports.track('api', 'api_users_allow_oauth2'); -} - -export function addIncomingHook(hook, success, error) { - $.ajax({ - url: '/api/v1/hooks/incoming/create', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(hook), - success, - error: (xhr, status, err) => { - var e = handleError('addIncomingHook', xhr, status, err); - error(e); - } - }); -} - -export function deleteIncomingHook(data, success, error) { - $.ajax({ - url: '/api/v1/hooks/incoming/delete', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: (xhr, status, err) => { - var e = handleError('deleteIncomingHook', xhr, status, err); - error(e); - } - }); -} - -export function listIncomingHooks(success, error) { - $.ajax({ - url: '/api/v1/hooks/incoming/list', - dataType: 'json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('listIncomingHooks', xhr, status, err); - error(e); - } - }); -} - -export function getAllPreferences(success, error) { - return $.ajax({ - url: '/api/v1/preferences/', - dataType: 'json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('getAllPreferences', xhr, status, err); - error(e); - } - }); -} - -export function getPreferenceCategory(category, success, error) { - $.ajax({ - url: `/api/v1/preferences/${category}`, - dataType: 'json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('getPreferenceCategory', xhr, status, err); - error(e); - } - }); -} - -export function savePreferences(preferences, success, error) { - $.ajax({ - url: '/api/v1/preferences/save', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(preferences), - success, - error: (xhr, status, err) => { - var e = handleError('savePreferences', xhr, status, err); - error(e); - } - }); -} - -export function addOutgoingHook(hook, success, error) { - $.ajax({ - url: '/api/v1/hooks/outgoing/create', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(hook), - success, - error: (xhr, status, err) => { - var e = handleError('addOutgoingHook', xhr, status, err); - error(e); - } - }); -} - -export function deleteOutgoingHook(data, success, error) { - $.ajax({ - url: '/api/v1/hooks/outgoing/delete', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: (xhr, status, err) => { - var e = handleError('deleteOutgoingHook', xhr, status, err); - error(e); - } - }); -} - -export function listOutgoingHooks(success, error) { - $.ajax({ - url: '/api/v1/hooks/outgoing/list', - dataType: 'json', - type: 'GET', - success, - error: (xhr, status, err) => { - var e = handleError('listOutgoingHooks', xhr, status, err); - error(e); - } - }); -} - -export function regenOutgoingHookToken(data, success, error) { - $.ajax({ - url: '/api/v1/hooks/outgoing/regen_token', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: (xhr, status, err) => { - var e = handleError('regenOutgoingHookToken', xhr, status, err); - error(e); - } - }); -} - -export function uploadLicenseFile(formData, success, error) { - $.ajax({ - url: '/api/v1/license/add', - type: 'POST', - data: formData, - cache: false, - contentType: false, - processData: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('uploadLicenseFile', xhr, status, err); - error(e); - } - }); - - track('api', 'api_license_upload'); -} - -export function removeLicenseFile(success, error) { - $.ajax({ - url: '/api/v1/license/remove', - type: 'POST', - cache: false, - contentType: false, - processData: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('removeLicenseFile', xhr, status, err); - error(e); - } - }); - - track('api', 'api_license_upload'); -} - -export function getClientLicenceConfig(success, error) { - return $.ajax({ - url: '/api/v1/license/client_config', - dataType: 'json', - contentType: 'application/json', - type: 'GET', - success, - error: function onError(xhr, status, err) { - var e = handleError('getClientLicenceConfig', xhr, status, err); - error(e); - } - }); -} - -export function getInviteInfo(success, error, id) { - $.ajax({ - url: '/api/v1/teams/get_invite_info', - type: 'POST', - dataType: 'json', - contentType: 'application/json', - data: JSON.stringify({invite_id: id}), - success, - error: function onError(xhr, status, err) { - var e = handleError('getInviteInfo', xhr, status, err); - if (error) { - error(e); - } - } - }); -} - -export function verifyEmail(success, error, uid, hid) { - $.ajax({ - url: '/api/v1/users/verify_email', - type: 'POST', - contentType: 'application/json', - dataType: 'text', - data: JSON.stringify({uid, hid}), - success, - error: function onError(xhr, status, err) { - var e = handleError('verifyEmail', xhr, status, err); - if (error) { - error(e); - } - } - }); -} - -export function resendVerification(success, error, teamName, email) { - $.ajax({ - url: '/api/v1/users/resend_verification', - type: 'POST', - contentType: 'application/json', - dataType: 'text', - data: JSON.stringify({team_name: teamName, email}), - success, - error: function onError(xhr, status, err) { - var e = handleError('resendVerification', xhr, status, err); - if (error) { - error(e); - } - } - }); -} - -export function updateMfa(data, success, error) { - $.ajax({ - url: '/api/v1/users/update_mfa', - dataType: 'json', - contentType: 'application/json', - type: 'POST', - data: JSON.stringify(data), - success, - error: (xhr, status, err) => { - var e = handleError('updateMfa', xhr, status, err); - error(e); - } - }); -} - -export function uploadBrandImage(image, success, error) { - const formData = new FormData(); - formData.append('image', image, image.name); - - $.ajax({ - url: '/api/v1/admin/upload_brand_image', - type: 'POST', - data: formData, - cache: false, - contentType: false, - processData: false, - success, - error: function onError(xhr, status, err) { - var e = handleError('uploadBrandImage', xhr, status, err); - error(e); - } - }); -} diff --git a/webapp/utils/constants.jsx b/webapp/utils/constants.jsx index 87f4153fb..9bdf348cd 100644 --- a/webapp/utils/constants.jsx +++ b/webapp/utils/constants.jsx @@ -61,6 +61,7 @@ export default { RECEIVED_ADD_MENTION: null, RECEIVED_PROFILES: null, + RECEIVED_DIRECT_PROFILES: null, RECEIVED_ME: null, RECEIVED_SESSIONS: null, RECEIVED_AUDITS: null, @@ -92,6 +93,9 @@ export default { RECEIVED_SERVER_AUDITS: null, RECEIVED_SERVER_COMPLIANCE_REPORTS: null, RECEIVED_ALL_TEAMS: null, + RECEIVED_ALL_TEAM_LISTINGS: null, + RECEIVED_TEAM_MEMBERS: null, + RECEIVED_MEMBERS_FOR_TEAM: null, RECEIVED_LOCALE: null, @@ -205,6 +209,7 @@ export default { LDAP_SERVICE: 'ldap', USERNAME_SERVICE: 'username', SIGNIN_CHANGE: 'signin_change', + PASSWORD_CHANGE: 'password_change', SIGNIN_VERIFIED: 'verified', SESSION_EXPIRED: 'expired', POST_CHUNK_SIZE: 60, diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx index c4f4a025e..a1e16928b 100644 --- a/webapp/utils/utils.jsx +++ b/webapp/utils/utils.jsx @@ -10,9 +10,8 @@ import PreferenceStore from 'stores/preference_store.jsx'; import TeamStore from 'stores/team_store.jsx'; import Constants from 'utils/constants.jsx'; var ActionTypes = Constants.ActionTypes; -import * as Client from './client.jsx'; import * as AsyncClient from './async_client.jsx'; -import * as client from './client.jsx'; +import Client from './web_client.jsx'; import React from 'react'; import {browserHistory} from 'react-router'; @@ -1114,7 +1113,7 @@ export function fileSizeToString(bytes) { // Converts a filename (like those attached to Post objects) to a url that can be used to retrieve attachments from the server. export function getFileUrl(filename) { - return getWindowLocationOrigin() + '/api/v1/files/get' + filename; + return getWindowLocationOrigin() + Client.getFilesRoute() + '/get' + filename; } // Gets the name of a file (including extension) from a given url or file path. @@ -1210,13 +1209,17 @@ export function importSlack(file, success, error) { formData.append('filesize', file.size); formData.append('importFrom', 'slack'); - client.importSlack(formData, success, error); + Client.importSlack(formData, success, error); } export function getTeamURLFromAddressBar() { return window.location.origin + '/' + window.location.pathname.split('/')[1]; } +export function getTeamNameFromUrl() { + return window.location.pathname.split('/')[1]; +} + export function getTeamURLNoOriginFromAddressBar() { return '/' + window.location.pathname.split('/')[1]; } @@ -1263,16 +1266,11 @@ export function openDirectChannelToUser(user, successCb, errorCb) { }; Client.createDirectChannel( - channel, user.id, (data) => { Client.getChannel( data.id, - (data2, textStatus, xhr) => { - if (xhr.status === 304 || !data2) { - return; - } - + (data2) => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_CHANNEL, channel: data2.channel, @@ -1400,7 +1398,7 @@ export function localizeMessage(id, defaultMessage) { } export function getProfilePicSrcForPost(post, timestamp) { - let src = '/api/v1/users/' + post.user_id + '/image?time=' + timestamp; + let src = Client.getUsersRoute() + '/' + post.user_id + '/image?time=' + timestamp; if (post.props && post.props.from_webhook && global.window.mm_config.EnablePostIconOverride === 'true') { if (post.props.override_icon_url) { src = post.props.override_icon_url; diff --git a/webapp/utils/web_client.jsx b/webapp/utils/web_client.jsx new file mode 100644 index 000000000..6071b4bb4 --- /dev/null +++ b/webapp/utils/web_client.jsx @@ -0,0 +1,67 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import Client from '../client/client.jsx'; +import TeamStore from '../stores/team_store.jsx'; +import BrowserStore from '../stores/browser_store.jsx'; +import * as GlobalActions from 'action_creators/global_actions.jsx'; + +const HTTP_UNAUTHORIZED = 401; + +class WebClientClass extends Client { + constructor() { + super(); + this.enableLogErrorsToConsole(true); + TeamStore.addChangeListener(this.onTeamStoreChanged); + } + + onTeamStoreChanged = () => { + this.setTeamId(TeamStore.getCurrentId()); + } + + track = (category, action, label, property, value) => { + if (global.window && global.window.analytics) { + global.window.analytics.track(action, {category, label, property, value}); + } + } + + trackPage = () => { + if (global.window && global.window.analytics) { + global.window.analytics.page(); + } + } + + handleError = (err, res) => { // eslint-disable-line no-unused-vars + if (err.status === HTTP_UNAUTHORIZED) { + GlobalActions.emitUserLoggedOutEvent('/login'); + } + } + + // not sure why but super.login doesn't work if using an () => arrow functions. + // I think this might be a webpack issue. + webLogin(email, username, password, token, success, error) { + this.login( + email, + username, + password, + token, + (data) => { + this.track('api', 'api_users_login_success', '', 'email', data.email); + BrowserStore.signalLogin(); + + if (success) { + success(data); + } + }, + (err) => { + this.track('api', 'api_users_login_fail', name, 'email', email); + if (error) { + error(err); + } + } + ); + } +} + +var WebClient = new WebClientClass(); +export default WebClient; -- cgit v1.2.3-1-g7c22