├── .gitignore ├── LICENSE.txt ├── Makefile ├── README.md ├── client.jsx ├── main.jsx ├── package.json ├── tests ├── client_admin.test.jsx ├── client_channel.test.jsx ├── client_command.test.jsx ├── client_emoji.test.jsx ├── client_general.test.jsx ├── client_hooks.test.jsx ├── client_oauth.test.jsx ├── client_post.test.jsx ├── client_preferences.test.jsx ├── client_team.test.jsx ├── client_user.test.jsx └── test_helper.jsx ├── webpack.config.js └── websocket_client.jsx /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | lib 3 | npm-* 4 | node_modules 5 | .tmp 6 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "{}" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright {yyyy} {name of copyright owner} 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean build publish npminstall 2 | 3 | BUILD_SERVER_DIR = ../platform 4 | 5 | npminstall: package.json 6 | @echo Install 7 | 8 | npm install 9 | 10 | test: npminstall 11 | cd $(BUILD_SERVER_DIR) && $(MAKE) internal-test-javascript-client 12 | 13 | build: npminstall 14 | @echo Building... 15 | 16 | npm run build 17 | 18 | publish: npminstall 19 | @echo Publishing 20 | 21 | npm login 22 | npm publish 23 | 24 | 25 | clean: 26 | @echo Cleaning... 27 | 28 | rm -f npm-debug.log 29 | rm -rf node_modules 30 | rm -rf lib 31 | rm -rf .tmp 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecation 2 | 3 | API version 3 is [scheduled for deprecation on January 16th, 2018](https://api.mattermost.com/#tag/APIv3-Deprecation) and as part of the deprecation process this driver is no longer maintained. We recommend using the new API version 4 driver available in our [mattermost-redux](https://github.com/mattermost/mattermost-redux) repository. 4 | 5 | # Mattermost JavaScript API Library 6 | 7 | [![npm](https://img.shields.io/npm/v/mattermost.svg?maxAge=2592000?style=plastic)](https://www.npmjs.com/package/mattermost) [![npm](https://img.shields.io/npm/l/mattermost.svg?maxAge=2592000?style=plastic)](https://github.com/mattermost/mattermost-driver-javascript/blob/master/LICENSE.txt) 8 | 9 | A library for interacting with the [Mattermost](https://github.com/mattermost/platform) API. 10 | 11 | Available on [NPM](https://www.npmjs.com/package/mattermost) 12 | 13 | To contribute, please see [Contribution Guidelines](http://docs.mattermost.com/developer/contribution-guide.html) 14 | 15 | ## Usage 16 | 17 | ### Installation 18 | 19 | The library can be installed using npm: 20 | 21 | ``` 22 | npm install mattermost --save 23 | ``` 24 | 25 | ### Usage 26 | 27 | Example import methods: 28 | 29 | ```javascript 30 | var Mattermost = require('mattermost'); 31 | var client = new Mattermost.Client() 32 | ``` 33 | 34 | ```javascript 35 | import {Client} from 'mattermost'; 36 | const client = new Client(); 37 | ``` 38 | 39 | ### API 40 | 41 | API Documentation is coming soon. For now, take a look at the [client.jsx](https://github.com/mattermost/mattermost-driver-javascript/blob/master/client.jsx) file. 42 | 43 | 44 | ## Development 45 | 46 | ### Building 47 | 48 | `make build` - Compiles client.jsx into a minified lib/client.js 49 | 50 | `make publish` - Walks though the steps to publish the package (manually) 51 | 52 | `make clean` - Cleans up compiled bits and npm dependencies. 53 | 54 | ### Tests 55 | 56 | The tests are in the [main repository](https://github.com/mattermost/platform/tree/master/webapp/tests). This is because they require the server to run. 57 | -------------------------------------------------------------------------------- /client.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import request from 'superagent'; 5 | 6 | const HEADER_X_VERSION_ID = 'x-version-id'; 7 | const HEADER_X_CLUSTER_ID = 'x-cluster-id'; 8 | const HEADER_TOKEN = 'token'; 9 | const HEADER_BEARER = 'BEARER'; 10 | const HEADER_AUTH = 'Authorization'; 11 | 12 | export default class Client { 13 | constructor() { 14 | this.teamId = ''; 15 | this.serverVersion = ''; 16 | this.clusterId = ''; 17 | this.logToConsole = false; 18 | this.useToken = false; 19 | this.token = ''; 20 | this.url = ''; 21 | this.urlVersion = '/api/v3'; 22 | this.defaultHeaders = { 23 | 'X-Requested-With': 'XMLHttpRequest' 24 | }; 25 | 26 | this.translations = { 27 | connectionError: 'There appears to be a problem with your internet connection.', 28 | unknownError: 'We received an unexpected status code from the server.' 29 | }; 30 | } 31 | 32 | setUrl(url) { 33 | this.url = url; 34 | } 35 | 36 | setAcceptLanguage(locale) { 37 | this.defaultHeaders['Accept-Language'] = locale; 38 | } 39 | 40 | setTeamId(id) { 41 | this.teamId = id; 42 | } 43 | 44 | getTeamId() { 45 | if (!this.teamId) { 46 | console.error('You are trying to use a route that requires a team_id, but you have not called setTeamId() in client.jsx'); // eslint-disable-line no-console 47 | } 48 | 49 | return this.teamId; 50 | } 51 | 52 | getServerVersion() { 53 | return this.serverVersion; 54 | } 55 | 56 | getBaseRoute() { 57 | return `${this.url}${this.urlVersion}`; 58 | } 59 | 60 | getAdminRoute() { 61 | return `${this.url}${this.urlVersion}/admin`; 62 | } 63 | 64 | getGeneralRoute() { 65 | return `${this.url}${this.urlVersion}/general`; 66 | } 67 | 68 | getLicenseRoute() { 69 | return `${this.url}${this.urlVersion}/license`; 70 | } 71 | 72 | getTeamsRoute() { 73 | return `${this.url}${this.urlVersion}/teams`; 74 | } 75 | 76 | getTeamNeededRoute(teamId = this.getTeamId()) { 77 | return `${this.url}${this.urlVersion}/teams/${teamId}`; 78 | } 79 | 80 | getChannelsRoute() { 81 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/channels`; 82 | } 83 | 84 | getChannelNameRoute(channelName) { 85 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/channels/name/${channelName}`; 86 | } 87 | 88 | getChannelNeededRoute(channelId) { 89 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/channels/${channelId}`; 90 | } 91 | 92 | getCommandsRoute() { 93 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/commands`; 94 | } 95 | 96 | getEmojiRoute() { 97 | return `${this.url}${this.urlVersion}/emoji`; 98 | } 99 | 100 | getHooksRoute() { 101 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/hooks`; 102 | } 103 | 104 | getPostsRoute(channelId) { 105 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/channels/${channelId}/posts`; 106 | } 107 | 108 | getUsersRoute() { 109 | return `${this.url}${this.urlVersion}/users`; 110 | } 111 | 112 | getTeamFilesRoute() { 113 | return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/files`; 114 | } 115 | 116 | getFileRoute(fileId) { 117 | return `${this.url}${this.urlVersion}/files/${fileId}`; 118 | } 119 | 120 | getOAuthRoute() { 121 | return `${this.url}${this.urlVersion}/oauth`; 122 | } 123 | 124 | getUserNeededRoute(userId) { 125 | return `${this.url}${this.urlVersion}/users/${userId}`; 126 | } 127 | 128 | getWebrtcRoute() { 129 | return `${this.url}${this.urlVersion}/webrtc`; 130 | } 131 | 132 | setTranslations(messages) { 133 | this.translations = messages; 134 | } 135 | 136 | enableLogErrorsToConsole(enabled) { 137 | this.logToConsole = enabled; 138 | } 139 | 140 | useHeaderToken() { 141 | this.useToken = true; 142 | if (this.token !== '') { 143 | this.defaultHeaders[HEADER_AUTH] = `${HEADER_BEARER} ${this.token}`; 144 | } 145 | } 146 | 147 | trackEvent(category, event, properties) { // eslint-disable-line no-unused-vars 148 | // NO-OP for inherited classes to override 149 | } 150 | 151 | handleError(err, res) { // eslint-disable-line no-unused-vars 152 | // NO-OP for inherited classes to override 153 | } 154 | 155 | handleSuccess(res) { // eslint-disable-line no-unused-vars 156 | // NO-OP for inherited classes to override 157 | } 158 | 159 | handleResponse(methodName, successCallback, errorCallback, err, res) { 160 | if (res && res.header) { 161 | if (res.header[HEADER_X_VERSION_ID]) { 162 | this.serverVersion = res.header[HEADER_X_VERSION_ID]; 163 | } 164 | 165 | if (res.header[HEADER_X_CLUSTER_ID]) { 166 | this.clusterId = res.header[HEADER_X_CLUSTER_ID]; 167 | } 168 | } 169 | 170 | if (err) { 171 | // test to make sure it looks like a server JSON error response 172 | var e = null; 173 | if (res && res.body && res.body.id) { 174 | e = res.body; 175 | } 176 | 177 | var msg = ''; 178 | 179 | if (e) { 180 | msg = 'method=' + methodName + ' msg=' + e.message + ' detail=' + e.detailed_error + ' rid=' + e.request_id; 181 | } else { 182 | msg = 'method=' + methodName + ' status=' + err.status + ' statusCode=' + err.statusCode + ' err=' + err; 183 | 184 | if (err.status === 0 || !err.status) { 185 | e = {message: this.translations.connectionError}; 186 | } else { 187 | e = {message: this.translations.unknownError + ' (' + err.status + ')'}; 188 | } 189 | } 190 | 191 | if (this.logToConsole) { 192 | console.error(msg); // eslint-disable-line no-console 193 | console.error(e); // eslint-disable-line no-console 194 | } 195 | 196 | this.handleError(err, res); 197 | 198 | if (errorCallback) { 199 | errorCallback(e, err, res); 200 | } 201 | return; 202 | } 203 | 204 | if (successCallback) { 205 | if (res && res.body !== undefined) { // eslint-disable-line no-undefined 206 | successCallback(res.body, res); 207 | } else { 208 | console.error('Missing response body for ' + methodName); // eslint-disable-line no-console 209 | successCallback('', res); 210 | } 211 | this.handleSuccess(res); 212 | } 213 | } 214 | 215 | // General Routes Section 216 | 217 | getClientConfig(success, error) { 218 | return request. 219 | get(`${this.getGeneralRoute()}/client_props`). 220 | set(this.defaultHeaders). 221 | type('application/json'). 222 | accept('application/json'). 223 | end(this.handleResponse.bind(this, 'getClientConfig', success, error)); 224 | } 225 | 226 | getPing(success, error) { 227 | return request. 228 | get(`${this.getGeneralRoute()}/ping`). 229 | set(this.defaultHeaders). 230 | type('application/json'). 231 | accept('application/json'). 232 | end(this.handleResponse.bind(this, 'getPing', success, error)); 233 | } 234 | 235 | logClientError(msg) { 236 | var l = {}; 237 | l.level = 'ERROR'; 238 | l.message = msg; 239 | 240 | request. 241 | post(`${this.getGeneralRoute()}/log_client`). 242 | set(this.defaultHeaders). 243 | type('application/json'). 244 | accept('application/json'). 245 | send(l). 246 | end(this.handleResponse.bind(this, 'logClientError', null, null)); 247 | } 248 | 249 | // Admin / Licensing Routes Section 250 | 251 | reloadConfig(success, error) { 252 | return request. 253 | get(`${this.getAdminRoute()}/reload_config`). 254 | set(this.defaultHeaders). 255 | type('application/json'). 256 | accept('application/json'). 257 | end(this.handleResponse.bind(this, 'reloadConfig', success, error)); 258 | } 259 | 260 | invalidateAllCaches(success, error) { 261 | return request. 262 | get(`${this.getAdminRoute()}/invalidate_all_caches`). 263 | set(this.defaultHeaders). 264 | type('application/json'). 265 | accept('application/json'). 266 | end(this.handleResponse.bind(this, 'invalidate_all_caches', success, error)); 267 | } 268 | 269 | recycleDatabaseConnection(success, error) { 270 | return request. 271 | get(`${this.getAdminRoute()}/recycle_db_conn`). 272 | set(this.defaultHeaders). 273 | type('application/json'). 274 | accept('application/json'). 275 | end(this.handleResponse.bind(this, 'recycleDatabaseConnection', success, error)); 276 | } 277 | 278 | getTranslations(url, success, error) { 279 | return request. 280 | get(url). 281 | set(this.defaultHeaders). 282 | type('application/json'). 283 | accept('application/json'). 284 | end(this.handleResponse.bind(this, 'getTranslations', success, error)); 285 | } 286 | 287 | getComplianceReports(success, error) { 288 | return request. 289 | get(`${this.getAdminRoute()}/compliance_reports`). 290 | set(this.defaultHeaders). 291 | type('application/json'). 292 | accept('application/json'). 293 | end(this.handleResponse.bind(this, 'getComplianceReports', success, error)); 294 | } 295 | 296 | uploadBrandImage(image, success, error) { 297 | request. 298 | post(`${this.getAdminRoute()}/upload_brand_image`). 299 | set(this.defaultHeaders). 300 | accept('application/json'). 301 | attach('image', image, image.name). 302 | end(this.handleResponse.bind(this, 'uploadBrandImage', success, error)); 303 | } 304 | 305 | saveComplianceReports(job, success, error) { 306 | return request. 307 | post(`${this.getAdminRoute()}/save_compliance_report`). 308 | set(this.defaultHeaders). 309 | type('application/json'). 310 | accept('application/json'). 311 | send(job). 312 | end(this.handleResponse.bind(this, 'saveComplianceReports', success, error)); 313 | } 314 | 315 | getLogs(success, error) { 316 | return request. 317 | get(`${this.getAdminRoute()}/logs`). 318 | set(this.defaultHeaders). 319 | type('application/json'). 320 | accept('application/json'). 321 | end(this.handleResponse.bind(this, 'getLogs', success, error)); 322 | } 323 | 324 | getClusterStatus(success, error) { 325 | return request. 326 | get(`${this.getAdminRoute()}/cluster_status`). 327 | set(this.defaultHeaders). 328 | type('application/json'). 329 | accept('application/json'). 330 | end(this.handleResponse.bind(this, 'getClusterStatus', success, error)); 331 | } 332 | 333 | getServerAudits(success, error) { 334 | return request. 335 | get(`${this.getAdminRoute()}/audits`). 336 | set(this.defaultHeaders). 337 | type('application/json'). 338 | accept('application/json'). 339 | end(this.handleResponse.bind(this, 'getServerAudits', success, error)); 340 | } 341 | 342 | getConfig(success, error) { 343 | return request. 344 | get(`${this.getAdminRoute()}/config`). 345 | set(this.defaultHeaders). 346 | type('application/json'). 347 | accept('application/json'). 348 | end(this.handleResponse.bind(this, 'getConfig', success, error)); 349 | } 350 | 351 | getAnalytics(name, teamId, success, error) { 352 | let url = `${this.getAdminRoute()}/analytics/`; 353 | if (teamId == null) { 354 | url += name; 355 | } else { 356 | url += teamId + '/' + name; 357 | } 358 | 359 | return request. 360 | get(url). 361 | set(this.defaultHeaders). 362 | type('application/json'). 363 | accept('application/json'). 364 | end(this.handleResponse.bind(this, 'getAnalytics', success, error)); 365 | } 366 | 367 | getTeamAnalytics(teamId, name, success, error) { 368 | return request. 369 | get(`${this.getAdminRoute()}/analytics/${teamId}/${name}`). 370 | set(this.defaultHeaders). 371 | type('application/json'). 372 | accept('application/json'). 373 | end(this.handleResponse.bind(this, 'getTeamAnalytics', success, error)); 374 | } 375 | 376 | saveConfig(config, success, error) { 377 | request. 378 | post(`${this.getAdminRoute()}/save_config`). 379 | set(this.defaultHeaders). 380 | type('application/json'). 381 | accept('application/json'). 382 | send(config). 383 | end(this.handleResponse.bind(this, 'saveConfig', success, error)); 384 | } 385 | 386 | testEmail(config, success, error) { 387 | request. 388 | post(`${this.getAdminRoute()}/test_email`). 389 | set(this.defaultHeaders). 390 | type('application/json'). 391 | accept('application/json'). 392 | send(config). 393 | end(this.handleResponse.bind(this, 'testEmail', success, error)); 394 | } 395 | 396 | getClientLicenceConfig(success, error) { 397 | request. 398 | get(`${this.getLicenseRoute()}/client_config`). 399 | set(this.defaultHeaders). 400 | type('application/json'). 401 | accept('application/json'). 402 | end(this.handleResponse.bind(this, 'getClientLicenceConfig', success, error)); 403 | } 404 | 405 | removeLicenseFile(success, error) { 406 | request. 407 | post(`${this.getLicenseRoute()}/remove`). 408 | set(this.defaultHeaders). 409 | type('application/json'). 410 | accept('application/json'). 411 | end(this.handleResponse.bind(this, 'removeLicenseFile', success, error)); 412 | } 413 | 414 | uploadLicenseFile(license, success, error) { 415 | request. 416 | post(`${this.getLicenseRoute()}/add`). 417 | set(this.defaultHeaders). 418 | accept('application/json'). 419 | attach('license', license, license.name). 420 | end(this.handleResponse.bind(this, 'uploadLicenseFile', success, error)); 421 | 422 | this.trackEvent('api', 'api_license_upload'); 423 | } 424 | 425 | importSlack(fileData, success, error) { 426 | request. 427 | post(`${this.getTeamNeededRoute()}/import_team`). 428 | set(this.defaultHeaders). 429 | accept('application/octet-stream'). 430 | send(fileData). 431 | end(this.handleResponse.bind(this, 'importSlack', success, error)); 432 | } 433 | 434 | exportTeam(success, error) { 435 | request. 436 | get(`${this.getTeamsRoute()}/export_team`). 437 | set(this.defaultHeaders). 438 | type('application/json'). 439 | accept('application/json'). 440 | end(this.handleResponse.bind(this, 'exportTeam', success, error)); 441 | } 442 | 443 | adminResetMfa(userId, success, error) { 444 | const data = {}; 445 | data.user_id = userId; 446 | 447 | request. 448 | post(`${this.getAdminRoute()}/reset_mfa`). 449 | set(this.defaultHeaders). 450 | type('application/json'). 451 | accept('application/json'). 452 | send(data). 453 | end(this.handleResponse.bind(this, 'adminResetMfa', success, error)); 454 | } 455 | 456 | adminResetPassword(userId, newPassword, success, error) { 457 | var data = {}; 458 | data.new_password = newPassword; 459 | data.user_id = userId; 460 | 461 | request. 462 | post(`${this.getAdminRoute()}/reset_password`). 463 | set(this.defaultHeaders). 464 | type('application/json'). 465 | accept('application/json'). 466 | send(data). 467 | end(this.handleResponse.bind(this, 'adminResetPassword', success, error)); 468 | 469 | this.trackEvent('api', 'api_admin_reset_password'); 470 | } 471 | 472 | ldapSyncNow(success, error) { 473 | request. 474 | post(`${this.getAdminRoute()}/ldap_sync_now`). 475 | set(this.defaultHeaders). 476 | type('application/json'). 477 | accept('application/json'). 478 | end(this.handleResponse.bind(this, 'ldapSyncNow', success, error)); 479 | } 480 | 481 | ldapTest(success, error) { 482 | request. 483 | post(`${this.getAdminRoute()}/ldap_test`). 484 | set(this.defaultHeaders). 485 | type('application/json'). 486 | accept('application/json'). 487 | end(this.handleResponse.bind(this, 'ldap_test', success, error)); 488 | } 489 | 490 | // Team Routes Section 491 | 492 | getTeam(teamId, success, error) { 493 | request. 494 | get(`${this.getTeamsRoute()}/${teamId}/me`). 495 | set(this.defaultHeaders). 496 | type('application/json'). 497 | accept('application/json'). 498 | end(this.handleResponse.bind(this, 'getTeam', success, error)); 499 | } 500 | 501 | findTeamByName(teamName, success, error) { 502 | request. 503 | post(`${this.getTeamsRoute()}/find_team_by_name`). 504 | set(this.defaultHeaders). 505 | type('application/json'). 506 | accept('application/json'). 507 | send({name: teamName}). 508 | end(this.handleResponse.bind(this, 'findTeamByName', success, error)); 509 | } 510 | 511 | getTeamByName(teamName, success, error) { 512 | request. 513 | get(`${this.getTeamsRoute()}/name/${teamName}`). 514 | set(this.defaultHeaders). 515 | type('application/json'). 516 | accept('application/json'). 517 | end(this.handleResponse.bind(this, 'getTeamByName', success, error)); 518 | } 519 | 520 | createTeam(team, success, error) { 521 | request. 522 | post(`${this.getTeamsRoute()}/create`). 523 | set(this.defaultHeaders). 524 | type('application/json'). 525 | accept('application/json'). 526 | send(team). 527 | end(this.handleResponse.bind(this, 'createTeam', success, error)); 528 | 529 | this.trackEvent('api', 'api_teams_create'); 530 | } 531 | 532 | updateTeam(team, success, error) { 533 | request. 534 | post(`${this.getTeamNeededRoute()}/update`). 535 | set(this.defaultHeaders). 536 | type('application/json'). 537 | accept('application/json'). 538 | send(team). 539 | end(this.handleResponse.bind(this, 'updateTeam', success, error)); 540 | 541 | this.trackEvent('api', 'api_teams_update_name', {team_id: this.getTeamId()}); 542 | } 543 | 544 | getAllTeams(success, error) { 545 | request. 546 | get(`${this.getTeamsRoute()}/all`). 547 | set(this.defaultHeaders). 548 | type('application/json'). 549 | accept('application/json'). 550 | end(this.handleResponse.bind(this, 'getAllTeams', success, error)); 551 | } 552 | 553 | getAllTeamListings(success, error) { 554 | request. 555 | get(`${this.getTeamsRoute()}/all_team_listings`). 556 | set(this.defaultHeaders). 557 | type('application/json'). 558 | accept('application/json'). 559 | end(this.handleResponse.bind(this, 'getAllTeamListings', success, error)); 560 | } 561 | 562 | getTeamsForUser(userId, success, error) { 563 | // Call out to API v4 since this call doesn't exist in v3 564 | request. 565 | get(`${this.url}/api/v4/users/${userId}/teams`). 566 | set(this.defaultHeaders). 567 | type('application/json'). 568 | accept('application/json'). 569 | end(this.handleResponse.bind(this, 'getTeamsForUser', success, error)); 570 | } 571 | 572 | getMyTeam(success, error) { 573 | request. 574 | get(`${this.getTeamNeededRoute()}/me`). 575 | set(this.defaultHeaders). 576 | type('application/json'). 577 | accept('application/json'). 578 | end(this.handleResponse.bind(this, 'getMyTeam', success, error)); 579 | } 580 | 581 | getTeamMembers(teamId, offset, limit, success, error) { 582 | request. 583 | get(`${this.getTeamNeededRoute(teamId)}/members/${offset}/${limit}`). 584 | set(this.defaultHeaders). 585 | type('application/json'). 586 | accept('application/json'). 587 | end(this.handleResponse.bind(this, 'getTeamMembers', success, error)); 588 | } 589 | 590 | getTeamMember(teamId, userId, success, error) { 591 | request. 592 | get(`${this.getTeamNeededRoute(teamId)}/members/${userId}`). 593 | set(this.defaultHeaders). 594 | type('application/json'). 595 | accept('application/json'). 596 | end(this.handleResponse.bind(this, 'getTeamMember', success, error)); 597 | } 598 | 599 | getMyTeamMembers(success, error) { 600 | request. 601 | get(`${this.getTeamsRoute()}/members`). 602 | set(this.defaultHeaders). 603 | type('application/json'). 604 | accept('application/json'). 605 | end(this.handleResponse.bind(this, 'getMyTeamMembers', success, error)); 606 | } 607 | 608 | getTeamMembersForUser(userId, success, error) { 609 | // Call out to API v4 since this call doesn't exist in v3 610 | request. 611 | get(`${this.url}/api/v4/users/${userId}/teams/members`). 612 | set(this.defaultHeaders). 613 | type('application/json'). 614 | accept('application/json'). 615 | end(this.handleResponse.bind(this, 'getTeamsForUser', success, error)); 616 | } 617 | 618 | getMyTeamsUnread(teamId, success, error) { 619 | let url = `${this.getTeamsRoute()}/unread`; 620 | 621 | if (teamId) { 622 | url += `?id=${encodeURIComponent(teamId)}`; 623 | } 624 | 625 | request. 626 | get(url). 627 | set(this.defaultHeaders). 628 | type('application/json'). 629 | accept('application/json'). 630 | end(this.handleResponse.bind(this, 'getMyTeamsUnread', success, error)); 631 | } 632 | 633 | getTeamMembersByIds(teamId, userIds, success, error) { 634 | request. 635 | post(`${this.getTeamNeededRoute(teamId)}/members/ids`). 636 | set(this.defaultHeaders). 637 | type('application/json'). 638 | accept('application/json'). 639 | send(userIds). 640 | end(this.handleResponse.bind(this, 'getTeamMembersByIds', success, error)); 641 | } 642 | 643 | getTeamStats(teamId, success, error) { 644 | request. 645 | get(`${this.getTeamNeededRoute(teamId)}/stats`). 646 | set(this.defaultHeaders). 647 | type('application/json'). 648 | accept('application/json'). 649 | end(this.handleResponse.bind(this, 'getTeamStats', success, error)); 650 | } 651 | 652 | inviteMembers(data, success, error) { 653 | request. 654 | post(`${this.getTeamNeededRoute()}/invite_members`). 655 | set(this.defaultHeaders). 656 | type('application/json'). 657 | accept('application/json'). 658 | send(data). 659 | end(this.handleResponse.bind(this, 'inviteMembers', success, error)); 660 | 661 | this.trackEvent('api', 'api_teams_invite_members', {team_id: this.getTeamId()}); 662 | } 663 | 664 | addUserToTeam(teamId, userId, success, error) { 665 | let nonEmptyTeamId = teamId; 666 | if (nonEmptyTeamId === '') { 667 | nonEmptyTeamId = this.getTeamId(); 668 | } 669 | 670 | request. 671 | post(`${this.getTeamsRoute()}/${nonEmptyTeamId}/add_user_to_team`). 672 | set(this.defaultHeaders). 673 | type('application/json'). 674 | accept('application/json'). 675 | send({user_id: userId}). 676 | end(this.handleResponse.bind(this, 'addUserToTeam', success, error)); 677 | 678 | this.trackEvent('api', 'api_teams_invite_members', {team_id: nonEmptyTeamId}); 679 | } 680 | 681 | addUserToTeamFromInvite(data, hash, inviteId, success, error) { 682 | request. 683 | post(`${this.getTeamsRoute()}/add_user_to_team_from_invite`). 684 | set(this.defaultHeaders). 685 | type('application/json'). 686 | accept('application/json'). 687 | send({hash, data, invite_id: inviteId}). 688 | end(this.handleResponse.bind(this, 'addUserToTeam', success, error)); 689 | 690 | this.trackEvent('api', 'api_teams_invite_members'); 691 | } 692 | 693 | addUsersToTeam(teamId, userIds, success, error) { 694 | let nonEmptyTeamId = teamId; 695 | if (nonEmptyTeamId === '') { 696 | nonEmptyTeamId = this.getTeamId(); 697 | } 698 | 699 | const teamMembers = userIds.map((userId) => { 700 | return { 701 | team_id: nonEmptyTeamId, 702 | user_id: userId 703 | }; 704 | }); 705 | 706 | request. 707 | post(`${this.url}/api/v4/teams/${nonEmptyTeamId}/members/batch`). 708 | set(this.defaultHeaders). 709 | type('application/json'). 710 | accept('application/json'). 711 | send(teamMembers). 712 | end(this.handleResponse.bind(this, 'addUsersToTeam', success, error)); 713 | 714 | this.trackEvent('api', 'api_teams_batch_add_members', {team_id: nonEmptyTeamId, count: teamMembers.length}); 715 | } 716 | 717 | removeUserFromTeam(teamId, userId, success, error) { 718 | let nonEmptyTeamId = teamId; 719 | if (nonEmptyTeamId === '') { 720 | nonEmptyTeamId = this.getTeamId(); 721 | } 722 | 723 | request. 724 | post(`${this.getTeamsRoute()}/${nonEmptyTeamId}/remove_user_from_team`). 725 | set(this.defaultHeaders). 726 | type('application/json'). 727 | accept('application/json'). 728 | send({user_id: userId}). 729 | end(this.handleResponse.bind(this, 'removeUserFromTeam', success, error)); 730 | 731 | this.trackEvent('api', 'api_teams_remove_members', {team_id: nonEmptyTeamId}); 732 | } 733 | 734 | getInviteInfo(inviteId, success, error) { 735 | request. 736 | post(`${this.getTeamsRoute()}/get_invite_info`). 737 | set(this.defaultHeaders). 738 | type('application/json'). 739 | accept('application/json'). 740 | send({invite_id: inviteId}). 741 | end(this.handleResponse.bind(this, 'getInviteInfo', success, error)); 742 | } 743 | 744 | // User Routes Setions 745 | 746 | createUser(user, success, error) { 747 | this.createUserWithInvite(user, null, null, null, success, error); 748 | } 749 | 750 | createUserWithInvite(user, data, emailHash, inviteId, success, error) { 751 | var url = `${this.getUsersRoute()}/create`; 752 | 753 | url += '?d=' + encodeURIComponent(data); 754 | 755 | if (emailHash) { 756 | url += '&h=' + encodeURIComponent(emailHash); 757 | } 758 | 759 | if (inviteId) { 760 | url += '&iid=' + encodeURIComponent(inviteId); 761 | } 762 | 763 | if (emailHash) { 764 | this.trackEvent('api', 'api_users_create_email'); 765 | } else if (inviteId) { 766 | this.trackEvent('api', 'api_users_create_link'); 767 | } else { 768 | this.trackEvent('api', 'api_users_create_spontaneous'); 769 | } 770 | 771 | request. 772 | post(url). 773 | set(this.defaultHeaders). 774 | type('application/json'). 775 | accept('application/json'). 776 | send(user). 777 | end(this.handleResponse.bind(this, 'createUser', success, error)); 778 | 779 | this.trackEvent('api', 'api_users_create'); 780 | } 781 | 782 | updateUser(user, type, success, error) { 783 | request. 784 | post(`${this.getUsersRoute()}/update`). 785 | set(this.defaultHeaders). 786 | type('application/json'). 787 | accept('application/json'). 788 | send(user). 789 | end(this.handleResponse.bind(this, 'updateUser', success, error)); 790 | 791 | if (type) { 792 | this.trackEvent('api', 'api_users_update_' + type); 793 | } else { 794 | this.trackEvent('api', 'api_users_update'); 795 | } 796 | } 797 | 798 | updatePassword(userId, currentPassword, newPassword, success, error) { 799 | var data = {}; 800 | data.user_id = userId; 801 | data.current_password = currentPassword; 802 | data.new_password = newPassword; 803 | 804 | request. 805 | post(`${this.getUsersRoute()}/newpassword`). 806 | set(this.defaultHeaders). 807 | type('application/json'). 808 | accept('application/json'). 809 | send(data). 810 | end(this.handleResponse.bind(this, 'updatePassword', success, error)); 811 | 812 | this.trackEvent('api', 'api_users_newpassword'); 813 | } 814 | 815 | updateUserNotifyProps(notifyProps, success, error) { 816 | request. 817 | post(`${this.getUsersRoute()}/update_notify`). 818 | set(this.defaultHeaders). 819 | type('application/json'). 820 | accept('application/json'). 821 | send(notifyProps). 822 | end(this.handleResponse.bind(this, 'updateUserNotifyProps', success, error)); 823 | 824 | this.trackEvent('api', 'api_users_update_notification_settings'); 825 | } 826 | 827 | updateUserRoles(userId, newRoles, success, error) { 828 | var data = { 829 | new_roles: newRoles 830 | }; 831 | 832 | request. 833 | post(`${this.getUserNeededRoute(userId)}/update_roles`). 834 | set(this.defaultHeaders). 835 | type('application/json'). 836 | accept('application/json'). 837 | send(data). 838 | end(this.handleResponse.bind(this, 'updateUserRoles', success, error)); 839 | 840 | this.trackEvent('api', 'api_users_update_roles'); 841 | } 842 | 843 | updateTeamMemberRoles(teamId, userId, newRoles, success, error) { 844 | var data = { 845 | user_id: userId, 846 | new_roles: newRoles 847 | }; 848 | 849 | request. 850 | post(`${this.getTeamNeededRoute(teamId)}/update_member_roles`). 851 | set(this.defaultHeaders). 852 | type('application/json'). 853 | accept('application/json'). 854 | send(data). 855 | end(this.handleResponse.bind(this, 'updateTeamMemberRoles', success, error)); 856 | 857 | this.trackEvent('api', 'api_teams_update_member_roles', {team_id: teamId}); 858 | } 859 | 860 | updateActive(userId, active, success, error) { 861 | var data = {}; 862 | data.user_id = userId; 863 | data.active = String(active); 864 | 865 | request. 866 | post(`${this.getUsersRoute()}/update_active`). 867 | set(this.defaultHeaders). 868 | type('application/json'). 869 | accept('application/json'). 870 | send(data). 871 | end(this.handleResponse.bind(this, 'updateActive', success, error)); 872 | 873 | this.trackEvent('api', 'api_users_update_active'); 874 | } 875 | 876 | sendPasswordReset(email, success, error) { 877 | var data = {}; 878 | data.email = email; 879 | 880 | request. 881 | post(`${this.getUsersRoute()}/send_password_reset`). 882 | set(this.defaultHeaders). 883 | type('application/json'). 884 | accept('application/json'). 885 | send(data). 886 | end(this.handleResponse.bind(this, 'sendPasswordReset', success, error)); 887 | 888 | this.trackEvent('api', 'api_users_send_password_reset'); 889 | } 890 | 891 | resetPassword(code, newPassword, success, error) { 892 | var data = {}; 893 | data.new_password = newPassword; 894 | data.code = code; 895 | 896 | request. 897 | post(`${this.getUsersRoute()}/reset_password`). 898 | set(this.defaultHeaders). 899 | type('application/json'). 900 | accept('application/json'). 901 | send(data). 902 | end(this.handleResponse.bind(this, 'resetPassword', success, error)); 903 | 904 | this.trackEvent('api', 'api_users_reset_password'); 905 | } 906 | 907 | emailToOAuth(email, password, token, service, success, error) { 908 | request. 909 | post(`${this.getUsersRoute()}/claim/email_to_oauth`). 910 | set(this.defaultHeaders). 911 | type('application/json'). 912 | accept('application/json'). 913 | send({password, email, token, service}). 914 | end(this.handleResponse.bind(this, 'emailToOAuth', success, error)); 915 | 916 | this.trackEvent('api', 'api_users_email_to_oauth'); 917 | } 918 | 919 | oauthToEmail(email, password, success, error) { 920 | var data = {}; 921 | data.password = password; 922 | data.email = email; 923 | 924 | request. 925 | post(`${this.getUsersRoute()}/claim/oauth_to_email`). 926 | set(this.defaultHeaders). 927 | type('application/json'). 928 | accept('application/json'). 929 | send(data). 930 | end(this.handleResponse.bind(this, 'oauthToEmail', success, error)); 931 | 932 | this.trackEvent('api', 'api_users_oauth_to_email'); 933 | } 934 | 935 | emailToLdap(email, password, token, ldapId, ldapPassword, success, error) { 936 | var data = {}; 937 | data.email_password = password; 938 | data.email = email; 939 | data.ldap_id = ldapId; 940 | data.ldap_password = ldapPassword; 941 | data.token = token; 942 | 943 | request. 944 | post(`${this.getUsersRoute()}/claim/email_to_ldap`). 945 | set(this.defaultHeaders). 946 | type('application/json'). 947 | accept('application/json'). 948 | send(data). 949 | end(this.handleResponse.bind(this, 'emailToLdap', success, error)); 950 | 951 | this.trackEvent('api', 'api_users_email_to_ldap'); 952 | } 953 | 954 | ldapToEmail(email, emailPassword, token, ldapPassword, success, error) { 955 | var data = {}; 956 | data.email = email; 957 | data.ldap_password = ldapPassword; 958 | data.email_password = emailPassword; 959 | data.token = token; 960 | 961 | request. 962 | post(`${this.getUsersRoute()}/claim/ldap_to_email`). 963 | set(this.defaultHeaders). 964 | type('application/json'). 965 | accept('application/json'). 966 | send(data). 967 | end(this.handleResponse.bind(this, 'ldapToEmail', success, error)); 968 | 969 | this.trackEvent('api', 'api_users_ldap_to_email'); 970 | } 971 | 972 | getInitialLoad(success, error) { 973 | request. 974 | get(`${this.getUsersRoute()}/initial_load`). 975 | set(this.defaultHeaders). 976 | type('application/json'). 977 | accept('application/json'). 978 | end(this.handleResponse.bind(this, 'getInitialLoad', success, error)); 979 | } 980 | 981 | getMe(success, error) { 982 | request. 983 | get(`${this.getUsersRoute()}/me`). 984 | set(this.defaultHeaders). 985 | type('application/json'). 986 | accept('application/json'). 987 | end(this.handleResponse.bind(this, 'getMe', success, error)); 988 | } 989 | 990 | getUser(userId, success, error) { 991 | request. 992 | get(`${this.getUserNeededRoute(userId)}/get`). 993 | set(this.defaultHeaders). 994 | type('application/json'). 995 | accept('application/json'). 996 | end(this.handleResponse.bind(this, 'getUser', success, error)); 997 | } 998 | 999 | getByUsername(userName, success, error) { 1000 | request. 1001 | get(`${this.getUsersRoute()}/name/${userName}`). 1002 | set(this.defaultHeaders). 1003 | type('application/json'). 1004 | accept('application/json'). 1005 | end(this.handleResponse.bind(this, 'getByUsername', success, error)); 1006 | } 1007 | 1008 | getByEmail(email, success, error) { 1009 | request. 1010 | get(`${this.getUsersRoute()}/email/${email}`). 1011 | set(this.defaultHeaders). 1012 | type('application/json'). 1013 | accept('application/json'). 1014 | end(this.handleResponse.bind(this, 'getByEmail', success, error)); 1015 | } 1016 | 1017 | login(loginId, password, mfaToken, success, error) { 1018 | this.doLogin({login_id: loginId, password, token: mfaToken}, success, error); 1019 | 1020 | this.trackEvent('api', 'api_users_login'); 1021 | } 1022 | 1023 | loginById(id, password, mfaToken, success, error) { 1024 | this.doLogin({id, password, token: mfaToken}, success, error); 1025 | 1026 | this.trackEvent('api', 'api_users_login'); 1027 | } 1028 | 1029 | loginByLdap(loginId, password, mfaToken, success, error) { 1030 | this.doLogin({login_id: loginId, password, token: mfaToken, ldap_only: 'true'}, success, error); 1031 | 1032 | this.trackEvent('api', 'api_users_login'); 1033 | this.trackEvent('api', 'api_users_login_ldap'); 1034 | } 1035 | 1036 | doLogin(outgoingData, success, error) { 1037 | var outer = this; // eslint-disable-line consistent-this 1038 | 1039 | request. 1040 | post(`${this.getUsersRoute()}/login`). 1041 | set(this.defaultHeaders). 1042 | type('application/json'). 1043 | accept('application/json'). 1044 | send(outgoingData). 1045 | end(this.handleResponse.bind( 1046 | this, 1047 | 'login', 1048 | (data, res) => { 1049 | if (res && res.header) { 1050 | outer.token = res.header[HEADER_TOKEN]; 1051 | 1052 | if (outer.useToken) { 1053 | outer.defaultHeaders[HEADER_AUTH] = `${HEADER_BEARER} ${outer.token}`; 1054 | } 1055 | } 1056 | 1057 | if (success) { 1058 | success(data, res); 1059 | } 1060 | }, 1061 | error 1062 | )); 1063 | } 1064 | 1065 | logout(success, error) { 1066 | request. 1067 | post(`${this.getUsersRoute()}/logout`). 1068 | set(this.defaultHeaders). 1069 | type('application/json'). 1070 | accept('application/json'). 1071 | end(this.handleResponse.bind(this, 'logout', success, error)); 1072 | 1073 | this.trackEvent('api', 'api_users_logout'); 1074 | } 1075 | 1076 | checkMfa(loginId, success, error) { 1077 | const data = { 1078 | login_id: loginId 1079 | }; 1080 | 1081 | request. 1082 | post(`${this.getUsersRoute()}/mfa`). 1083 | set(this.defaultHeaders). 1084 | type('application/json'). 1085 | accept('application/json'). 1086 | send(data). 1087 | end(this.handleResponse.bind(this, 'checkMfa', success, error)); 1088 | 1089 | this.trackEvent('api', 'api_users_oauth_to_email'); 1090 | } 1091 | 1092 | generateMfaSecret(success, error) { 1093 | request. 1094 | get(`${this.getUsersRoute()}/generate_mfa_secret`). 1095 | set(this.defaultHeaders). 1096 | type('application/json'). 1097 | accept('application/json'). 1098 | end(this.handleResponse.bind(this, 'generateMfaSecret', success, error)); 1099 | } 1100 | 1101 | revokeSession(altId, success, error) { 1102 | request. 1103 | post(`${this.getUsersRoute()}/revoke_session`). 1104 | set(this.defaultHeaders). 1105 | type('application/json'). 1106 | accept('application/json'). 1107 | send({id: altId}). 1108 | end(this.handleResponse.bind(this, 'revokeSession', success, error)); 1109 | } 1110 | 1111 | getSessions(userId, success, error) { 1112 | request. 1113 | get(`${this.getUserNeededRoute(userId)}/sessions`). 1114 | set(this.defaultHeaders). 1115 | type('application/json'). 1116 | accept('application/json'). 1117 | end(this.handleResponse.bind(this, 'getSessions', success, error)); 1118 | } 1119 | 1120 | getAudits(userId, success, error) { 1121 | request. 1122 | get(`${this.getUserNeededRoute(userId)}/audits`). 1123 | set(this.defaultHeaders). 1124 | type('application/json'). 1125 | accept('application/json'). 1126 | end(this.handleResponse.bind(this, 'getAudits', success, error)); 1127 | } 1128 | 1129 | getRecentlyActiveUsers(id, success, error) { 1130 | request. 1131 | get(`${this.getAdminRoute()}/recently_active_users/${id}`). 1132 | set(this.defaultHeaders). 1133 | type('application/json'). 1134 | accept('application/json'). 1135 | end(this.handleResponse.bind(this, 'getRecentlyActiveUsers', success, error)); 1136 | } 1137 | 1138 | getProfiles(offset, limit, success, error) { 1139 | request. 1140 | get(`${this.getUsersRoute()}/${offset}/${limit}`). 1141 | set(this.defaultHeaders). 1142 | type('application/json'). 1143 | accept('application/json'). 1144 | end(this.handleResponse.bind(this, 'getProfiles', success, error)); 1145 | 1146 | this.trackEvent('api', 'api_profiles_get'); 1147 | } 1148 | 1149 | getProfilesInTeam(teamId, offset, limit, success, error) { 1150 | request. 1151 | get(`${this.getTeamNeededRoute(teamId)}/users/${offset}/${limit}`). 1152 | set(this.defaultHeaders). 1153 | type('application/json'). 1154 | accept('application/json'). 1155 | end(this.handleResponse.bind(this, 'getProfilesInTeam', success, error)); 1156 | 1157 | this.trackEvent('api', 'api_profiles_get_in_team', {team_id: teamId}); 1158 | } 1159 | 1160 | getProfilesNotInTeam(teamId, offset, limit, success, error) { 1161 | // Super hacky, but this option only exists in api v4 1162 | function wrappedSuccess(data, res) { 1163 | // Convert the profile list provided by api v4 to a map to match similar v3 calls 1164 | const profiles = {}; 1165 | 1166 | for (const profile of data) { 1167 | profiles[profile.id] = profile; 1168 | } 1169 | 1170 | success(profiles, res); 1171 | } 1172 | 1173 | request. 1174 | get(`${this.url}/api/v4/users?not_in_team=${this.getTeamId()}&page=${offset}&per_page=${limit}`). 1175 | set(this.defaultHeaders). 1176 | type('application/json'). 1177 | accept('application/json'). 1178 | end(this.handleResponse.bind(this, 'getProfilesNotInTeam', wrappedSuccess, error)); 1179 | 1180 | this.trackEvent('api', 'api_profiles_get_not_in_team', {team_id: teamId}); 1181 | } 1182 | 1183 | getProfilesInChannel(channelId, offset, limit, success, error) { 1184 | request. 1185 | get(`${this.getChannelNeededRoute(channelId)}/users/${offset}/${limit}`). 1186 | set(this.defaultHeaders). 1187 | type('application/json'). 1188 | accept('application/json'). 1189 | end(this.handleResponse.bind(this, 'getProfilesInChannel', success, error)); 1190 | 1191 | this.trackEvent('api', 'api_profiles_get_in_channel', {team_id: this.getTeamId(), channel_id: channelId}); 1192 | } 1193 | 1194 | getProfilesNotInChannel(channelId, offset, limit, success, error) { 1195 | request. 1196 | get(`${this.getChannelNeededRoute(channelId)}/users/not_in_channel/${offset}/${limit}`). 1197 | set(this.defaultHeaders). 1198 | type('application/json'). 1199 | accept('application/json'). 1200 | end(this.handleResponse.bind(this, 'getProfilesNotInChannel', success, error)); 1201 | 1202 | this.trackEvent('api', 'api_profiles_get_not_in_channel', {team_id: this.getTeamId(), channel_id: channelId}); 1203 | } 1204 | 1205 | getProfilesWithoutTeam(page, perPage, success, error) { 1206 | // Super hacky, but this option only exists in api v4 1207 | function wrappedSuccess(data, res) { 1208 | // Convert the profile list provided by api v4 to a map to match similar v3 calls 1209 | const profiles = {}; 1210 | 1211 | for (const profile of data) { 1212 | profiles[profile.id] = profile; 1213 | } 1214 | 1215 | success(profiles, res); 1216 | } 1217 | 1218 | request. 1219 | get(`${this.url}/api/v4/users?without_team=1&page=${page}&per_page=${perPage}`). 1220 | set(this.defaultHeaders). 1221 | type('application/json'). 1222 | accept('application/json'). 1223 | end(this.handleResponse.bind(this, 'getProfilesWithoutTeam', wrappedSuccess, error)); 1224 | 1225 | this.trackEvent('api', 'api_profiles_get_without_team'); 1226 | } 1227 | 1228 | getProfilesByIds(userIds, success, error) { 1229 | request. 1230 | post(`${this.getUsersRoute()}/ids`). 1231 | set(this.defaultHeaders). 1232 | type('application/json'). 1233 | accept('application/json'). 1234 | send(userIds). 1235 | end(this.handleResponse.bind(this, 'getProfilesByIds', success, error)); 1236 | 1237 | this.trackEvent('api', 'api_profiles_get_by_ids'); 1238 | } 1239 | 1240 | searchUsers(term, teamId, options, success, error) { 1241 | request. 1242 | post(`${this.getUsersRoute()}/search`). 1243 | set(this.defaultHeaders). 1244 | type('application/json'). 1245 | accept('application/json'). 1246 | send({term, team_id: teamId, ...options}). 1247 | end(this.handleResponse.bind(this, 'searchUsers', success, error)); 1248 | } 1249 | 1250 | searchUsersNotInTeam(term, teamId, options, success, error) { 1251 | // Note that this is calling an APIv4 Endpoint since no APIv3 equivalent exists. 1252 | request. 1253 | post(`${this.url}/api/v4/users/search`). 1254 | set(this.defaultHeaders). 1255 | type('application/json'). 1256 | accept('application/json'). 1257 | send({term, not_in_team_id: teamId, ...options}). 1258 | end(this.handleResponse.bind(this, 'searchUsersNotInTeam', success, error)); 1259 | 1260 | this.trackEvent('api', 'api_search_users_not_in_team', {team_id: teamId}); 1261 | } 1262 | 1263 | autocompleteUsersInChannel(term, channelId, success, error) { 1264 | request. 1265 | get(`${this.getChannelNeededRoute(channelId)}/users/autocomplete?term=${encodeURIComponent(term)}`). 1266 | set(this.defaultHeaders). 1267 | type('application/json'). 1268 | accept('application/json'). 1269 | end(this.handleResponse.bind(this, 'autocompleteUsersInChannel', success, error)); 1270 | } 1271 | 1272 | autocompleteUsersInTeam(term, success, error) { 1273 | request. 1274 | get(`${this.getTeamNeededRoute()}/users/autocomplete?term=${encodeURIComponent(term)}`). 1275 | set(this.defaultHeaders). 1276 | type('application/json'). 1277 | accept('application/json'). 1278 | end(this.handleResponse.bind(this, 'autocompleteUsersInTeam', success, error)); 1279 | } 1280 | 1281 | autocompleteUsers(term, success, error) { 1282 | request. 1283 | get(`${this.getUsersRoute()}/autocomplete?term=${encodeURIComponent(term)}`). 1284 | set(this.defaultHeaders). 1285 | type('application/json'). 1286 | accept('application/json'). 1287 | end(this.handleResponse.bind(this, 'autocompleteUsers', success, error)); 1288 | } 1289 | 1290 | getStatuses(success, error) { 1291 | request. 1292 | get(`${this.getUsersRoute()}/status`). 1293 | set(this.defaultHeaders). 1294 | type('application/json'). 1295 | accept('application/json'). 1296 | end(this.handleResponse.bind(this, 'getStatuses', success, error)); 1297 | } 1298 | 1299 | getStatusesByIds(userIds, success, error) { 1300 | request. 1301 | post(`${this.getUsersRoute()}/status/ids`). 1302 | set(this.defaultHeaders). 1303 | type('application/json'). 1304 | accept('application/json'). 1305 | send(userIds). 1306 | end(this.handleResponse.bind(this, 'getStatuses', success, error)); 1307 | } 1308 | 1309 | // SCHEDULED FOR DEPRECATION IN 3.8 - use viewChannel instead 1310 | setActiveChannel(id, success, error) { 1311 | request. 1312 | post(`${this.getUsersRoute()}/status/set_active_channel`). 1313 | set(this.defaultHeaders). 1314 | type('application/json'). 1315 | accept('application/json'). 1316 | send({channel_id: id}). 1317 | end(this.handleResponse.bind(this, 'setActiveChannel', success, error)); 1318 | 1319 | this.trackEvent('api', 'api_channels_set_active', {channel_id: id}); 1320 | } 1321 | 1322 | verifyEmail(token, success, error) { 1323 | request. 1324 | post(`${this.url}/api/v4/users/email/verify`). 1325 | set(this.defaultHeaders). 1326 | type('application/json'). 1327 | accept('application/json'). 1328 | send({token}). 1329 | end(this.handleResponse.bind(this, 'verifyEmail', success, error)); 1330 | } 1331 | 1332 | resendVerification(email, success, error) { 1333 | request. 1334 | post(`${this.url}/api/v4/users/email/verify/send`). 1335 | set(this.defaultHeaders). 1336 | type('application/json'). 1337 | accept('application/json'). 1338 | send({email}). 1339 | end(this.handleResponse.bind(this, 'resendVerification', success, error)); 1340 | } 1341 | 1342 | updateMfa(token, activate, success, error) { 1343 | const data = {}; 1344 | data.activate = activate; 1345 | data.token = token; 1346 | 1347 | request. 1348 | post(`${this.getUsersRoute()}/update_mfa`). 1349 | set(this.defaultHeaders). 1350 | type('application/json'). 1351 | accept('application/json'). 1352 | send(data). 1353 | end(this.handleResponse.bind(this, 'updateMfa', success, error)); 1354 | } 1355 | 1356 | uploadProfileImage(image, success, error) { 1357 | request. 1358 | post(`${this.getUsersRoute()}/newimage`). 1359 | set(this.defaultHeaders). 1360 | attach('image', image, image.name). 1361 | accept('application/json'). 1362 | end(this.handleResponse.bind(this, 'uploadProfileImage', success, error)); 1363 | 1364 | this.trackEvent('api', 'api_users_update_profile_picture'); 1365 | } 1366 | 1367 | getProfilePictureUrl(id, lastPictureUpdate) { 1368 | let url = `${this.getUsersRoute()}/${id}/image`; 1369 | 1370 | if (lastPictureUpdate) { 1371 | url += `?time=${lastPictureUpdate}`; 1372 | } 1373 | 1374 | return url; 1375 | } 1376 | 1377 | // Channel Routes Section 1378 | 1379 | createChannel(channel, success, error) { 1380 | request. 1381 | post(`${this.getChannelsRoute()}/create`). 1382 | set(this.defaultHeaders). 1383 | type('application/json'). 1384 | accept('application/json'). 1385 | send(channel). 1386 | end(this.handleResponse.bind(this, 'createChannel', success, error)); 1387 | 1388 | this.trackEvent('api', 'api_channels_create', {team_id: this.getTeamId()}); 1389 | } 1390 | 1391 | createDirectChannel(userId, success, error) { 1392 | request. 1393 | post(`${this.getChannelsRoute()}/create_direct`). 1394 | set(this.defaultHeaders). 1395 | type('application/json'). 1396 | accept('application/json'). 1397 | send({user_id: userId}). 1398 | end(this.handleResponse.bind(this, 'createDirectChannel', success, error)); 1399 | 1400 | this.trackEvent('api', 'api_channels_create_direct', {team_id: this.getTeamId()}); 1401 | } 1402 | 1403 | createGroupChannel(userIds, success, error) { 1404 | request. 1405 | post(`${this.getChannelsRoute()}/create_group`). 1406 | set(this.defaultHeaders). 1407 | type('application/json'). 1408 | accept('application/json'). 1409 | send(userIds). 1410 | end(this.handleResponse.bind(this, 'createGroupChannel', success, error)); 1411 | } 1412 | 1413 | updateChannel(channel, success, error) { 1414 | request. 1415 | post(`${this.getChannelsRoute()}/update`). 1416 | set(this.defaultHeaders). 1417 | type('application/json'). 1418 | accept('application/json'). 1419 | send(channel). 1420 | end(this.handleResponse.bind(this, 'updateChannel', success, error)); 1421 | 1422 | this.trackEvent('api', 'api_channels_update', {team_id: this.getTeamId(), channel_id: channel.id}); 1423 | } 1424 | 1425 | updateChannelHeader(channelId, header, success, error) { 1426 | const data = { 1427 | channel_id: channelId, 1428 | channel_header: header 1429 | }; 1430 | 1431 | request. 1432 | post(`${this.getChannelsRoute()}/update_header`). 1433 | set(this.defaultHeaders). 1434 | type('application/json'). 1435 | accept('application/json'). 1436 | send(data). 1437 | end(this.handleResponse.bind(this, 'updateChannel', success, error)); 1438 | 1439 | this.trackEvent('api', 'api_channels_header', {team_id: this.getTeamId(), channel_id: channelId}); 1440 | } 1441 | 1442 | updateChannelPurpose(channelId, purpose, success, error) { 1443 | const data = { 1444 | channel_id: channelId, 1445 | channel_purpose: purpose 1446 | }; 1447 | 1448 | request. 1449 | post(`${this.getChannelsRoute()}/update_purpose`). 1450 | set(this.defaultHeaders). 1451 | type('application/json'). 1452 | accept('application/json'). 1453 | send(data). 1454 | end(this.handleResponse.bind(this, 'updateChannelPurpose', success, error)); 1455 | 1456 | this.trackEvent('api', 'api_channels_purpose', {team_id: this.getTeamId(), channel_id: channelId}); 1457 | } 1458 | 1459 | updateChannelNotifyProps(data, success, error) { 1460 | request. 1461 | post(`${this.getChannelsRoute()}/update_notify_props`). 1462 | set(this.defaultHeaders). 1463 | type('application/json'). 1464 | accept('application/json'). 1465 | send(data). 1466 | end(this.handleResponse.bind(this, 'updateChannelNotifyProps', success, error)); 1467 | } 1468 | 1469 | leaveChannel(channelId, success, error) { 1470 | request. 1471 | post(`${this.getChannelNeededRoute(channelId)}/leave`). 1472 | set(this.defaultHeaders). 1473 | type('application/json'). 1474 | accept('application/json'). 1475 | end(this.handleResponse.bind(this, 'leaveChannel', success, error)); 1476 | 1477 | this.trackEvent('api', 'api_channels_leave', {team_id: this.getTeamId(), channel_id: channelId}); 1478 | } 1479 | 1480 | joinChannel(channelId, success, error) { 1481 | request. 1482 | post(`${this.getChannelNeededRoute(channelId)}/join`). 1483 | set(this.defaultHeaders). 1484 | type('application/json'). 1485 | accept('application/json'). 1486 | end(this.handleResponse.bind(this, 'joinChannel', success, error)); 1487 | 1488 | this.trackEvent('api', 'api_channels_join', {team_id: this.getTeamId(), channel_id: channelId}); 1489 | } 1490 | 1491 | joinChannelByName(name, success, error) { 1492 | request. 1493 | post(`${this.getChannelNameRoute(name)}/join`). 1494 | set(this.defaultHeaders). 1495 | type('application/json'). 1496 | accept('application/json'). 1497 | end(this.handleResponse.bind(this, 'joinChannelByName', success, error)); 1498 | 1499 | this.trackEvent('api', 'api_channels_join_name', {team_id: this.getTeamId()}); 1500 | } 1501 | 1502 | deleteChannel(channelId, success, error) { 1503 | request. 1504 | post(`${this.getChannelNeededRoute(channelId)}/delete`). 1505 | set(this.defaultHeaders). 1506 | type('application/json'). 1507 | accept('application/json'). 1508 | end(this.handleResponse.bind(this, 'deleteChannel', success, error)); 1509 | 1510 | this.trackEvent('api', 'api_channels_delete', {team_id: this.getTeamId(), channel_id: channelId}); 1511 | } 1512 | 1513 | viewChannel(channelId, prevChannelId = '', time = 0, success, error) { 1514 | request. 1515 | post(`${this.getChannelsRoute()}/view`). 1516 | set(this.defaultHeaders). 1517 | type('application/json'). 1518 | accept('application/json'). 1519 | send({channel_id: channelId, prev_channel_id: prevChannelId, time}). 1520 | end(this.handleResponse.bind(this, 'viewChannel', success, error)); 1521 | } 1522 | 1523 | // SCHEDULED FOR DEPRECATION IN 3.8 - use viewChannel instead 1524 | updateLastViewedAt(channelId, active, success, error) { 1525 | request. 1526 | post(`${this.getChannelNeededRoute(channelId)}/update_last_viewed_at`). 1527 | set(this.defaultHeaders). 1528 | type('application/json'). 1529 | accept('application/json'). 1530 | send({active}). 1531 | end(this.handleResponse.bind(this, 'updateLastViewedAt', success, error)); 1532 | } 1533 | 1534 | // SCHEDULED FOR DEPRECATION IN 3.8 1535 | setLastViewedAt(channelId, lastViewedAt, success, error) { 1536 | request. 1537 | post(`${this.getChannelNeededRoute(channelId)}/set_last_viewed_at`). 1538 | set(this.defaultHeaders). 1539 | type('application/json'). 1540 | accept('application/json'). 1541 | send({last_viewed_at: lastViewedAt}). 1542 | end(this.handleResponse.bind(this, 'setLastViewedAt', success, error)); 1543 | } 1544 | 1545 | getChannels(success, error) { 1546 | request. 1547 | get(`${this.getChannelsRoute()}/`). 1548 | set(this.defaultHeaders). 1549 | type('application/json'). 1550 | accept('application/json'). 1551 | end(this.handleResponse.bind(this, 'getChannels', success, error)); 1552 | } 1553 | 1554 | getChannel(channelId, success, error) { 1555 | request. 1556 | get(`${this.getChannelNeededRoute(channelId)}/`). 1557 | set(this.defaultHeaders). 1558 | type('application/json'). 1559 | accept('application/json'). 1560 | end(this.handleResponse.bind(this, 'getChannel', success, error)); 1561 | 1562 | this.trackEvent('api', 'api_channel_get', {team_id: this.getTeamId(), channel_id: channelId}); 1563 | } 1564 | 1565 | getMoreChannelsPage(offset, limit, success, error) { 1566 | request. 1567 | get(`${this.getChannelsRoute()}/more/${offset}/${limit}`). 1568 | set(this.defaultHeaders). 1569 | type('application/json'). 1570 | accept('application/json'). 1571 | end(this.handleResponse.bind(this, 'getMoreChannelsPage', success, error)); 1572 | 1573 | this.trackEvent('api', 'api_channels_more_page', {team_id: this.getTeamId()}); 1574 | } 1575 | 1576 | searchMoreChannels(term, success, error) { 1577 | request. 1578 | post(`${this.getChannelsRoute()}/more/search`). 1579 | set(this.defaultHeaders). 1580 | type('application/json'). 1581 | accept('application/json'). 1582 | send({term}). 1583 | end(this.handleResponse.bind(this, 'searchMoreChannels', success, error)); 1584 | } 1585 | 1586 | autocompleteChannels(term, success, error) { 1587 | request. 1588 | get(`${this.getChannelsRoute()}/autocomplete?term=${encodeURIComponent(term)}`). 1589 | set(this.defaultHeaders). 1590 | type('application/json'). 1591 | accept('application/json'). 1592 | end(this.handleResponse.bind(this, 'autocompleteChannels', success, error)); 1593 | } 1594 | 1595 | getChannelCounts(success, error) { 1596 | request. 1597 | get(`${this.getChannelsRoute()}/counts`). 1598 | set(this.defaultHeaders). 1599 | type('application/json'). 1600 | accept('application/json'). 1601 | end(this.handleResponse.bind(this, 'getChannelCounts', success, error)); 1602 | } 1603 | 1604 | getMyChannelMembers(success, error) { 1605 | request. 1606 | get(`${this.getChannelsRoute()}/members`). 1607 | set(this.defaultHeaders). 1608 | type('application/json'). 1609 | accept('application/json'). 1610 | end(this.handleResponse.bind(this, 'getMyChannelMembers', success, error)); 1611 | } 1612 | 1613 | getMyChannelMembersForTeam(teamId, success, error) { 1614 | request. 1615 | get(`${this.getTeamsRoute()}/${teamId}/channels/members`). 1616 | set(this.defaultHeaders). 1617 | type('application/json'). 1618 | accept('application/json'). 1619 | end(this.handleResponse.bind(this, 'getMyChannelMembersForTeam', success, error)); 1620 | } 1621 | 1622 | getChannelByName(channelName, success, error) { 1623 | request. 1624 | get(`${this.getChannelsRoute()}/name/${channelName}`). 1625 | set(this.defaultHeaders). 1626 | type('application/json'). 1627 | accept('application/json'). 1628 | end(this.handleResponse.bind(this, 'getChannelByName', success, error)); 1629 | } 1630 | 1631 | getChannelStats(channelId, success, error) { 1632 | request. 1633 | get(`${this.getChannelNeededRoute(channelId)}/stats`). 1634 | set(this.defaultHeaders). 1635 | type('application/json'). 1636 | accept('application/json'). 1637 | end(this.handleResponse.bind(this, 'getChannelStats', success, error)); 1638 | } 1639 | 1640 | getChannelMember(channelId, userId, success, error) { 1641 | request. 1642 | get(`${this.getChannelNeededRoute(channelId)}/members/${userId}`). 1643 | set(this.defaultHeaders). 1644 | type('application/json'). 1645 | accept('application/json'). 1646 | end(this.handleResponse.bind(this, 'getChannelMember', success, error)); 1647 | } 1648 | 1649 | getChannelMembersByIds(channelId, userIds, success, error) { 1650 | request. 1651 | post(`${this.getChannelNeededRoute(channelId)}/members/ids`). 1652 | set(this.defaultHeaders). 1653 | type('application/json'). 1654 | accept('application/json'). 1655 | send(userIds). 1656 | end(this.handleResponse.bind(this, 'getChannelMembersByIds', success, error)); 1657 | } 1658 | 1659 | addChannelMember(channelId, userId, success, error) { 1660 | request. 1661 | post(`${this.getChannelNeededRoute(channelId)}/add`). 1662 | set(this.defaultHeaders). 1663 | type('application/json'). 1664 | accept('application/json'). 1665 | send({user_id: userId}). 1666 | end(this.handleResponse.bind(this, 'addChannelMember', success, error)); 1667 | 1668 | this.trackEvent('api', 'api_channels_add_member', {team_id: this.getTeamId(), channel_id: channelId}); 1669 | } 1670 | 1671 | removeChannelMember(channelId, userId, success, error) { 1672 | request. 1673 | post(`${this.getChannelNeededRoute(channelId)}/remove`). 1674 | set(this.defaultHeaders). 1675 | type('application/json'). 1676 | accept('application/json'). 1677 | send({user_id: userId}). 1678 | end(this.handleResponse.bind(this, 'removeChannelMember', success, error)); 1679 | 1680 | this.trackEvent('api', 'api_channels_remove_member', {team_id: this.getTeamId(), channel_id: channelId}); 1681 | } 1682 | 1683 | updateChannelMemberRoles(channelId, userId, newRoles, success, error) { 1684 | var data = { 1685 | user_id: userId, 1686 | new_roles: newRoles 1687 | }; 1688 | 1689 | request. 1690 | post(`${this.getChannelNeededRoute(channelId)}/update_member_roles`). 1691 | set(this.defaultHeaders). 1692 | type('application/json'). 1693 | accept('application/json'). 1694 | send(data). 1695 | end(this.handleResponse.bind(this, 'updateChannelMemberRoles', success, error)); 1696 | } 1697 | 1698 | // Routes for Commands 1699 | 1700 | listCommands(success, error) { 1701 | request. 1702 | get(`${this.getCommandsRoute()}/list`). 1703 | set(this.defaultHeaders). 1704 | type('application/json'). 1705 | accept('application/json'). 1706 | end(this.handleResponse.bind(this, 'listCommands', success, error)); 1707 | } 1708 | 1709 | executeCommand(command, commandArgs, success, error) { 1710 | request. 1711 | post(`${this.getCommandsRoute()}/execute`). 1712 | set(this.defaultHeaders). 1713 | type('application/json'). 1714 | accept('application/json'). 1715 | send({command, ...commandArgs}). 1716 | end(this.handleResponse.bind(this, 'executeCommand', success, error)); 1717 | 1718 | this.trackEvent('api', 'api_integrations_used'); 1719 | } 1720 | 1721 | addCommand(command, success, error) { 1722 | request. 1723 | post(`${this.getCommandsRoute()}/create`). 1724 | set(this.defaultHeaders). 1725 | type('application/json'). 1726 | accept('application/json'). 1727 | send(command). 1728 | end(this.handleResponse.bind(this, 'addCommand', success, error)); 1729 | 1730 | this.trackEvent('api', 'api_integrations_created'); 1731 | } 1732 | 1733 | editCommand(command, success, error) { 1734 | request. 1735 | post(`${this.getCommandsRoute()}/update`). 1736 | set(this.defaultHeaders). 1737 | type('application/json'). 1738 | accept('application/json'). 1739 | send(command). 1740 | end(this.handleResponse.bind(this, 'editCommand', success, error)); 1741 | 1742 | this.trackEvent('api', 'api_integrations_created'); 1743 | } 1744 | 1745 | deleteCommand(commandId, success, error) { 1746 | request. 1747 | post(`${this.getCommandsRoute()}/delete`). 1748 | set(this.defaultHeaders). 1749 | type('application/json'). 1750 | accept('application/json'). 1751 | send({id: commandId}). 1752 | end(this.handleResponse.bind(this, 'deleteCommand', success, error)); 1753 | 1754 | this.trackEvent('api', 'api_integrations_deleted'); 1755 | } 1756 | 1757 | listTeamCommands(success, error) { 1758 | request. 1759 | get(`${this.getCommandsRoute()}/list_team_commands`). 1760 | set(this.defaultHeaders). 1761 | type('application/json'). 1762 | accept('application/json'). 1763 | end(this.handleResponse.bind(this, 'listTeamCommands', success, error)); 1764 | } 1765 | 1766 | regenCommandToken(commandId, success, error) { 1767 | request. 1768 | post(`${this.getCommandsRoute()}/regen_token`). 1769 | set(this.defaultHeaders). 1770 | type('application/json'). 1771 | accept('application/json'). 1772 | send({id: commandId}). 1773 | end(this.handleResponse.bind(this, 'regenCommandToken', success, error)); 1774 | } 1775 | 1776 | // Routes for Posts 1777 | 1778 | createPost(post, success, error) { 1779 | request. 1780 | post(`${this.getPostsRoute(post.channel_id)}/create`). 1781 | set(this.defaultHeaders). 1782 | type('application/json'). 1783 | accept('application/json'). 1784 | send({...post, create_at: 0}). 1785 | end(this.handleResponse.bind(this, 'createPost', success, error)); 1786 | 1787 | this.trackEvent('api', 'api_posts_create', {team_id: this.getTeamId(), channel_id: post.channel_id}); 1788 | 1789 | if (post.parent_id != null && post.parent_id !== '') { 1790 | this.trackEvent('api', 'api_posts_replied', {team_id: this.getTeamId(), channel_id: post.channel_id}); 1791 | } 1792 | } 1793 | 1794 | // This is a temporary route to get around a problem with the permissions system that 1795 | // will be fixed in 3.1 or 3.2 1796 | getPermalinkTmp(postId, success, error) { 1797 | request. 1798 | get(`${this.getTeamNeededRoute()}/pltmp/${postId}`). 1799 | set(this.defaultHeaders). 1800 | type('application/json'). 1801 | accept('application/json'). 1802 | end(this.handleResponse.bind(this, 'getPermalinkTmp', success, error)); 1803 | 1804 | this.trackEvent('api', 'api_channels_permalink', {team_id: this.getTeamId()}); 1805 | } 1806 | 1807 | getPostById(postId, success, error) { 1808 | request. 1809 | get(`${this.getTeamNeededRoute()}/posts/${postId}`). 1810 | set(this.defaultHeaders). 1811 | type('application/json'). 1812 | accept('application/json'). 1813 | end(this.handleResponse.bind(this, 'getPostById', success, error)); 1814 | } 1815 | 1816 | getPost(channelId, postId, success, error) { 1817 | request. 1818 | get(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/get`). 1819 | set(this.defaultHeaders). 1820 | type('application/json'). 1821 | accept('application/json'). 1822 | end(this.handleResponse.bind(this, 'getPost', success, error)); 1823 | } 1824 | 1825 | updatePost(post, success, error) { 1826 | request. 1827 | post(`${this.getPostsRoute(post.channel_id)}/update`). 1828 | set(this.defaultHeaders). 1829 | type('application/json'). 1830 | accept('application/json'). 1831 | send(post). 1832 | end(this.handleResponse.bind(this, 'updatePost', success, error)); 1833 | 1834 | this.trackEvent('api', 'api_posts_update', {team_id: this.getTeamId(), channel_id: post.channel_id}); 1835 | } 1836 | 1837 | deletePost(channelId, postId, success, error) { 1838 | request. 1839 | post(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/delete`). 1840 | set(this.defaultHeaders). 1841 | type('application/json'). 1842 | accept('application/json'). 1843 | end(this.handleResponse.bind(this, 'deletePost', success, error)); 1844 | 1845 | this.trackEvent('api', 'api_posts_delete', {team_id: this.getTeamId(), channel_id: channelId}); 1846 | } 1847 | 1848 | search(terms, isOrSearch, success, error) { 1849 | const data = {}; 1850 | data.terms = terms; 1851 | data.is_or_search = isOrSearch; 1852 | 1853 | request. 1854 | post(`${this.getTeamNeededRoute()}/posts/search`). 1855 | set(this.defaultHeaders). 1856 | type('application/json'). 1857 | accept('application/json'). 1858 | send(data). 1859 | end(this.handleResponse.bind(this, 'search', success, error)); 1860 | 1861 | this.trackEvent('api', 'api_posts_search', {team_id: this.getTeamId()}); 1862 | } 1863 | 1864 | getPostsPage(channelId, offset, limit, success, error) { 1865 | request. 1866 | get(`${this.getPostsRoute(channelId)}/page/${offset}/${limit}`). 1867 | set(this.defaultHeaders). 1868 | type('application/json'). 1869 | accept('application/json'). 1870 | end(this.handleResponse.bind(this, 'getPostsPage', success, error)); 1871 | } 1872 | 1873 | getPosts(channelId, since, success, error) { 1874 | request. 1875 | get(`${this.getPostsRoute(channelId)}/since/${since}`). 1876 | set(this.defaultHeaders). 1877 | type('application/json'). 1878 | accept('application/json'). 1879 | end(this.handleResponse.bind(this, 'getPosts', success, error)); 1880 | } 1881 | 1882 | getPostsBefore(channelId, postId, offset, numPost, success, error) { 1883 | request. 1884 | get(`${this.getPostsRoute(channelId)}/${postId}/before/${offset}/${numPost}`). 1885 | set(this.defaultHeaders). 1886 | type('application/json'). 1887 | accept('application/json'). 1888 | end(this.handleResponse.bind(this, 'getPostsBefore', success, error)); 1889 | 1890 | this.trackEvent('api', 'api_posts_get_before', {team_id: this.getTeamId(), channel_id: channelId}); 1891 | } 1892 | 1893 | getPostsAfter(channelId, postId, offset, numPost, success, error) { 1894 | request. 1895 | get(`${this.getPostsRoute(channelId)}/${postId}/after/${offset}/${numPost}`). 1896 | set(this.defaultHeaders). 1897 | type('application/json'). 1898 | accept('application/json'). 1899 | end(this.handleResponse.bind(this, 'getPostsAfter', success, error)); 1900 | 1901 | this.trackEvent('api', 'api_posts_get_after', {team_id: this.getTeamId(), channel_id: channelId}); 1902 | } 1903 | 1904 | getFlaggedPosts(offset, limit, success, error) { 1905 | request. 1906 | get(`${this.getTeamNeededRoute()}/posts/flagged/${offset}/${limit}`). 1907 | set(this.defaultHeaders). 1908 | type('application/json'). 1909 | accept('application/json'). 1910 | end(this.handleResponse.bind(this, 'getFlaggedPosts', success, error)); 1911 | 1912 | this.trackEvent('api', 'api_posts_get_flagged', {team_id: this.getTeamId()}); 1913 | } 1914 | 1915 | getPinnedPosts(channelId, success, error) { 1916 | request. 1917 | get(`${this.getChannelNeededRoute(channelId)}/pinned`). 1918 | set(this.defaultHeaders). 1919 | type('application/json'). 1920 | accept('application/json'). 1921 | end(this.handleResponse.bind(this, 'getPinnedPosts', success, error)); 1922 | } 1923 | 1924 | getFileInfosForPost(channelId, postId, success, error) { 1925 | request. 1926 | get(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/get_file_infos`). 1927 | set(this.defaultHeaders). 1928 | type('application/json'). 1929 | accept('application/json'). 1930 | end(this.handleResponse.bind(this, 'getFileInfosForPost', success, error)); 1931 | } 1932 | 1933 | getOpenGraphMetadata(url, success, error) { 1934 | request. 1935 | post(`${this.getBaseRoute()}/get_opengraph_metadata`). 1936 | set(this.defaultHeaders). 1937 | type('application/json'). 1938 | accept('application/json'). 1939 | send({url}). 1940 | end(this.handleResponse.bind(this, 'getOpenGraphMetadata', success, error)); 1941 | } 1942 | 1943 | // Routes for Files 1944 | 1945 | uploadFile(file, filename, channelId, clientId, success, error) { 1946 | this.trackEvent('api', 'api_files_upload', {team_id: this.getTeamId(), channel_id: channelId}); 1947 | 1948 | return request. 1949 | post(`${this.getTeamFilesRoute()}/upload`). 1950 | set(this.defaultHeaders). 1951 | attach('files', file, filename). 1952 | field('channel_id', channelId). 1953 | field('client_ids', clientId). 1954 | accept('application/json'). 1955 | end(this.handleResponse.bind(this, 'uploadFile', success, error)); 1956 | } 1957 | 1958 | getFile(fileId, success, error) { 1959 | request. 1960 | get(`${this.getFileRoute(fileId)}/get`). 1961 | set(this.defaultHeaders). 1962 | type('application/json'). 1963 | accept('application/json'). 1964 | end(this.handleResponse.bind(this, 'getFile', success, error)); 1965 | } 1966 | 1967 | getFileThumbnail(fileId, success, error) { 1968 | request. 1969 | get(`${this.getFileRoute(fileId)}/get_thumbnail`). 1970 | set(this.defaultHeaders). 1971 | type('application/json'). 1972 | accept('application/json'). 1973 | end(this.handleResponse.bind(this, 'getFileThumbnail', success, error)); 1974 | } 1975 | 1976 | getFilePreview(fileId, success, error) { 1977 | request. 1978 | get(`${this.getFileRoute(fileId)}/get`). 1979 | set(this.defaultHeaders). 1980 | type('application/json'). 1981 | accept('application/json'). 1982 | end(this.handleResponse.bind(this, 'getFilePreview', success, error)); 1983 | } 1984 | 1985 | getFileInfo(fileId, success, error) { 1986 | request. 1987 | get(`${this.getFileRoute(fileId)}/get_info`). 1988 | set(this.defaultHeaders). 1989 | type('application/json'). 1990 | accept('application/json'). 1991 | end(this.handleResponse.bind(this, 'getFileInfo', success, error)); 1992 | } 1993 | 1994 | getPublicLink(fileId, success, error) { 1995 | request. 1996 | get(`${this.getFileRoute(fileId)}/get_public_link`). 1997 | set(this.defaultHeaders). 1998 | type('application/json'). 1999 | accept('application/json'). 2000 | end(this.handleResponse.bind(this, 'getPublicLink', success, error)); 2001 | } 2002 | 2003 | // Routes for OAuth 2004 | 2005 | registerOAuthApp(app, success, error) { 2006 | request. 2007 | post(`${this.getOAuthRoute()}/register`). 2008 | set(this.defaultHeaders). 2009 | type('application/json'). 2010 | accept('application/json'). 2011 | send(app). 2012 | end(this.handleResponse.bind(this, 'registerOAuthApp', success, error)); 2013 | 2014 | this.trackEvent('api', 'api_apps_register'); 2015 | } 2016 | 2017 | allowOAuth2(responseType, clientId, redirectUri, state, scope, success, error) { 2018 | request. 2019 | get(`${this.getOAuthRoute()}/allow`). 2020 | set(this.defaultHeaders). 2021 | type('application/json'). 2022 | accept('application/json'). 2023 | query({response_type: responseType}). 2024 | query({client_id: clientId}). 2025 | query({redirect_uri: redirectUri}). 2026 | query({scope}). 2027 | query({state}). 2028 | end(this.handleResponse.bind(this, 'allowOAuth2', success, error)); 2029 | } 2030 | 2031 | listOAuthApps(success, error) { 2032 | request. 2033 | get(`${this.getOAuthRoute()}/list`). 2034 | set(this.defaultHeaders). 2035 | type('application/json'). 2036 | accept('application/json'). 2037 | send(). 2038 | end(this.handleResponse.bind(this, 'getOAuthApps', success, error)); 2039 | } 2040 | 2041 | deleteOAuthApp(id, success, error) { 2042 | request. 2043 | post(`${this.getOAuthRoute()}/delete`). 2044 | set(this.defaultHeaders). 2045 | type('application/json'). 2046 | accept('application/json'). 2047 | send({id}). 2048 | end(this.handleResponse.bind(this, 'deleteOAuthApp', success, error)); 2049 | 2050 | this.trackEvent('api', 'api_apps_delete'); 2051 | } 2052 | 2053 | getOAuthAppInfo(id, success, error) { 2054 | request. 2055 | get(`${this.getOAuthRoute()}/app/${id}`). 2056 | set(this.defaultHeaders). 2057 | type('application/json'). 2058 | accept('application/json'). 2059 | send(). 2060 | end(this.handleResponse.bind(this, 'getOAuthAppInfo', success, error)); 2061 | } 2062 | 2063 | getAuthorizedApps(success, error) { 2064 | request. 2065 | get(`${this.getOAuthRoute()}/authorized`). 2066 | set(this.defaultHeaders). 2067 | type('application/json'). 2068 | accept('application/json'). 2069 | send(). 2070 | end(this.handleResponse.bind(this, 'getAuthorizedApps', success, error)); 2071 | } 2072 | 2073 | deauthorizeOAuthApp(id, success, error) { 2074 | request. 2075 | post(`${this.getOAuthRoute()}/${id}/deauthorize`). 2076 | set(this.defaultHeaders). 2077 | type('application/json'). 2078 | accept('application/json'). 2079 | send(). 2080 | end(this.handleResponse.bind(this, 'deauthorizeOAuthApp', success, error)); 2081 | } 2082 | 2083 | regenerateOAuthAppSecret(id, success, error) { 2084 | request. 2085 | post(`${this.getOAuthRoute()}/${id}/regen_secret`). 2086 | set(this.defaultHeaders). 2087 | type('application/json'). 2088 | accept('application/json'). 2089 | send(). 2090 | end(this.handleResponse.bind(this, 'regenerateOAuthAppSecret', success, error)); 2091 | } 2092 | 2093 | // Routes for Hooks 2094 | 2095 | addIncomingHook(hook, success, error) { 2096 | request. 2097 | post(`${this.getHooksRoute()}/incoming/create`). 2098 | set(this.defaultHeaders). 2099 | type('application/json'). 2100 | accept('application/json'). 2101 | send(hook). 2102 | end(this.handleResponse.bind(this, 'addIncomingHook', success, error)); 2103 | 2104 | this.trackEvent('api', 'api_integrations_created', {team_id: this.getTeamId()}); 2105 | } 2106 | 2107 | updateIncomingHook(hook, success, error) { 2108 | request. 2109 | post(`${this.getHooksRoute()}/incoming/update`). 2110 | set(this.defaultHeaders). 2111 | type('application/json'). 2112 | accept('application/json'). 2113 | send(hook). 2114 | end(this.handleResponse.bind(this, 'updateIncomingHook', success, error)); 2115 | 2116 | this.trackEvent('api', 'api_integrations_updated', {team_id: this.getTeamId()}); 2117 | } 2118 | 2119 | deleteIncomingHook(hookId, success, error) { 2120 | request. 2121 | post(`${this.getHooksRoute()}/incoming/delete`). 2122 | set(this.defaultHeaders). 2123 | type('application/json'). 2124 | accept('application/json'). 2125 | send({id: hookId}). 2126 | end(this.handleResponse.bind(this, 'deleteIncomingHook', success, error)); 2127 | 2128 | this.trackEvent('api', 'api_integrations_deleted', {team_id: this.getTeamId()}); 2129 | } 2130 | 2131 | listIncomingHooks(success, error) { 2132 | request. 2133 | get(`${this.getHooksRoute()}/incoming/list`). 2134 | set(this.defaultHeaders). 2135 | type('application/json'). 2136 | accept('application/json'). 2137 | end(this.handleResponse.bind(this, 'listIncomingHooks', success, error)); 2138 | } 2139 | 2140 | addOutgoingHook(hook, success, error) { 2141 | request. 2142 | post(`${this.getHooksRoute()}/outgoing/create`). 2143 | set(this.defaultHeaders). 2144 | type('application/json'). 2145 | accept('application/json'). 2146 | send(hook). 2147 | end(this.handleResponse.bind(this, 'addOutgoingHook', success, error)); 2148 | 2149 | this.trackEvent('api', 'api_integrations_created', {team_id: this.getTeamId()}); 2150 | } 2151 | 2152 | updateOutgoingHook(hook, success, error) { 2153 | request. 2154 | post(`${this.getHooksRoute()}/outgoing/update`). 2155 | set(this.defaultHeaders). 2156 | type('application/json'). 2157 | accept('application/json'). 2158 | send(hook). 2159 | end(this.handleResponse.bind(this, 'updateOutgoingHook', success, error)); 2160 | 2161 | this.trackEvent('api', 'api_integrations_updated', {team_id: this.getTeamId()}); 2162 | } 2163 | 2164 | deleteOutgoingHook(hookId, success, error) { 2165 | request. 2166 | post(`${this.getHooksRoute()}/outgoing/delete`). 2167 | set(this.defaultHeaders). 2168 | type('application/json'). 2169 | accept('application/json'). 2170 | send({id: hookId}). 2171 | end(this.handleResponse.bind(this, 'deleteOutgoingHook', success, error)); 2172 | 2173 | this.trackEvent('api', 'api_integrations_deleted', {team_id: this.getTeamId()}); 2174 | } 2175 | 2176 | listOutgoingHooks(success, error) { 2177 | request. 2178 | get(`${this.getHooksRoute()}/outgoing/list`). 2179 | set(this.defaultHeaders). 2180 | type('application/json'). 2181 | accept('application/json'). 2182 | end(this.handleResponse.bind(this, 'listOutgoingHooks', success, error)); 2183 | } 2184 | 2185 | regenOutgoingHookToken(hookId, success, error) { 2186 | request. 2187 | post(`${this.getHooksRoute()}/outgoing/regen_token`). 2188 | set(this.defaultHeaders). 2189 | type('application/json'). 2190 | accept('application/json'). 2191 | send({id: hookId}). 2192 | end(this.handleResponse.bind(this, 'regenOutgoingHookToken', success, error)); 2193 | } 2194 | 2195 | // Routes for Preferences 2196 | 2197 | getAllPreferences(success, error) { 2198 | request. 2199 | get(`${this.getBaseRoute()}/preferences/`). 2200 | set(this.defaultHeaders). 2201 | type('application/json'). 2202 | accept('application/json'). 2203 | end(this.handleResponse.bind(this, 'getAllPreferences', success, error)); 2204 | } 2205 | 2206 | savePreferences(preferences, success, error) { 2207 | request. 2208 | post(`${this.getBaseRoute()}/preferences/save`). 2209 | set(this.defaultHeaders). 2210 | type('application/json'). 2211 | accept('application/json'). 2212 | send(preferences). 2213 | end(this.handleResponse.bind(this, 'savePreferences', success, error)); 2214 | } 2215 | 2216 | getPreferenceCategory(category, success, error) { 2217 | request. 2218 | get(`${this.getBaseRoute()}/preferences/${category}`). 2219 | set(this.defaultHeaders). 2220 | type('application/json'). 2221 | accept('application/json'). 2222 | end(this.handleResponse.bind(this, 'getPreferenceCategory', success, error)); 2223 | } 2224 | 2225 | deletePreferences(preferences, success, error) { 2226 | request. 2227 | post(`${this.getBaseRoute()}/preferences/delete`). 2228 | set(this.defaultHeaders). 2229 | type('application/json'). 2230 | accept('application/json'). 2231 | send(preferences). 2232 | end(this.handleResponse.bind(this, 'deletePreferences', success, error)); 2233 | } 2234 | 2235 | // Routes for Emoji 2236 | 2237 | listEmoji(success, error) { 2238 | request. 2239 | get(`${this.getEmojiRoute()}/list`). 2240 | set(this.defaultHeaders). 2241 | type('application/json'). 2242 | accept('application/json'). 2243 | end(this.handleResponse.bind(this, 'listEmoji', success, error)); 2244 | } 2245 | 2246 | addEmoji(emoji, image, success, error) { 2247 | request. 2248 | post(`${this.getEmojiRoute()}/create`). 2249 | set(this.defaultHeaders). 2250 | accept('application/json'). 2251 | attach('image', image, image.name). 2252 | field('emoji', JSON.stringify(emoji)). 2253 | end(this.handleResponse.bind(this, 'addEmoji', success, error)); 2254 | 2255 | this.trackEvent('api', 'api_emoji_custom_add'); 2256 | } 2257 | 2258 | deleteEmoji(id, success, error) { 2259 | request. 2260 | post(`${this.getEmojiRoute()}/delete`). 2261 | set(this.defaultHeaders). 2262 | type('application/json'). 2263 | accept('application/json'). 2264 | send({id}). 2265 | end(this.handleResponse.bind(this, 'deleteEmoji', success, error)); 2266 | 2267 | this.trackEvent('api', 'api_emoji_custom_delete'); 2268 | } 2269 | 2270 | getCustomEmojiImageUrl(id) { 2271 | return `${this.getEmojiRoute()}/${id}`; 2272 | } 2273 | 2274 | uploadCertificateFile(file, success, error) { 2275 | request. 2276 | post(`${this.getAdminRoute()}/add_certificate`). 2277 | set(this.defaultHeaders). 2278 | accept('application/json'). 2279 | attach('certificate', file, file.name). 2280 | end(this.handleResponse.bind(this, 'uploadCertificateFile', success, error)); 2281 | } 2282 | 2283 | removeCertificateFile(filename, success, error) { 2284 | request. 2285 | post(`${this.getAdminRoute()}/remove_certificate`). 2286 | set(this.defaultHeaders). 2287 | accept('application/json'). 2288 | send({filename}). 2289 | end(this.handleResponse.bind(this, 'removeCertificateFile', success, error)); 2290 | } 2291 | 2292 | samlCertificateStatus(success, error) { 2293 | request.get(`${this.getAdminRoute()}/saml_cert_status`). 2294 | set(this.defaultHeaders). 2295 | type('application/json'). 2296 | accept('application/json'). 2297 | end((err, res) => { 2298 | if (err) { 2299 | return error(err); 2300 | } 2301 | 2302 | if (!res.body) { 2303 | console.error('Missing response body for samlCertificateStatus'); // eslint-disable-line no-console 2304 | } 2305 | 2306 | return success(res.body); 2307 | }); 2308 | } 2309 | 2310 | pinPost(channelId, postId, success, error) { 2311 | request. 2312 | post(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/pin`). 2313 | set(this.defaultHeaders). 2314 | accept('application/json'). 2315 | send(). 2316 | end(this.handleResponse.bind(this, 'pinPost', success, error)); 2317 | } 2318 | 2319 | unpinPost(channelId, postId, success, error) { 2320 | request. 2321 | post(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/unpin`). 2322 | set(this.defaultHeaders). 2323 | accept('application/json'). 2324 | send(). 2325 | end(this.handleResponse.bind(this, 'unpinPost', success, error)); 2326 | } 2327 | 2328 | saveReaction(channelId, reaction, success, error) { 2329 | request. 2330 | post(`${this.getChannelNeededRoute(channelId)}/posts/${reaction.post_id}/reactions/save`). 2331 | set(this.defaultHeaders). 2332 | type('application/json'). 2333 | accept('application/json'). 2334 | send(reaction). 2335 | end(this.handleResponse.bind(this, 'saveReaction', success, error)); 2336 | 2337 | this.trackEvent('api', 'api_reactions_save', {team_id: this.getTeamId(), channel_id: channelId, post_id: reaction.post_id}); 2338 | } 2339 | 2340 | deleteReaction(channelId, reaction, success, error) { 2341 | request. 2342 | post(`${this.getChannelNeededRoute(channelId)}/posts/${reaction.post_id}/reactions/delete`). 2343 | set(this.defaultHeaders). 2344 | type('application/json'). 2345 | accept('application/json'). 2346 | send(reaction). 2347 | end(this.handleResponse.bind(this, 'deleteReaction', success, error)); 2348 | 2349 | this.trackEvent('api', 'api_reactions_delete', {team_id: this.getTeamId(), channel_id: channelId, post_id: reaction.post_id}); 2350 | } 2351 | 2352 | listReactions(channelId, postId, success, error) { 2353 | request. 2354 | get(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/reactions`). 2355 | set(this.defaultHeaders). 2356 | type('application/json'). 2357 | accept('application/json'). 2358 | end(this.handleResponse.bind(this, 'listReactions', success, error)); 2359 | } 2360 | 2361 | webrtcToken(success, error) { 2362 | request.post(`${this.getWebrtcRoute()}/token`). 2363 | set(this.defaultHeaders). 2364 | type('application/json'). 2365 | accept('application/json'). 2366 | end(this.handleResponse.bind(this, 'webrtcToken', success, error)); 2367 | } 2368 | } 2369 | -------------------------------------------------------------------------------- /main.jsx: -------------------------------------------------------------------------------- 1 | import Client from './client.jsx' 2 | import WebSocketClient from './websocket_client.jsx' 3 | 4 | module.exports.Client = Client 5 | module.exports.WebSocketClient = WebSocketClient 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mattermost", 3 | "version": "3.4.0", 4 | "description": "Javascript library for interacting with the Mattermost API", 5 | "main": "lib/client.js", 6 | "files": [ 7 | "client.jsx", 8 | "websocket_client.jsx", 9 | "README.md", 10 | "Makefile", 11 | "lib", 12 | "lib/client.js", 13 | "lib/client.js.map" 14 | ], 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/mattermost/mattermost-javascript.git" 18 | }, 19 | "keywords": [ 20 | "mattermost", 21 | "webhook", 22 | "integration" 23 | ], 24 | "author": "Mattermost", 25 | "license": "Apache-2.0", 26 | "bugs": { 27 | "url": "https://github.com/mattermost/platform/issues" 28 | }, 29 | "homepage": "https://www.mattermost.org/", 30 | "dependencies": { 31 | "superagent": "1.8.3" 32 | }, 33 | "devDependencies": { 34 | "babel-loader": "6.2.4", 35 | "babel-plugin-transform-runtime": "6.8.0", 36 | "babel-polyfill": "6.8.0", 37 | "babel-preset-es2015-webpack": "6.4.1", 38 | "babel-preset-stage-0": "6.5.0", 39 | "mocha": "2.4.5", 40 | "mocha-jsdom": "1.1.0", 41 | "mocha-webpack": "0.3.0", 42 | "jquery-deferred": "0.3.0", 43 | "webpack": "2.1.0-beta.7", 44 | "webpack-node-externals": "1.2.0" 45 | }, 46 | "scripts": { 47 | "build": "webpack", 48 | "prepublish": "webpack", 49 | "test": "mocha-webpack --webpack-config webpack.config.js \"**/*.test.jsx\"" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/client_admin.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | var assert = require('assert'); 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Admin', function() { 8 | this.timeout(10000); 9 | 10 | it('Admin.reloadConfig', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 13 | TestHelper.basicClient().reloadConfig( 14 | function() { 15 | done(new Error('should need system admin permissions')); 16 | }, 17 | function(err) { 18 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 19 | done(); 20 | } 21 | ); 22 | }); 23 | }); 24 | 25 | it('Admin.recycleDatabaseConnection', function(done) { 26 | TestHelper.initBasic(() => { 27 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 28 | TestHelper.basicClient().recycleDatabaseConnection( 29 | function() { 30 | done(new Error('should need system admin permissions')); 31 | }, 32 | function(err) { 33 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 34 | done(); 35 | } 36 | ); 37 | }); 38 | }); 39 | 40 | it('Admin.getComplianceReports', function(done) { 41 | TestHelper.initBasic(() => { 42 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 43 | TestHelper.basicClient().getComplianceReports( 44 | function() { 45 | done(new Error('should need system admin permissions')); 46 | }, 47 | function(err) { 48 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 49 | done(); 50 | } 51 | ); 52 | }); 53 | }); 54 | 55 | it('Admin.saveComplianceReports', function(done) { 56 | TestHelper.initBasic(() => { 57 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 58 | 59 | var job = {}; 60 | job.desc = 'desc'; 61 | job.emails = ''; 62 | job.keywords = 'test'; 63 | job.start_at = new Date(); 64 | job.end_at = new Date(); 65 | 66 | TestHelper.basicClient().saveComplianceReports( 67 | job, 68 | function() { 69 | done(new Error('should need system admin permissions')); 70 | }, 71 | function(err) { 72 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 73 | done(); 74 | } 75 | ); 76 | }); 77 | }); 78 | 79 | it('Admin.getLogs', function(done) { 80 | TestHelper.initBasic(() => { 81 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 82 | TestHelper.basicClient().getLogs( 83 | function() { 84 | done(new Error('should need system admin permissions')); 85 | }, 86 | function(err) { 87 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 88 | done(); 89 | } 90 | ); 91 | }); 92 | }); 93 | 94 | it('Admin.getServerAudits', function(done) { 95 | TestHelper.initBasic(() => { 96 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 97 | TestHelper.basicClient().getServerAudits( 98 | function() { 99 | done(new Error('should need system admin permissions')); 100 | }, 101 | function(err) { 102 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 103 | done(); 104 | } 105 | ); 106 | }); 107 | }); 108 | 109 | it('Admin.getConfig', function(done) { 110 | TestHelper.initBasic(() => { 111 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 112 | TestHelper.basicClient().getConfig( 113 | function() { 114 | done(new Error('should need system admin permissions')); 115 | }, 116 | function(err) { 117 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 118 | done(); 119 | } 120 | ); 121 | }); 122 | }); 123 | 124 | it('Admin.getAnalytics', function(done) { 125 | TestHelper.initBasic(() => { 126 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 127 | TestHelper.basicClient().getAnalytics( 128 | 'standard', 129 | null, 130 | function() { 131 | done(new Error('should need system admin permissions')); 132 | }, 133 | function(err) { 134 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 135 | done(); 136 | } 137 | ); 138 | }); 139 | }); 140 | 141 | it('Admin.getTeamAnalytics', function(done) { 142 | TestHelper.initBasic(() => { 143 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 144 | TestHelper.basicClient().getTeamAnalytics( 145 | TestHelper.basicTeam().id, 146 | 'standard', 147 | function() { 148 | done(new Error('should need system admin permissions')); 149 | }, 150 | function(err) { 151 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 152 | done(); 153 | } 154 | ); 155 | }); 156 | }); 157 | 158 | it('Admin.saveConfig', function(done) { 159 | TestHelper.initBasic(() => { 160 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 161 | var config = {}; 162 | config.site_name = 'test'; 163 | 164 | TestHelper.basicClient().saveConfig( 165 | config, 166 | function() { 167 | done(new Error('should need system admin permissions')); 168 | }, 169 | function(err) { 170 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 171 | done(); 172 | } 173 | ); 174 | }); 175 | }); 176 | 177 | it('Admin.testEmail', function(done) { 178 | TestHelper.initBasic(() => { 179 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 180 | var config = {}; 181 | config.site_name = 'test'; 182 | 183 | TestHelper.basicClient().testEmail( 184 | config, 185 | function() { 186 | done(new Error('should need system admin permissions')); 187 | }, 188 | function(err) { 189 | assert.equal(err.id, 'api.context.system_permissions.app_error'); 190 | done(); 191 | } 192 | ); 193 | }); 194 | }); 195 | 196 | it('Admin.adminResetMfa', function(done) { 197 | TestHelper.initBasic(() => { 198 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 199 | 200 | TestHelper.basicClient().adminResetMfa( 201 | TestHelper.basicUser().id, 202 | function() { 203 | done(new Error('should need a license')); 204 | }, 205 | function(err) { 206 | assert.equal(err.id, 'api.context.permissions.app_error'); 207 | done(); 208 | } 209 | ); 210 | }); 211 | }); 212 | 213 | it('Admin.adminResetPassword', function(done) { 214 | TestHelper.initBasic(() => { 215 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 216 | var user = TestHelper.basicUser(); 217 | 218 | TestHelper.basicClient().resetPassword( 219 | user.id, 220 | 'new_password', 221 | function() { 222 | throw Error('shouldnt work'); 223 | }, 224 | function(err) { 225 | // this should fail since you're not a system admin 226 | assert.equal(err.id, 'api.context.invalid_param.app_error'); 227 | done(); 228 | } 229 | ); 230 | }); 231 | }); 232 | 233 | it('License.getClientLicenceConfig', function(done) { 234 | TestHelper.initBasic(() => { 235 | TestHelper.basicClient().getClientLicenceConfig( 236 | function(data) { 237 | assert.equal(data.IsLicensed, 'false'); 238 | done(); 239 | }, 240 | function(err) { 241 | done(new Error(err.message)); 242 | } 243 | ); 244 | }); 245 | }); 246 | 247 | it('License.removeLicenseFile', function(done) { 248 | TestHelper.initBasic(() => { 249 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 250 | TestHelper.basicClient().removeLicenseFile( 251 | function() { 252 | done(new Error('not enabled')); 253 | }, 254 | function(err) { 255 | assert.equal(err.id, 'api.context.permissions.app_error'); 256 | done(); 257 | } 258 | ); 259 | }); 260 | }); 261 | 262 | it('Admin.ldapSyncNow', function(done) { 263 | TestHelper.initBasic(() => { 264 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 265 | 266 | TestHelper.basicClient().ldapSyncNow( 267 | function() { 268 | throw Error('shouldnt work'); 269 | }, 270 | function(err) { 271 | // this should fail since you're not a system admin 272 | done(); 273 | } 274 | ); 275 | }); 276 | }); 277 | 278 | /*it('License.uploadLicenseFile', function(done) { 279 | TestHelper.initBasic(() => { 280 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 281 | TestHelper.basicClient().uploadLicenseFile( 282 | 'form data', 283 | function() { 284 | done(new Error('not enabled')); 285 | }, 286 | function(err) { 287 | assert.equal(err.id, 'api.context.permissions.app_error'); 288 | done(); 289 | } 290 | ); 291 | }); 292 | });*/ 293 | 294 | // TODO XXX FIX ME - this test depends on make dist 295 | 296 | // it('General.getTranslations', function(done) { 297 | // TestHelper.initBasic(() => { 298 | // TestHelper.basicClient().getTranslations( 299 | // 'http://localhost:8065/static/i18n/es.json', 300 | // function(data) { 301 | // assert.equal(data['login.or'], 'o'); 302 | // done(); 303 | // }, 304 | // function(err) { 305 | // done(new Error(err.message)); 306 | // } 307 | // ); 308 | // }); 309 | // }); 310 | }); 311 | 312 | -------------------------------------------------------------------------------- /tests/client_channel.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Channels', function() { 8 | this.timeout(100000); 9 | 10 | it('createChannel', function(done) { 11 | TestHelper.initBasic(() => { 12 | var channel = TestHelper.fakeChannel(); 13 | channel.team_id = TestHelper.basicTeam().id; 14 | TestHelper.basicClient().createChannel( 15 | channel, 16 | function(data) { 17 | assert.equal(data.id.length > 0, true); 18 | assert.equal(data.name, channel.name); 19 | done(); 20 | }, 21 | function(err) { 22 | done(new Error(err.message)); 23 | } 24 | ); 25 | }); 26 | }); 27 | 28 | it('createDirectChannel', function(done) { 29 | TestHelper.initBasic(() => { 30 | TestHelper.basicClient().createUser( 31 | TestHelper.fakeUser(), 32 | function(user2) { 33 | TestHelper.basicClient().addUserToTeam( 34 | user2.id, 35 | function() { 36 | TestHelper.basicClient().createDirectChannel( 37 | user2.id, 38 | function(data) { 39 | assert.equal(data.id.length > 0, true); 40 | done(); 41 | }, 42 | function(err) { 43 | done(new Error(err.message)); 44 | } 45 | ); 46 | }, 47 | function(err) { 48 | done(new Error(err.message)); 49 | } 50 | ); 51 | }, 52 | function(err) { 53 | done(new Error(err.message)); 54 | } 55 | ); 56 | }); 57 | }); 58 | 59 | it('updateChannel', function(done) { 60 | TestHelper.initBasic(() => { 61 | var channel = TestHelper.basicChannel(); 62 | channel.display_name = 'changed'; 63 | TestHelper.basicClient().updateChannel( 64 | channel, 65 | function(data) { 66 | assert.equal(data.id.length > 0, true); 67 | assert.equal(data.display_name, 'changed'); 68 | done(); 69 | }, 70 | function(err) { 71 | done(new Error(err.message)); 72 | } 73 | ); 74 | }); 75 | }); 76 | 77 | it('updateChannelHeader', function(done) { 78 | TestHelper.initBasic(() => { 79 | var channel = TestHelper.basicChannel(); 80 | channel.display_name = 'changed'; 81 | TestHelper.basicClient().updateChannelHeader( 82 | channel.id, 83 | 'new header', 84 | function(data) { 85 | assert.equal(data.id.length > 0, true); 86 | assert.equal(data.header, 'new header'); 87 | done(); 88 | }, 89 | function(err) { 90 | done(new Error(err.message)); 91 | } 92 | ); 93 | }); 94 | }); 95 | 96 | it('updateChannelPurpose', function(done) { 97 | TestHelper.initBasic(() => { 98 | var channel = TestHelper.basicChannel(); 99 | channel.display_name = 'changed'; 100 | TestHelper.basicClient().updateChannelPurpose( 101 | channel.id, 102 | 'new purpose', 103 | function(data) { 104 | assert.equal(data.id.length > 0, true); 105 | assert.equal(data.purpose, 'new purpose'); 106 | done(); 107 | }, 108 | function(err) { 109 | done(new Error(err.message)); 110 | } 111 | ); 112 | }); 113 | }); 114 | 115 | it('updateChannelNotifyProps', function(done) { 116 | TestHelper.initBasic(() => { 117 | var props = {}; 118 | props.channel_id = TestHelper.basicChannel().id; 119 | props.user_id = TestHelper.basicUser().id; 120 | props.desktop = 'all'; 121 | TestHelper.basicClient().updateChannelNotifyProps( 122 | props, 123 | function(data) { 124 | assert.equal(data.desktop, 'all'); 125 | done(); 126 | }, 127 | function(err) { 128 | done(new Error(err.message)); 129 | } 130 | ); 131 | }); 132 | }); 133 | 134 | it('leaveChannel', function(done) { 135 | TestHelper.initBasic(() => { 136 | var channel = TestHelper.basicChannel(); 137 | TestHelper.basicClient().leaveChannel( 138 | channel.id, 139 | function(data) { 140 | assert.equal(data.id, channel.id); 141 | done(); 142 | }, 143 | function(err) { 144 | done(new Error(err.message)); 145 | } 146 | ); 147 | }); 148 | }); 149 | 150 | it('joinChannel', function(done) { 151 | TestHelper.initBasic(() => { 152 | var channel = TestHelper.basicChannel(); 153 | TestHelper.basicClient().leaveChannel( 154 | channel.id, 155 | function() { 156 | TestHelper.basicClient().joinChannel( 157 | channel.id, 158 | function() { 159 | done(); 160 | }, 161 | function(err) { 162 | done(new Error(err.message)); 163 | } 164 | ); 165 | }, 166 | function(err) { 167 | done(new Error(err.message)); 168 | } 169 | ); 170 | }); 171 | }); 172 | 173 | it('joinChannelByName', function(done) { 174 | TestHelper.initBasic(() => { 175 | var channel = TestHelper.basicChannel(); 176 | TestHelper.basicClient().leaveChannel( 177 | channel.id, 178 | function() { 179 | TestHelper.basicClient().joinChannelByName( 180 | channel.name, 181 | function() { 182 | done(); 183 | }, 184 | function(err) { 185 | done(new Error(err.message)); 186 | } 187 | ); 188 | }, 189 | function(err) { 190 | done(new Error(err.message)); 191 | } 192 | ); 193 | }); 194 | }); 195 | 196 | it('deleteChannel', function(done) { 197 | TestHelper.initBasic(() => { 198 | var channel = TestHelper.basicChannel(); 199 | TestHelper.basicClient().deleteChannel( 200 | channel.id, 201 | function(data) { 202 | assert.equal(data.id, channel.id); 203 | done(); 204 | }, 205 | function(err) { 206 | done(new Error(err.message)); 207 | } 208 | ); 209 | }); 210 | }); 211 | 212 | it('updateLastViewedAt', function(done) { 213 | TestHelper.initBasic(() => { 214 | var channel = TestHelper.basicChannel(); 215 | TestHelper.basicClient().updateLastViewedAt( 216 | channel.id, 217 | function(data) { 218 | assert.equal(data.id, channel.id); 219 | done(); 220 | }, 221 | function(err) { 222 | done(new Error(err.message)); 223 | } 224 | ); 225 | }); 226 | }); 227 | 228 | it('getChannels', function(done) { 229 | TestHelper.initBasic(() => { 230 | TestHelper.basicClient().getChannels( 231 | function(data) { 232 | assert.equal(data.channels.length, 3); 233 | done(); 234 | }, 235 | function(err) { 236 | done(new Error(err.message)); 237 | } 238 | ); 239 | }); 240 | }); 241 | 242 | it('getChannel', function(done) { 243 | TestHelper.initBasic(() => { 244 | TestHelper.basicClient().getChannel( 245 | TestHelper.basicChannel().id, 246 | function(data) { 247 | assert.equal(TestHelper.basicChannel().id, data.channel.id); 248 | done(); 249 | }, 250 | function(err) { 251 | done(new Error(err.message)); 252 | } 253 | ); 254 | }); 255 | }); 256 | 257 | it('getMoreChannels', function(done) { 258 | TestHelper.initBasic(() => { 259 | TestHelper.basicClient().getMoreChannels( 260 | function(data) { 261 | assert.equal(data.channels.length, 0); 262 | done(); 263 | }, 264 | function(err) { 265 | done(new Error(err.message)); 266 | } 267 | ); 268 | }); 269 | }); 270 | 271 | it('getChannelCounts', function(done) { 272 | TestHelper.initBasic(() => { 273 | TestHelper.basicClient().getChannelCounts( 274 | function(data) { 275 | assert.equal(data.counts[TestHelper.basicChannel().id], 1); 276 | done(); 277 | }, 278 | function(err) { 279 | done(new Error(err.message)); 280 | } 281 | ); 282 | }); 283 | }); 284 | 285 | it('getChannelExtraInfo', function(done) { 286 | TestHelper.initBasic(() => { 287 | TestHelper.basicClient().getChannelExtraInfo( 288 | TestHelper.basicChannel().id, 289 | 5, 290 | function(data) { 291 | assert.equal(data.member_count, 1); 292 | done(); 293 | }, 294 | function(err) { 295 | done(new Error(err.message)); 296 | } 297 | ); 298 | }); 299 | }); 300 | 301 | it('addChannelMember', function(done) { 302 | TestHelper.initBasic(() => { 303 | TestHelper.basicClient().createUser( 304 | TestHelper.fakeUser(), 305 | function(user2) { 306 | TestHelper.basicClient().addUserToTeam( 307 | user2.id, 308 | function() { 309 | TestHelper.basicClient().addChannelMember( 310 | TestHelper.basicChannel().id, 311 | user2.id, 312 | function(data) { 313 | assert.equal(data.channel_id.length > 0, true); 314 | done(); 315 | }, 316 | function(err) { 317 | done(new Error(err.message)); 318 | } 319 | ); 320 | }, 321 | function(err) { 322 | done(new Error(err.message)); 323 | } 324 | ); 325 | }, 326 | function(err) { 327 | done(new Error(err.message)); 328 | } 329 | ); 330 | }); 331 | }); 332 | 333 | it('removeChannelMember', function(done) { 334 | TestHelper.initBasic(() => { 335 | TestHelper.basicClient().removeChannelMember( 336 | TestHelper.basicChannel().id, 337 | TestHelper.basicUser().id, 338 | function(data) { 339 | assert.equal(data.channel_id.length > 0, true); 340 | done(); 341 | }, 342 | function(err) { 343 | done(new Error(err.message)); 344 | } 345 | ); 346 | }); 347 | }); 348 | }); 349 | 350 | -------------------------------------------------------------------------------- /tests/client_command.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Commands', function() { 8 | this.timeout(100000); 9 | 10 | it('listCommands', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().listCommands( 13 | function(data) { 14 | assert.equal(data.length > 0, true); 15 | done(); 16 | }, 17 | function(err) { 18 | done(new Error(err.message)); 19 | } 20 | ); 21 | }); 22 | }); 23 | 24 | it('listTeamCommands', function(done) { 25 | TestHelper.initBasic(() => { 26 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 27 | TestHelper.basicClient().listTeamCommands( 28 | function() { 29 | done(new Error('cmds not enabled')); 30 | }, 31 | function(err) { 32 | assert.equal(err.id, 'api.command.disabled.app_error'); 33 | done(); 34 | } 35 | ); 36 | }); 37 | }); 38 | 39 | it('executeCommand', function(done) { 40 | TestHelper.initBasic(() => { 41 | TestHelper.basicClient().executeCommand( 42 | TestHelper.basicChannel().id, 43 | '/shrug', 44 | null, 45 | function(data) { 46 | assert.equal(data.response_type, 'in_channel'); 47 | done(); 48 | }, 49 | function(err) { 50 | done(new Error(err.message)); 51 | } 52 | ); 53 | }); 54 | }); 55 | 56 | it('addCommand', function(done) { 57 | TestHelper.initBasic(() => { 58 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 59 | 60 | var cmd = {}; 61 | cmd.url = 'http://www.gonowhere.com'; 62 | cmd.trigger = '/hello'; 63 | cmd.method = 'P'; 64 | cmd.username = ''; 65 | cmd.icon_url = ''; 66 | cmd.auto_complete = false; 67 | cmd.auto_complete_desc = ''; 68 | cmd.auto_complete_hint = ''; 69 | cmd.display_name = 'Unit Test'; 70 | 71 | TestHelper.basicClient().addCommand( 72 | cmd, 73 | function() { 74 | done(new Error('cmds not enabled')); 75 | }, 76 | function(err) { 77 | assert.equal(err.id, 'api.command.disabled.app_error'); 78 | done(); 79 | } 80 | ); 81 | }); 82 | }); 83 | 84 | it('deleteCommand', function(done) { 85 | TestHelper.initBasic(() => { 86 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 87 | TestHelper.basicClient().deleteCommand( 88 | TestHelper.generateId(), 89 | function() { 90 | done(new Error('cmds not enabled')); 91 | }, 92 | function(err) { 93 | assert.equal(err.id, 'api.command.disabled.app_error'); 94 | done(); 95 | } 96 | ); 97 | }); 98 | }); 99 | 100 | it('regenCommandToken', function(done) { 101 | TestHelper.initBasic(() => { 102 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 103 | TestHelper.basicClient().regenCommandToken( 104 | TestHelper.generateId(), 105 | function() { 106 | done(new Error('cmds not enabled')); 107 | }, 108 | function(err) { 109 | assert.equal(err.id, 'api.command.disabled.app_error'); 110 | done(); 111 | } 112 | ); 113 | }); 114 | }); 115 | }); 116 | 117 | -------------------------------------------------------------------------------- /tests/client_emoji.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | const fs = require('fs'); 8 | 9 | describe('Client.Emoji', function() { 10 | this.timeout(100000); 11 | 12 | before(function() { 13 | // write a temporary file so that we have something to upload for testing 14 | const buffer = new Buffer('R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=', 'base64'); 15 | 16 | const testGif = fs.openSync('test.gif', 'w+'); 17 | fs.writeFileSync(testGif, buffer) 18 | }); 19 | 20 | after(function() { 21 | fs.unlinkSync('test.gif'); 22 | }); 23 | 24 | it('addEmoji', function(done) { 25 | TestHelper.initBasic(() => { 26 | const name = TestHelper.generateId(); 27 | 28 | TestHelper.basicClient().addEmoji( 29 | {creator_id: TestHelper.basicUser().id, name}, 30 | fs.createReadStream('test.gif'), 31 | function(data) { 32 | assert.equal(data.name, name); 33 | assert.notEqual(data.id, null); 34 | 35 | TestHelper.basicClient().deleteEmoji(data.id); 36 | done(); 37 | }, 38 | function(err) { 39 | done(new Error(err.message)); 40 | } 41 | ); 42 | }); 43 | }); 44 | 45 | it('deleteEmoji', function(done) { 46 | TestHelper.initBasic(() => { 47 | TestHelper.basicClient().addEmoji( 48 | {creator_id: TestHelper.basicUser().id, name: TestHelper.generateId()}, 49 | fs.createReadStream('test.gif'), 50 | function(data) { 51 | TestHelper.basicClient().deleteEmoji( 52 | data.id, 53 | function() { 54 | done(); 55 | }, 56 | function(err) { 57 | done(new Error(err.message)); 58 | } 59 | ); 60 | }, 61 | function(err) { 62 | done(new Error(err.message)); 63 | } 64 | ); 65 | }); 66 | }); 67 | 68 | it('listEmoji', function(done) { 69 | TestHelper.initBasic(() => { 70 | const name = TestHelper.generateId(); 71 | TestHelper.basicClient().addEmoji( 72 | {creator_id: TestHelper.basicUser().id, name}, 73 | fs.createReadStream('test.gif'), 74 | function() { 75 | TestHelper.basicClient().listEmoji( 76 | function(data) { 77 | assert(data.length > 0, true); 78 | 79 | let found = false; 80 | for (const emoji of data) { 81 | if (emoji.name === name) { 82 | found = true; 83 | break; 84 | } 85 | } 86 | 87 | if (found) { 88 | done(); 89 | } else { 90 | done(new Error('test emoji wasn\'t returned')); 91 | } 92 | }, 93 | function(err) { 94 | done(new Error(err.message)); 95 | } 96 | ); 97 | }, 98 | function(err) { 99 | done(new Error(err.message)); 100 | } 101 | ); 102 | }); 103 | }); 104 | }); 105 | -------------------------------------------------------------------------------- /tests/client_general.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | var assert = require('assert'); 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.General', function() { 8 | this.timeout(10000); 9 | 10 | it('General.getClientConfig', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().getClientConfig( 13 | function(data) { 14 | assert.equal(data.SiteName, 'Mattermost'); 15 | done(); 16 | }, 17 | function(err) { 18 | done(new Error(err.message)); 19 | } 20 | ); 21 | }); 22 | }); 23 | 24 | it('General.getPing', function(done) { 25 | TestHelper.initBasic(() => { 26 | TestHelper.basicClient().getPing( 27 | function(data) { 28 | assert.equal(data.version.length > 0, true); 29 | done(); 30 | }, 31 | function(err) { 32 | done(new Error(err.message)); 33 | } 34 | ); 35 | }); 36 | }); 37 | 38 | it('General.logClientError', function(done) { 39 | TestHelper.initBasic(() => { 40 | var config = {}; 41 | config.site_name = 'test'; 42 | TestHelper.basicClient().logClientError('this is a test'); 43 | done(); 44 | }); 45 | }); 46 | 47 | it('File.getFileInfo', function(done) { 48 | TestHelper.initBasic(() => { 49 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 50 | 51 | TestHelper.basicClient().getFileInfo( 52 | `/${TestHelper.basicChannel().id}/${TestHelper.basicUser().id}/filename.txt`, 53 | function(data) { 54 | assert.equal(data.filename, 'filename.txt'); 55 | done(); 56 | }, 57 | function(err) { 58 | done(new Error(err.message)); 59 | } 60 | ); 61 | }); 62 | }); 63 | 64 | it('File.getPublicLink', function(done) { 65 | TestHelper.initBasic(() => { 66 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 67 | var data = {}; 68 | data.channel_id = TestHelper.basicChannel().id; 69 | data.user_id = TestHelper.basicUser().id; 70 | data.filename = `/${TestHelper.basicChannel().id}/${TestHelper.basicUser().id}/filename.txt`; 71 | 72 | TestHelper.basicClient().getPublicLink( 73 | data, 74 | function() { 75 | done(new Error('not enabled')); 76 | }, 77 | function(err) { 78 | assert.equal(err.id, 'api.file.get_public_link.disabled.app_error'); 79 | done(); 80 | } 81 | ); 82 | }); 83 | }); 84 | }); 85 | 86 | -------------------------------------------------------------------------------- /tests/client_hooks.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Hooks', function() { 8 | this.timeout(100000); 9 | 10 | it('addIncomingHook', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 13 | 14 | var hook = {}; 15 | hook.channel_id = TestHelper.basicChannel().id; 16 | hook.description = 'desc'; 17 | hook.display_name = 'Unit Test'; 18 | 19 | TestHelper.basicClient().addIncomingHook( 20 | hook, 21 | function() { 22 | done(new Error('hooks not enabled')); 23 | }, 24 | function(err) { 25 | assert.equal(err.id, 'api.webhook.create_incoming.disabled.app_errror'); 26 | done(); 27 | } 28 | ); 29 | }); 30 | }); 31 | 32 | it('deleteIncomingHook', function(done) { 33 | TestHelper.initBasic(() => { 34 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 35 | TestHelper.basicClient().deleteIncomingHook( 36 | TestHelper.generateId(), 37 | function() { 38 | done(new Error('hooks not enabled')); 39 | }, 40 | function(err) { 41 | assert.equal(err.id, 'api.webhook.delete_incoming.disabled.app_errror'); 42 | done(); 43 | } 44 | ); 45 | }); 46 | }); 47 | 48 | it('listIncomingHooks', function(done) { 49 | TestHelper.initBasic(() => { 50 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 51 | TestHelper.basicClient().listIncomingHooks( 52 | function() { 53 | done(new Error('hooks not enabled')); 54 | }, 55 | function(err) { 56 | assert.equal(err.id, 'api.webhook.get_incoming.disabled.app_error'); 57 | done(); 58 | } 59 | ); 60 | }); 61 | }); 62 | 63 | it('addOutgoingHook', function(done) { 64 | TestHelper.initBasic(() => { 65 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 66 | 67 | var hook = {}; 68 | hook.channel_id = TestHelper.basicChannel().id; 69 | hook.description = 'desc'; 70 | hook.display_name = 'Unit Test'; 71 | 72 | TestHelper.basicClient().addOutgoingHook( 73 | hook, 74 | function() { 75 | done(new Error('hooks not enabled')); 76 | }, 77 | function(err) { 78 | assert.equal(err.id, 'api.webhook.create_outgoing.disabled.app_error'); 79 | done(); 80 | } 81 | ); 82 | }); 83 | }); 84 | 85 | it('deleteOutgoingHook', function(done) { 86 | TestHelper.initBasic(() => { 87 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 88 | TestHelper.basicClient().deleteOutgoingHook( 89 | TestHelper.generateId(), 90 | function() { 91 | done(new Error('hooks not enabled')); 92 | }, 93 | function(err) { 94 | assert.equal(err.id, 'api.webhook.delete_outgoing.disabled.app_error'); 95 | done(); 96 | } 97 | ); 98 | }); 99 | }); 100 | 101 | it('listOutgoingHooks', function(done) { 102 | TestHelper.initBasic(() => { 103 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 104 | TestHelper.basicClient().listOutgoingHooks( 105 | function() { 106 | done(new Error('hooks not enabled')); 107 | }, 108 | function(err) { 109 | assert.equal(err.id, 'api.webhook.get_outgoing.disabled.app_error'); 110 | done(); 111 | } 112 | ); 113 | }); 114 | }); 115 | 116 | it('regenOutgoingHookToken', function(done) { 117 | TestHelper.initBasic(() => { 118 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 119 | TestHelper.basicClient().regenOutgoingHookToken( 120 | TestHelper.generateId(), 121 | function() { 122 | done(new Error('hooks not enabled')); 123 | }, 124 | function(err) { 125 | assert.equal(err.id, 'api.webhook.regen_outgoing_token.disabled.app_error'); 126 | done(); 127 | } 128 | ); 129 | }); 130 | }); 131 | }); 132 | 133 | -------------------------------------------------------------------------------- /tests/client_oauth.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.OAuth', function() { 8 | this.timeout(100000); 9 | 10 | it('registerOAuthApp', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 13 | 14 | var app = {}; 15 | app.name = 'test'; 16 | app.homepage = 'homepage'; 17 | app.description = 'desc'; 18 | app.callback_urls = ''; 19 | 20 | TestHelper.basicClient().registerOAuthApp( 21 | app, 22 | function() { 23 | done(new Error('not enabled')); 24 | }, 25 | function(err) { 26 | assert.equal(err.id, 'api.oauth.register_oauth_app.turn_off.app_error'); 27 | done(); 28 | } 29 | ); 30 | }); 31 | }); 32 | 33 | it('allowOAuth2', function(done) { 34 | TestHelper.initBasic(() => { 35 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 36 | 37 | TestHelper.basicClient().allowOAuth2( 38 | 'GET', 39 | '123456', 40 | 'http://nowhere.com', 41 | 'state', 42 | 'scope', 43 | function() { 44 | done(new Error('not enabled')); 45 | }, 46 | function(err) { 47 | assert.equal(err.id, 'api.oauth.allow_oauth.turn_off.app_error'); 48 | done(); 49 | } 50 | ); 51 | }); 52 | }); 53 | }); 54 | -------------------------------------------------------------------------------- /tests/client_post.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Posts', function() { 8 | this.timeout(100000); 9 | 10 | it('createPost', function(done) { 11 | TestHelper.initBasic(() => { 12 | var post = TestHelper.fakePost(); 13 | post.channel_id = TestHelper.basicChannel().id; 14 | 15 | TestHelper.basicClient().createPost( 16 | post, 17 | function(data) { 18 | assert.equal(data.id.length > 0, true); 19 | done(); 20 | }, 21 | function(err) { 22 | done(new Error(err.message)); 23 | } 24 | ); 25 | }); 26 | }); 27 | 28 | it('getPostById', function(done) { 29 | TestHelper.initBasic(() => { 30 | TestHelper.basicClient().getPostById( 31 | TestHelper.basicPost().id, 32 | function(data) { 33 | assert.equal(data.order[0], TestHelper.basicPost().id); 34 | done(); 35 | }, 36 | function(err) { 37 | done(new Error(err.message)); 38 | } 39 | ); 40 | }); 41 | }); 42 | 43 | it('getPost', function(done) { 44 | TestHelper.initBasic(() => { 45 | TestHelper.basicClient().getPost( 46 | TestHelper.basicChannel().id, 47 | TestHelper.basicPost().id, 48 | function(data) { 49 | assert.equal(data.order[0], TestHelper.basicPost().id); 50 | done(); 51 | }, 52 | function(err) { 53 | done(new Error(err.message)); 54 | } 55 | ); 56 | }); 57 | }); 58 | 59 | it('updatePost', function(done) { 60 | TestHelper.initBasic(() => { 61 | var post = TestHelper.basicPost(); 62 | post.message = 'new message'; 63 | post.channel_id = TestHelper.basicChannel().id; 64 | 65 | TestHelper.basicClient().updatePost( 66 | post, 67 | function(data) { 68 | assert.equal(data.id.length > 0, true); 69 | done(); 70 | }, 71 | function(err) { 72 | done(new Error(err.message)); 73 | } 74 | ); 75 | }); 76 | }); 77 | 78 | it('deletePost', function(done) { 79 | TestHelper.initBasic(() => { 80 | TestHelper.basicClient().deletePost( 81 | TestHelper.basicChannel().id, 82 | TestHelper.basicPost().id, 83 | function(data) { 84 | assert.equal(data.id, TestHelper.basicPost().id); 85 | done(); 86 | }, 87 | function(err) { 88 | done(new Error(err.message)); 89 | } 90 | ); 91 | }); 92 | }); 93 | 94 | it('searchPost', function(done) { 95 | TestHelper.initBasic(() => { 96 | TestHelper.basicClient().search( 97 | 'unit test', 98 | false, 99 | function(data) { 100 | assert.equal(data.order[0], TestHelper.basicPost().id); 101 | done(); 102 | }, 103 | function(err) { 104 | done(new Error(err.message)); 105 | } 106 | ); 107 | }); 108 | }); 109 | 110 | it('getPostsPage', function(done) { 111 | TestHelper.initBasic(() => { 112 | TestHelper.basicClient().getPostsPage( 113 | TestHelper.basicChannel().id, 114 | 0, 115 | 10, 116 | function(data) { 117 | assert.equal(data.order[0], TestHelper.basicPost().id); 118 | done(); 119 | }, 120 | function(err) { 121 | done(new Error(err.message)); 122 | } 123 | ); 124 | }); 125 | }); 126 | 127 | it('getPosts', function(done) { 128 | TestHelper.initBasic(() => { 129 | TestHelper.basicClient().getPosts( 130 | TestHelper.basicChannel().id, 131 | 0, 132 | function(data) { 133 | assert.equal(data.order[0], TestHelper.basicPost().id); 134 | done(); 135 | }, 136 | function(err) { 137 | done(new Error(err.message)); 138 | } 139 | ); 140 | }); 141 | }); 142 | 143 | it('getPostsBefore', function(done) { 144 | TestHelper.initBasic(() => { 145 | var post = TestHelper.fakePost(); 146 | post.channel_id = TestHelper.basicChannel().id; 147 | 148 | TestHelper.basicClient().createPost( 149 | post, 150 | function(rpost) { 151 | TestHelper.basicClient().getPostsBefore( 152 | TestHelper.basicChannel().id, 153 | rpost.id, 154 | 0, 155 | 10, 156 | function(data) { 157 | assert.equal(data.order[0], TestHelper.basicPost().id); 158 | done(); 159 | }, 160 | function(err) { 161 | done(new Error(err.message)); 162 | } 163 | ); 164 | }, 165 | function(err) { 166 | done(new Error(err.message)); 167 | } 168 | ); 169 | }); 170 | }); 171 | 172 | it('getPostsAfter', function(done) { 173 | TestHelper.initBasic(() => { 174 | var post = TestHelper.fakePost(); 175 | post.channel_id = TestHelper.basicChannel().id; 176 | 177 | TestHelper.basicClient().createPost( 178 | post, 179 | function(rpost) { 180 | TestHelper.basicClient().getPostsAfter( 181 | TestHelper.basicChannel().id, 182 | TestHelper.basicPost().id, 183 | 0, 184 | 10, 185 | function(data) { 186 | assert.equal(data.order[0], rpost.id); 187 | done(); 188 | }, 189 | function(err) { 190 | done(new Error(err.message)); 191 | } 192 | ); 193 | }, 194 | function(err) { 195 | done(new Error(err.message)); 196 | } 197 | ); 198 | }); 199 | }); 200 | }); 201 | 202 | -------------------------------------------------------------------------------- /tests/client_preferences.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Preferences', function() { 8 | this.timeout(100000); 9 | 10 | it('getAllPreferences', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().getAllPreferences( 13 | function(data) { 14 | assert.equal(data[0].category, 'tutorial_step'); 15 | assert.equal(data[0].user_id, TestHelper.basicUser().id); 16 | done(); 17 | }, 18 | function(err) { 19 | done(new Error(err.message)); 20 | } 21 | ); 22 | }); 23 | }); 24 | 25 | it('savePreferences', function(done) { 26 | TestHelper.initBasic(() => { 27 | var perf = {}; 28 | perf.user_id = TestHelper.basicUser().id; 29 | perf.category = 'test'; 30 | perf.name = 'name'; 31 | perf.value = 'value'; 32 | 33 | var perfs = []; 34 | perfs.push(perf); 35 | 36 | TestHelper.basicClient().savePreferences( 37 | perfs, 38 | function(data) { 39 | assert.equal(data, true); 40 | done(); 41 | }, 42 | function(err) { 43 | done(new Error(err.message)); 44 | } 45 | ); 46 | }); 47 | }); 48 | 49 | it('getPreferenceCategory', function(done) { 50 | TestHelper.initBasic(() => { 51 | TestHelper.basicClient().getPreferenceCategory( 52 | 'tutorial_step', 53 | function(data) { 54 | assert.equal(data[0].category, 'tutorial_step'); 55 | assert.equal(data[0].user_id, TestHelper.basicUser().id); 56 | done(); 57 | }, 58 | function(err) { 59 | done(new Error(err.message)); 60 | } 61 | ); 62 | }); 63 | }); 64 | }); 65 | 66 | -------------------------------------------------------------------------------- /tests/client_team.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.Team', function() { 8 | this.timeout(100000); 9 | 10 | it('findTeamByName', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().findTeamByName( 13 | TestHelper.basicTeam().name, 14 | function(data) { 15 | assert.equal(data, true); 16 | done(); 17 | }, 18 | function(err) { 19 | done(new Error(err.message)); 20 | } 21 | ); 22 | }); 23 | }); 24 | 25 | it('signupTeam', function(done) { 26 | var client = TestHelper.createClient(); 27 | var email = TestHelper.fakeEmail(); 28 | 29 | client.signupTeam( 30 | email, 31 | function(data) { 32 | assert.equal(data.email, email); 33 | done(); 34 | }, 35 | function(err) { 36 | done(new Error(err.message)); 37 | } 38 | ); 39 | }); 40 | 41 | it('createTeamFromSignup', function(done) { 42 | var client = TestHelper.createClient(); 43 | var email = TestHelper.fakeEmail(); 44 | 45 | client.signupTeam( 46 | email, 47 | function(data) { 48 | var teamSignup = {}; 49 | teamSignup.invites = []; 50 | teamSignup.data = decodeURIComponent(data.follow_link.split('&h=')[0].replace('/signup_team_complete/?d=', '')); 51 | teamSignup.hash = decodeURIComponent(data.follow_link.split('&h=')[1]); 52 | 53 | teamSignup.user = TestHelper.fakeUser(); 54 | teamSignup.team = TestHelper.fakeTeam(); 55 | teamSignup.team.email = teamSignup.user.email; 56 | 57 | client.createTeamFromSignup( 58 | teamSignup, 59 | function(data2) { 60 | assert.equal(data2.team.id.length > 0, true); 61 | assert.equal(data2.user.id.length > 0, true); 62 | done(); 63 | }, 64 | function(err) { 65 | done(new Error(err.message)); 66 | } 67 | ); 68 | }, 69 | function(err) { 70 | done(new Error(err.message)); 71 | } 72 | ); 73 | }); 74 | 75 | it('createTeam', function(done) { 76 | var client = TestHelper.createClient(); 77 | var team = TestHelper.fakeTeam(); 78 | client.createTeam( 79 | team, 80 | function(data) { 81 | assert.equal(data.id.length > 0, true); 82 | assert.equal(data.name, team.name); 83 | done(); 84 | }, 85 | function(err) { 86 | done(new Error(err.message)); 87 | } 88 | ); 89 | }); 90 | 91 | it('getAllTeams', function(done) { 92 | TestHelper.initBasic(() => { 93 | TestHelper.basicClient().getAllTeams( 94 | function(data) { 95 | assert.equal(data[TestHelper.basicTeam().id].name, TestHelper.basicTeam().name); 96 | done(); 97 | }, 98 | function(err) { 99 | done(new Error(err.message)); 100 | } 101 | ); 102 | }); 103 | }); 104 | 105 | it('getAllTeamListings', function(done) { 106 | TestHelper.initBasic(() => { 107 | TestHelper.basicClient().getAllTeamListings( 108 | function(data) { 109 | assert.equal(data != null, true); 110 | done(); 111 | }, 112 | function(err) { 113 | done(new Error(err.message)); 114 | } 115 | ); 116 | }); 117 | }); 118 | 119 | it('getMyTeam', function(done) { 120 | TestHelper.initBasic(() => { 121 | TestHelper.basicClient().getMyTeam( 122 | function(data) { 123 | assert.equal(data.name, TestHelper.basicTeam().name); 124 | done(); 125 | }, 126 | function(err) { 127 | done(new Error(err.message)); 128 | } 129 | ); 130 | }); 131 | }); 132 | 133 | it('GetTeamMembers', function(done) { 134 | TestHelper.initBasic(() => { 135 | TestHelper.basicClient().getTeamMembers( 136 | TestHelper.basicTeam().id, 137 | function(data) { 138 | assert.equal(data.length > 0, true); 139 | done(); 140 | }, 141 | function(err) { 142 | done(new Error(err.message)); 143 | } 144 | ); 145 | }); 146 | }); 147 | 148 | it('inviteMembers', function(done) { 149 | TestHelper.initBasic(() => { 150 | var data = {}; 151 | data.invites = []; 152 | var invite = {}; 153 | invite.email = TestHelper.fakeEmail(); 154 | invite.firstName = 'first'; 155 | invite.lastName = 'last'; 156 | data.invites.push(invite); 157 | 158 | TestHelper.basicClient().inviteMembers( 159 | data, 160 | function(dataBack) { 161 | assert.equal(dataBack.invites.length, 1); 162 | done(); 163 | }, 164 | function(err) { 165 | done(new Error(err.message)); 166 | } 167 | ); 168 | }); 169 | }); 170 | 171 | it('updateTeam', function(done) { 172 | TestHelper.initBasic(() => { 173 | var team = TestHelper.basicTeam(); 174 | team.display_name = 'test_updated'; 175 | 176 | TestHelper.basicClient().updateTeam( 177 | team, 178 | function(data) { 179 | assert.equal(data.display_name, 'test_updated'); 180 | done(); 181 | }, 182 | function(err) { 183 | done(new Error(err.message)); 184 | } 185 | ); 186 | }); 187 | }); 188 | 189 | it('addUserToTeam', function(done) { 190 | TestHelper.initBasic(() => { 191 | TestHelper.basicClient().createUser( 192 | TestHelper.fakeUser(), 193 | function(user2) { 194 | TestHelper.basicClient().addUserToTeam( 195 | "", 196 | user2.id, 197 | function(data) { 198 | assert.equal(data.user_id, user2.id); 199 | done(); 200 | }, 201 | function(err) { 202 | done(new Error(err.message)); 203 | } 204 | ); 205 | }, 206 | function(err) { 207 | done(new Error(err.message)); 208 | } 209 | ); 210 | }); 211 | }); 212 | 213 | it('removeUserFromTeam', function(done) { 214 | TestHelper.initBasic(() => { 215 | TestHelper.basicClient().removeUserFromTeam( 216 | "", 217 | TestHelper.basicUser().id, 218 | function(data) { 219 | assert.equal(data.user_id, TestHelper.basicUser().id); 220 | done(); 221 | }, 222 | function(err) { 223 | done(new Error(err.message)); 224 | } 225 | ); 226 | }); 227 | }); 228 | 229 | it('getInviteInfo', function(done) { 230 | TestHelper.initBasic(() => { 231 | TestHelper.basicClient().getInviteInfo( 232 | TestHelper.basicTeam().invite_id, 233 | function(data) { 234 | assert.equal(data.display_name.length > 0, true); 235 | done(); 236 | }, 237 | function(err) { 238 | done(new Error(err.message)); 239 | } 240 | ); 241 | }); 242 | }); 243 | }); 244 | 245 | -------------------------------------------------------------------------------- /tests/client_user.test.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import assert from 'assert'; 5 | import TestHelper from './test_helper.jsx'; 6 | 7 | describe('Client.User', function() { 8 | this.timeout(100000); 9 | 10 | it('getMe', function(done) { 11 | TestHelper.initBasic(() => { 12 | TestHelper.basicClient().getMe( 13 | function(data) { 14 | assert.equal(data.id, TestHelper.basicUser().id); 15 | done(); 16 | }, 17 | function(err) { 18 | done(new Error(err.message)); 19 | } 20 | ); 21 | }); 22 | }); 23 | 24 | it('getInitialLoad', function(done) { 25 | TestHelper.initBasic(() => { 26 | TestHelper.basicClient().getInitialLoad( 27 | function(data) { 28 | assert.equal(data.user.id.length > 0, true); 29 | done(); 30 | }, 31 | function(err) { 32 | done(new Error(err.message)); 33 | } 34 | ); 35 | }); 36 | }); 37 | 38 | it('createUser', function(done) { 39 | var client = TestHelper.createClient(); 40 | var user = TestHelper.fakeUser(); 41 | client.createUser( 42 | user, 43 | function(data) { 44 | assert.equal(data.id.length > 0, true); 45 | assert.equal(data.email, user.email); 46 | done(); 47 | }, 48 | function(err) { 49 | done(new Error(err.message)); 50 | } 51 | ); 52 | }); 53 | 54 | it('loginByEmail', function(done) { 55 | var client = TestHelper.createClient(); 56 | var user = TestHelper.fakeUser(); 57 | client.createUser( 58 | user, 59 | function() { 60 | client.login( 61 | user.email, 62 | user.password, 63 | null, 64 | function(data) { 65 | assert.equal(data.id.length > 0, true); 66 | assert.equal(data.email, user.email); 67 | done(); 68 | }, 69 | function(err) { 70 | done(new Error(err.message)); 71 | } 72 | ); 73 | }, 74 | function(err) { 75 | done(new Error(err.message)); 76 | } 77 | ); 78 | }); 79 | 80 | it('loginById', function(done) { 81 | var client = TestHelper.createClient(); 82 | var user = TestHelper.fakeUser(); 83 | client.createUser( 84 | user, 85 | function(newUser) { 86 | assert.equal(user.email, newUser.email); 87 | client.loginById( 88 | newUser.id, 89 | user.password, 90 | null, 91 | function(data) { 92 | assert.equal(data.id.length > 0, true); 93 | assert.equal(data.email, user.email); 94 | done(); 95 | }, 96 | function(err) { 97 | done(new Error(err.message)); 98 | } 99 | ); 100 | }, 101 | function(err) { 102 | done(new Error(err.message)); 103 | } 104 | ); 105 | }); 106 | 107 | it('loginByUsername', function(done) { 108 | var client = TestHelper.createClient(); 109 | var user = TestHelper.fakeUser(); 110 | client.createUser( 111 | user, 112 | function() { 113 | client.login( 114 | user.username, 115 | user.password, 116 | null, 117 | function(data) { 118 | assert.equal(data.id.length > 0, true); 119 | assert.equal(data.email, user.email); 120 | done(); 121 | }, 122 | function(err) { 123 | done(new Error(err.message)); 124 | } 125 | ); 126 | }, 127 | function(err) { 128 | done(new Error(err.message)); 129 | } 130 | ); 131 | }); 132 | 133 | it('updateUser', function(done) { 134 | TestHelper.initBasic(() => { 135 | var user = TestHelper.basicUser(); 136 | user.nickname = 'updated'; 137 | 138 | TestHelper.basicClient().updateUser( 139 | user, null, 140 | function(data) { 141 | assert.equal(data.nickname, 'updated'); 142 | done(); 143 | }, 144 | function(err) { 145 | done(new Error(err.message)); 146 | } 147 | ); 148 | }); 149 | }); 150 | 151 | it('updatePassword', function(done) { 152 | TestHelper.initBasic(() => { 153 | var user = TestHelper.basicUser(); 154 | 155 | TestHelper.basicClient().updatePassword( 156 | user.id, 157 | user.password, 158 | 'update_password', 159 | function(data) { 160 | assert.equal(data.user_id, user.id); 161 | done(); 162 | }, 163 | function(err) { 164 | done(new Error(err.message)); 165 | } 166 | ); 167 | }); 168 | }); 169 | 170 | it('updateUserNotifyProps', function(done) { 171 | TestHelper.initBasic(() => { 172 | var user = TestHelper.basicUser(); 173 | 174 | var notifyProps = { 175 | all: 'true', 176 | channel: 'true', 177 | desktop: 'all', 178 | desktop_sound: 'true', 179 | email: 'false', 180 | first_name: 'false', 181 | mention_keys: '', 182 | user_id: user.id 183 | }; 184 | 185 | TestHelper.basicClient().updateUserNotifyProps( 186 | notifyProps, 187 | function(data) { 188 | assert.equal(data.notify_props.email, 'false'); 189 | done(); 190 | }, 191 | function(err) { 192 | done(new Error(err.message)); 193 | } 194 | ); 195 | }); 196 | }); 197 | 198 | it('updateRoles', function(done) { 199 | TestHelper.initBasic(() => { 200 | var user = TestHelper.basicUser(); 201 | var team = TestHelper.basicTeam(); 202 | 203 | TestHelper.basicClient().updateRoles( 204 | team.id, 205 | user.id, 206 | '', 207 | function(data) { 208 | assert.equal(data.user_id, user.id); 209 | done(); 210 | }, 211 | function(err) { 212 | done(new Error(err.message)); 213 | } 214 | ); 215 | }); 216 | }); 217 | 218 | it('updateActive', function(done) { 219 | TestHelper.initBasic(() => { 220 | var user = TestHelper.basicUser(); 221 | 222 | TestHelper.basicClient().updateActive( 223 | user.id, 224 | false, 225 | function(data) { 226 | assert.equal(data.last_activity_at > 0, true); 227 | done(); 228 | }, 229 | function(err) { 230 | done(new Error(err.message)); 231 | } 232 | ); 233 | }); 234 | }); 235 | 236 | it('sendPasswordReset', function(done) { 237 | TestHelper.initBasic(() => { 238 | var user = TestHelper.basicUser(); 239 | 240 | TestHelper.basicClient().sendPasswordReset( 241 | user.email, 242 | function(data) { 243 | assert.equal(data.email, user.email); 244 | done(); 245 | }, 246 | function(err) { 247 | done(new Error(err.message)); 248 | } 249 | ); 250 | }); 251 | }); 252 | 253 | it('resetPassword', function(done) { 254 | TestHelper.initBasic(() => { 255 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 256 | 257 | TestHelper.basicClient().resetPassword( 258 | '', 259 | 'new_password', 260 | function() { 261 | throw Error('shouldnt work'); 262 | }, 263 | function(err) { 264 | // this should fail since you're not a system admin 265 | assert.equal(err.id, 'api.context.invalid_param.app_error'); 266 | done(); 267 | } 268 | ); 269 | }); 270 | }); 271 | 272 | it('emailToOAuth', function(done) { 273 | TestHelper.initBasic(() => { 274 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 275 | var user = TestHelper.basicUser(); 276 | 277 | TestHelper.basicClient().emailToOAuth( 278 | user.email, 279 | 'new_password', 280 | 'gitlab', 281 | function() { 282 | throw Error('shouldnt work'); 283 | }, 284 | function(err) { 285 | // this should fail since you're not a system admin 286 | assert.equal(err.id, 'api.user.check_user_password.invalid.app_error'); 287 | done(); 288 | } 289 | ); 290 | }); 291 | }); 292 | 293 | it('oauthToEmail', function(done) { 294 | TestHelper.initBasic(() => { 295 | var user = TestHelper.basicUser(); 296 | 297 | TestHelper.basicClient().oauthToEmail( 298 | user.email, 299 | 'new_password', 300 | function(data) { 301 | assert.equal(data.follow_link.length > 0, true); 302 | done(); 303 | }, 304 | function(err) { 305 | done(new Error(err.message)); 306 | } 307 | ); 308 | }); 309 | }); 310 | 311 | it('emailToLdap', function(done) { 312 | TestHelper.initBasic(() => { 313 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 314 | var user = TestHelper.basicUser(); 315 | 316 | TestHelper.basicClient().emailToLdap( 317 | user.email, 318 | user.password, 319 | 'unknown_id', 320 | 'unknown_pwd', 321 | function() { 322 | throw Error('shouldnt work'); 323 | }, 324 | function() { 325 | done(); 326 | } 327 | ); 328 | }); 329 | }); 330 | 331 | it('ldapToEmail', function(done) { 332 | TestHelper.initBasic(() => { 333 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 334 | var user = TestHelper.basicUser(); 335 | 336 | TestHelper.basicClient().ldapToEmail( 337 | user.email, 338 | 'new_password', 339 | 'new_password', 340 | function() { 341 | throw Error('shouldnt work'); 342 | }, 343 | function(err) { 344 | assert.equal(err.id, 'api.user.ldap_to_email.not_ldap_account.app_error'); 345 | done(); 346 | } 347 | ); 348 | }); 349 | }); 350 | 351 | it('logout', function(done) { 352 | TestHelper.initBasic(() => { 353 | TestHelper.basicClient().logout( 354 | function(data) { 355 | assert.equal(data.user_id, TestHelper.basicUser().id); 356 | done(); 357 | }, 358 | function(err) { 359 | done(new Error(err.message)); 360 | } 361 | ); 362 | }); 363 | }); 364 | 365 | it('checkMfa', function(done) { 366 | TestHelper.initBasic(() => { 367 | TestHelper.basicClient().checkMfa( 368 | TestHelper.generateId(), 369 | function(data) { 370 | assert.equal(data.mfa_required, 'false'); 371 | done(); 372 | }, 373 | function(err) { 374 | done(new Error(err.message)); 375 | } 376 | ); 377 | }); 378 | }); 379 | 380 | it('getSessions', function(done) { 381 | TestHelper.initBasic(() => { 382 | TestHelper.basicClient().getSessions( 383 | TestHelper.basicUser().id, 384 | function(data) { 385 | assert.equal(data[0].user_id, TestHelper.basicUser().id); 386 | done(); 387 | }, 388 | function(err) { 389 | done(new Error(err.message)); 390 | } 391 | ); 392 | }); 393 | }); 394 | 395 | it('revokeSession', function(done) { 396 | TestHelper.initBasic(() => { 397 | TestHelper.basicClient().getSessions( 398 | TestHelper.basicUser().id, 399 | function(sessions) { 400 | TestHelper.basicClient().revokeSession( 401 | sessions[0].id, 402 | function(data) { 403 | assert.equal(data.id, sessions[0].id); 404 | done(); 405 | }, 406 | function(err) { 407 | done(new Error(err.message)); 408 | } 409 | ); 410 | }, 411 | function(err) { 412 | done(new Error(err.message)); 413 | } 414 | ); 415 | }); 416 | }); 417 | 418 | it('getAudits', function(done) { 419 | TestHelper.initBasic(() => { 420 | TestHelper.basicClient().getAudits( 421 | TestHelper.basicUser().id, 422 | function(data) { 423 | assert.equal(data[0].user_id, TestHelper.basicUser().id); 424 | done(); 425 | }, 426 | function(err) { 427 | done(new Error(err.message)); 428 | } 429 | ); 430 | }); 431 | }); 432 | 433 | it('getDirectProfiles', function(done) { 434 | TestHelper.initBasic(() => { 435 | TestHelper.basicClient().getDirectProfiles( 436 | function(data) { 437 | assert.equal(Object.keys(data).length === 0, true); 438 | done(); 439 | }, 440 | function(err) { 441 | done(new Error(err.getDirectProfiles)); 442 | } 443 | ); 444 | }); 445 | }); 446 | 447 | it('getProfiles', function(done) { 448 | TestHelper.initBasic(() => { 449 | TestHelper.basicClient().getProfiles( 450 | function(data) { 451 | assert.equal(data[TestHelper.basicUser().id].id, TestHelper.basicUser().id); 452 | done(); 453 | }, 454 | function(err) { 455 | done(new Error(err.message)); 456 | } 457 | ); 458 | }); 459 | }); 460 | 461 | it('getProfilesForTeam', function(done) { 462 | TestHelper.initBasic(() => { 463 | TestHelper.basicClient().getProfilesForTeam( 464 | TestHelper.basicTeam().id, 465 | function(data) { 466 | assert.equal(data[TestHelper.basicUser().id].id, TestHelper.basicUser().id); 467 | done(); 468 | }, 469 | function(err) { 470 | done(new Error(err.message)); 471 | } 472 | ); 473 | }); 474 | }); 475 | 476 | it('getProfilesForDirectMessageList', function(done) { 477 | TestHelper.initBasic(() => { 478 | TestHelper.basicClient().getProfilesForDirectMessageList( 479 | function(data) { 480 | assert.equal(Object.keys(data).length > 0, true); 481 | done(); 482 | }, 483 | function(err) { 484 | done(new Error(err.message)); 485 | } 486 | ); 487 | }); 488 | }); 489 | 490 | it('getStatuses', function(done) { 491 | TestHelper.initBasic(() => { 492 | var ids = []; 493 | ids.push(TestHelper.basicUser().id); 494 | 495 | TestHelper.basicClient().getStatuses( 496 | ids, 497 | function(data) { 498 | assert.equal(data[TestHelper.basicUser().id], 'online'); 499 | done(); 500 | }, 501 | function(err) { 502 | done(new Error(err.message)); 503 | } 504 | ); 505 | }); 506 | }); 507 | 508 | it('verifyEmail', function(done) { 509 | TestHelper.initBasic(() => { 510 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 511 | TestHelper.basicClient().verifyEmail( 512 | 'junk', 513 | 'junk', 514 | function() { 515 | done(new Error('should be invalid')); 516 | }, 517 | function(err) { 518 | assert.equal(err.id, 'api.context.invalid_param.app_error'); 519 | done(); 520 | } 521 | ); 522 | }); 523 | }); 524 | 525 | it('resendVerification', function(done) { 526 | TestHelper.initBasic(() => { 527 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 528 | TestHelper.basicClient().resendVerification( 529 | TestHelper.basicUser().email, 530 | function() { 531 | done(); 532 | }, 533 | function(err) { 534 | done(new Error(err.message)); 535 | } 536 | ); 537 | }); 538 | }); 539 | 540 | it('updateMfa', function(done) { 541 | TestHelper.initBasic(() => { 542 | TestHelper.basicClient().enableLogErrorsToConsole(false); // Disabling since this unit test causes an error 543 | TestHelper.basicClient().updateMfa( 544 | 'junk', 545 | true, 546 | function() { 547 | done(new Error('not enabled')); 548 | }, 549 | function() { 550 | done(); 551 | } 552 | ); 553 | }); 554 | }); 555 | }); 556 | -------------------------------------------------------------------------------- /tests/test_helper.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | import Client from '../client.jsx'; 5 | import jqd from 'jquery-deferred'; 6 | 7 | class TestHelperClass { 8 | basicClient = () => { 9 | return this.basicc; 10 | } 11 | 12 | basicTeam = () => { 13 | return this.basict; 14 | } 15 | 16 | basicUser = () => { 17 | return this.basicu; 18 | } 19 | 20 | basicChannel = () => { 21 | return this.basicch; 22 | } 23 | 24 | basicPost = () => { 25 | return this.basicp; 26 | } 27 | 28 | generateId = () => { 29 | // implementation taken from http://stackoverflow.com/a/2117523 30 | var id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; 31 | 32 | id = id.replace(/[xy]/g, function replaceRandom(c) { 33 | var r = Math.floor(Math.random() * 16); 34 | 35 | var v; 36 | if (c === 'x') { 37 | v = r; 38 | } else { 39 | v = r & 0x3 | 0x8; 40 | } 41 | 42 | return v.toString(16); 43 | }); 44 | 45 | return 'uid' + id; 46 | } 47 | 48 | createClient() { 49 | var c = new Client(); 50 | c.setUrl('http://localhost:8065'); 51 | c.useHeaderToken(); 52 | c.enableLogErrorsToConsole(true); 53 | return c; 54 | } 55 | 56 | fakeEmail = () => { 57 | return 'success' + this.generateId() + '@simulator.amazonses.com'; 58 | } 59 | 60 | fakeUser = () => { 61 | var user = {}; 62 | user.email = this.fakeEmail(); 63 | user.allow_marketing = true; 64 | user.password = 'password1'; 65 | user.username = this.generateId(); 66 | return user; 67 | } 68 | 69 | fakeTeam = () => { 70 | var team = {}; 71 | team.name = this.generateId(); 72 | team.display_name = `Unit Test ${team.name}`; 73 | team.type = 'O'; 74 | team.email = this.fakeEmail(); 75 | team.allowed_domains = ''; 76 | return team; 77 | } 78 | 79 | fakeChannel = () => { 80 | var channel = {}; 81 | channel.name = this.generateId(); 82 | channel.display_name = `Unit Test ${channel.name}`; 83 | channel.type = 'O'; // open channel 84 | return channel; 85 | } 86 | 87 | fakePost = () => { 88 | var post = {}; 89 | post.message = `Unit Test ${this.generateId()}`; 90 | return post; 91 | } 92 | 93 | initBasic = (callback) => { 94 | this.basicc = this.createClient(); 95 | 96 | var d1 = jqd.Deferred(); 97 | var email = this.fakeEmail(); 98 | var outer = this; // eslint-disable-line consistent-this 99 | 100 | this.basicClient().signupTeam( 101 | email, 102 | function(rsignUp) { 103 | var teamSignup = {}; 104 | teamSignup.invites = []; 105 | teamSignup.data = decodeURIComponent(rsignUp.follow_link.split('&h=')[0].replace('/signup_team_complete/?d=', '')); 106 | teamSignup.hash = decodeURIComponent(rsignUp.follow_link.split('&h=')[1]); 107 | 108 | teamSignup.user = outer.fakeUser(); 109 | teamSignup.team = outer.fakeTeam(); 110 | teamSignup.team.email = email; 111 | teamSignup.user.email = email; 112 | var password = teamSignup.user.password; 113 | 114 | outer.basicClient().createTeamFromSignup( 115 | teamSignup, 116 | function(rteamSignup) { 117 | outer.basict = rteamSignup.team; 118 | outer.basicu = rteamSignup.user; 119 | outer.basicu.password = password; 120 | outer.basicClient().setTeamId(outer.basict.id); 121 | outer.basicClient().login( 122 | rteamSignup.user.email, 123 | password, 124 | null, 125 | function() { 126 | outer.basicClient().useHeaderToken(); 127 | var channel = outer.fakeChannel(); 128 | channel.team_id = outer.basicTeam().id; 129 | outer.basicClient().createChannel( 130 | channel, 131 | function(rchannel) { 132 | outer.basicch = rchannel; 133 | var post = outer.fakePost(); 134 | post.channel_id = rchannel.id; 135 | 136 | outer.basicClient().createPost( 137 | post, 138 | function(rpost) { 139 | outer.basicp = rpost; 140 | d1.resolve(); 141 | }, 142 | function(err) { 143 | throw err; 144 | } 145 | ); 146 | }, 147 | function(err) { 148 | throw err; 149 | } 150 | ); 151 | }, 152 | function(err) { 153 | throw err; 154 | } 155 | ); 156 | }, 157 | function(err) { 158 | throw err; 159 | } 160 | ); 161 | }, 162 | function(err) { 163 | throw err; 164 | } 165 | ); 166 | 167 | jqd.when(d1).done(() => { 168 | callback(); 169 | }); 170 | } 171 | } 172 | 173 | var TestHelper = new TestHelperClass(); 174 | export default TestHelper; 175 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'); 2 | var path = require('path'); 3 | var nodeExternals = require('webpack-node-externals'); 4 | 5 | const NPM_TARGET = process.env.npm_lifecycle_event; //eslint-disable-line no-process-env 6 | 7 | var config = { 8 | entry: './main.jsx', 9 | output: { 10 | path: 'lib', 11 | libraryTarget: "commonjs2", 12 | library: "Mattermost", 13 | filename: 'client.js' 14 | }, 15 | module: { 16 | loaders: [ 17 | { 18 | test: /\.jsx?$/, 19 | loader: 'babel', 20 | exclude: /(node_modules|non_npm_dependencies)/, 21 | query: { 22 | presets: ['es2015-webpack', 'stage-0'], 23 | plugins: ['transform-runtime'], 24 | } 25 | } 26 | ] 27 | }, 28 | plugins: [ 29 | new webpack.LoaderOptionsPlugin({ 30 | minimize: true, 31 | debug: false 32 | }) 33 | ], 34 | resolve: { 35 | alias: { 36 | superagent: 'node_modules/superagent/lib/client' 37 | } 38 | }, 39 | externals: [nodeExternals()] 40 | }; 41 | 42 | if (NPM_TARGET === 'test') { 43 | config.target = 'node'; 44 | } 45 | 46 | config.devtool = 'source-map'; 47 | config.plugins.push( 48 | new webpack.optimize.UglifyJsPlugin({ 49 | 'screw-ie8': true, 50 | mangle: { 51 | toplevel: false 52 | }, 53 | compress: { 54 | warnings: false 55 | }, 56 | comments: false 57 | }) 58 | ); 59 | config.plugins.push( 60 | new webpack.optimize.AggressiveMergingPlugin() 61 | ); 62 | config.plugins.push( 63 | new webpack.optimize.OccurrenceOrderPlugin(true) 64 | ); 65 | config.plugins.push( 66 | new webpack.optimize.DedupePlugin() 67 | ); 68 | 69 | module.exports = config; 70 | -------------------------------------------------------------------------------- /websocket_client.jsx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 | // See License.txt for license information. 3 | 4 | const MAX_WEBSOCKET_FAILS = 7; 5 | const MIN_WEBSOCKET_RETRY_TIME = 3000; // 3 sec 6 | const MAX_WEBSOCKET_RETRY_TIME = 300000; // 5 mins 7 | 8 | export default class WebSocketClient { 9 | constructor() { 10 | this.conn = null; 11 | this.connectionUrl = null; 12 | this.sequence = 1; 13 | this.eventSequence = 0; 14 | this.connectFailCount = 0; 15 | this.eventCallback = null; 16 | this.responseCallbacks = {}; 17 | this.firstConnectCallback = null; 18 | this.reconnectCallback = null; 19 | this.missedEventCallback = null; 20 | this.errorCallback = null; 21 | this.closeCallback = null; 22 | } 23 | 24 | initialize(connectionUrl = this.connectionUrl, token) { 25 | if (this.conn) { 26 | return; 27 | } 28 | 29 | if (connectionUrl == null) { 30 | console.log('websocket must have connection url'); //eslint-disable-line no-console 31 | return; 32 | } 33 | 34 | if (this.connectFailCount === 0) { 35 | console.log('websocket connecting to ' + connectionUrl); //eslint-disable-line no-console 36 | } 37 | 38 | this.conn = new WebSocket(connectionUrl); 39 | this.connectionUrl = connectionUrl; 40 | 41 | this.conn.onopen = () => { 42 | this.eventSequence = 0; 43 | 44 | if (token) { 45 | this.sendMessage('authentication_challenge', {token}); 46 | } 47 | 48 | if (this.connectFailCount > 0) { 49 | console.log('websocket re-established connection'); //eslint-disable-line no-console 50 | if (this.reconnectCallback) { 51 | this.reconnectCallback(); 52 | } 53 | } else if (this.firstConnectCallback) { 54 | this.firstConnectCallback(); 55 | } 56 | 57 | this.connectFailCount = 0; 58 | }; 59 | 60 | this.conn.onclose = () => { 61 | this.conn = null; 62 | this.sequence = 1; 63 | 64 | if (this.connectFailCount === 0) { 65 | console.log('websocket closed'); //eslint-disable-line no-console 66 | } 67 | 68 | this.connectFailCount++; 69 | 70 | if (this.closeCallback) { 71 | this.closeCallback(this.connectFailCount); 72 | } 73 | 74 | let retryTime = MIN_WEBSOCKET_RETRY_TIME; 75 | 76 | // If we've failed a bunch of connections then start backing off 77 | if (this.connectFailCount > MAX_WEBSOCKET_FAILS) { 78 | retryTime = MIN_WEBSOCKET_RETRY_TIME * this.connectFailCount * this.connectFailCount; 79 | if (retryTime > MAX_WEBSOCKET_RETRY_TIME) { 80 | retryTime = MAX_WEBSOCKET_RETRY_TIME; 81 | } 82 | } 83 | 84 | setTimeout( 85 | () => { 86 | this.initialize(connectionUrl, token); 87 | }, 88 | retryTime 89 | ); 90 | }; 91 | 92 | this.conn.onerror = (evt) => { 93 | if (this.connectFailCount <= 1) { 94 | console.log('websocket error'); //eslint-disable-line no-console 95 | console.log(evt); //eslint-disable-line no-console 96 | } 97 | 98 | if (this.errorCallback) { 99 | this.errorCallback(evt); 100 | } 101 | }; 102 | 103 | this.conn.onmessage = (evt) => { 104 | const msg = JSON.parse(evt.data); 105 | if (msg.seq_reply) { 106 | if (msg.error) { 107 | console.log(msg); //eslint-disable-line no-console 108 | } 109 | 110 | if (this.responseCallbacks[msg.seq_reply]) { 111 | this.responseCallbacks[msg.seq_reply](msg); 112 | Reflect.deleteProperty(this.responseCallbacks, msg.seq_reply); 113 | } 114 | } else if (this.eventCallback) { 115 | if (msg.seq !== this.eventSequence && this.missedEventCallback) { 116 | console.log('missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.eventSequence); //eslint-disable-line no-console 117 | this.missedEventCallback(); 118 | } 119 | this.eventSequence = msg.seq + 1; 120 | this.eventCallback(msg); 121 | } 122 | }; 123 | } 124 | 125 | setEventCallback(callback) { 126 | this.eventCallback = callback; 127 | } 128 | 129 | setFirstConnectCallback(callback) { 130 | this.firstConnectCallback = callback; 131 | } 132 | 133 | setReconnectCallback(callback) { 134 | this.reconnectCallback = callback; 135 | } 136 | 137 | setMissedEventCallback(callback) { 138 | this.missedEventCallback = callback; 139 | } 140 | 141 | setErrorCallback(callback) { 142 | this.errorCallback = callback; 143 | } 144 | 145 | setCloseCallback(callback) { 146 | this.closeCallback = callback; 147 | } 148 | 149 | close() { 150 | this.connectFailCount = 0; 151 | this.sequence = 1; 152 | if (this.conn && this.conn.readyState === WebSocket.OPEN) { 153 | this.conn.onclose = () => {}; //eslint-disable-line no-empty-function 154 | this.conn.close(); 155 | this.conn = null; 156 | console.log('websocket closed'); //eslint-disable-line no-console 157 | } 158 | } 159 | 160 | sendMessage(action, data, responseCallback) { 161 | const msg = { 162 | action, 163 | seq: this.sequence++, 164 | data 165 | }; 166 | 167 | if (responseCallback) { 168 | this.responseCallbacks[msg.seq] = responseCallback; 169 | } 170 | 171 | if (this.conn && this.conn.readyState === WebSocket.OPEN) { 172 | this.conn.send(JSON.stringify(msg)); 173 | } else if (!this.conn || this.conn.readyState === WebSocket.CLOSED) { 174 | this.conn = null; 175 | this.initialize(); 176 | } 177 | } 178 | 179 | userTyping(channelId, parentId, callback) { 180 | const data = {}; 181 | data.channel_id = channelId; 182 | data.parent_id = parentId; 183 | 184 | this.sendMessage('user_typing', data, callback); 185 | } 186 | 187 | getStatuses(callback) { 188 | this.sendMessage('get_statuses', null, callback); 189 | } 190 | 191 | getStatusesByIds(userIds, callback) { 192 | const data = {}; 193 | data.user_ids = userIds; 194 | this.sendMessage('get_statuses_by_ids', data, callback); 195 | } 196 | } 197 | --------------------------------------------------------------------------------