mezon-js 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client.ts ADDED
@@ -0,0 +1,1848 @@
1
+ /**
2
+ * Copyright 2020 The Mezon Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import {
18
+ ApiAccount,
19
+ ApiAccountCustom,
20
+ ApiAccountDevice,
21
+ ApiAccountEmail,
22
+ ApiAccountFacebook,
23
+ ApiAccountFacebookInstantGame,
24
+ ApiAccountGoogle,
25
+ ApiAccountGameCenter,
26
+ ApiAccountSteam,
27
+ ApiChannelMessageList,
28
+ ApiChannelDescList,
29
+ ApiChannelDescription,
30
+ ApiCreateChannelDescRequest,
31
+ ApiDeleteRoleRequest,
32
+ ApiClanDescList,
33
+ ApiCreateClanDescRequest,
34
+ ApiClanDesc,
35
+ ApiCategoryDesc,
36
+ ApiCategoryDescList,
37
+ ApiRoleList,
38
+ ApiPermissionList,
39
+ ApiRoleUserList,
40
+ ApiRole,
41
+ ApiCreateRoleRequest,
42
+ ApiAddRoleChannelDescRequest,
43
+ ApiCreateCategoryDescRequest,
44
+ ApiUpdateCategoryDescRequest,
45
+ ApiDeleteStorageObjectsRequest,
46
+ ApiEvent,
47
+ ApiFriendList,
48
+ ApiNotificationList,
49
+ ApiReadStorageObjectsRequest,
50
+ ApiRpc,
51
+ ApiStorageObjectAcks,
52
+ ApiStorageObjectList,
53
+ ApiStorageObjects,
54
+ ApiUpdateAccountRequest,
55
+ ApiUsers,
56
+ ApiWriteStorageObjectsRequest,
57
+ MezonApi,
58
+ ApiSession,
59
+ ApiAccountApple,
60
+ ApiLinkSteamRequest,
61
+ ApiClanDescProfile,
62
+ ApiClanProfile,
63
+ ApiChannelUserList,
64
+ ApiClanUserList,
65
+ ApiLinkInviteUserRequest,
66
+ ApiLinkInviteUser,
67
+ ApiInviteUserRes,
68
+ ApiUploadAttachmentRequest,
69
+ ApiUploadAttachment,
70
+ ApiMessageReaction,
71
+ ApiMessageMention,
72
+ ApiMessageAttachment,
73
+ ApiMessageRef,
74
+ ApiChannelMessageHeader,
75
+ ApiVoiceChannelUserList,
76
+ } from "./api.gen";
77
+
78
+ import { Session } from "./session";
79
+ import { DefaultSocket, Socket } from "./socket";
80
+ import { WebSocketAdapter, WebSocketAdapterText } from "./web_socket_adapter";
81
+
82
+ const DEFAULT_HOST = "127.0.0.1";
83
+ const DEFAULT_PORT = "7350";
84
+ const DEFAULT_SERVER_KEY = "defaultkey";
85
+ const DEFAULT_TIMEOUT_MS = 7000;
86
+ const DEFAULT_EXPIRED_TIMESPAN_MS = 5 * 60 * 1000;
87
+
88
+ export enum ChannelType {
89
+ CHANNEL_TYPE_TEXT = 1,
90
+ CHANNEL_TYPE_GROUP = 2,
91
+ CHANNEL_TYPE_DM = 3,
92
+ CHANNEL_TYPE_VOICE = 4,
93
+ CHANNEL_TYPE_FORUM = 5,
94
+ CHANNEL_TYPE_ANNOUNCEMENT = 6,
95
+ }
96
+ export enum ChannelStreamMode {
97
+ STREAM_MODE_CHANNEL = 2,
98
+ STREAM_MODE_GROUP = 3,
99
+ STREAM_MODE_DM = 4,
100
+ }
101
+
102
+ /** Response for an RPC function executed on the server. */
103
+ export interface RpcResponse {
104
+ /** The identifier of the function. */
105
+ id?: string;
106
+ /** The payload of the function which must be a JSON object. */
107
+ payload?: object;
108
+ }
109
+
110
+ /** The object to store. */
111
+ export interface WriteStorageObject {
112
+ /** The collection to store the object. */
113
+ collection?: string;
114
+ /** The key for the object within the collection. */
115
+ key?: string;
116
+ /** The read access permissions for the object. */
117
+ permission_read?: number;
118
+ /** The write access permissions for the object. */
119
+ permission_write?: number;
120
+ /** The value of the object. */
121
+ value?: object;
122
+ /** The version hash of the object to check. Possible values are: ["", "*", "#hash#"]. */
123
+ version?: string;
124
+ }
125
+
126
+ /** An object within the storage engine. */
127
+ export interface StorageObject {
128
+ /** The collection which stores the object. */
129
+ collection?: string;
130
+ /** The UNIX time when the object was created. */
131
+ create_time?: string;
132
+ /** The key of the object within the collection. */
133
+ key?: string;
134
+ /** The read access permissions for the object. */
135
+ permission_read?: number;
136
+ /** The write access permissions for the object. */
137
+ permission_write?: number;
138
+ /** The UNIX time when the object was last updated. */
139
+ update_time?: string;
140
+ /** The user owner of the object. */
141
+ user_id?: string;
142
+ /** The value of the object. */
143
+ value?: object;
144
+ /** The version hash of the object. */
145
+ version?: string;
146
+ }
147
+
148
+ /** List of storage objects. */
149
+ export interface StorageObjectList {
150
+ /** The cursor associated with the query a page of results. */
151
+ cursor?: string;
152
+ /** The list of storage objects. */
153
+ objects: Array<StorageObject>;
154
+ }
155
+
156
+ /** Batch of storage objects. */
157
+ export interface StorageObjects {
158
+ /** The batch of storage objects. */
159
+ objects: Array<StorageObject>;
160
+ }
161
+
162
+ /** A message sent on a channel. */
163
+ export interface ChannelMessage {
164
+ //
165
+ avatar?: string;
166
+ //The channel this message belongs to.
167
+ channel_id: string;
168
+ //The name of the chat room, or an empty string if this message was not sent through a chat room.
169
+ channel_label: string;
170
+ //The clan this message belong to.
171
+ clan_id?: string;
172
+ //The code representing a message type or category.
173
+ code: number;
174
+ //The content payload.
175
+ content: string;
176
+ //The UNIX time (for gRPC clients) or ISO string (for REST clients) when the message was created.
177
+ create_time: string;
178
+ //
179
+ reactions?: Array<ApiMessageReaction>;
180
+ //
181
+ mentions?: Array<ApiMessageMention>;
182
+ //
183
+ attachments?: Array<ApiMessageAttachment>;
184
+ //
185
+ references?: Array<ApiMessageRef>;
186
+ //
187
+ referenced_message?: ChannelMessage;
188
+
189
+ //The unique ID of this message.
190
+ id: string;
191
+ //True if the message was persisted to the channel's history, false otherwise.
192
+ persistent?: boolean;
193
+ //Message sender, usually a user ID.
194
+ sender_id: string;
195
+ //The UNIX time (for gRPC clients) or ISO string (for REST clients) when the message was last updated.
196
+ update_time?: string;
197
+ //The ID of the first DM user, or an empty string if this message was not sent through a DM chat.
198
+ user_id_one?: string;
199
+ //The ID of the second DM user, or an empty string if this message was not sent through a DM chat.
200
+ user_id_two?: string;
201
+ //The username of the message sender, if any.
202
+ username?: string;
203
+ }
204
+
205
+ /** A list of channel messages, usually a result of a list operation. */
206
+ export interface ChannelMessageList {
207
+ /** Cacheable cursor to list newer messages. Durable and designed to be stored, unlike next/prev cursors. */
208
+ cacheable_cursor?: string;
209
+ /**last seen message from user on channel */
210
+ last_seen_message?: ApiChannelMessageHeader;
211
+ /** A list of messages. */
212
+ messages?: Array<ChannelMessage>;
213
+ /** The cursor to send when retireving the next page, if any. */
214
+ next_cursor?: string;
215
+ /** The cursor to send when retrieving the previous page, if any. */
216
+ prev_cursor?: string;
217
+ }
218
+
219
+ /** A user in the system. */
220
+ export interface User {
221
+ /** A URL for an avatar image. */
222
+ avatar_url?: string;
223
+ /** The UNIX time when the user was created. */
224
+ create_time?: string;
225
+ /** The display name of the user. */
226
+ display_name?: string;
227
+ /** Number of related edges to this user. */
228
+ edge_count?: number;
229
+ /** The Facebook id in the user's account. */
230
+ facebook_id?: string;
231
+ /** The Facebook Instant Game ID in the user's account. */
232
+ facebook_instant_game_id?: string;
233
+ /** The Apple Game Center in of the user's account. */
234
+ gamecenter_id?: string;
235
+ /** The Google id in the user's account. */
236
+ google_id?: string;
237
+ /** The id of the user's account. */
238
+ id?: string;
239
+ /** The language expected to be a tag which follows the BCP-47 spec. */
240
+ lang_tag?: string;
241
+ /** The location set by the user. */
242
+ location?: string;
243
+ /** Additional information stored as a JSON object. */
244
+ metadata?: {};
245
+ /** Indicates whether the user is currently online. */
246
+ online?: boolean;
247
+ /** The Steam id in the user's account. */
248
+ steam_id?: string;
249
+ /** The timezone set by the user. */
250
+ timezone?: string;
251
+ /** The UNIX time when the user was last updated. */
252
+ update_time?: string;
253
+ /** The username of the user's account. */
254
+ username?: string;
255
+ }
256
+
257
+ /** A collection of zero or more users. */
258
+ export interface Users {
259
+ /** The User objects. */
260
+ users?: Array<User>;
261
+ }
262
+
263
+ /** A friend of a user. */
264
+ export interface Friend {
265
+ /** The friend status. */
266
+ state?: number;
267
+ /** The user object. */
268
+ user?: User;
269
+ }
270
+
271
+ /** A collection of zero or more friends of the user. */
272
+ export interface Friends {
273
+ /** The Friend objects. */
274
+ friends?: Array<Friend>;
275
+ /** Cursor for the next page of results, if any. */
276
+ cursor?: string;
277
+ }
278
+
279
+ /** A user-role pair representing the user's role in a group. */
280
+ export interface GroupUser {
281
+ /** The user. */
282
+ user?: User;
283
+ /** Their role within the group. */
284
+ state?: number;
285
+ }
286
+
287
+ /** A list of users belonging to a group along with their role in it. */
288
+ export interface GroupUserList {
289
+ /** The user-role pairs. */
290
+ group_users?: Array<GroupUser>;
291
+ /** Cursor for the next page of results, if any. */
292
+ cursor?: string;
293
+ }
294
+
295
+ /** A group in the server. */
296
+ export interface Group {
297
+ /** A URL for an avatar image. */
298
+ avatar_url?: string;
299
+ /** The UNIX time when the group was created. */
300
+ create_time?: string;
301
+ /** The id of the user who created the group. */
302
+ creator_id?: string;
303
+ /** A description for the group. */
304
+ description?: string;
305
+ /** The current count of all members in the group. */
306
+ edge_count?: number;
307
+ /** The id of a group. */
308
+ id?: string;
309
+ /** The language expected to be a tag which follows the BCP-47 spec. */
310
+ lang_tag?: string;
311
+ /** The maximum number of members allowed. */
312
+ max_count?: number;
313
+ /** Additional information stored as a JSON object. */
314
+ metadata?: {};
315
+ /** The unique name of the group. */
316
+ name?: string;
317
+ /** Anyone can join open groups, otherwise only admins can accept members. */
318
+ open?: boolean;
319
+ /** The UNIX time when the group was last updated. */
320
+ update_time?: string;
321
+ }
322
+
323
+ /** One or more groups returned from a listing operation. */
324
+ export interface GroupList {
325
+ /** A cursor used to get the next page. */
326
+ cursor?: string;
327
+ /** One or more groups. */
328
+ groups?: Array<Group>;
329
+ }
330
+
331
+ /** A group-role pair representing the user's groups and their role in each. */
332
+ export interface UserGroup {
333
+ /** The group. */
334
+ group?: Group;
335
+ /** The user's role within the group. */
336
+ state?: number;
337
+ }
338
+
339
+ /** A list of groups belonging to a user along with their role in it. */
340
+ export interface UserGroupList {
341
+ /** The group-role pairs. */
342
+ user_groups?: Array<UserGroup>;
343
+ /** Cursor for the next page of results, if any. */
344
+ cursor?: string;
345
+ }
346
+
347
+ /** A notification in the server. */
348
+ export interface Notification {
349
+ /** Category code for this notification. */
350
+ code?: number;
351
+ /** Content of the notification in JSON. */
352
+ content?: {};
353
+ /** The UNIX time when the notification was created. */
354
+ create_time?: string;
355
+ /** ID of the Notification. */
356
+ id?: string;
357
+ /** True if this notification was persisted to the database. */
358
+ persistent?: boolean;
359
+ /** ID of the sender, if a user. Otherwise 'null'. */
360
+ sender_id?: string;
361
+ /** Subject of the notification. */
362
+ subject?: string;
363
+ }
364
+
365
+ /** A collection of zero or more notifications. */
366
+ export interface NotificationList {
367
+ /** Use this cursor to paginate notifications. Cache this to catch up to new notifications. */
368
+ cacheable_cursor?: string;
369
+ /** Collection of notifications. */
370
+ notifications?: Array<Notification>;
371
+ }
372
+
373
+ /** Update fields in a given channel. */
374
+ export interface ApiUpdateChannelDescRequest {
375
+ /** The ID of the channel to update. */
376
+ channel_id: string;
377
+ /** The channel lable */
378
+ channel_lable:
379
+ | string
380
+ | undefined;
381
+ /** The category of channel */
382
+ category_id: string | undefined;
383
+ }
384
+
385
+ /** Add users to a channel. */
386
+ export interface ApiAddChannelUsersRequest {
387
+ /** The channel to add users to. */
388
+ channel_id: string;
389
+ /** The users to add. */
390
+ user_ids: string[];
391
+ }
392
+
393
+ /** Kick a set of users from a channel. */
394
+ export interface ApiKickChannelUsersRequest {
395
+ /** The channel ID to kick from. */
396
+ channel_id: string;
397
+ /** The users to kick. */
398
+ user_ids: string[];
399
+ }
400
+
401
+ /** Leave a channel. */
402
+ export interface ApiLeaveChannelRequest {
403
+ /** The channel ID to leave. */
404
+ channel_id: string;
405
+ }
406
+
407
+ /** Update Clan information */
408
+ export interface ApiUpdateClanDescRequest {
409
+ clan_id: string;
410
+ /** Clan creator */
411
+ creator_id: string;
412
+ /** Clan name */
413
+ clan_name: string;
414
+ /** Clan logo */
415
+ logo: string;
416
+ /** Clan banner */
417
+ banner: string;
418
+ }
419
+
420
+ /** Update Clan profile information */
421
+ export interface ApiUpdateClanDescProfileRequest {
422
+ /** Clan id */
423
+ clan_id: string;
424
+ /** Clan nick name */
425
+ nick_name: string;
426
+ /** Clan profile banner */
427
+ profile_banner: string;
428
+ /** Clan profile theme */
429
+ profile_theme: string;
430
+ /** Clan profile avatar */
431
+ avatar_url: string;
432
+ }
433
+
434
+ export interface ApiUpdateClanProfileRequest {
435
+ /** Clan id*/
436
+ clan_id: string;
437
+ /** Clan nick name */
438
+ nick_name: string;
439
+ /** Clan profile avatar */
440
+ avatar: string;
441
+ }
442
+
443
+ /** Update fields in a given role. */
444
+ export interface ApiUpdateRoleRequest {
445
+ /** The ID of the role to update. */
446
+ role_id: string;
447
+ title: string | undefined;
448
+ color: string | undefined;
449
+ role_icon: string | undefined;
450
+ description: string | undefined;
451
+ display_online: number | undefined;
452
+ allow_mention:
453
+ | number
454
+ | undefined;
455
+ /** The users to add. */
456
+ add_user_ids: string[];
457
+ /** The permissions to add. */
458
+ active_permission_ids: string[];
459
+ /** The users to remove. */
460
+ remove_user_ids: string[];
461
+ /** The permissions to remove. */
462
+ remove_permission_ids: string[];
463
+ }
464
+
465
+ /** A client for Mezon server. */
466
+ export class Client {
467
+
468
+ /** The expired timespan used to check session lifetime. */
469
+ public expiredTimespanMs = DEFAULT_EXPIRED_TIMESPAN_MS;
470
+
471
+ /** The low level API client for Mezon server. */
472
+ private readonly apiClient: MezonApi;
473
+
474
+ constructor(
475
+ readonly serverkey = DEFAULT_SERVER_KEY,
476
+ readonly host = DEFAULT_HOST,
477
+ readonly port = DEFAULT_PORT,
478
+ readonly useSSL = false,
479
+ readonly timeout = DEFAULT_TIMEOUT_MS,
480
+ readonly autoRefreshSession = true) {
481
+ const scheme = (useSSL) ? "https://" : "http://";
482
+ const basePath = `${scheme}${host}:${port}`;
483
+
484
+ this.apiClient = new MezonApi(serverkey, basePath, timeout);
485
+ }
486
+
487
+ /** Add users to a channel, or accept their join requests. */
488
+ async addChannelUsers(session: Session, channelId: string, ids?: Array<string>): Promise<boolean> {
489
+
490
+ if (this.autoRefreshSession && session.refresh_token &&
491
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
492
+ await this.sessionRefresh(session);
493
+ }
494
+
495
+ return this.apiClient.addChannelUsers(session.token, channelId, ids).then((response: any) => {
496
+ return response !== undefined;
497
+ });
498
+ }
499
+
500
+ /** Add friends by ID or username to a user's account. */
501
+ async addFriends(session: Session, ids?: Array<string>, usernames?: Array<string>): Promise<boolean> {
502
+
503
+ if (this.autoRefreshSession && session.refresh_token &&
504
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
505
+ await this.sessionRefresh(session);
506
+ }
507
+
508
+ return this.apiClient.addFriends(session.token, ids, usernames).then((response: any) => {
509
+ return response !== undefined;
510
+ });
511
+ }
512
+
513
+ /** Authenticate a user with an Apple ID against the server. */
514
+ async authenticateApple(token: string, create?: boolean, username?: string, vars: Record<string, string> = {}, options: any = {}) {
515
+
516
+ const request = {
517
+ "token": token,
518
+ "vars": vars
519
+ };
520
+
521
+ return this.apiClient.authenticateApple(this.serverkey, "", request, create, username, options).then((apiSession : ApiSession) => {
522
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
523
+ });
524
+ }
525
+
526
+ /** Authenticate a user with a custom id against the server. */
527
+ authenticateCustom(id: string, create?: boolean, username?: string, vars: Record<string, string> = {}, options: any = {}): Promise<Session> {
528
+ const request = {
529
+ "id": id,
530
+ "vars": vars
531
+ };
532
+ return this.apiClient.authenticateCustom(this.serverkey, "", request, create, username, options).then((apiSession : ApiSession) => {
533
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
534
+ });
535
+ }
536
+
537
+ /** Authenticate a user with a device id against the server. */
538
+ authenticateDevice(id : string, create?: boolean, username?: string, vars? : Record<string, string>): Promise<Session> {
539
+ const request = {
540
+ "id": id,
541
+ "vars": vars
542
+ };
543
+
544
+ return this.apiClient.authenticateDevice(this.serverkey, "", request, create, username).then((apiSession : ApiSession) => {
545
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
546
+ });
547
+ }
548
+
549
+ /** Authenticate a user with an email+password against the server. */
550
+ authenticateEmail(email: string, password: string, create?: boolean, username?: string, vars?: Record<string,string>): Promise<Session> {
551
+ const request = {
552
+ "email": email,
553
+ "password": password,
554
+ "vars": vars
555
+ };
556
+
557
+ return this.apiClient.authenticateEmail(this.serverkey, "", request, create, username).then((apiSession : ApiSession) => {
558
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
559
+ });
560
+ }
561
+
562
+ /** Authenticate a user with a Facebook Instant Game token against the server. */
563
+ authenticateFacebookInstantGame(signedPlayerInfo: string, create?: boolean, username?: string, vars?: Record<string, string>, options: any = {}): Promise<Session> {
564
+ const request = {
565
+ "signed_player_info": signedPlayerInfo,
566
+ "vars": vars
567
+ };
568
+
569
+ return this.apiClient.authenticateFacebookInstantGame(this.serverkey, "",
570
+ {signed_player_info: request.signed_player_info, vars: request.vars}, create, username, options).then((apiSession : ApiSession) => {
571
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
572
+ });
573
+ }
574
+
575
+ /** Authenticate a user with a Facebook OAuth token against the server. */
576
+ authenticateFacebook(token : string, create?: boolean, username?: string, sync?: boolean, vars? : Record<string, string>, options: any = {}): Promise<Session> {
577
+ const request = {
578
+ "token": token,
579
+ "vars": vars
580
+ };
581
+
582
+ return this.apiClient.authenticateFacebook(this.serverkey, "", request, create, username, sync, options).then((apiSession : ApiSession) => {
583
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
584
+ });
585
+ }
586
+
587
+ /** Authenticate a user with Google against the server. */
588
+ async authenticateGoogle(
589
+ token: string,
590
+ create?: boolean,
591
+ username?: string,
592
+ vars?: Record<string, string>,
593
+ options: any = {}
594
+ ): Promise<Session> {
595
+ const request: ApiAccountGoogle = {
596
+ token,
597
+ vars,
598
+ };
599
+
600
+ const apiSession = await this.apiClient.authenticateGoogle(
601
+ this.serverkey,
602
+ "",
603
+ request,
604
+ create,
605
+ username,
606
+ options
607
+ );
608
+
609
+ return new Session(
610
+ apiSession.token || "",
611
+ apiSession.refresh_token || "",
612
+ apiSession.created || false
613
+ );
614
+ }
615
+
616
+ /** Authenticate a user with GameCenter against the server. */
617
+ async authenticateGameCenter(
618
+ bundleId: string,
619
+ playerId: string,
620
+ publicKeyUrl: string,
621
+ salt: string,
622
+ signature: string,
623
+ timestamp: string,
624
+ username?: string,
625
+ create?: boolean,
626
+ vars?: Record<string, string>,
627
+ options: any = {},
628
+ ): Promise<Session> {
629
+ const request: ApiAccountGameCenter = {
630
+ bundle_id: bundleId,
631
+ player_id: playerId,
632
+ public_key_url: publicKeyUrl,
633
+ salt,
634
+ signature,
635
+ timestamp_seconds: timestamp,
636
+ vars,
637
+ };
638
+
639
+ const apiSession = await this.apiClient.authenticateGameCenter(
640
+ this.serverkey,
641
+ "",
642
+ request,
643
+ create,
644
+ username,
645
+ options
646
+ );
647
+
648
+ return new Session(
649
+ apiSession.token || "",
650
+ apiSession.refresh_token || "",
651
+ apiSession.created || false
652
+ );
653
+ }
654
+
655
+ /** Authenticate a user with Steam against the server. */
656
+ async authenticateSteam(token : string, create?: boolean, username?: string, sync?: boolean, vars? : Record<string, string>) : Promise<Session> {
657
+ const request = {
658
+ "token": token,
659
+ "vars": vars,
660
+ "sync": sync
661
+ };
662
+
663
+ return this.apiClient.authenticateSteam(this.serverkey, "", request, create, username).then((apiSession : ApiSession) => {
664
+ return new Session(apiSession.token || "", apiSession.refresh_token || "", apiSession.created || false);
665
+ });
666
+ }
667
+
668
+ /** Block one or more users by ID or username. */
669
+ async blockFriends(session: Session, ids?: Array<string>, usernames?: Array<string>): Promise<boolean> {
670
+ if (this.autoRefreshSession && session.refresh_token &&
671
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
672
+ await this.sessionRefresh(session);
673
+ }
674
+
675
+ return this.apiClient.blockFriends(session.token, ids, usernames).then((response: any) => {
676
+ return Promise.resolve(response != undefined);
677
+ });
678
+ }
679
+
680
+ /** Create a new group with the current user as the creator and superadmin. */
681
+ async uploadAttachmentFile(session: Session, request: ApiUploadAttachmentRequest): Promise<ApiUploadAttachment> {
682
+ if (this.autoRefreshSession && session.refresh_token &&
683
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
684
+ await this.sessionRefresh(session);
685
+ }
686
+
687
+ return this.apiClient.uploadAttachmentFile(session.token, request);
688
+ }
689
+
690
+ /** Create a channel within clan */
691
+ async createChannelDesc(session: Session, request: ApiCreateChannelDescRequest): Promise<ApiChannelDescription> {
692
+ if (this.autoRefreshSession && session.refresh_token &&
693
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
694
+ await this.sessionRefresh(session);
695
+ }
696
+
697
+ return this.apiClient.createChannelDesc(session.token, request).then((response: ApiChannelDescription) => {
698
+ return Promise.resolve(response);
699
+ });
700
+ }
701
+
702
+ /** Create a clan */
703
+ async createClanDesc(session: Session, request: ApiCreateClanDescRequest): Promise<ApiClanDesc> {
704
+ if (this.autoRefreshSession && session.refresh_token &&
705
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
706
+ await this.sessionRefresh(session);
707
+ }
708
+
709
+ return this.apiClient.createClanDesc(session.token, request).then((response: ApiClanDesc) => {
710
+ return Promise.resolve(response);
711
+ });
712
+ }
713
+
714
+ /** */
715
+ async createCategoryDesc(session: Session, request: ApiCreateCategoryDescRequest): Promise<ApiCategoryDesc> {
716
+ if (this.autoRefreshSession && session.refresh_token &&
717
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
718
+ await this.sessionRefresh(session);
719
+ }
720
+
721
+ return this.apiClient.createCategoryDesc(session.token, request).then((response: ApiCategoryDesc) => {
722
+ return Promise.resolve(response);
723
+ });
724
+ }
725
+
726
+ /** Create a new role for clan. */
727
+ async createRole(session: Session, request: ApiCreateRoleRequest): Promise<ApiRole> {
728
+ if (this.autoRefreshSession && session.refresh_token &&
729
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
730
+ await this.sessionRefresh(session);
731
+ }
732
+
733
+ return this.apiClient.createRole(session.token, request).then((response: ApiRole) => {
734
+ return Promise.resolve(response);
735
+ });
736
+ }
737
+
738
+ /** add role for channel. */
739
+ async addRolesChannelDesc(session: Session, request: ApiAddRoleChannelDescRequest): Promise<boolean> {
740
+ if (this.autoRefreshSession && session.refresh_token &&
741
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
742
+ await this.sessionRefresh(session);
743
+ }
744
+
745
+ return this.apiClient.addRolesChannelDesc(session.token, request).then((response: ApiRole) => {
746
+ return response !== undefined;
747
+ });
748
+ }
749
+
750
+ /** Update action role when delete role */
751
+ async deleteRoleChannelDesc(session: Session, request:ApiDeleteRoleRequest): Promise<boolean> {
752
+ if (this.autoRefreshSession && session.refresh_token &&
753
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
754
+ await this.sessionRefresh(session);
755
+ }
756
+
757
+ return this.apiClient.deleteRoleChannelDesc(session.token, request).then((response: any) => {
758
+ return response !== undefined;
759
+ });
760
+ }
761
+
762
+ /** A socket created with the client's configuration. */
763
+ createSocket(useSSL = false, verbose: boolean = false, adapter : WebSocketAdapter = new WebSocketAdapterText(), sendTimeoutMs : number = DefaultSocket.DefaultSendTimeoutMs): Socket {
764
+ return new DefaultSocket(this.host, this.port, useSSL, verbose, adapter, sendTimeoutMs);
765
+ }
766
+
767
+ /** Delete one or more users by ID or username. */
768
+ async deleteFriends(session: Session, ids?: Array<string>, usernames?: Array<string>): Promise<boolean> {
769
+ if (this.autoRefreshSession && session.refresh_token &&
770
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
771
+ await this.sessionRefresh(session);
772
+ }
773
+
774
+ return this.apiClient.deleteFriends(session.token, ids, usernames).then((response: any) => {
775
+ return response !== undefined;
776
+ });
777
+ }
778
+
779
+ /** Delete a channel by ID. */
780
+ async deleteChannelDesc(session: Session, channelId: string): Promise<boolean> {
781
+ if (this.autoRefreshSession && session.refresh_token &&
782
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
783
+ await this.sessionRefresh(session);
784
+ }
785
+
786
+ return this.apiClient.deleteChannelDesc(session.token, channelId).then((response: any) => {
787
+ return response !== undefined;
788
+ });
789
+ }
790
+
791
+ /** Delete a clan desc by ID. */
792
+ async deleteClanDesc(session: Session, clanDescId: string): Promise<boolean> {
793
+ if (this.autoRefreshSession && session.refresh_token &&
794
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
795
+ await this.sessionRefresh(session);
796
+ }
797
+
798
+ return this.apiClient.deleteClanDesc(session.token, clanDescId).then((response: any) => {
799
+ return response !== undefined;
800
+ });
801
+ }
802
+
803
+ /** Delete a category by ID. */
804
+ async deleteCategoryDesc(session: Session, creatorId: string): Promise<boolean> {
805
+ if (this.autoRefreshSession && session.refresh_token &&
806
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
807
+ await this.sessionRefresh(session);
808
+ }
809
+
810
+ return this.apiClient.deleteCategoryDesc(session.token, creatorId).then((response: any) => {
811
+ return response !== undefined;
812
+ });
813
+ }
814
+
815
+ /** Delete one or more notifications */
816
+ async deleteNotifications(session: Session, ids?: Array<string>): Promise<boolean> {
817
+ if (this.autoRefreshSession && session.refresh_token &&
818
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
819
+ await this.sessionRefresh(session);
820
+ }
821
+
822
+ return this.apiClient.deleteNotifications(session.token, ids).then((response: any) => {
823
+ return Promise.resolve(response != undefined);
824
+ });
825
+ }
826
+
827
+ /** Delete one or more storage objects */
828
+ async deleteStorageObjects(session: Session, request: ApiDeleteStorageObjectsRequest): Promise<boolean> {
829
+ if (this.autoRefreshSession && session.refresh_token &&
830
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
831
+ await this.sessionRefresh(session);
832
+ }
833
+
834
+ return this.apiClient.deleteStorageObjects(session.token, request).then((response: any) => {
835
+ return Promise.resolve(response != undefined);
836
+ });
837
+ }
838
+
839
+ /** Delete a role by ID. */
840
+ async deleteRole(session: Session, roleId: string): Promise<boolean> {
841
+ if (this.autoRefreshSession && session.refresh_token &&
842
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
843
+ await this.sessionRefresh(session);
844
+ }
845
+
846
+ return this.apiClient.deleteRole(session.token, roleId).then((response: any) => {
847
+ return response !== undefined;
848
+ });
849
+ }
850
+
851
+ /** Submit an event for processing in the server's registered runtime custom events handler. */
852
+ async emitEvent(session: Session, request: ApiEvent): Promise<boolean> {
853
+ if (this.autoRefreshSession && session.refresh_token &&
854
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
855
+ await this.sessionRefresh(session);
856
+ }
857
+
858
+ return this.apiClient.event(session.token, request).then((response: any) => {
859
+ return Promise.resolve(response != undefined);
860
+ });
861
+ }
862
+
863
+ /** Fetch the current user's account. */
864
+ async getAccount(session: Session): Promise<ApiAccount> {
865
+
866
+ if (this.autoRefreshSession && session.refresh_token &&
867
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
868
+ await this.sessionRefresh(session);
869
+ }
870
+
871
+ return this.apiClient.getAccount(session.token);
872
+ }
873
+
874
+ /** Import Facebook friends and add them to a user's account. */
875
+ async importFacebookFriends(session: Session, request: ApiAccountFacebook): Promise<boolean> {
876
+ if (this.autoRefreshSession && session.refresh_token &&
877
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
878
+ await this.sessionRefresh(session);
879
+ }
880
+
881
+ return this.apiClient.importFacebookFriends(session.token, request).then((response: any) => {
882
+ return response !== undefined;
883
+ });
884
+ }
885
+
886
+ /** Import Steam friends and add them to a user's account. */
887
+ async importSteamFriends(session: Session, request: ApiAccountSteam, reset: boolean): Promise<boolean> {
888
+ if (this.autoRefreshSession && session.refresh_token &&
889
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
890
+ await this.sessionRefresh(session);
891
+ }
892
+
893
+ return this.apiClient.importSteamFriends(session.token, request, reset).then((response: any) => {
894
+ return response !== undefined;
895
+ });
896
+ }
897
+
898
+ /** Fetch zero or more users by ID and/or username. */
899
+ async getUsers(session: Session, ids?: Array<string>, usernames?: Array<string>, facebookIds?: Array<string>): Promise<Users> {
900
+ if (this.autoRefreshSession && session.refresh_token &&
901
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
902
+ await this.sessionRefresh(session);
903
+ }
904
+
905
+ return this.apiClient.getUsers(session.token, ids, usernames, facebookIds).then((response: ApiUsers) => {
906
+ var result: Users = {
907
+ users: []
908
+ };
909
+
910
+ if (response.users == null) {
911
+ return Promise.resolve(result);
912
+ }
913
+
914
+ response.users!.forEach(u => {
915
+ result.users!.push({
916
+ avatar_url: u.avatar_url,
917
+ create_time: u.create_time,
918
+ display_name: u.display_name,
919
+ edge_count: u.edge_count ? Number(u.edge_count) : 0,
920
+ facebook_id: u.facebook_id,
921
+ gamecenter_id: u.gamecenter_id,
922
+ google_id: u.google_id,
923
+ id: u.id,
924
+ lang_tag: u.lang_tag,
925
+ location: u.location,
926
+ online: u.online,
927
+ steam_id: u.steam_id,
928
+ timezone: u.timezone,
929
+ update_time: u.update_time,
930
+ username: u.username,
931
+ metadata: u.metadata ? JSON.parse(u.metadata) : undefined
932
+ })
933
+ });
934
+ return Promise.resolve(result);
935
+ });
936
+ }
937
+
938
+ /** Kick users from a channel, or decline their join requests. */
939
+ async removeChannelUsers(session: Session, channelId: string, ids?: Array<string>): Promise<boolean> {
940
+ if (this.autoRefreshSession && session.refresh_token &&
941
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
942
+ await this.sessionRefresh(session);
943
+ }
944
+
945
+ return this.apiClient.removeChannelUsers(session.token, channelId, ids).then((response: any) => {
946
+ return Promise.resolve(response != undefined);
947
+ });
948
+ }
949
+
950
+ /** List a channel's message history. */
951
+ async listChannelMessages(session: Session, channelId: string, messageId?: string, direction?: number, limit?: number): Promise<ChannelMessageList> {
952
+ if (this.autoRefreshSession && session.refresh_token &&
953
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
954
+ await this.sessionRefresh(session);
955
+ }
956
+
957
+ return this.apiClient.listChannelMessages(session.token, channelId, messageId, direction, limit).then((response: ApiChannelMessageList) => {
958
+ var result: ChannelMessageList = {
959
+ messages: [],
960
+ last_seen_message: response.last_seen_message
961
+ };
962
+
963
+ if (response.messages == null) {
964
+ return Promise.resolve(result);
965
+ }
966
+
967
+ response.messages!.forEach(m => {
968
+ result.messages!.push({
969
+ channel_id: m.channel_id,
970
+ code: m.code ? Number(m.code) : 0,
971
+ create_time: m.create_time || '',
972
+ id: m.message_id,
973
+ sender_id: m.sender_id,
974
+ update_time: m.update_time,
975
+ username: m.username,
976
+ avatar: m.avatar,
977
+ content: m.content ? JSON.parse(m.content) : undefined,
978
+ channel_label: m.channel_label,
979
+ user_id_one: m.user_id_one,
980
+ user_id_two: m.user_id_two,
981
+ attachments: m.attachments ? JSON.parse(m.attachments) : [],
982
+ mentions: m.mentions ? JSON.parse(m.mentions) : [],
983
+ reactions: m.reactions ? JSON.parse(m.reactions) : [],
984
+ references: m.references ? JSON.parse(m.references) : [],
985
+ })
986
+ });
987
+ return Promise.resolve(result);
988
+ });
989
+ }
990
+
991
+ /** List a channel's users. */
992
+ async listChannelVoiceUsers(session: Session, clanId: string, channelId: string, channelType: number, state?: number, limit?: number, cursor?: string): Promise<ApiVoiceChannelUserList> {
993
+ if (this.autoRefreshSession && session.refresh_token &&
994
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
995
+ await this.sessionRefresh(session);
996
+ }
997
+
998
+ return this.apiClient.listChannelVoiceUsers(session.token, clanId, channelId, channelType, limit, state, cursor).then((response: ApiVoiceChannelUserList) => {
999
+ var result: ApiVoiceChannelUserList = {
1000
+ voice_channel_users: []
1001
+ };
1002
+
1003
+ if (response.voice_channel_users == null) {
1004
+ return Promise.resolve(result);
1005
+ }
1006
+
1007
+ response.voice_channel_users!.forEach(gu => {
1008
+ result.voice_channel_users!.push({
1009
+ jid: gu.jid,
1010
+ channel_id: gu.channel_id,
1011
+ user_id: gu.user_id
1012
+ })
1013
+ });
1014
+ return Promise.resolve(result);
1015
+ });
1016
+ }
1017
+
1018
+ /** List a channel's users. */
1019
+ async listChannelUsers(session: Session, clanId: string, channelId: string, channelType: number, state?: number, limit?: number, cursor?: string): Promise<ApiChannelUserList> {
1020
+ if (this.autoRefreshSession && session.refresh_token &&
1021
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1022
+ await this.sessionRefresh(session);
1023
+ }
1024
+
1025
+ return this.apiClient.listChannelUsers(session.token, clanId, channelId, channelType, limit, state, cursor).then((response: ApiChannelUserList) => {
1026
+ var result: ApiChannelUserList = {
1027
+ channel_users: [],
1028
+ cursor: response.cursor,
1029
+ channel_id: response.channel_id
1030
+ };
1031
+
1032
+ if (response.channel_users == null) {
1033
+ return Promise.resolve(result);
1034
+ }
1035
+
1036
+ response.channel_users!.forEach(gu => {
1037
+ result.channel_users!.push({
1038
+ user: {
1039
+ avatar_url: gu.user!.avatar_url,
1040
+ create_time: gu.user!.create_time,
1041
+ display_name: gu.user!.display_name,
1042
+ edge_count: gu.user!.edge_count ? Number(gu.user!.edge_count): 0,
1043
+ facebook_id: gu.user!.facebook_id,
1044
+ gamecenter_id: gu.user!.gamecenter_id,
1045
+ google_id: gu.user!.google_id,
1046
+ id: gu.user!.id,
1047
+ lang_tag: gu.user!.lang_tag,
1048
+ location: gu.user!.location,
1049
+ online: gu.user!.online,
1050
+ steam_id: gu.user!.steam_id,
1051
+ timezone: gu.user!.timezone,
1052
+ update_time: gu.user!.update_time,
1053
+ username: gu.user!.username,
1054
+ metadata: gu.user!.metadata ? JSON.parse(gu.user!.metadata!) : undefined
1055
+ },
1056
+ role_id: gu!.role_id,
1057
+ thread_id: gu.thread_id,
1058
+ id: gu.id,
1059
+ })
1060
+ });
1061
+ return Promise.resolve(result);
1062
+ });
1063
+ }
1064
+
1065
+ /** List a channel's users. */
1066
+ async listClanUsers(session: Session, clanId: string): Promise<ApiClanUserList> {
1067
+ if (this.autoRefreshSession && session.refresh_token &&
1068
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1069
+ await this.sessionRefresh(session);
1070
+ }
1071
+
1072
+ return this.apiClient.listClanUsers(session.token, clanId).then((response: ApiClanUserList) => {
1073
+ var result: ApiClanUserList = {
1074
+ clan_users: [],
1075
+ cursor: response.cursor,
1076
+ clan_id: response.clan_id
1077
+ };
1078
+
1079
+ if (response.clan_users == null) {
1080
+ return Promise.resolve(result);
1081
+ }
1082
+
1083
+ response.clan_users!.forEach(gu => {
1084
+ result.clan_users!.push({
1085
+ user: {
1086
+ avatar_url: gu.user!.avatar_url,
1087
+ create_time: gu.user!.create_time,
1088
+ display_name: gu.user!.display_name,
1089
+ edge_count: gu.user!.edge_count ? Number(gu.user!.edge_count): 0,
1090
+ facebook_id: gu.user!.facebook_id,
1091
+ gamecenter_id: gu.user!.gamecenter_id,
1092
+ google_id: gu.user!.google_id,
1093
+ id: gu.user!.id,
1094
+ lang_tag: gu.user!.lang_tag,
1095
+ location: gu.user!.location,
1096
+ online: gu.user!.online,
1097
+ steam_id: gu.user!.steam_id,
1098
+ timezone: gu.user!.timezone,
1099
+ update_time: gu.user!.update_time,
1100
+ username: gu.user!.username,
1101
+ metadata: gu.user!.metadata ? JSON.parse(gu.user!.metadata!) : undefined
1102
+ },
1103
+ role_id: gu!.role_id
1104
+ })
1105
+ });
1106
+ return Promise.resolve(result);
1107
+ });
1108
+ }
1109
+
1110
+ /** List channels. */
1111
+ async listChannelDescs(session: Session, limit?: number, state?:number, cursor?: string, clanId?: string, channelType?:number): Promise<ApiChannelDescList> {
1112
+ if (this.autoRefreshSession && session.refresh_token &&
1113
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1114
+ await this.sessionRefresh(session);
1115
+ }
1116
+
1117
+ return this.apiClient.listChannelDescs(session.token, limit, state, cursor, clanId, channelType).then((response: ApiChannelDescList) => {
1118
+ var result: ApiChannelDescList = {
1119
+ channeldesc: [],
1120
+ next_cursor: response.next_cursor,
1121
+ prev_cursor: response.prev_cursor,
1122
+ cacheable_cursor: response.cacheable_cursor
1123
+ };
1124
+
1125
+ if (response.channeldesc == null) {
1126
+ return Promise.resolve(result);
1127
+ }
1128
+
1129
+ result.channeldesc = response.channeldesc;
1130
+ return Promise.resolve(result);
1131
+ });
1132
+ }
1133
+
1134
+ /** List clans */
1135
+ async listClanDescs(session: Session, limit?: number, state?:number, cursor?: string): Promise<ApiClanDescList> {
1136
+ if (this.autoRefreshSession && session.refresh_token &&
1137
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1138
+ await this.sessionRefresh(session);
1139
+ }
1140
+
1141
+ return this.apiClient.listClanDescs(session.token, limit, state, cursor).then((response: ApiClanDescList) => {
1142
+ var result: ApiClanDescList = {
1143
+ clandesc: [],
1144
+ };
1145
+
1146
+ if (response.clandesc == null) {
1147
+ return Promise.resolve(result);
1148
+ }
1149
+
1150
+ result.clandesc = response.clandesc;
1151
+ return Promise.resolve(result);
1152
+ });
1153
+ }
1154
+
1155
+ /** List categories. */
1156
+ async listCategoryDescs(session: Session, clanId:string, creatorId?:string, categoryName?:string): Promise<ApiCategoryDescList> {
1157
+ if (this.autoRefreshSession && session.refresh_token &&
1158
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1159
+ await this.sessionRefresh(session);
1160
+ }
1161
+
1162
+ return this.apiClient.listCategoryDescs(session.token, clanId, creatorId, categoryName).then((response: ApiCategoryDescList) => {
1163
+ var result: ApiCategoryDescList = {
1164
+ categorydesc: [],
1165
+ };
1166
+
1167
+ if (response.categorydesc == null) {
1168
+ return Promise.resolve(result);
1169
+ }
1170
+
1171
+ result.categorydesc = response.categorydesc;
1172
+ return Promise.resolve(result);
1173
+ });
1174
+ }
1175
+
1176
+ /** List user roles */
1177
+ async listRoles(session: Session, limit?:number, state?:number, cursor?:string, clanId?:string): Promise<ApiRoleList> {
1178
+ if (this.autoRefreshSession && session.refresh_token &&
1179
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1180
+ await this.sessionRefresh(session);
1181
+ }
1182
+
1183
+ return this.apiClient.listRoles(session.token, limit, state, cursor, clanId).then((response: ApiRoleList) => {
1184
+ return Promise.resolve(response);
1185
+ });
1186
+ }
1187
+
1188
+ /** List permission */
1189
+ async getListPermission(session: Session): Promise<ApiPermissionList> {
1190
+ if (this.autoRefreshSession && session.refresh_token &&
1191
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1192
+ await this.sessionRefresh(session);
1193
+ }
1194
+
1195
+ return this.apiClient.getListPermission(session.token).then((response: ApiPermissionList) => {
1196
+ return Promise.resolve(response);
1197
+ });
1198
+ }
1199
+
1200
+ /** Update action role when delete role */
1201
+ async updateRoleDelete(session: Session, roleId:string, request:{}): Promise<boolean> {
1202
+ if (this.autoRefreshSession && session.refresh_token &&
1203
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1204
+ await this.sessionRefresh(session);
1205
+ }
1206
+
1207
+ return this.apiClient.updateRoleDelete(session.token, roleId, request).then((response: any) => {
1208
+ return response !== undefined;
1209
+ });
1210
+ }
1211
+
1212
+ /** List user roles */
1213
+ async listRolePermissions(session: Session, roleId:string): Promise<ApiPermissionList> {
1214
+ if (this.autoRefreshSession && session.refresh_token &&
1215
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1216
+ await this.sessionRefresh(session);
1217
+ }
1218
+
1219
+ return this.apiClient.listRolePermissions(session.token, roleId).then((response: ApiPermissionList) => {
1220
+ return Promise.resolve(response);
1221
+ });
1222
+ }
1223
+
1224
+ /** List user roles */
1225
+ async listRoleUsers(session: Session, roleId:string, limit?:number, cursor?:string): Promise<ApiRoleUserList> {
1226
+ if (this.autoRefreshSession && session.refresh_token &&
1227
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1228
+ await this.sessionRefresh(session);
1229
+ }
1230
+
1231
+ return this.apiClient.listRoleUsers(session.token, roleId, limit, cursor).then((response: ApiRoleUserList) => {
1232
+ return Promise.resolve(response);
1233
+ });
1234
+ }
1235
+
1236
+ async registFCMDeviceToken(session: Session, tokenId:string): Promise<boolean> {
1237
+ if (this.autoRefreshSession && session.refresh_token &&
1238
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1239
+ await this.sessionRefresh(session);
1240
+ }
1241
+
1242
+ return this.apiClient.registFCMDeviceToken(session.token, tokenId).then((response: any) => {
1243
+ return response !== undefined;
1244
+ });
1245
+ }
1246
+
1247
+ /** Get a clan desc profile */
1248
+ async getClanDescProfile(session: Session, clanId:string): Promise<ApiClanDescProfile> {
1249
+ if (this.autoRefreshSession && session.refresh_token &&
1250
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1251
+ await this.sessionRefresh(session);
1252
+ }
1253
+
1254
+ return this.apiClient.getClanDescProfile(session.token, clanId).then((response: ApiClanDescProfile) => {
1255
+ return Promise.resolve(response);
1256
+ });
1257
+ }
1258
+
1259
+ async getUserProfileOnClan(session: Session, clanId:string): Promise<ApiClanProfile> {
1260
+ if (this.autoRefreshSession && session.refresh_token &&
1261
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1262
+ await this.sessionRefresh(session);
1263
+ }
1264
+
1265
+ return this.apiClient.getUserProfileOnClan(session.token, clanId).then((response: ApiClanProfile) => {
1266
+ return Promise.resolve(response);
1267
+ });
1268
+ }
1269
+
1270
+ /** Add an Apple ID to the social profiles on the current user's account. */
1271
+ async linkApple(session: Session, request: ApiAccountApple): Promise<boolean> {
1272
+ if (this.autoRefreshSession && session.refresh_token &&
1273
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1274
+ await this.sessionRefresh(session);
1275
+ }
1276
+
1277
+ return this.apiClient.linkApple(session.token, request).then((response: any) => {
1278
+ return response !== undefined;
1279
+ });
1280
+ }
1281
+
1282
+ /** Add a custom ID to the social profiles on the current user's account. */
1283
+ async linkCustom(session: Session, request: ApiAccountCustom): Promise<boolean> {
1284
+ if (this.autoRefreshSession && session.refresh_token &&
1285
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1286
+ await this.sessionRefresh(session);
1287
+ }
1288
+
1289
+ return this.apiClient.linkCustom(session.token, request).then((response: any) => {
1290
+ return response !== undefined;
1291
+ });
1292
+ }
1293
+
1294
+ /** Add a device ID to the social profiles on the current user's account. */
1295
+ async linkDevice(session: Session, request: ApiAccountDevice): Promise<boolean> {
1296
+ if (this.autoRefreshSession && session.refresh_token &&
1297
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1298
+ await this.sessionRefresh(session);
1299
+ }
1300
+
1301
+ return this.apiClient.linkDevice(session.token, request).then((response: any) => {
1302
+ return response !== undefined;
1303
+ });
1304
+ }
1305
+
1306
+ /** Add an email+password to the social profiles on the current user's account. */
1307
+ async linkEmail(session: Session, request: ApiAccountEmail): Promise<boolean> {
1308
+ if (this.autoRefreshSession && session.refresh_token &&
1309
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1310
+ await this.sessionRefresh(session);
1311
+ }
1312
+
1313
+ return this.apiClient.linkEmail(session.token, request).then((response: any) => {
1314
+ return response !== undefined;
1315
+ });
1316
+ }
1317
+
1318
+ /** Add Facebook to the social profiles on the current user's account. */
1319
+ async linkFacebook(session: Session, request: ApiAccountFacebook): Promise<boolean> {
1320
+ if (this.autoRefreshSession && session.refresh_token &&
1321
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1322
+ await this.sessionRefresh(session);
1323
+ }
1324
+
1325
+ return this.apiClient.linkFacebook(session.token, request).then((response: any) => {
1326
+ return response !== undefined;
1327
+ });
1328
+ }
1329
+
1330
+ /** Add Facebook Instant to the social profiles on the current user's account. */
1331
+ async linkFacebookInstantGame(session: Session, request: ApiAccountFacebookInstantGame): Promise<boolean> {
1332
+ if (this.autoRefreshSession && session.refresh_token &&
1333
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1334
+ await this.sessionRefresh(session);
1335
+ }
1336
+
1337
+ return this.apiClient.linkFacebookInstantGame(session.token, request).then((response: any) => {
1338
+ return response !== undefined;
1339
+ });
1340
+ }
1341
+
1342
+ /** Add Google to the social profiles on the current user's account. */
1343
+ async linkGoogle(session: Session, request: ApiAccountGoogle): Promise<boolean> {
1344
+ if (this.autoRefreshSession && session.refresh_token &&
1345
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1346
+ await this.sessionRefresh(session);
1347
+ }
1348
+
1349
+ return this.apiClient.linkGoogle(session.token, request).then((response: any) => {
1350
+ return response !== undefined;
1351
+ });
1352
+ }
1353
+
1354
+ /** Add GameCenter to the social profiles on the current user's account. */
1355
+ async linkGameCenter(session: Session, request: ApiAccountGameCenter): Promise<boolean> {
1356
+ if (this.autoRefreshSession && session.refresh_token &&
1357
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1358
+ await this.sessionRefresh(session);
1359
+ }
1360
+
1361
+ return this.apiClient.linkGameCenter(session.token, request).then((response: any) => {
1362
+ return response !== undefined;
1363
+ });
1364
+ }
1365
+
1366
+ /** Add Steam to the social profiles on the current user's account. */
1367
+ async linkSteam(session: Session, request: ApiLinkSteamRequest): Promise<boolean> {
1368
+ if (this.autoRefreshSession && session.refresh_token &&
1369
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1370
+ await this.sessionRefresh(session);
1371
+ }
1372
+
1373
+ return this.apiClient.linkSteam(session.token, request).then((response: any) => {
1374
+ return response !== undefined;
1375
+ });
1376
+ }
1377
+
1378
+ /** List all friends for the current user. */
1379
+ async listFriends(session: Session, state?: number, limit?: number, cursor?: string): Promise<Friends> {
1380
+ if (this.autoRefreshSession && session.refresh_token &&
1381
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1382
+ await this.sessionRefresh(session);
1383
+ }
1384
+
1385
+ return this.apiClient.listFriends(session.token, limit, state, cursor).then((response: ApiFriendList) => {
1386
+
1387
+ var result: Friends = {
1388
+ friends: [],
1389
+ cursor: response.cursor
1390
+ };
1391
+
1392
+ if (response.friends == null) {
1393
+ return Promise.resolve(result);
1394
+ }
1395
+
1396
+ response.friends!.forEach(f => {
1397
+ result.friends!.push({
1398
+ user: {
1399
+ avatar_url: f.user!.avatar_url,
1400
+ create_time: f.user!.create_time,
1401
+ display_name: f.user!.display_name,
1402
+ edge_count: f.user!.edge_count ? Number(f.user!.edge_count) : 0,
1403
+ facebook_id: f.user!.facebook_id,
1404
+ gamecenter_id: f.user!.gamecenter_id,
1405
+ google_id: f.user!.google_id,
1406
+ id: f.user!.id,
1407
+ lang_tag: f.user!.lang_tag,
1408
+ location: f.user!.location,
1409
+ online: f.user!.online,
1410
+ steam_id: f.user!.steam_id,
1411
+ timezone: f.user!.timezone,
1412
+ update_time: f.user!.update_time,
1413
+ username: f.user!.username,
1414
+ metadata: f.user!.metadata ? JSON.parse(f.user!.metadata!) : undefined,
1415
+ },
1416
+ state: f.state
1417
+ })
1418
+ });
1419
+ return Promise.resolve(result);
1420
+ });
1421
+ }
1422
+
1423
+ /** Fetch list of notifications. */
1424
+ async listNotifications(session: Session, limit?: number, cacheableCursor?: string): Promise<NotificationList> {
1425
+ if (this.autoRefreshSession && session.refresh_token &&
1426
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1427
+ await this.sessionRefresh(session);
1428
+ }
1429
+
1430
+ return this.apiClient.listNotifications(session.token, limit, cacheableCursor).then((response: ApiNotificationList) => {
1431
+ var result: NotificationList = {
1432
+ cacheable_cursor: response.cacheable_cursor,
1433
+ notifications: [],
1434
+ };
1435
+
1436
+ if (response.notifications == null) {
1437
+ return Promise.resolve(result);
1438
+ }
1439
+
1440
+ response.notifications!.forEach(n => {
1441
+ result.notifications!.push({
1442
+ code: n.code ? Number(n.code) : 0,
1443
+ create_time: n.create_time,
1444
+ id: n.id,
1445
+ persistent: n.persistent,
1446
+ sender_id: n.sender_id,
1447
+ subject: n.subject,
1448
+ content: n.content ? JSON.parse(n.content) : undefined
1449
+ })
1450
+ });
1451
+ return Promise.resolve(result);
1452
+ });
1453
+ }
1454
+
1455
+ /** List storage objects. */
1456
+ async listStorageObjects(session: Session, collection: string, userId?: string, limit?: number, cursor?: string): Promise<StorageObjectList> {
1457
+ if (this.autoRefreshSession && session.refresh_token &&
1458
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1459
+ await this.sessionRefresh(session);
1460
+ }
1461
+
1462
+ return this.apiClient.listStorageObjects(session.token, collection, userId, limit, cursor).then((response: ApiStorageObjectList) => {
1463
+ var result: StorageObjectList = {
1464
+ objects: [],
1465
+ cursor: response.cursor
1466
+ };
1467
+
1468
+ if (response.objects == null) {
1469
+ return Promise.resolve(result);
1470
+ }
1471
+
1472
+ response.objects!.forEach(o => {
1473
+ result.objects.push({
1474
+ collection: o.collection,
1475
+ key: o.key,
1476
+ permission_read: o.permission_read ? Number(o.permission_read) : 0,
1477
+ permission_write: o.permission_write ? Number(o.permission_write) : 0,
1478
+ value: o.value ? JSON.parse(o.value) : undefined,
1479
+ version: o.version,
1480
+ user_id: o.user_id,
1481
+ create_time: o.create_time,
1482
+ update_time: o.update_time
1483
+ })
1484
+ });
1485
+ return Promise.resolve(result);
1486
+ });
1487
+ }
1488
+
1489
+ /** Fetch storage objects. */
1490
+ async readStorageObjects(session: Session, request: ApiReadStorageObjectsRequest): Promise<StorageObjects> {
1491
+ if (this.autoRefreshSession && session.refresh_token &&
1492
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1493
+ await this.sessionRefresh(session);
1494
+ }
1495
+
1496
+ return this.apiClient.readStorageObjects(session.token, request).then((response: ApiStorageObjects) => {
1497
+ var result: StorageObjects = {objects: []};
1498
+
1499
+ if (response.objects == null) {
1500
+ return Promise.resolve(result);
1501
+ }
1502
+
1503
+ response.objects!.forEach(o => {
1504
+ result.objects.push({
1505
+ collection: o.collection,
1506
+ key: o.key,
1507
+ permission_read: o.permission_read ? Number(o.permission_read) : 0,
1508
+ permission_write: o.permission_write ? Number(o.permission_write) : 0,
1509
+ value: o.value ? JSON.parse(o.value) : undefined,
1510
+ version: o.version,
1511
+ user_id: o.user_id,
1512
+ create_time: o.create_time,
1513
+ update_time: o.update_time
1514
+ })
1515
+ });
1516
+ return Promise.resolve(result);
1517
+ });
1518
+ }
1519
+
1520
+ /** Execute an RPC function on the server. */
1521
+ async rpc(session: Session, basicAuthUsername: string,
1522
+ basicAuthPassword: string, id: string, input: object): Promise<RpcResponse> {
1523
+ if (this.autoRefreshSession && session.refresh_token &&
1524
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1525
+ await this.sessionRefresh(session);
1526
+ }
1527
+
1528
+ return this.apiClient.rpcFunc(session.token, basicAuthUsername, basicAuthPassword, id, JSON.stringify(input)).then((response: ApiRpc) => {
1529
+ return Promise.resolve({
1530
+ id: response.id,
1531
+ payload: (!response.payload) ? undefined : JSON.parse(response.payload)
1532
+ });
1533
+ });
1534
+ }
1535
+
1536
+ /** Execute an RPC function on the server. */
1537
+ async rpcHttpKey(httpKey: string, id: string, input?: object): Promise<RpcResponse> {
1538
+ return this.apiClient.rpcFunc2("", id, input && JSON.stringify(input) || "", httpKey)
1539
+ .then((response: ApiRpc) => {
1540
+ return Promise.resolve({
1541
+ id: response.id,
1542
+ payload: (!response.payload) ? undefined : JSON.parse(response.payload)
1543
+ });
1544
+ }).catch((err: any) => {
1545
+ throw err;
1546
+ });
1547
+ }
1548
+
1549
+ /** Log out a session, invalidate a refresh token, or log out all sessions/refresh tokens for a user. */
1550
+ async sessionLogout(session: Session, token: string, refreshToken: string, ) : Promise<boolean> {
1551
+ if (this.autoRefreshSession && session.refresh_token &&
1552
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1553
+ await this.sessionRefresh(session);
1554
+ }
1555
+
1556
+ return this.apiClient.sessionLogout(session.token, {refresh_token: refreshToken, token: token}).then((response: any) => {
1557
+ return response !== undefined;
1558
+ });
1559
+ }
1560
+
1561
+ /** Refresh a user's session using a refresh token retrieved from a previous authentication request. */
1562
+ async sessionRefresh(session: Session, vars: Record<string, string> = {}) : Promise<Session> {
1563
+
1564
+ if (!session) {
1565
+ console.error("Cannot refresh a null session.");
1566
+ return session;
1567
+ }
1568
+
1569
+ if (session.created && session.expires_at! - session.created_at < 70) {
1570
+ console.warn("Session lifetime too short, please set '--session.token_expiry_sec' option. See the documentation for more info: https://heroiclabs.com/docs/mezon/getting-started/configuration/#session");
1571
+ }
1572
+
1573
+ if (session.created && session.refresh_expires_at! - session.created_at < 3700) {
1574
+ console.warn("Session refresh lifetime too short, please set '--session.refresh_token_expiry_sec' option. See the documentation for more info: https://heroiclabs.com/docs/mezon/getting-started/configuration/#session");
1575
+ }
1576
+
1577
+ const apiSession = await this.apiClient.sessionRefresh(this.serverkey, "", {token: session.refresh_token, vars: vars});
1578
+ session.update(apiSession.token!, apiSession.refresh_token!);
1579
+ return session;
1580
+ }
1581
+
1582
+ /** Remove the Apple ID from the social profiles on the current user's account. */
1583
+ async unlinkApple(session: Session, request: ApiAccountApple): Promise<boolean> {
1584
+ if (this.autoRefreshSession && session.refresh_token &&
1585
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1586
+ await this.sessionRefresh(session);
1587
+ }
1588
+
1589
+ return this.apiClient.unlinkApple(session.token, request).then((response: any) => {
1590
+ return response !== undefined;
1591
+ });
1592
+ }
1593
+
1594
+
1595
+ /** Remove custom ID from the social profiles on the current user's account. */
1596
+ async unlinkCustom(session: Session, request: ApiAccountCustom): Promise<boolean> {
1597
+ if (this.autoRefreshSession && session.refresh_token &&
1598
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1599
+ await this.sessionRefresh(session);
1600
+ }
1601
+
1602
+ return this.apiClient.unlinkCustom(session.token, request).then((response: any) => {
1603
+ return response !== undefined;
1604
+ });
1605
+ }
1606
+
1607
+ /** Remove a device ID from the social profiles on the current user's account. */
1608
+ async unlinkDevice(session: Session, request: ApiAccountDevice): Promise<boolean> {
1609
+ if (this.autoRefreshSession && session.refresh_token &&
1610
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1611
+ await this.sessionRefresh(session);
1612
+ }
1613
+
1614
+ return this.apiClient.unlinkDevice(session.token, request).then((response: any) => {
1615
+ return response !== undefined;
1616
+ });
1617
+ }
1618
+
1619
+ /** Remove an email+password from the social profiles on the current user's account. */
1620
+ async unlinkEmail(session: Session, request: ApiAccountEmail): Promise<boolean> {
1621
+ if (this.autoRefreshSession && session.refresh_token &&
1622
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1623
+ await this.sessionRefresh(session);
1624
+ }
1625
+
1626
+ return this.apiClient.unlinkEmail(session.token, request).then((response: any) => {
1627
+ return response !== undefined;
1628
+ });
1629
+ }
1630
+
1631
+ /** Remove Facebook from the social profiles on the current user's account. */
1632
+ async unlinkFacebook(session: Session, request: ApiAccountFacebook): Promise<boolean> {
1633
+ if (this.autoRefreshSession && session.refresh_token &&
1634
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1635
+ await this.sessionRefresh(session);
1636
+ }
1637
+
1638
+ return this.apiClient.unlinkFacebook(session.token, request).then((response: any) => {
1639
+ return response !== undefined;
1640
+ });
1641
+ }
1642
+
1643
+ /** Remove Facebook Instant social profiles from the current user's account. */
1644
+ async unlinkFacebookInstantGame(session: Session, request: ApiAccountFacebookInstantGame): Promise<boolean> {
1645
+ if (this.autoRefreshSession && session.refresh_token &&
1646
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1647
+ await this.sessionRefresh(session);
1648
+ }
1649
+
1650
+ return this.apiClient.unlinkFacebookInstantGame(session.token, request).then((response: any) => {
1651
+ return response !== undefined;
1652
+ });
1653
+ }
1654
+
1655
+ /** Remove Google from the social profiles on the current user's account. */
1656
+ async unlinkGoogle(session: Session, request: ApiAccountGoogle): Promise<boolean> {
1657
+ if (this.autoRefreshSession && session.refresh_token &&
1658
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1659
+ await this.sessionRefresh(session);
1660
+ }
1661
+
1662
+ return this.apiClient.unlinkGoogle(session.token, request).then((response: any) => {
1663
+ return response !== undefined;
1664
+ });
1665
+ }
1666
+
1667
+ /** Remove GameCenter from the social profiles on the current user's account. */
1668
+ async unlinkGameCenter(session: Session, request: ApiAccountGameCenter): Promise<boolean> {
1669
+ if (this.autoRefreshSession && session.refresh_token &&
1670
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1671
+ await this.sessionRefresh(session);
1672
+ }
1673
+
1674
+ return this.apiClient.unlinkGameCenter(session.token, request).then((response: any) => {
1675
+ return response !== undefined;
1676
+ });
1677
+ }
1678
+
1679
+ /** Remove Steam from the social profiles on the current user's account. */
1680
+ async unlinkSteam(session: Session, request: ApiAccountSteam): Promise<boolean> {
1681
+ if (this.autoRefreshSession && session.refresh_token &&
1682
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1683
+ await this.sessionRefresh(session);
1684
+ }
1685
+ return this.apiClient.unlinkSteam(session.token, request).then((response: any) => {
1686
+ return response !== undefined;
1687
+ });
1688
+ }
1689
+
1690
+ /** Update fields in the current user's account. */
1691
+ async updateAccount(session: Session, request: ApiUpdateAccountRequest): Promise<boolean> {
1692
+ if (this.autoRefreshSession && session.refresh_token &&
1693
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1694
+ await this.sessionRefresh(session);
1695
+ }
1696
+
1697
+ return this.apiClient.updateAccount(session.token, request).then((response: any) => {
1698
+ return response !== undefined;
1699
+ });
1700
+ }
1701
+
1702
+ /** Update fields in a given channel */
1703
+ async updateChannelDesc(session: Session, channelId: string, request: ApiUpdateChannelDescRequest): Promise<boolean> {
1704
+ if (this.autoRefreshSession && session.refresh_token &&
1705
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1706
+ await this.sessionRefresh(session);
1707
+ }
1708
+
1709
+ return this.apiClient.updateChannelDesc(session.token, channelId, request).then((response: any) => {
1710
+ return response !== undefined;
1711
+ });
1712
+ }
1713
+
1714
+ /** Update fields in a given clan. */
1715
+ async updateClanDesc(session: Session, clanId: string, request: ApiUpdateClanDescRequest): Promise<boolean> {
1716
+ if (this.autoRefreshSession && session.refresh_token &&
1717
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1718
+ await this.sessionRefresh(session);
1719
+ }
1720
+
1721
+ return this.apiClient.updateClanDesc(session.token, clanId, request?.creator_id, request?.clan_name, request?.logo, request?.banner).then((response: any) => {
1722
+ return response !== undefined;
1723
+ });
1724
+ }
1725
+
1726
+ /** Update fields in a given category. */
1727
+ async updateCategory(session: Session, request: ApiUpdateCategoryDescRequest): Promise<boolean> {
1728
+ if (this.autoRefreshSession && session.refresh_token &&
1729
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1730
+ await this.sessionRefresh(session);
1731
+ }
1732
+
1733
+ return this.apiClient.updateCategory(session.token, request).then((response: any) => {
1734
+ return response !== undefined;
1735
+ });
1736
+ }
1737
+
1738
+ /** Update fields in a given clan profile. */
1739
+ async updateClanDescProfile(session: Session, clanId: string, request: ApiUpdateClanDescProfileRequest): Promise<boolean> {
1740
+ if (this.autoRefreshSession && session.refresh_token &&
1741
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1742
+ await this.sessionRefresh(session);
1743
+ }
1744
+
1745
+ return this.apiClient.updateClanDescProfile(session.token, clanId, request).then((response: any) => {
1746
+ return response !== undefined;
1747
+ });
1748
+ }
1749
+
1750
+ async updateUserProfileByClan(session: Session, clanId: string, request: ApiUpdateClanProfileRequest): Promise<boolean> {
1751
+ if (this.autoRefreshSession && session.refresh_token &&
1752
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1753
+ await this.sessionRefresh(session);
1754
+ }
1755
+
1756
+ return this.apiClient.updateUserProfileByClan(session.token, clanId, request).then((response: any) => {
1757
+ return response !== undefined;
1758
+ });
1759
+ }
1760
+
1761
+ /** Update fields in a given role. */
1762
+ async updateRole(session: Session, roleId: string, request: ApiUpdateRoleRequest): Promise<boolean> {
1763
+ if (this.autoRefreshSession && session.refresh_token &&
1764
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1765
+ await this.sessionRefresh(session);
1766
+ }
1767
+
1768
+ return this.apiClient.updateRole(session.token, roleId, request).then((response: any) => {
1769
+ return response !== undefined;
1770
+ });
1771
+ }
1772
+
1773
+ /** Update fields in a given clan profile. */
1774
+ async createLinkInviteUser(session: Session, request: ApiLinkInviteUserRequest): Promise<ApiLinkInviteUser> {
1775
+ if (this.autoRefreshSession && session.refresh_token &&
1776
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1777
+ await this.sessionRefresh(session);
1778
+ }
1779
+
1780
+ return this.apiClient.createLinkInviteUser(session.token, request).then((response: ApiLinkInviteUser) => {
1781
+ return Promise.resolve(response);
1782
+
1783
+ });
1784
+ }
1785
+
1786
+ /** Get link invite user */
1787
+ async getLinkInvite(session: Session, inviteId:string): Promise<ApiInviteUserRes> {
1788
+ if (this.autoRefreshSession && session.refresh_token &&
1789
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1790
+ await this.sessionRefresh(session);
1791
+ }
1792
+
1793
+ return this.apiClient.getLinkInvite(session.token, inviteId).then((response: ApiInviteUserRes) => {
1794
+ return Promise.resolve(response);
1795
+
1796
+ });
1797
+ }
1798
+
1799
+ /** Get permission of user in the clan */
1800
+ async GetPermissionOfUserInTheClan(session: Session, clanId:string): Promise<ApiPermissionList> {
1801
+ if (this.autoRefreshSession && session.refresh_token &&
1802
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1803
+ await this.sessionRefresh(session);
1804
+ }
1805
+
1806
+ return this.apiClient.GetPermissionOfUserInTheClan(session.token, clanId).then((response: ApiPermissionList) => {
1807
+ return Promise.resolve(response);
1808
+
1809
+ });
1810
+ }
1811
+
1812
+ /** invite user */
1813
+ async inviteUser(session: Session, inviteId:string): Promise<ApiInviteUserRes> {
1814
+ if (this.autoRefreshSession && session.refresh_token &&
1815
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1816
+ await this.sessionRefresh(session);
1817
+ }
1818
+
1819
+ return this.apiClient.inviteUser(session.token, inviteId).then((response: ApiInviteUserRes) => {
1820
+ return Promise.resolve(response);
1821
+
1822
+ });
1823
+ }
1824
+
1825
+ /** Write storage objects. */
1826
+ async writeStorageObjects(session: Session, objects: Array<WriteStorageObject>): Promise<ApiStorageObjectAcks> {
1827
+ if (this.autoRefreshSession && session.refresh_token &&
1828
+ session.isexpired((Date.now() + this.expiredTimespanMs)/1000)) {
1829
+ await this.sessionRefresh(session);
1830
+ }
1831
+
1832
+ var request: ApiWriteStorageObjectsRequest = {objects: []};
1833
+ objects.forEach(o => {
1834
+ request.objects!.push({
1835
+ collection: o.collection,
1836
+ key: o.key,
1837
+ permission_read: o.permission_read,
1838
+ permission_write: o.permission_write,
1839
+ value: JSON.stringify(o.value),
1840
+ version: o.version
1841
+ })
1842
+ })
1843
+
1844
+ return this.apiClient.writeStorageObjects(session.token, request);
1845
+ }
1846
+ };
1847
+
1848
+