Files
mattermost/web/react/utils/client.jsx

934 lines
28 KiB
React
Raw Normal View History

2015-06-14 23:53:32 -08:00
// See License.txt for license information.
var BrowserStore = require('../stores/browser_store.jsx');
var TeamStore = require('../stores/team_store.jsx');
2015-06-14 23:53:32 -08:00
module.exports.track = function(category, action, label, prop, val) {
global.window.snowplow('trackStructEvent', category, action, label, prop, val);
global.window.analytics.track(action, {category: category, label: label, property: prop, value: val});
2015-06-14 23:53:32 -08:00
};
module.exports.trackPage = function() {
global.window.snowplow('trackPageView');
global.window.analytics.page();
2015-06-14 23:53:32 -08:00
};
2015-08-18 09:43:22 -04:00
function handleError(methodName, xhr, status, err) {
var LTracker = global.window.LTracker || [];
2015-06-14 23:53:32 -08:00
var e = null;
try {
e = JSON.parse(xhr.responseText);
2015-08-18 09:43:22 -04:00
} catch(parseError) {
e = null;
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
var msg = '';
2015-06-14 23:53:32 -08:00
if (e) {
2015-08-18 09:43:22 -04:00
msg = 'error in ' + methodName + ' msg=' + e.message + ' detail=' + e.detailed_error + ' rid=' + e.request_id;
} else {
msg = 'error in ' + methodName + ' status=' + status + ' statusCode=' + xhr.status + ' err=' + err;
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (xhr.status === 0) {
e = {message: 'There appears to be a problem with your internet connection'};
} else {
e = {message: 'We received an unexpected status code from the server (' + xhr.status + ')'};
}
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
console.error(msg); //eslint-disable-line no-console
console.error(e); //eslint-disable-line no-console
LTracker.push(msg);
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
module.exports.track('api', 'api_weberror', methodName, 'message', msg);
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (xhr.status === 401) {
if (window.location.href.indexOf('/channels') === 0) {
window.location.pathname = '/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search);
} else {
var teamURL = window.location.href.split('/channels')[0];
2015-08-18 09:43:22 -04:00
window.location.href = teamURL + '/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search);
}
2015-06-14 23:53:32 -08:00
}
return e;
}
2015-08-18 09:43:22 -04:00
module.exports.createTeamFromSignup = function(teamSignup, success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/create_from_signup',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
2015-08-18 09:43:22 -04:00
data: JSON.stringify(teamSignup),
2015-06-14 23:53:32 -08:00
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('createTeamFromSignup', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
2015-08-18 09:43:22 -04:00
module.exports.createUser = function(user, data, emailHash, success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/create?d=' + encodeURIComponent(data) + '&h=' + encodeURIComponent(emailHash),
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(user),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('createUser', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_create', user.team_id, 'email', user.email);
};
module.exports.updateUser = function(user, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/update',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(user),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateUser', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_update');
};
module.exports.updatePassword = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/newpassword',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('newPassword', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_newpassword');
};
module.exports.updateUserNotifyProps = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/update_notify',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateUserNotifyProps', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.updateRoles = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/update_roles',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateRoles', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_update_roles');
};
module.exports.updateActive = function(userId, active, success, error) {
2015-08-18 09:43:22 -04:00
var data = {};
data.user_id = userId;
data.active = '' + active;
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/update_active',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateActive', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_update_roles');
};
module.exports.sendPasswordReset = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/send_password_reset',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('sendPasswordReset', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_send_password_reset');
};
module.exports.resetPassword = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/reset_password',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('resetPassword', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_users_reset_password');
};
module.exports.logout = function() {
module.exports.track('api', 'api_users_logout');
var currentTeamUrl = TeamStore.getCurrentTeamUrl();
BrowserStore.clear();
2015-08-18 09:43:22 -04:00
window.location.href = currentTeamUrl + '/logout';
2015-06-14 23:53:32 -08:00
};
module.exports.loginByEmail = function(name, email, password, success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/login',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({name: name, email: email, password: password}),
2015-08-18 09:43:22 -04:00
success: function onSuccess(data, textStatus, xhr) {
2015-06-14 23:53:32 -08:00
module.exports.track('api', 'api_users_login_success', data.team_id, 'email', data.email);
success(data, textStatus, xhr);
},
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
2015-06-14 23:53:32 -08:00
module.exports.track('api', 'api_users_login_fail', window.getSubDomain(), 'email', email);
2015-08-18 09:43:22 -04:00
var e = handleError('loginByEmail', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.revokeSession = function(altId, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/revoke_session',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({id: altId}),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('revokeSession', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.getSessions = function(userId, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/' + userId + '/sessions',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'GET',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getSessions', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.getAudits = function(userId, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/' + userId + '/audits',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'GET',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getAudits', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.getMeSynchronous = function(success, error) {
var currentUser = null;
2015-06-14 23:53:32 -08:00
$.ajax({
async: false,
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/me',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'GET',
success: function gotUser(data, textStatus, xhr) {
currentUser = data;
if (success) {
success(data, textStatus, xhr);
}
},
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
2015-07-02 18:02:00 -07:00
var ieChecker = window.navigator.userAgent; // This and the condition below is used to check specifically for browsers IE10 & 11 to suppress a 200 'OK' error from appearing on login
2015-08-18 09:43:22 -04:00
if (xhr.status !== 200 || !(ieChecker.indexOf('Trident/7.0') > 0 || ieChecker.indexOf('Trident/6.0') > 0)) {
if (error) {
2015-08-18 09:43:22 -04:00
var e = handleError('getMeSynchronous', xhr, status, err);
error(e);
2015-08-18 09:43:22 -04:00
}
}
2015-06-14 23:53:32 -08:00
}
});
return currentUser;
2015-06-14 23:53:32 -08:00
};
module.exports.inviteMembers = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/invite_members',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('inviteMembers', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_teams_invite_members');
};
module.exports.updateTeamDisplayName = function(data, success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/update_name',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateTeamDisplayName', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_teams_update_name');
};
module.exports.signupTeam = function(email, success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/signup',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({email: email}),
2015-06-14 23:53:32 -08:00
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('singupTeam', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_teams_signup');
};
module.exports.createTeam = function(team, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/create',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(team),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('createTeam', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.findTeamByName = function(teamName, success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/find_team_by_name',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({name: teamName}),
2015-06-14 23:53:32 -08:00
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('findTeamByName', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.findTeamsSendEmail = function(email, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/email_teams',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({email: email}),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('findTeamsSendEmail', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_teams_email_teams');
};
module.exports.findTeams = function(email, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/find_teams',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({email: email}),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('findTeams', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.createChannel = function(channel, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/create',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(channel),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('createChannel', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_create', channel.type, 'name', channel.name);
};
module.exports.createDirectChannel = function(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: success,
error: function(xhr, status, err) {
var e = handleError('createDirectChannel', xhr, status, err);
error(e);
}
});
module.exports.track('api', 'api_channels_create_direct', channel.type, 'name', channel.name);
};
2015-06-14 23:53:32 -08:00
module.exports.updateChannel = function(channel, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/update',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(channel),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateChannel', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_update');
};
module.exports.updateChannelDesc = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/update_desc',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateChannelDesc', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_desc');
};
module.exports.updateNotifyLevel = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/update_notify_level',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateNotifyLevel', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.joinChannel = function(id, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/join',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('joinChannel', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_join');
};
module.exports.leaveChannel = function(id, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/leave',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('leaveChannel', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_leave');
};
module.exports.deleteChannel = function(id, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/delete',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('deleteChannel', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_delete');
};
module.exports.updateLastViewedAt = function(channelId, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + channelId + '/update_last_viewed_at',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateLastViewedAt', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
function getChannels(success, error) {
2015-06-14 23:53:32 -08:00
$.ajax({
url: '/api/v1/channels/',
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'GET',
success: success,
ifModified: true,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getChannels', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
}
module.exports.getChannels = getChannels;
2015-06-14 23:53:32 -08:00
module.exports.getChannel = function(id, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/',
dataType: 'json',
type: 'GET',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getChannel', xhr, status, err);
error(e);
}
});
module.exports.track('api', 'api_channel_get');
};
2015-06-14 23:53:32 -08:00
module.exports.getMoreChannels = function(success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/more',
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'GET',
success: success,
ifModified: true,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getMoreChannels', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
function getChannelCounts(success, error) {
$.ajax({
url: '/api/v1/channels/counts',
dataType: 'json',
type: 'GET',
success: success,
ifModified: true,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getChannelCounts', xhr, status, err);
error(e);
}
});
}
module.exports.getChannelCounts = getChannelCounts;
2015-06-14 23:53:32 -08:00
module.exports.getChannelExtraInfo = function(id, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/extra_info',
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'GET',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getChannelExtraInfo', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.executeCommand = function(channelId, command, suggest, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/command',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
2015-08-18 09:43:22 -04:00
data: JSON.stringify({channelId: channelId, command: command, suggest: '' + suggest}),
2015-06-14 23:53:32 -08:00
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('executeCommand', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.getPosts = function(channelId, offset, limit, success, error, complete) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + channelId + '/posts/' + offset + '/' + limit,
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'GET',
ifModified: true,
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getPosts', xhr, status, err);
error(e);
2015-06-14 23:53:32 -08:00
},
complete: complete
});
};
module.exports.getPost = function(channelId, postId, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + channelId + '/post/' + postId,
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'GET',
ifModified: false,
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getPost', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.search = function(terms, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/posts/search',
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'GET',
2015-08-18 09:43:22 -04:00
data: {terms: terms},
2015-06-14 23:53:32 -08:00
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('search', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_posts_search');
};
module.exports.deletePost = function(channelId, id, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + channelId + '/post/' + id + '/delete',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('deletePost', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_posts_delete');
};
module.exports.createPost = function(post, channel, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + post.channel_id + '/create',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(post),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('createPost', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.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,
2015-08-18 09:43:22 -04:00
// mentions: (post.message.match('/<mention>/g') || []).length
2015-06-14 23:53:32 -08:00
// });
};
module.exports.updatePost = function(post, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + post.channel_id + '/update',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(post),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updatePost', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_posts_update');
};
module.exports.addChannelMember = function(id, data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/add',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('addChannelMember', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_add_member');
};
module.exports.removeChannelMember = function(id, data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/channels/' + id + '/remove',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('removeChannelMember', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
module.exports.track('api', 'api_channels_remove_member');
};
module.exports.getProfiles = function(success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/profiles',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'GET',
success: success,
ifModified: true,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getProfiles', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.uploadFile = function(formData, success, error) {
var request = $.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/files/upload',
2015-06-14 23:53:32 -08:00
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
if (err !== 'abort') {
2015-08-18 09:43:22 -04:00
var e = handleError('uploadFile', xhr, status, err);
error(e);
}
2015-06-14 23:53:32 -08:00
}
});
module.exports.track('api', 'api_files_upload');
return request;
2015-06-14 23:53:32 -08:00
};
module.exports.getPublicLink = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/files/get_public_link',
2015-06-14 23:53:32 -08:00
dataType: 'json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getPublicLink', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.uploadProfileImage = function(imageData, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/newimage',
2015-06-14 23:53:32 -08:00
type: 'POST',
data: imageData,
cache: false,
contentType: false,
processData: false,
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('uploadProfileImage', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
2015-07-07 09:16:13 -04:00
module.exports.importSlack = function(fileData, success, error) {
$.ajax({
url: '/api/v1/teams/import_team',
2015-07-07 09:16:13 -04:00
type: 'POST',
data: fileData,
cache: false,
contentType: false,
processData: false,
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('importTeam', xhr, status, err);
2015-07-07 09:16:13 -04:00
error(e);
}
});
2015-08-18 09:43:22 -04:00
};
2015-07-07 09:16:13 -04:00
2015-06-14 23:53:32 -08:00
module.exports.getStatuses = function(success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/users/status',
2015-06-14 23:53:32 -08:00
dataType: 'json',
contentType: 'application/json',
type: 'GET',
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getStatuses', xhr, status, err);
2015-06-14 23:53:32 -08:00
error(e);
}
});
};
module.exports.getMyTeam = function(success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/me',
dataType: 'json',
type: 'GET',
success: success,
ifModified: true,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getMyTeam', xhr, status, err);
error(e);
}
});
};
module.exports.updateValetFeature = function(data, success, error) {
$.ajax({
2015-08-18 09:43:22 -04:00
url: '/api/v1/teams/update_valet_feature',
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(data),
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('updateValetFeature', xhr, status, err);
error(e);
}
});
module.exports.track('api', 'api_teams_update_valet_feature');
};
function getConfig(success, error) {
$.ajax({
url: '/api/v1/config/get_all',
dataType: 'json',
type: 'GET',
ifModified: true,
success: success,
2015-08-18 09:43:22 -04:00
error: function onError(xhr, status, err) {
var e = handleError('getConfig', xhr, status, err);
error(e);
}
});
2015-08-18 09:43:22 -04:00
}
module.exports.getConfig = getConfig;