client.jsx 34.1 KB
Newer Older
=Corey Hulen's avatar
=Corey Hulen committed
1 2
// See License.txt for license information.

3
var BrowserStore = require('../stores/browser_store.jsx');
4
var TeamStore = require('../stores/team_store.jsx');
5
var ErrorStore = require('../stores/error_store.jsx');
=Corey Hulen's avatar
=Corey Hulen committed
6

7
export function track(category, action, label, prop, val) {
8
    global.window.analytics.track(action, {category: category, label: label, property: prop, value: val});
9
}
=Corey Hulen's avatar
=Corey Hulen committed
10

11
export function trackPage() {
12
    global.window.analytics.page();
13
}
=Corey Hulen's avatar
=Corey Hulen committed
14

15
function handleError(methodName, xhr, status, err) {
=Corey Hulen's avatar
=Corey Hulen committed
16 17 18
    var e = null;
    try {
        e = JSON.parse(xhr.responseText);
19
    } catch (parseError) {
20
        e = null;
=Corey Hulen's avatar
=Corey Hulen committed
21 22
    }

23
    var msg = '';
=Corey Hulen's avatar
=Corey Hulen committed
24 25

    if (e) {
26 27 28
        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;
=Corey Hulen's avatar
=Corey Hulen committed
29

30
        if (xhr.status === 0) {
31 32 33 34 35 36 37 38 39 40
            let errorCount = 1;
            const oldError = ErrorStore.getLastError();
            let connectError = 'There appears to be a problem with your internet connection';

            if (oldError && oldError.connErrorCount) {
                errorCount += oldError.connErrorCount;
                connectError = 'We cannot reach the Mattermost service.  The service may be down or misconfigured.  Please contact an administrator to make sure the WebSocket port is configured properly.';
            }

            e = {message: connectError, connErrorCount: errorCount};
41 42 43
        } else {
            e = {message: 'We received an unexpected status code from the server (' + xhr.status + ')'};
        }
=Corey Hulen's avatar
=Corey Hulen committed
44 45
    }

46 47
    console.error(msg); //eslint-disable-line no-console
    console.error(e); //eslint-disable-line no-console
=Corey Hulen's avatar
=Corey Hulen committed
48

49
    track('api', 'api_weberror', methodName, 'message', msg);
=Corey Hulen's avatar
=Corey Hulen committed
50

=Corey Hulen's avatar
=Corey Hulen committed
51 52 53 54 55 56 57 58
    // 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];
    //         window.location.href = teamURL + '/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search);
    //     }
    // }
=Corey Hulen's avatar
=Corey Hulen committed
59 60 61 62

    return e;
}

