forked from tinode/tinode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.js
2419 lines (2279 loc) · 74.3 KB
/
topic.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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Topic management.
*
* @copyright 2015-2022 Tinode LLC.
*/
'use strict';
import AccessMode from './access-mode.js';
import CBuffer from './cbuffer.js';
import CommError from './comm-error.js';
import * as Const from './config.js';
import Drafty from './drafty.js';
import MetaGetBuilder from './meta-builder.js';
import {
mergeObj,
mergeToCache,
normalizeArray
} from './utils.js';
/**
* Topic is a class representing a logical communication channel.
*/
export class Topic {
/**
* @callback onData
* @param {Data} data - Data packet
*/
/**
* Create topic.
* @param {string} name - Name of the topic to create.
* @param {Object=} callbacks - Object with various event callbacks.
* @param {onData} callbacks.onData - Callback which receives a <code>{data}</code> message.
* @param {callback} callbacks.onMeta - Callback which receives a <code>{meta}</code> message.
* @param {callback} callbacks.onPres - Callback which receives a <code>{pres}</code> message.
* @param {callback} callbacks.onInfo - Callback which receives an <code>{info}</code> message.
* @param {callback} callbacks.onMetaDesc - Callback which receives changes to topic desctioption {@link desc}.
* @param {callback} callbacks.onMetaSub - Called for a single subscription record change.
* @param {callback} callbacks.onSubsUpdated - Called after a batch of subscription changes have been recieved and cached.
* @param {callback} callbacks.onDeleteTopic - Called after the topic is deleted.
* @param {callback} callbacls.onAllMessagesReceived - Called when all requested <code>{data}</code> messages have been recived.
*/
constructor(name, callbacks) {
// Parent Tinode object.
this._tinode = null;
// Server-provided data, locally immutable.
// topic name
this.name = name;
// Timestamp when the topic was created.
this.created = null;
// Timestamp when the topic was last updated.
this.updated = null;
// Timestamp of the last messages
this.touched = new Date(0);
// Access mode, see AccessMode
this.acs = new AccessMode(null);
// Per-topic private data (accessible by current user only).
this.private = null;
// Per-topic public data (accessible by all users).
this.public = null;
// Per-topic system-provided data (accessible by all users).
this.trusted = null;
// Locally cached data
// Subscribed users, for tracking read/recv/msg notifications.
this._users = {};
// Current value of locally issued seqId, used for pending messages.
this._queuedSeqId = Const.LOCAL_SEQID;
// The maximum known {data.seq} value.
this._maxSeq = 0;
// The minimum known {data.seq} value.
this._minSeq = 0;
// Indicator that the last request for earlier messages returned 0.
this._noEarlierMsgs = false;
// The maximum known deletion ID.
this._maxDel = 0;
// Timer object used to send 'recv' notifications.
this._recvNotificationTimer = null;
// User discovery tags
this._tags = [];
// Credentials such as email or phone number.
this._credentials = [];
// Message versions cache (e.g. for edited messages).
// Keys: original message seq ID.
// Values: CBuffers containing newer versions of the original message
// ordered by seq id.
this._messageVersions = {};
// Message cache, sorted by message seq values, from old to new.
this._messages = new CBuffer((a, b) => {
return a.seq - b.seq;
}, true);
// Boolean, true if the topic is currently live
this._attached = false;
// Timestap of the most recently updated subscription.
this._lastSubsUpdate = new Date(0);
// Topic created but not yet synced with the server. Used only during initialization.
this._new = true;
// The topic is deleted at the server, this is a local copy.
this._deleted = false;
// Timer used to trgger {leave} request after a delay.
this._delayedLeaveTimer = null;
// Callbacks
if (callbacks) {
this.onData = callbacks.onData;
this.onMeta = callbacks.onMeta;
this.onPres = callbacks.onPres;
this.onInfo = callbacks.onInfo;
// A single desc update;
this.onMetaDesc = callbacks.onMetaDesc;
// A single subscription record;
this.onMetaSub = callbacks.onMetaSub;
// All subscription records received;
this.onSubsUpdated = callbacks.onSubsUpdated;
this.onTagsUpdated = callbacks.onTagsUpdated;
this.onCredsUpdated = callbacks.onCredsUpdated;
this.onDeleteTopic = callbacks.onDeleteTopic;
this.onAllMessagesReceived = callbacks.onAllMessagesReceived;
}
}
// Static methods.
/**
* Determine topic type from topic's name: grp, p2p, me, fnd, sys.
*
* @param {string} name - Name of the topic to test.
* @returns {string} One of <code>"me"</code>, <code>"fnd"</code>, <code>"sys"</code>, <code>"grp"</code>,
* <code>"p2p"</code> or <code>undefined</code>.
*/
static topicType(name) {
const types = {
'me': Const.TOPIC_ME,
'fnd': Const.TOPIC_FND,
'grp': Const.TOPIC_GRP,
'new': Const.TOPIC_GRP,
'nch': Const.TOPIC_GRP,
'chn': Const.TOPIC_GRP,
'usr': Const.TOPIC_P2P,
'sys': Const.TOPIC_SYS
};
return types[(typeof name == 'string') ? name.substring(0, 3) : 'xxx'];
}
/**
* Check if the given topic name is a name of a 'me' topic.
*
* @param {string} name - Name of the topic to test.
* @returns {boolean} <code>true</code> if the name is a name of a 'me' topic, <code>false</code> otherwise.
*/
static isMeTopicName(name) {
return Topic.topicType(name) == Const.TOPIC_ME;
}
/**
* Check if the given topic name is a name of a group topic.
* @static
*
* @param {string} name - Name of the topic to test.
* @returns {boolean} <code>true</code> if the name is a name of a group topic, <code>false</code> otherwise.
*/
static isGroupTopicName(name) {
return Topic.topicType(name) == Const.TOPIC_GRP;
}
/**
* Check if the given topic name is a name of a p2p topic.
* @static
*
* @param {string} name - Name of the topic to test.
* @returns {boolean} <code>true</code> if the name is a name of a p2p topic, <code>false</code> otherwise.
*/
static isP2PTopicName(name) {
return Topic.topicType(name) == Const.TOPIC_P2P;
}
/**
* Check if the given topic name is a name of a communication topic, i.e. P2P or group.
* @static
*
* @param {string} name - Name of the topic to test.
* @returns {boolean} <code>true</code> if the name is a name of a p2p or group topic, <code>false</code> otherwise.
*/
static isCommTopicName(name) {
return Topic.isP2PTopicName(name) || Topic.isGroupTopicName(name);
}
/**
* Check if the topic name is a name of a new topic.
* @static
*
* @param {string} name - topic name to check.
* @returns {boolean} <code>true</code> if the name is a name of a new topic, <code>false</code> otherwise.
*/
static isNewGroupTopicName(name) {
return (typeof name == 'string') &&
(name.substring(0, 3) == Const.TOPIC_NEW || name.substring(0, 3) == Const.TOPIC_NEW_CHAN);
}
/**
* Check if the topic name is a name of a channel.
* @static
*
* @param {string} name - topic name to check.
* @returns {boolean} <code>true</code> if the name is a name of a channel, <code>false</code> otherwise.
*/
static isChannelTopicName(name) {
return (typeof name == 'string') &&
(name.substring(0, 3) == Const.TOPIC_CHAN || name.substring(0, 3) == Const.TOPIC_NEW_CHAN);
}
/**
* Check if the topic is subscribed.
* @returns {boolean} True is topic is attached/subscribed, false otherwise.
*/
isSubscribed() {
return this._attached;
}
/**
* Request topic to subscribe. Wrapper for {@link Tinode#subscribe}.
*
* @param {Tinode.GetQuery=} getParams - get query parameters.
* @param {Tinode.SetParams=} setParams - set parameters.
* @returns {Promise} Promise to be resolved/rejected when the server responds to the request.
*/
subscribe(getParams, setParams) {
// Clear request to leave topic.
clearTimeout(this._delayedLeaveTimer);
this._delayedLeaveTimer = null;
// If the topic is already subscribed, return resolved promise
if (this._attached) {
return Promise.resolve(this);
}
// Send subscribe message, handle async response.
// If topic name is explicitly provided, use it. If no name, then it's a new group topic,
// use "new".
return this._tinode.subscribe(this.name || Const.TOPIC_NEW, getParams, setParams).then(ctrl => {
if (ctrl.code >= 300) {
// Do nothing if subscription status has not changed.
return ctrl;
}
this._attached = true;
this._deleted = false;
this.acs = (ctrl.params && ctrl.params.acs) ? ctrl.params.acs : this.acs;
// Set topic name for new topics and add it to cache.
if (this._new) {
delete this._new;
if (this.name != ctrl.topic) {
// Name may change new123456 -> grpAbCdEf. Remove from cache under the old name.
this._cacheDelSelf();
this.name = ctrl.topic;
}
this._cachePutSelf();
this.created = ctrl.ts;
this.updated = ctrl.ts;
if (this.name != Const.TOPIC_ME && this.name != Const.TOPIC_FND) {
// Add the new topic to the list of contacts maintained by the 'me' topic.
const me = this._tinode.getMeTopic();
if (me.onMetaSub) {
me.onMetaSub(this);
}
if (me.onSubsUpdated) {
me.onSubsUpdated([this.name], 1);
}
}
if (setParams && setParams.desc) {
setParams.desc._noForwarding = true;
this._processMetaDesc(setParams.desc);
}
}
return ctrl;
});
}
/**
* Create a draft of a message without sending it to the server.
* @memberof Tinode.Topic#
*
* @param {string | Object} data - Content to wrap in a draft.
* @param {boolean=} noEcho - If <code>true</code> server will not echo message back to originating
* session. Otherwise the server will send a copy of the message to sender.
*
* @returns {Object} message draft.
*/
createMessage(data, noEcho) {
return this._tinode.createMessage(this.name, data, noEcho);
}
/**
* Immediately publish data to topic. Wrapper for {@link Tinode#publish}.
* @memberof Tinode.Topic#
*
* @param {string | Object} data - Message to publish, either plain string or a Drafty object.
* @param {boolean=} noEcho - If <code>true</code> server will not echo message back to originating
* @returns {Promise} Promise to be resolved/rejected when the server responds to the request.
*/
publish(data, noEcho) {
return this.publishMessage(this.createMessage(data, noEcho));
}
/**
* Publish message created by {@link Tinode.Topic#createMessage}.
* @memberof Tinode.Topic#
*
* @param {Object} pub - {data} object to publish. Must be created by {@link Tinode.Topic#createMessage}
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to the request.
*/
publishMessage(pub) {
if (!this._attached) {
return Promise.reject(new Error("Cannot publish on inactive topic"));
}
if (this._sending) {
return Promise.reject(new Error("The message is already being sent"));
}
// Send data.
pub._sending = true;
pub._failed = false;
// Extract refereces to attachments and out of band image records.
let attachments = null;
if (Drafty.hasEntities(pub.content)) {
attachments = [];
Drafty.entities(pub.content, data => {
if (data) {
if (data.ref) {
attachments.push(data.ref);
}
if (data.preref) {
attachments.push(data.preref);
}
}
});
if (attachments.length == 0) {
attachments = null;
}
}
return this._tinode.publishMessage(pub, attachments).then(ctrl => {
pub._sending = false;
pub.ts = ctrl.ts;
this.swapMessageId(pub, ctrl.params.seq);
this._maybeUpdateMessageVersionsCache(pub);
this._routeData(pub);
return ctrl;
}).catch(err => {
this._tinode.logger("WARNING: Message rejected by the server", err);
pub._sending = false;
pub._failed = true;
if (this.onData) {
this.onData();
}
});
}
/**
* Add message to local message cache, send to the server when the promise is resolved.
* If promise is null or undefined, the message will be sent immediately.
* The message is sent when the
* The message should be created by {@link Tinode.Topic#createMessage}.
* This is probably not the final API.
* @memberof Tinode.Topic#
*
* @param {Object} pub - Message to use as a draft.
* @param {Promise} prom - Message will be sent when this promise is resolved, discarded if rejected.
*
* @returns {Promise} derived promise.
*/
publishDraft(pub, prom) {
const seq = pub.seq || this._getQueuedSeqId();
if (!pub._noForwarding) {
// The 'seq', 'ts', and 'from' are added to mimic {data}. They are removed later
// before the message is sent.
pub._noForwarding = true;
pub.seq = seq;
pub.ts = new Date();
pub.from = this._tinode.getCurrentUserID();
// Don't need an echo message because the message is added to local cache right away.
pub.noecho = true;
// Add to cache.
this._messages.put(pub);
this._tinode._db.addMessage(pub);
if (this.onData) {
this.onData(pub);
}
}
// If promise is provided, send the queued message when it's resolved.
// If no promise is provided, create a resolved one and send immediately.
return (prom || Promise.resolve())
.then(_ => {
if (pub._cancelled) {
return {
code: 300,
text: "cancelled"
};
}
return this.publishMessage(pub);
}).catch(err => {
this._tinode.logger("WARNING: Message draft rejected", err);
pub._sending = false;
pub._failed = true;
pub._fatal = err instanceof CommError ? (err.code >= 400 && err.code < 500) : false;
if (this.onData) {
this.onData();
}
// Rethrow to let caller know that the operation failed.
throw err;
});
}
/**
* Leave the topic, optionally unsibscribe. Leaving the topic means the topic will stop
* receiving updates from the server. Unsubscribing will terminate user's relationship with the topic.
* Wrapper for {@link Tinode#leave}.
* @memberof Tinode.Topic#
*
* @param {boolean=} unsub - If true, unsubscribe, otherwise just leave.
* @returns {Promise} Promise to be resolved/rejected when the server responds to the request.
*/
leave(unsub) {
// It's possible to unsubscribe (unsub==true) from inactive topic.
if (!this._attached && !unsub) {
return Promise.reject(new Error("Cannot leave inactive topic"));
}
// Send a 'leave' message, handle async response
return this._tinode.leave(this.name, unsub).then(ctrl => {
this._resetSub();
if (unsub) {
this._gone();
}
return ctrl;
});
}
/**
* Leave the topic, optionally unsibscribe after a delay. Leaving the topic means the topic will stop
* receiving updates from the server. Unsubscribing will terminate user's relationship with the topic.
* Wrapper for {@link Tinode#leave}.
* @memberof Tinode.Topic#
*
* @param {boolean} unsub - If true, unsubscribe, otherwise just leave.
* @param {number} delay - time in milliseconds to delay leave request.
*/
leaveDelayed(unsub, delay) {
clearTimeout(this._delayedLeaveTimer);
this._delayedLeaveTimer = setTimeout(_ => {
this._delayedLeaveTimer = null;
this.leave(unsub)
}, delay);
}
/**
* Request topic metadata from the server.
* @memberof Tinode.Topic#
*
* @param {Tinode.GetQuery} request parameters
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
getMeta(params) {
// Send {get} message, return promise.
return this._tinode.getMeta(this.name, params);
}
/**
* Request more messages from the server
* @memberof Tinode.Topic#
*
* @param {number} limit number of messages to get.
* @param {boolean} forward if true, request newer messages.
*/
getMessagesPage(limit, forward) {
let query = forward ?
this.startMetaQuery().withLaterData(limit) :
this.startMetaQuery().withEarlierData(limit);
// First try fetching from DB, then from the server.
return this._loadMessages(this._tinode._db, query.extract('data'))
.then((count) => {
if (count == limit) {
// Got enough messages from local cache.
return Promise.resolve({
topic: this.name,
code: 200,
params: {
count: count
}
});
}
// Reduce the count of requested messages.
limit -= count;
// Update query with new values loaded from DB.
query = forward ? this.startMetaQuery().withLaterData(limit) :
this.startMetaQuery().withEarlierData(limit);
let promise = this.getMeta(query.build());
if (!forward) {
promise = promise.then(ctrl => {
if (ctrl && ctrl.params && !ctrl.params.count) {
this._noEarlierMsgs = true;
}
});
}
return promise;
});
}
/**
* Update topic metadata.
* @memberof Tinode.Topic#
*
* @param {Tinode.SetParams} params parameters to update.
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
setMeta(params) {
if (params.tags) {
params.tags = normalizeArray(params.tags);
}
// Send Set message, handle async response.
return this._tinode.setMeta(this.name, params)
.then(ctrl => {
if (ctrl && ctrl.code >= 300) {
// Not modified
return ctrl;
}
if (params.sub) {
params.sub.topic = this.name;
if (ctrl.params && ctrl.params.acs) {
params.sub.acs = ctrl.params.acs;
params.sub.updated = ctrl.ts;
}
if (!params.sub.user) {
// This is a subscription update of the current user.
// Assign user ID otherwise the update will be ignored by _processMetaSubs.
params.sub.user = this._tinode.getCurrentUserID();
if (!params.desc) {
// Force update to topic's asc.
params.desc = {};
}
}
params.sub._noForwarding = true;
this._processMetaSubs([params.sub]);
}
if (params.desc) {
if (ctrl.params && ctrl.params.acs) {
params.desc.acs = ctrl.params.acs;
params.desc.updated = ctrl.ts;
}
this._processMetaDesc(params.desc);
}
if (params.tags) {
this._processMetaTags(params.tags);
}
if (params.cred) {
this._processMetaCreds([params.cred], true);
}
return ctrl;
});
}
/**
* Update access mode of the current user or of another topic subsriber.
* @memberof Tinode.Topic#
*
* @param {string} uid - UID of the user to update or null to update current user.
* @param {string} update - the update value, full or delta.
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
updateMode(uid, update) {
const user = uid ? this.subscriber(uid) : null;
const am = user ?
user.acs.updateGiven(update).getGiven() :
this.getAccessMode().updateWant(update).getWant();
return this.setMeta({
sub: {
user: uid,
mode: am
}
});
}
/**
* Create new topic subscription. Wrapper for {@link Tinode#setMeta}.
* @memberof Tinode.Topic#
*
* @param {string} uid - ID of the user to invite
* @param {string=} mode - Access mode. <code>null</code> means to use default.
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
invite(uid, mode) {
return this.setMeta({
sub: {
user: uid,
mode: mode
}
});
}
/**
* Archive or un-archive the topic. Wrapper for {@link Tinode#setMeta}.
* @memberof Tinode.Topic#
*
* @param {boolean} arch - true to archive the topic, false otherwise.
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
archive(arch) {
if (this.private && (!this.private.arch == !arch)) {
return Promise.resolve(arch);
}
return this.setMeta({
desc: {
private: {
arch: arch ? true : Const.DEL_CHAR
}
}
});
}
/**
* Delete messages. Hard-deleting messages requires Owner permission.
* Wrapper for {@link Tinode#delMessages}.
* @memberof Tinode.Topic#
*
* @param {Tinode.DelRange[]} ranges - Ranges of message IDs to delete.
* @param {boolean=} hard - Hard or soft delete
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
delMessages(ranges, hard) {
if (!this._attached) {
return Promise.reject(new Error("Cannot delete messages in inactive topic"));
}
// Sort ranges in accending order by low, the descending by hi.
ranges.sort((r1, r2) => {
if (r1.low < r2.low) {
return true;
}
if (r1.low == r2.low) {
return !r2.hi || (r1.hi >= r2.hi);
}
return false;
});
// Remove pending messages from ranges possibly clipping some ranges.
let tosend = ranges.reduce((out, r) => {
if (r.low < Const.LOCAL_SEQID) {
if (!r.hi || r.hi < Const.LOCAL_SEQID) {
out.push(r);
} else {
// Clip hi to max allowed value.
out.push({
low: r.low,
hi: this._maxSeq + 1
});
}
}
return out;
}, []);
// Send {del} message, return promise
let result;
if (tosend.length > 0) {
result = this._tinode.delMessages(this.name, tosend, hard);
} else {
result = Promise.resolve({
params: {
del: 0
}
});
}
// Update local cache.
return result.then(ctrl => {
if (ctrl.params.del > this._maxDel) {
this._maxDel = ctrl.params.del;
}
ranges.forEach((r) => {
if (r.hi) {
this.flushMessageRange(r.low, r.hi);
} else {
this.flushMessage(r.low);
}
});
if (this.onData) {
// Calling with no parameters to indicate the messages were deleted.
this.onData();
}
return ctrl;
});
}
/**
* Delete all messages. Hard-deleting messages requires Deleter permission.
* @memberof Tinode.Topic#
*
* @param {boolean} hardDel - true if messages should be hard-deleted.
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
delMessagesAll(hardDel) {
if (!this._maxSeq || this._maxSeq <= 0) {
// There are no messages to delete.
return Promise.resolve();
}
return this.delMessages([{
low: 1,
hi: this._maxSeq + 1,
_all: true
}], hardDel);
}
/**
* Delete multiple messages defined by their IDs. Hard-deleting messages requires Deleter permission.
* @memberof Tinode.Topic#
*
* @param {Array.<number>} list - list of seq IDs to delete.
* @param {boolean=} hardDel - true if messages should be hard-deleted.
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
delMessagesList(list, hardDel) {
// Sort the list in ascending order
list.sort((a, b) => a - b);
// Convert the array of IDs to ranges.
let ranges = list.reduce((out, id) => {
if (out.length == 0) {
// First element.
out.push({
low: id
});
} else {
let prev = out[out.length - 1];
if ((!prev.hi && (id != prev.low + 1)) || (id > prev.hi)) {
// New range.
out.push({
low: id
});
} else {
// Expand existing range.
prev.hi = prev.hi ? Math.max(prev.hi, id + 1) : id + 1;
}
}
return out;
}, []);
// Send {del} message, return promise
return this.delMessages(ranges, hardDel);
}
/**
* Delete original message and edited variants. Hard-deleting messages requires Deleter permission.
* @memberof Tinode.Topic#
*
* @param {number} seq - original seq ID of the message to delete.
* @param {boolean=} hardDel - true if messages should be hard-deleted.
*
* @returns {Promise} Promise to be resolved/rejected when the server responds to the request.
*/
delMessagesEdits(seq, hardDel) {
const list = [seq];
this.messageVersions(seq, msg => list.push(msg.seq));
// Send {del} message, return promise
return this.delMessagesList(list, hardDel);
}
/**
* Delete topic. Requires Owner permission. Wrapper for {@link Tinode#delTopic}.
* @memberof Tinode.Topic#
*
* @param {boolean} hard - had-delete topic.
* @returns {Promise} Promise to be resolved/rejected when the server responds to the request.
*/
delTopic(hard) {
if (this._deleted) {
// The topic is already deleted at the server, just remove from DB.
this._gone();
return Promise.resolve(null);
}
return this._tinode.delTopic(this.name, hard).then(ctrl => {
this._deleted = true;
this._resetSub();
this._gone();
return ctrl;
});
}
/**
* Delete subscription. Requires Share permission. Wrapper for {@link Tinode#delSubscription}.
* @memberof Tinode.Topic#
*
* @param {string} user - ID of the user to remove subscription for.
* @returns {Promise} Promise to be resolved/rejected when the server responds to request.
*/
delSubscription(user) {
if (!this._attached) {
return Promise.reject(new Error("Cannot delete subscription in inactive topic"));
}
// Send {del} message, return promise
return this._tinode.delSubscription(this.name, user).then(ctrl => {
// Remove the object from the subscription cache;
delete this._users[user];
// Notify listeners
if (this.onSubsUpdated) {
this.onSubsUpdated(Object.keys(this._users));
}
return ctrl;
});
}
/**
* Send a read/recv notification.
* @memberof Tinode.Topic#
*
* @param {string} what - what notification to send: <code>recv</code>, <code>read</code>.
* @param {number} seq - ID or the message read or received.
*/
note(what, seq) {
if (!this._attached) {
// Cannot sending {note} on an inactive topic".
return;
}
// Update local cache with the new count.
const user = this._users[this._tinode.getCurrentUserID()];
let update = false;
if (user) {
// Self-subscription is found.
if (!user[what] || user[what] < seq) {
user[what] = seq;
update = true;
}
} else {
// Self-subscription is not found.
update = (this[what] | 0) < seq;
}
if (update) {
// Send notification to the server.
this._tinode.note(this.name, what, seq);
// Update locally cached contact with the new count.
this._updateMyReadRecv(what, seq);
if (this.acs != null && !this.acs.isMuted()) {
const me = this._tinode.getMeTopic();
// Sent a notification to 'me' listeners.
me._refreshContact(what, this);
}
}
}
/**
* Send a 'recv' receipt. Wrapper for {@link Tinode#noteRecv}.
* @memberof Tinode.Topic#
*
* @param {number} seq - ID of the message to aknowledge.
*/
noteRecv(seq) {
this.note('recv', seq);
}
/**
* Send a 'read' receipt. Wrapper for {@link Tinode#noteRead}.
* @memberof Tinode.Topic#
*
* @param {number} seq - ID of the message to aknowledge or 0/undefined to acknowledge the latest messages.
*/
noteRead(seq) {
seq = seq || this._maxSeq;
if (seq > 0) {
this.note('read', seq);
}
}
/**
* Send a key-press notification. Wrapper for {@link Tinode#noteKeyPress}.
* @memberof Tinode.Topic#
*/
noteKeyPress() {
if (this._attached) {
this._tinode.noteKeyPress(this.name);
} else {
this._tinode.logger("INFO: Cannot send notification in inactive topic");
}
}
/**
* Send a notification than a video or audio message is . Wrapper for {@link Tinode#noteKeyPress}.
* @memberof Tinode.Topic#
* @param audioOnly - true if the recording is audio-only, false if it's a video recording.
*/
noteRecording(audioOnly) {
if (this._attached) {
this._tinode.noteKeyPress(this.name, audioOnly ? 'kpa' : 'kpv');
} else {
this._tinode.logger("INFO: Cannot send notification in inactive topic");
}
}
/**
* Send a {note what='call'}. Wrapper for {@link Tinode#videoCall}.
* @memberof Tinode#
*
* @param {string} evt - Call event.
* @param {int} seq - ID of the call message the event pertains to.
* @param {string} payload - Payload associated with this event (e.g. SDP string).
*
* @returns {Promise} Promise (for some call events) which will
* be resolved/rejected on receiving server reply
*/
videoCall(evt, seq, payload) {
if (!this._attached && !['ringing', 'hang-up'].includes(evt)) {
// Cannot {call} on an inactive topic".
return;
}
return this._tinode.videoCall(this.name, seq, evt, payload);
}
// Update cached read/recv/unread counts for the current user.
_updateMyReadRecv(what, seq, ts) {
let oldVal, doUpdate = false;
seq = seq | 0;
this.seq = this.seq | 0;
this.read = this.read | 0;
this.recv = this.recv | 0;
switch (what) {
case 'recv':
oldVal = this.recv;
this.recv = Math.max(this.recv, seq);
doUpdate = (oldVal != this.recv);
break;
case 'read':
oldVal = this.read;
this.read = Math.max(this.read, seq);
doUpdate = (oldVal != this.read);
break;
case 'msg':
oldVal = this.seq;
this.seq = Math.max(this.seq, seq);
if (!this.touched || this.touched < ts) {
this.touched = ts;
}
doUpdate = (oldVal != this.seq);
break;
}
// Sanity checks.
if (this.recv < this.read) {
this.recv = this.read;
doUpdate = true;
}
if (this.seq < this.recv) {
this.seq = this.recv;
if (!this.touched || this.touched < ts) {
this.touched = ts;
}
doUpdate = true;
}
this.unread = this.seq - this.read;
return doUpdate;
}
/**
* Get user description from global cache. The user does not need to be a
* subscriber of this topic.
* @memberof Tinode.Topic#
*
* @param {string} uid - ID of the user to fetch.
* @return {Object} user description or undefined.
*/
userDesc(uid) {
// TODO: handle asynchronous requests
const user = this._cacheGetUser(uid);
if (user) {
return user; // Promise.resolve(user)
}
}
/**
* Get description of the p2p peer from subscription cache.
* @memberof Tinode.Topic#
*
* @return {Object} peer's description or undefined.
*/
p2pPeerDesc() {
if (!this.isP2PType()) {
return undefined;