This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypetalk.js
718 lines (660 loc) · 34.1 KB
/
typetalk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// License: MIT
'use strict';
/**
* @fileoverview typetalk-js aims to provide a complete, asynchronous client library for the Typetalk API.
* For API details and how to use promises, see the JavaScript Promises articles.
* @author shoito
*/
if (typeof window === 'undefined') {
var Promise = Promise || require('promise');
var XMLHttpRequest = XMLHttpRequest || require('xmlhttprequest').XMLHttpRequest;
}
(function() {
var Typetalk = (function() {
Typetalk.API_BASE_URL = 'https://typetalk.in/api/v1/';
Typetalk.OAUTH_BASE_URL = 'https://typetalk.in/oauth2/';
var self,
clientId,
clientSecret,
redirectUri,
scope = 'topic.read'; // @see {@link http://developer.nulab-inc.com/docs/typetalk/auth#scope}
/**
* Typetalk API client
* @global
* @class Typetalk
* @param {Object} [options] - API parameters
* @param {String} [options.client_id] - client id
* @param {String} [options.client_secret] - client secret
* @param {String} [options.scope=topic.read] - scope
* @param {String} [options.redirect_uri] - redirect uri
* @param {String} [options.access_token] - access token
* @param {String} [options.refresh_token] - refresh token
* @param {Number} [options.timeout=3000] - timeout(ms)
* @see {@link https://developer.nulab-inc.com/docs/typetalk/auth}
*/
function Typetalk(options) {
self = this;
options = options || {};
self.accessToken = options.access_token;
self.refreshToken = options.refresh_token;
self.timeout = options.timeout || 3000;
clientId = options.client_id;
clientSecret = options.client_secret;
redirectUri = options.redirect_uri;
scope = options.scope || scope;
}
var toQueryString = function(obj) {
var queryString = [];
for(var prop in obj) {
if (obj.hasOwnProperty(prop)) {
queryString.push(encodeURIComponent(prop) + '=' + encodeURIComponent(obj[prop]));
}
}
return queryString.join('&');
};
var requestAccessToken = function(params) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
// { "error": "invalid_request", "error_description": "grant_type not found"}
reject(JSON.parse(xhr.responseText));
}
};
xhr.onerror = reject;
xhr.open('POST', Typetalk.OAUTH_BASE_URL + 'access_token');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.timeout = self.timeout;
xhr.send(params);
});
};
var requestApi = function(url, method, params, options) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else if (xhr.status === 400 || xhr.status === 401) {
// 400; Bad Request
// 400: WWW-Authenticate: Bearer error="invalid_request", error_description="Access token was not specified"
// 401: WWW-Authenticate: Bearer error="invalid_token", error_description="Invalid access token"
// 401: WWW-Authenticate: Bearer error="invalid_token", error_description="The access token expired"
// 401: WWW-Authenticate: Bearer error="invalid_scope"
var authMessage = (xhr.getResponseHeader('WWW-Authenticate') || ''),
errorMatch = (authMessage.match(/error="(\w+)"/) || []),
errorDescriptionMatch = (authMessage.match(/error_description="(\w+)"/) || []),
error = (errorMatch.length > 1) ? errorMatch[1] : xhr.statusText,
errorDescription = (errorDescriptionMatch.length > 1) ? errorDescriptionMatch[1] : '';
reject({'status': xhr.status, 'error': error, 'error_description': errorDescription});
} else {
reject({'status': xhr.status, 'error': xhr.statusText, 'error_description': 'An error has occurred while requesting api'});
}
};
xhr.onerror = reject;
xhr.open(method, url);
if (options && options['Content-Type']) {
xhr.setRequestHeader('Content-Type', options['Content-Type']);
}
xhr.setRequestHeader('Authorization', 'Bearer ' + encodeURIComponent(self.accessToken));
xhr.timeout = self.timeout;
xhr.send(params);
});
};
/**
* Starts an auth flow at the typetalk oauth2 URL.
* @memberof Typetalk
* @method
* @param {Object} [options] - oAuth2 parameters
* @param {String} [options.client_id] - client id
* @param {String} [options.scope] - scope
* @param {String} [options.redirect_uri] - redirect uri
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
*/
Typetalk.prototype.authorizeChromeApp = function(options) {
options = options || {};
return new Promise(function(resolve, reject) {
if (!(chrome && chrome.identity)) {
reject(new Error('chrome.identity API is unsupported'));
return;
}
var authorizeUrl = Typetalk.OAUTH_BASE_URL + 'authorize?client_id=' + encodeURIComponent(clientId || options.client_id) +
'&redirect_uri=' + encodeURIComponent(redirectUri || options.redirect_uri) +
'&scope=' + encodeURIComponent(scope || options.scope) + '&response_type=code';
chrome.identity.launchWebAuthFlow(
{'url': authorizeUrl, 'interactive': true},
function(responseUrl) {
if (typeof responseUrl === 'undefined') {
reject(new Error('Cannot get response url'));
return;
}
var code = responseUrl.match(/code=(.+)/)[1];
if (typeof code === 'undefined') {
reject(new Error('authorization code is required'));
return;
}
self.getAccessTokenUsingAuthorizationCode(code).then(function(data) {
resolve(data);
}, function(err) {
reject(err);
});
}
);
});
};
/**
* Check if this instance has the access token and the refresh token
* @memberof Typetalk
* @method
* @return {Boolean} - Returns true if this instance has the access token and the refresh token
*/
Typetalk.prototype.hasToken = function() {
return !!self.accessToken && !!self.refreshToken;
};
/**
* Removes your access token from this instance
* @memberof Typetalk
* @method
*/
Typetalk.prototype.clearToken = function() {
self.accessToken = null;
self.refreshToken = null;
};
/**
* Validate your access token
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
*/
Typetalk.prototype.validateAccessToken = function() {
return self.getMyProfile();
};
/**
* Get access token using authorization code
* @memberof Typetalk
* @method
* @param {Object} [options] - oAuth2 parameters
* @param {String} [options.client_id] - client id
* @param {String} [options.client_secret] - client secret
* @param {String} [options.scope] - scope
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link https://developer.nulab-inc.com/docs/typetalk/auth#client}
*/
Typetalk.prototype.getAccessTokenUsingClientCredentials = function(options) {
options = options || {};
var param = 'client_id=' + encodeURIComponent(clientId || options.client_id) +
'&client_secret=' + encodeURIComponent(clientSecret || options.client_secret) +
'&grant_type=client_credentials' +
'&scope=' + encodeURIComponent(scope || options.scope);
return requestAccessToken(param);
};
/**
* Redirect users to request Typetalk access
* @memberof Typetalk
* @method
* @param {Object} [options] - oAuth2 parameters
* @param {String} [options.client_id] - client id
* @param {String} [options.scope] - scope
* @param {String} [options.redirect_uri] - redirect uri
* @see {@link https://developer.nulab-inc.com/docs/typetalk/auth#code}
*/
Typetalk.prototype.requestAuthorization = function(options) {
options = options || {};
var param = 'client_id=' + encodeURIComponent(clientId || options.client_id) +
'&redirect_uri=' + encodeURIComponent(redirectUri || options.redirect_uri) +
'&scope=' + encodeURIComponent(scope || options.scope) +
'&response_type=code';
location.href = Typetalk.OAUTH_BASE_URL + 'authorize?' + param;
};
/**
* Get an access token using authorization code
* @memberof Typetalk
* @method
* @param {Object} [options] - oAuth2 parameters
* @param {String} [options.client_id] - client id
* @param {String} [options.client_secret] - client secret
* @param {String} [options.redirect_uri] - redirect uri
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link https://developer.nulab-inc.com/docs/typetalk/auth#code}
*/
Typetalk.prototype.getAccessTokenUsingAuthorizationCode = function(code, options) {
options = options || {};
var param = 'client_id=' + encodeURIComponent(clientId || options.client_id) +
'&client_secret=' + encodeURIComponent(clientSecret || options.client_secret) +
'&redirect_uri=' + encodeURIComponent(redirectUri || options.redirect_uri) +
'&grant_type=authorization_code' +
'&code=' + encodeURIComponent(code);
return requestAccessToken(param);
};
/**
* Get an access token using authorization code
* @memberof Typetalk
* @method
* @param {Object} [options] - oAuth2 parameters and refresh token
* @param {String} [options.client_id] - client id
* @param {String} [options.client_secret] - client secret
* @param {String} [options.refresh_token] - refresh token
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link https://developer.nulab-inc.com/docs/typetalk/auth#refresh}
*/
Typetalk.prototype.refreshAccessToken = function(options) {
options = options || {};
var param = 'client_id=' + encodeURIComponent(clientId || options.client_id) +
'&client_secret=' + encodeURIComponent(clientSecret || options.client_secret) +
'&grant_type=refresh_token' +
'&refresh_token=' + encodeURIComponent(self.refreshToken || options.refresh_token);
return requestAccessToken(param);
};
/**
* Get my profile
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-profile}
*/
Typetalk.prototype.getMyProfile = function() {
return requestApi(Typetalk.API_BASE_URL + 'profile', 'GET', null);
};
/**
* Get my topics
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-topics}
*/
Typetalk.prototype.getMyTopics = function() {
return requestApi(Typetalk.API_BASE_URL + 'topics', 'GET', null);
};
/**
* Get topic messages
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Object} [options] - Query parameters
* @param {Number} [options.count] - default value: 20, maximum: 100
* @param {Number} [options.from] - references Post ID
* @param {String} [options.direction] - "backward" or "forward"
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-messages}
*/
Typetalk.prototype.getTopicMessages = function(topicId, options) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '?' + toQueryString(options), 'GET', null);
};
/**
* Post message
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {String} message - your message, maximum length: 4096
* @param {Object} [options] - Form parameters
* @param {Number} [options.replyTo] - references Post ID
* @param {String} [options.fileKeys[0-5]] - attachment file key, maximum count: 5
* @param {Number} [options.talkIds[0-5]] - Talk IDs that you want to put the message in, maximum count: 5
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/post-message}
*/
Typetalk.prototype.postMessage = function(topicId, message, options) {
options = options || {};
options.message = message;
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId), 'POST', toQueryString(options), {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Upload attachment file
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Binaly} file - max file size: 10MB
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/upload-attachment}
*/
Typetalk.prototype.uploadAttachmentFile = function(topicId, file) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId), 'POST', {'file': file}, {'Content-Type': 'multipart/form-data'});
};
/**
* Get topic members
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-topic-members}
*/
Typetalk.prototype.getTopicMembers = function(topicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/members/status', 'GET', null);
};
/**
* Get message
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} postId - Post ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-message}
*/
Typetalk.prototype.getMessage = function(topicId, postId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/posts/' + encodeURIComponent(postId), 'GET', null);
};
/**
* Remove message
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} postId - Post ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/remove-message}
*/
Typetalk.prototype.removeMessage = function(topicId, postId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/posts/' + encodeURIComponent(postId), 'DELETE', null);
};
/**
* Like message
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} postId - Post ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/like-message}
*/
Typetalk.prototype.likeMessage = function(topicId, postId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/posts/' + encodeURIComponent(postId) + '/like', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Unlike message
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} postId - Post ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/unlike-message}
*/
Typetalk.prototype.unlikeMessage = function(topicId, postId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/posts/' + encodeURIComponent(postId) + '/like', 'DELETE', null);
};
/**
* Favorite topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/favorite-topics}
*/
Typetalk.prototype.favoriteTopic = function(topicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/favorite', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Unfavorite topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/unfavorite-topics}
*/
Typetalk.prototype.unfavoriteTopic = function(topicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/favorite', 'DELETE', null);
};
/**
* Get notification list
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-notifications}
*/
Typetalk.prototype.getNotificationList = function() {
return requestApi(Typetalk.API_BASE_URL + 'notifications', 'GET', null);
};
/**
* Get notification count
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-notification-status}
*/
Typetalk.prototype.getNotificationCount = function() {
return requestApi(Typetalk.API_BASE_URL + 'notifications/status', 'GET', null);
};
/**
* Read notification
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/open-notification}
*/
Typetalk.prototype.readNotification = function() {
return requestApi(Typetalk.API_BASE_URL + 'notifications/open', 'PUT', null);
};
/**
* Read messages in topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Object} [options] - Form parameters
* @param {Number} [options.postId] - Post ID ( if no parameter, read all posts )
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/save-read-topic}
*/
Typetalk.prototype.readMessagesInTopic = function(topicId, options) {
options = options || {};
options.topicId = topicId;
return requestApi(Typetalk.API_BASE_URL + 'bookmarks', 'PUT', toQueryString(options), {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Get mention list
* @memberof Typetalk
* @method
* @param {Object} [options] - Form parameters
* @param {Number} [options.from] - Mention ID
* @param {Boolean} [options.unread] - true: only unread mentions, false: all mentions
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-mentions}
*/
Typetalk.prototype.getMentionList = function(options) {
options = options || {};
return requestApi(Typetalk.API_BASE_URL + 'mentions?' + toQueryString(options), 'GET', null);
};
/**
* Read mention
* @memberof Typetalk
* @method
* @param {Number} mentionId - Mention ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/save-read-mention}
*/
Typetalk.prototype.readMention = function(mentionId) {
return requestApi(Typetalk.API_BASE_URL + 'mentions/' + encodeURIComponent(mentionId), 'PUT', null);
};
/**
* Accept team invitation
* @memberof Typetalk
* @method
* @param {Number} teamId - Team ID
* @param {Number} inviteTeamId - Team invitation ID (invites.teams[x].id in Get notification list)
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/accept-team-invite}
*/
Typetalk.prototype.acceptTeamInvitation = function(teamId, inviteTeamId) {
return requestApi(Typetalk.API_BASE_URL + 'teams/' + encodeURIComponent(teamId) + '/members/invite/' + encodeURIComponent(inviteTeamId) + '/accept', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Decline team invitation
* @memberof Typetalk
* @method
* @param {Number} teamId - Team ID
* @param {Number} inviteTeamId - Team invitation ID (invites.teams[x].id in Get notification list)
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/decline-team-invite}
*/
Typetalk.prototype.declineTeamInvitation = function(teamId, inviteTeamId) {
return requestApi(Typetalk.API_BASE_URL + 'teams/' + encodeURIComponent(teamId) + '/members/invite/' + encodeURIComponent(inviteTeamId) + '/decline', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Accept topic invitation
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} inviteTeamId - Topic invitation ID (invites.topics[x].id in Get notification list)
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/accept-team-invite}
*/
Typetalk.prototype.acceptTopicInvitation = function(topicId, inviteTopicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/members/invite/' + encodeURIComponent(inviteTopicId) + '/accept', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Decline topic invitation
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} inviteTeamId - Topic invitation ID (invites.topics[x].id in Get notification list)
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/decline-team-invite}
*/
Typetalk.prototype.declineTopicInvitation = function(topicId, inviteTopicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/members/invite/' + encodeURIComponent(inviteTopicId) + '/decline', 'POST', {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Create topic
* @memberof Typetalk
* @method
* @param {String} name - Topic Name
* @param {Object} [options] - Form parameters
* @param {Number} [options.teamId] - Team ID
* @param {String} [options.inviteMembers[0..N]] - account.name or e-mail address
* @param {String} [options.inviteMessage - Invite message
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/create-topic}
*/
Typetalk.prototype.createTopic = function(name, options) {
options = options || {};
options.name = name;
return requestApi(Typetalk.API_BASE_URL + 'topics', 'POST', toQueryString(options), {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Update topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Object} [options] - Form parameters
* @param {String} [options.name] - Topic Name (not to use in team if team ID is empty string)
* @param {Number} [options.teamId - Team ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/update-topic}
*/
Typetalk.prototype.updateTopic = function(topicId, options) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId), 'PUT', toQueryString(options), {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Delete topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/update-topic}
*/
Typetalk.prototype.deleteTopic = function(topicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId), 'DELETE', null);
};
/**
* Get topic details
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-topic-details}
*/
Typetalk.prototype.getTopicDetails = function(topicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/details', 'GET', null);
};
/**
* Invite members to topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Object} [options] - Form parameters
* @param {String} [options.inviteMembers[0..N]] - account.name or e-mail address
* @param {String} [options.inviteMessage - Invite message
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/invite-topic-member}
*/
Typetalk.prototype.inviteTopicMember = function(topicId, options) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/members/invite', 'POST', toQueryString(options), {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Remove members and invites from topic
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Object} [options] - Form parameters
* @param {Number} [options.removeInviteIds[0..N]] - Invite ID (invites[x].id in get-topic-details)
* @param {Number} [options.removeMemberIds[0..N]] - Account ID (accounts[x].id in get-topic-details)
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/remove-topic-member}
*/
Typetalk.prototype.removeTopicMember = function(topicId, options) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/members/remove', 'POST', toQueryString(options), {'Content-Type': 'application/x-www-form-urlencoded'});
};
/**
* Get my teams
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-teams}
*/
Typetalk.prototype.getTeams = function() {
return requestApi(Typetalk.API_BASE_URL + 'teams', 'GET', null);
};
/**
* Get my friends
* @memberof Typetalk
* @method
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-friends}
*/
Typetalk.prototype.getFriends = function() {
return requestApi(Typetalk.API_BASE_URL + 'search/friends', 'GET', null);
};
/**
* Search accounts
* @memberof Typetalk
* @method
* @param {String} nameOrEmailAddress - account.name or e-mail address
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/search-accounts}
*/
Typetalk.prototype.searchAccounts = function(nameOrEmailAddress) {
return requestApi(Typetalk.API_BASE_URL + 'search/accounts?nameOrEmailAddress=' + encodeURIComponent(nameOrEmailAddress), 'GET', null);
};
/**
* Get talk list
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-talks}
*/
Typetalk.prototype.getTalks = function(topicId) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/talks', 'GET', null);
};
/**
* Get messages in talk
* @memberof Typetalk
* @method
* @param {Number} topicId - Topic ID
* @param {Number} talkId - Talk ID
* @param {Object} [options] - Form parameters
* @param {Number} [options.count] - default value: 20, maximum: 100
* @param {Number} [options.from] - references Post ID
* @param {String} [options.direction] - "backward" or "forward"
* @return {Promise} promise object - It will resolve with `response` data or fail with `error` object
* @see {@link http://developer.nulab-inc.com/docs/typetalk/api/1/get-talk}
*/
Typetalk.prototype.getTalk = function(topicId, talkId, options) {
return requestApi(Typetalk.API_BASE_URL + 'topics/' + encodeURIComponent(topicId) + '/talks/' + encodeURIComponent(talkId) + '/posts', 'GET', toQueryString(options));
};
return Typetalk;
})();
if (typeof module !== 'undefined') {
module.exports = Typetalk;
} else {
// <script src="https://www.promisejs.org/polyfills/promise-5.0.0.min.js"></script>
this.Typetalk = Typetalk;
}
}).call(this);