63
export function createTeamFromSignup(teamSignup, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
64
    $.ajax({
65
        url: '/api/v1/teams/create_from_signup',
=Corey Hulen's avatar
=Corey Hulen committed
66 67 68
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
69
        data: JSON.stringify(teamSignup),
JoramWilander's avatar
JoramWilander committed
70
        success,
71 72
        error: function onError(xhr, status, err) {
            var e = handleError('createTeamFromSignup', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
73 74 75
            error(e);
        }
    });
76
}
=Corey Hulen's avatar
=Corey Hulen committed
77

78
export function createTeamWithSSO(team, service, success, error) {
79 80 81 82 83 84
    $.ajax({
        url: '/api/v1/teams/create_with_sso/' + service,
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(team),
JoramWilander's avatar
JoramWilander committed
85
        success,
86 87 88 89 90
        error: function onError(xhr, status, err) {
            var e = handleError('createTeamWithSSO', xhr, status, err);
            error(e);
        }
    });
91
}
92

93
export function createUser(user, data, emailHash, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
94
    $.ajax({
95
        url: '/api/v1/users/create?d=' + encodeURIComponent(data) + '&h=' + encodeURIComponent(emailHash),
=Corey Hulen's avatar
=Corey Hulen committed
96 97 98 99
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(user),
JoramWilander's avatar
JoramWilander committed
100
        success,
101 102
        error: function onError(xhr, status, err) {
            var e = handleError('createUser', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
103 104 105 106
            error(e);
        }
    });

107 108
    track('api', 'api_users_create', user.team_id, 'email', user.email);
}
=Corey Hulen's avatar
=Corey Hulen committed
109

110
export function updateUser(user, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
111
    $.ajax({
112
        url: '/api/v1/users/update',
=Corey Hulen's avatar
=Corey Hulen committed
113 114 115 116
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(user),
JoramWilander's avatar
JoramWilander committed
117
        success,
118 119
        error: function onError(xhr, status, err) {
            var e = handleError('updateUser', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
120 121 122 123
            error(e);
        }
    });

124 125
    track('api', 'api_users_update');
}
=Corey Hulen's avatar
=Corey Hulen committed
126

127
export function updatePassword(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
128
    $.ajax({
129
        url: '/api/v1/users/newpassword',
=Corey Hulen's avatar
=Corey Hulen committed
130 131 132 133
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
134
        success,
135 136
        error: function onError(xhr, status, err) {
            var e = handleError('newPassword', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
137 138 139 140
            error(e);
        }
    });

141 142
    track('api', 'api_users_newpassword');
}
=Corey Hulen's avatar
=Corey Hulen committed
143

144
export function updateUserNotifyProps(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
145
    $.ajax({
146
        url: '/api/v1/users/update_notify',
=Corey Hulen's avatar
=Corey Hulen committed
147 148 149 150
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
151
        success,
152 153
        error: function onError(xhr, status, err) {
            var e = handleError('updateUserNotifyProps', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
154 155 156
            error(e);
        }
    });
157
}
=Corey Hulen's avatar
=Corey Hulen committed
158

159
export function updateRoles(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
160
    $.ajax({
161
        url: '/api/v1/users/update_roles',
=Corey Hulen's avatar
=Corey Hulen committed
162 163 164 165
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
166
        success,
167 168
        error: function onError(xhr, status, err) {
            var e = handleError('updateRoles', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
169 170 171 172
            error(e);
        }
    });

173 174
    track('api', 'api_users_update_roles');
}
=Corey Hulen's avatar
=Corey Hulen committed
175

176
export function updateActive(userId, active, success, error) {
177 178 179 180
    var data = {};
    data.user_id = userId;
    data.active = '' + active;

=Corey Hulen's avatar
=Corey Hulen committed
181
    $.ajax({
182
        url: '/api/v1/users/update_active',
=Corey Hulen's avatar
=Corey Hulen committed
183 184 185 186
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
187
        success,
188 189
        error: function onError(xhr, status, err) {
            var e = handleError('updateActive', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
190 191 192 193
            error(e);
        }
    });

194 195
    track('api', 'api_users_update_roles');
}
=Corey Hulen's avatar
=Corey Hulen committed
196

197
export function sendPasswordReset(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
198
    $.ajax({
199
        url: '/api/v1/users/send_password_reset',
=Corey Hulen's avatar
=Corey Hulen committed
200 201 202 203
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
204
        success,
205 206
        error: function onError(xhr, status, err) {
            var e = handleError('sendPasswordReset', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
207 208 209 210
            error(e);
        }
    });

211 212
    track('api', 'api_users_send_password_reset');
}
=Corey Hulen's avatar
=Corey Hulen committed
213

214
export function resetPassword(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
215
    $.ajax({
216
        url: '/api/v1/users/reset_password',
=Corey Hulen's avatar
=Corey Hulen committed
217 218 219 220
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
221
        success,
222 223
        error: function onError(xhr, status, err) {
            var e = handleError('resetPassword', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
224 225 226 227
            error(e);
        }
    });

228 229
    track('api', 'api_users_reset_password');
}
=Corey Hulen's avatar
=Corey Hulen committed
230

231 232
export function logout() {
    track('api', 'api_users_logout');
233 234
    var currentTeamUrl = TeamStore.getCurrentTeamUrl();
    BrowserStore.clear();
235
    ErrorStore.storeLastError(null);
236
    window.location.href = currentTeamUrl + '/logout';
237
}
=Corey Hulen's avatar
=Corey Hulen committed
238

239
export function loginByEmail(name, email, password, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
240
    $.ajax({
241
        url: '/api/v1/users/login',
=Corey Hulen's avatar
=Corey Hulen committed
242 243 244
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
245
        data: JSON.stringify({name: name, email: email, password: password}),
246
        success: function onSuccess(data, textStatus, xhr) {
247
            track('api', 'api_users_login_success', data.team_id, 'email', data.email);
=Corey Hulen's avatar
=Corey Hulen committed
248 249
            success(data, textStatus, xhr);
        },
250
        error: function onError(xhr, status, err) {
251
            track('api', 'api_users_login_fail', name, 'email', email);
=Corey Hulen's avatar
=Corey Hulen committed
252

253
            var e = handleError('loginByEmail', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
254 255 256
            error(e);
        }
    });
257
}
=Corey Hulen's avatar
=Corey Hulen committed
258

259
export function revokeSession(altId, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
260
    $.ajax({
261
        url: '/api/v1/users/revoke_session',
=Corey Hulen's avatar
=Corey Hulen committed
262 263 264 265
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify({id: altId}),
JoramWilander's avatar
JoramWilander committed
266
        success,
267 268
        error: function onError(xhr, status, err) {
            var e = handleError('revokeSession', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
269 270 271
            error(e);
        }
    });
272
}
=Corey Hulen's avatar
=Corey Hulen committed
273

274
export function getSessions(userId, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
275
    $.ajax({
276
        cache: false,
277
        url: '/api/v1/users/' + userId + '/sessions',
=Corey Hulen's avatar
=Corey Hulen committed
278 279 280
        dataType: 'json',
        contentType: 'application/json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
281
        success,
282 283
        error: function onError(xhr, status, err) {
            var e = handleError('getSessions', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
284 285 286
            error(e);
        }
    });
287
}
=Corey Hulen's avatar
=Corey Hulen committed
288

289
export function getAudits(userId, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
290
    $.ajax({
291
        url: '/api/v1/users/' + userId + '/audits',
=Corey Hulen's avatar
=Corey Hulen committed
292 293 294
        dataType: 'json',
        contentType: 'application/json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
295
        success,
296 297
        error: function onError(xhr, status, err) {
            var e = handleError('getAudits', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
298 299 300
            error(e);
        }
    });
301
}
=Corey Hulen's avatar
=Corey Hulen committed
302

=Corey Hulen's avatar
=Corey Hulen committed
303 304 305 306 307 308
export function getLogs(success, error) {
    $.ajax({
        url: '/api/v1/admin/logs',
        dataType: 'json',
        contentType: 'application/json',
        type: 'GET',
309
        success,
=Corey Hulen's avatar
=Corey Hulen committed
310 311 312 313 314 315 316
        error: function onError(xhr, status, err) {
            var e = handleError('getLogs', xhr, status, err);
            error(e);
        }
    });
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
export function getConfig(success, error) {
    $.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 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);
        }
    });
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359
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)
    });
}

=Corey Hulen's avatar
=Corey Hulen committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
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);
        }
    });
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388
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);
        }
    });
}

389
export function getMe(success, error) {
390
    var currentUser = null;
=Corey Hulen's avatar
=Corey Hulen committed
391
    $.ajax({
392
        cache: false,
393
        url: '/api/v1/users/me',
=Corey Hulen's avatar
=Corey Hulen committed
394 395 396
        dataType: 'json',
        contentType: 'application/json',
        type: 'GET',
397 398 399 400 401 402
        success: function gotUser(data, textStatus, xhr) {
            currentUser = data;
            if (success) {
                success(data, textStatus, xhr);
            }
        },
403
        error: function onError(xhr, status, err) {
404
            if (error) {
405
                var e = handleError('getMe', xhr, status, err);
406
                error(e);
407
            }
=Corey Hulen's avatar
=Corey Hulen committed
408 409
        }
    });
410 411

    return currentUser;
412
}
=Corey Hulen's avatar
=Corey Hulen committed
413

414
export function inviteMembers(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
415
    $.ajax({
416
        url: '/api/v1/teams/invite_members',
=Corey Hulen's avatar
=Corey Hulen committed
417 418 419 420
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
421
        success,
422 423
        error: function onError(xhr, status, err) {
            var e = handleError('inviteMembers', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
424 425 426 427
            error(e);
        }
    });

428 429
    track('api', 'api_teams_invite_members');
}
=Corey Hulen's avatar
=Corey Hulen committed
430

431
export function updateTeamDisplayName(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
432
    $.ajax({
433
        url: '/api/v1/teams/update_name',
=Corey Hulen's avatar
=Corey Hulen committed
434 435 436 437
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
438
        success,
439 440
        error: function onError(xhr, status, err) {
            var e = handleError('updateTeamDisplayName', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
441 442 443 444
            error(e);
        }
    });

445 446
    track('api', 'api_teams_update_name');
}
=Corey Hulen's avatar
=Corey Hulen committed
447

448
export function signupTeam(email, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
449
    $.ajax({
450
        url: '/api/v1/teams/signup',
=Corey Hulen's avatar
=Corey Hulen committed
451 452 453
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
454
        data: JSON.stringify({email: email}),
JoramWilander's avatar
JoramWilander committed
455
        success,
456 457
        error: function onError(xhr, status, err) {
            var e = handleError('singupTeam', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
458 459 460 461
            error(e);
        }
    });

462 463
    track('api', 'api_teams_signup');
}
=Corey Hulen's avatar
=Corey Hulen committed
464

465
export function createTeam(team, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
466
    $.ajax({
467
        url: '/api/v1/teams/create',
=Corey Hulen's avatar
=Corey Hulen committed
468 469 470 471
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(team),
JoramWilander's avatar
JoramWilander committed
472
        success,
473 474
        error: function onError(xhr, status, err) {
            var e = handleError('createTeam', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
475 476 477
            error(e);
        }
    });
478
}
=Corey Hulen's avatar
=Corey Hulen committed
479

480
export function findTeamByName(teamName, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
481
    $.ajax({
482
        url: '/api/v1/teams/find_team_by_name',
=Corey Hulen's avatar
=Corey Hulen committed
483 484 485
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
486
        data: JSON.stringify({name: teamName}),
JoramWilander's avatar
JoramWilander committed
487
        success,
488 489
        error: function onError(xhr, status, err) {
            var e = handleError('findTeamByName', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
490 491 492
            error(e);
        }
    });
493
}
=Corey Hulen's avatar
=Corey Hulen committed
494

495
export function findTeamsSendEmail(email, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
496
    $.ajax({
497
        url: '/api/v1/teams/email_teams',
=Corey Hulen's avatar
=Corey Hulen committed
498 499 500 501
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify({email: email}),
JoramWilander's avatar
JoramWilander committed
502
        success,
503 504
        error: function onError(xhr, status, err) {
            var e = handleError('findTeamsSendEmail', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
505 506 507 508
            error(e);
        }
    });

509 510
    track('api', 'api_teams_email_teams');
}
=Corey Hulen's avatar
=Corey Hulen committed
511

512
export function findTeams(email, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
513
    $.ajax({
514
        url: '/api/v1/teams/find_teams',
=Corey Hulen's avatar
=Corey Hulen committed
515 516 517 518
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify({email: email}),
JoramWilander's avatar
JoramWilander committed
519
        success,
520 521
        error: function onError(xhr, status, err) {
            var e = handleError('findTeams', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
522 523 524
            error(e);
        }
    });
525
}
=Corey Hulen's avatar
=Corey Hulen committed
526

527
export function createChannel(channel, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
528
    $.ajax({
529
        url: '/api/v1/channels/create',
=Corey Hulen's avatar
=Corey Hulen committed
530 531 532 533
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(channel),
JoramWilander's avatar
JoramWilander committed
534
        success,
535 536
        error: function onError(xhr, status, err) {
            var e = handleError('createChannel', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
537 538 539 540
            error(e);
        }
    });

541 542
    track('api', 'api_channels_create', channel.type, 'name', channel.name);
}
=Corey Hulen's avatar
=Corey Hulen committed
543

544
export function createDirectChannel(channel, userId, success, error) {
545 546 547 548 549 550
    $.ajax({
        url: '/api/v1/channels/create_direct',
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify({user_id: userId}),
JoramWilander's avatar
JoramWilander committed
551
        success,
552
        error: function onError(xhr, status, err) {
553
            var e = handleError('createDirectChannel', xhr, status, err);
554 555 556 557
            error(e);
        }
    });

558 559
    track('api', 'api_channels_create_direct', channel.type, 'name', channel.name);
}
560

561
export function updateChannel(channel, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
562
    $.ajax({
563
        url: '/api/v1/channels/update',
=Corey Hulen's avatar
=Corey Hulen committed
564 565 566 567
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(channel),
JoramWilander's avatar
JoramWilander committed
568
        success,
569 570
        error: function onError(xhr, status, err) {
            var e = handleError('updateChannel', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
571 572 573 574
            error(e);
        }
    });

575 576
    track('api', 'api_channels_update');
}
=Corey Hulen's avatar
=Corey Hulen committed
577

578
export function updateChannelDesc(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
579
    $.ajax({
580
        url: '/api/v1/channels/update_desc',
=Corey Hulen's avatar
=Corey Hulen committed
581 582 583 584
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
585
        success,
586 587
        error: function onError(xhr, status, err) {
            var e = handleError('updateChannelDesc', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
588 589 590 591
            error(e);
        }
    });

592 593
    track('api', 'api_channels_desc');
}
=Corey Hulen's avatar
=Corey Hulen committed
594

595
export function updateNotifyProps(data, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
596
    $.ajax({
597
        url: '/api/v1/channels/update_notify_props',
=Corey Hulen's avatar
=Corey Hulen committed
598 599 600 601
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify(data),
JoramWilander's avatar
JoramWilander committed
602
        success,
603
        error: function onError(xhr, status, err) {
604
            var e = handleError('updateNotifyProps', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
605 606 607
            error(e);
        }
    });
608
}
=Corey Hulen's avatar
=Corey Hulen committed
609

610
export function joinChannel(id, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
611
    $.ajax({
612
        url: '/api/v1/channels/' + id + '/join',
=Corey Hulen's avatar
=Corey Hulen committed
613 614 615
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
JoramWilander's avatar
JoramWilander committed
616
        success,
617 618
        error: function onError(xhr, status, err) {
            var e = handleError('joinChannel', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
619 620 621 622
            error(e);
        }
    });

623 624
    track('api', 'api_channels_join');
}
=Corey Hulen's avatar
=Corey Hulen committed
625

626
export function leaveChannel(id, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
627
    $.ajax({
628
        url: '/api/v1/channels/' + id + '/leave',
=Corey Hulen's avatar
=Corey Hulen committed
629 630 631
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
JoramWilander's avatar
JoramWilander committed
632
        success,
633 634
        error: function onError(xhr, status, err) {
            var e = handleError('leaveChannel', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
635 636 637 638
            error(e);
        }
    });

639 640
    track('api', 'api_channels_leave');
}
=Corey Hulen's avatar
=Corey Hulen committed
641

642
export function deleteChannel(id, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
643
    $.ajax({
644
        url: '/api/v1/channels/' + id + '/delete',
=Corey Hulen's avatar
=Corey Hulen committed
645 646 647
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
JoramWilander's avatar
JoramWilander committed
648
        success,
649 650
        error: function onError(xhr, status, err) {
            var e = handleError('deleteChannel', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
651 652 653 654
            error(e);
        }
    });

655 656
    track('api', 'api_channels_delete');
}
=Corey Hulen's avatar
=Corey Hulen committed
657

658
export function updateLastViewedAt(channelId, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
659
    $.ajax({
660
        url: '/api/v1/channels/' + channelId + '/update_last_viewed_at',
=Corey Hulen's avatar
=Corey Hulen committed
661 662 663
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
JoramWilander's avatar
JoramWilander committed
664
        success,
665 666
        error: function onError(xhr, status, err) {
            var e = handleError('updateLastViewedAt', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
667 668 669
            error(e);
        }
    });
670
}
=Corey Hulen's avatar
=Corey Hulen committed
671

672
export function getChannels(success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
673
    $.ajax({
674
        cache: false,
675
        url: '/api/v1/channels/',
=Corey Hulen's avatar
=Corey Hulen committed
676 677
        dataType: 'json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
678
        success,
=Corey Hulen's avatar
=Corey Hulen committed
679
        ifModified: true,
680
        error: function onError(xhr, status, err) {
681
            var e = handleError('getChannels', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
682 683 684
            error(e);
        }
    });
685
}
=Corey Hulen's avatar
=Corey Hulen committed
686

687
export function getChannel(id, success, error) {
688
    $.ajax({
689
        cache: false,
690
        url: '/api/v1/channels/' + id + '/',
691 692
        dataType: 'json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
693
        success,
694 695
        error: function onError(xhr, status, err) {
            var e = handleError('getChannel', xhr, status, err);
696 697 698 699
            error(e);
        }
    });

700 701
    track('api', 'api_channel_get');
}
702

703
export function getMoreChannels(success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
704
    $.ajax({
705
        url: '/api/v1/channels/more',
=Corey Hulen's avatar
=Corey Hulen committed
706 707
        dataType: 'json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
708
        success,
=Corey Hulen's avatar
=Corey Hulen committed
709
        ifModified: true,
710 711
        error: function onError(xhr, status, err) {
            var e = handleError('getMoreChannels', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
712 713 714
            error(e);
        }
    });
715
}
=Corey Hulen's avatar
=Corey Hulen committed
716

717
export function getChannelCounts(success, error) {
718
    $.ajax({
719
        cache: false,
720 721 722
        url: '/api/v1/channels/counts',
        dataType: 'json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
723
        success,
724
        ifModified: true,
725
        error: function onError(xhr, status, err) {
726 727 728 729 730 731
            var e = handleError('getChannelCounts', xhr, status, err);
            error(e);
        }
    });
}

732
export function getChannelExtraInfo(id, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
733
    $.ajax({
734
        url: '/api/v1/channels/' + id + '/extra_info',
=Corey Hulen's avatar
=Corey Hulen committed
735 736
        dataType: 'json',
        type: 'GET',
JoramWilander's avatar
JoramWilander committed
737
        success,
738 739
        error: function onError(xhr, status, err) {
            var e = handleError('getChannelExtraInfo', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
740 741 742
            error(e);
        }
    });
743
}
=Corey Hulen's avatar
=Corey Hulen committed
744

745
export function executeCommand(channelId, command, suggest, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
746
    $.ajax({
747
        url: '/api/v1/command',
=Corey Hulen's avatar
=Corey Hulen committed
748 749 750
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
751
        data: JSON.stringify({channelId: channelId, command: command, suggest: '' + suggest}),
JoramWilander's avatar
JoramWilander committed
752
        success,
753 754
        error: function onError(xhr, status, err) {
            var e = handleError('executeCommand', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
755 756 757
            error(e);
        }
    });
758
}
=Corey Hulen's avatar
=Corey Hulen committed
759

760
export function getPostsPage(channelId, offset, limit, success, error, complete) {
=Corey Hulen's avatar
=Corey Hulen committed
761
    $.ajax({
762
        cache: false,
763
        url: '/api/v1/channels/' + channelId + '/posts/' + offset + '/' + limit,
=Corey Hulen's avatar
=Corey Hulen committed
764 765 766
        dataType: 'json',
        type: 'GET',
        ifModified: true,
JoramWilander's avatar
JoramWilander committed
767
        success,
768 769
        error: function onError(xhr, status, err) {
            var e = handleError('getPosts', xhr, status, err);
770 771 772 773
            error(e);
        },
        complete: complete
    });
774
}
775

776
export function getPosts(channelId, since, success, error, complete) {
777 778 779 780 781
    $.ajax({
        url: '/api/v1/channels/' + channelId + '/posts/' + since,
        dataType: 'json',
        type: 'GET',
        ifModified: true,
JoramWilander's avatar
JoramWilander committed
782
        success,
783 784
        error: function onError(xhr, status, err) {
            var e = handleError('getPosts', xhr, status, err);
785
            error(e);
=Corey Hulen's avatar
=Corey Hulen committed
786 787 788
        },
        complete: complete
    });
789
}
=Corey Hulen's avatar
=Corey Hulen committed
790

791
export function getPost(channelId, postId, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
792
    $.ajax({
793
        cache: false,
794
        url: '/api/v1/channels/' + channelId + '/post/' + postId,
=Corey Hulen's avatar
=Corey Hulen committed
795 796 797
        dataType: 'json',
        type: 'GET',
        ifModified: false,
JoramWilander's avatar
JoramWilander committed
798
        success,
799 800
        error: function onError(xhr, status, err) {
            var e = handleError('getPost', xhr, status, err);
=Corey Hulen's avatar
=Corey Hulen committed
801 802 803
            error(e);
        }
    });
804
}
=Corey Hulen's avatar
=Corey Hulen committed
805

806
export function search(terms, success, error) {
=Corey Hulen's avatar
=Corey Hulen committed
807
    $.ajax({
808
        url: '/api/v1/posts/search',
=Corey Hulen's avatar
=Corey Hulen committed
809 810
        dataType: 'json',
        type: 'GET',
811
        data: {terms: terms},
JoramWilander's avatar
JoramWilander committed
812
        success,