1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.Room = Room;
|
9 |
|
10 | var _events = require("events");
|
11 |
|
12 | var _eventTimelineSet = require("./event-timeline-set");
|
13 |
|
14 | var _eventTimeline = require("./event-timeline");
|
15 |
|
16 | var _contentRepo = require("../content-repo");
|
17 |
|
18 | var utils = _interopRequireWildcard(require("../utils"));
|
19 |
|
20 | var _event = require("./event");
|
21 |
|
22 | var _roomMember = require("./room-member");
|
23 |
|
24 | var _roomSummary = require("./room-summary");
|
25 |
|
26 | var _logger = require("../logger");
|
27 |
|
28 | var _ReEmitter = require("../ReEmitter");
|
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 | const KNOWN_SAFE_ROOM_VERSION = '5';
|
58 | const SAFE_ROOM_VERSIONS = ['1', '2', '3', '4', '5'];
|
59 |
|
60 | function synthesizeReceipt(userId, event, receiptType) {
|
61 |
|
62 |
|
63 |
|
64 | const fakeReceipt = {
|
65 | content: {},
|
66 | type: "m.receipt",
|
67 | room_id: event.getRoomId()
|
68 | };
|
69 | fakeReceipt.content[event.getId()] = {};
|
70 | fakeReceipt.content[event.getId()][receiptType] = {};
|
71 | fakeReceipt.content[event.getId()][receiptType][userId] = {
|
72 | ts: event.getTs()
|
73 | };
|
74 | return new _event.MatrixEvent(fakeReceipt);
|
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 | function Room(roomId, client, myUserId, opts) {
|
139 | opts = opts || {};
|
140 | opts.pendingEventOrdering = opts.pendingEventOrdering || "chronological";
|
141 | this.reEmitter = new _ReEmitter.ReEmitter(this);
|
142 |
|
143 | if (["chronological", "detached"].indexOf(opts.pendingEventOrdering) === -1) {
|
144 | throw new Error("opts.pendingEventOrdering MUST be either 'chronological' or " + "'detached'. Got: '" + opts.pendingEventOrdering + "'");
|
145 | }
|
146 |
|
147 | this.myUserId = myUserId;
|
148 | this.roomId = roomId;
|
149 | this.name = roomId;
|
150 | this.tags = {
|
151 |
|
152 | };
|
153 | this.accountData = {
|
154 | };
|
155 | this.summary = null;
|
156 | this.storageToken = opts.storageToken;
|
157 | this._opts = opts;
|
158 | this._txnToEvent = {};
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | this._receipts = {
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | };
|
171 | this._receiptCacheByEventId = {
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | };
|
177 |
|
178 | this._realReceipts = {};
|
179 | this._notificationCounts = {};
|
180 |
|
181 |
|
182 | this._timelineSets = [new _eventTimelineSet.EventTimelineSet(this, opts)];
|
183 | this.reEmitter.reEmit(this.getUnfilteredTimelineSet(), ["Room.timeline", "Room.timelineReset"]);
|
184 |
|
185 | this._fixUpLegacyTimelineFields();
|
186 |
|
187 |
|
188 | this._filteredTimelineSets = {
|
189 | };
|
190 |
|
191 | if (this._opts.pendingEventOrdering == "detached") {
|
192 | this._pendingEventList = [];
|
193 | }
|
194 |
|
195 |
|
196 | this._blacklistUnverifiedDevices = null;
|
197 | this._selfMembership = null;
|
198 | this._summaryHeroes = null;
|
199 |
|
200 | this._client = client;
|
201 |
|
202 | if (!this._opts.lazyLoadMembers) {
|
203 | this._membersPromise = Promise.resolve();
|
204 | } else {
|
205 | this._membersPromise = null;
|
206 | }
|
207 | }
|
208 |
|
209 | utils.inherits(Room, _events.EventEmitter);
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | Room.prototype.getVersion = function () {
|
216 | const createEvent = this.currentState.getStateEvents("m.room.create", "");
|
217 |
|
218 | if (!createEvent) {
|
219 | _logger.logger.warn("Room " + this.room_id + " does not have an m.room.create event");
|
220 |
|
221 | return '1';
|
222 | }
|
223 |
|
224 | const ver = createEvent.getContent()['room_version'];
|
225 | if (ver === undefined) return '1';
|
226 | return ver;
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | Room.prototype.shouldUpgradeToVersion = function () {
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | if (!SAFE_ROOM_VERSIONS.includes(this.getVersion())) {
|
244 | return KNOWN_SAFE_ROOM_VERSION;
|
245 | }
|
246 |
|
247 | return null;
|
248 | };
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | Room.prototype.getRecommendedVersion = async function () {
|
263 | const capabilities = await this._client.getCapabilities();
|
264 | let versionCap = capabilities["m.room_versions"];
|
265 |
|
266 | if (!versionCap) {
|
267 | versionCap = {
|
268 | default: KNOWN_SAFE_ROOM_VERSION,
|
269 | available: {}
|
270 | };
|
271 |
|
272 | for (const safeVer of SAFE_ROOM_VERSIONS) {
|
273 | versionCap.available[safeVer] = "stable";
|
274 | }
|
275 | }
|
276 |
|
277 | let result = this._checkVersionAgainstCapability(versionCap);
|
278 |
|
279 | if (result.urgent && result.needsUpgrade) {
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | _logger.logger.warn("Refreshing room version capability because the server looks " + "to be supporting a newer room version we don't know about.");
|
287 |
|
288 | const caps = await this._client.getCapabilities(true);
|
289 | versionCap = caps["m.room_versions"];
|
290 |
|
291 | if (!versionCap) {
|
292 | _logger.logger.warn("No room version capability - assuming upgrade required.");
|
293 |
|
294 | return result;
|
295 | } else {
|
296 | result = this._checkVersionAgainstCapability(versionCap);
|
297 | }
|
298 | }
|
299 |
|
300 | return result;
|
301 | };
|
302 |
|
303 | Room.prototype._checkVersionAgainstCapability = function (versionCap) {
|
304 | const currentVersion = this.getVersion();
|
305 |
|
306 | _logger.logger.log(`[${this.roomId}] Current version: ${currentVersion}`);
|
307 |
|
308 | _logger.logger.log(`[${this.roomId}] Version capability: `, versionCap);
|
309 |
|
310 | const result = {
|
311 | version: currentVersion,
|
312 | needsUpgrade: false,
|
313 | urgent: false
|
314 | };
|
315 |
|
316 | if (currentVersion === versionCap.default) return result;
|
317 | const stableVersions = Object.keys(versionCap.available).filter(v => versionCap.available[v] === 'stable');
|
318 |
|
319 |
|
320 |
|
321 | if (!stableVersions.includes(currentVersion)) {
|
322 | result.version = versionCap.default;
|
323 | result.needsUpgrade = true;
|
324 | result.urgent = !!this.getVersion().match(/^[0-9]+[0-9.]*$/g);
|
325 |
|
326 | if (result.urgent) {
|
327 | _logger.logger.warn(`URGENT upgrade required on ${this.roomId}`);
|
328 | } else {
|
329 | _logger.logger.warn(`Non-urgent upgrade required on ${this.roomId}`);
|
330 | }
|
331 |
|
332 | return result;
|
333 | }
|
334 |
|
335 |
|
336 |
|
337 | return result;
|
338 | };
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | Room.prototype.userMayUpgradeRoom = function (userId) {
|
347 | return this.currentState.maySendStateEvent("m.room.tombstone", userId);
|
348 | };
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | Room.prototype.getPendingEvents = function () {
|
360 | if (this._opts.pendingEventOrdering !== "detached") {
|
361 | throw new Error("Cannot call getPendingEvents with pendingEventOrdering == " + this._opts.pendingEventOrdering);
|
362 | }
|
363 |
|
364 | return this._pendingEventList;
|
365 | };
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | Room.prototype.hasPendingEvent = function (eventId) {
|
376 | if (this._opts.pendingEventOrdering !== "detached") {
|
377 | throw new Error("Cannot call hasPendingEvent with pendingEventOrdering == " + this._opts.pendingEventOrdering);
|
378 | }
|
379 |
|
380 | return this._pendingEventList.some(event => event.getId() === eventId);
|
381 | };
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | Room.prototype.getLiveTimeline = function () {
|
390 | return this.getUnfilteredTimelineSet().getLiveTimeline();
|
391 | };
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | Room.prototype.getLastActiveTimestamp = function () {
|
400 | const timeline = this.getLiveTimeline();
|
401 | const events = timeline.getEvents();
|
402 |
|
403 | if (events.length) {
|
404 | const lastEvent = events[events.length - 1];
|
405 | return lastEvent.getTs();
|
406 | } else {
|
407 | return Number.MIN_SAFE_INTEGER;
|
408 | }
|
409 | };
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 | Room.prototype.getMyMembership = function () {
|
417 | return this._selfMembership;
|
418 | };
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | Room.prototype.getDMInviter = function () {
|
427 | if (this.myUserId) {
|
428 | const me = this.getMember(this.myUserId);
|
429 |
|
430 | if (me) {
|
431 | return me.getDMInviter();
|
432 | }
|
433 | }
|
434 |
|
435 | if (this._selfMembership === "invite") {
|
436 |
|
437 | const memberCount = this.getInvitedAndJoinedMemberCount();
|
438 |
|
439 | if (memberCount == 2 && this._summaryHeroes.length) {
|
440 | return this._summaryHeroes[0];
|
441 | }
|
442 | }
|
443 | };
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | Room.prototype.guessDMUserId = function () {
|
451 | const me = this.getMember(this.myUserId);
|
452 |
|
453 | if (me) {
|
454 | const inviterId = me.getDMInviter();
|
455 |
|
456 | if (inviterId) {
|
457 | return inviterId;
|
458 | }
|
459 | }
|
460 |
|
461 |
|
462 |
|
463 | const hasHeroes = Array.isArray(this._summaryHeroes) && this._summaryHeroes.length;
|
464 |
|
465 | if (hasHeroes) {
|
466 | return this._summaryHeroes[0];
|
467 | }
|
468 |
|
469 | const members = this.currentState.getMembers();
|
470 | const anyMember = members.find(m => m.userId !== this.myUserId);
|
471 |
|
472 | if (anyMember) {
|
473 | return anyMember.userId;
|
474 | }
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | return this.myUserId;
|
480 | };
|
481 |
|
482 | Room.prototype.getAvatarFallbackMember = function () {
|
483 | const memberCount = this.getInvitedAndJoinedMemberCount();
|
484 |
|
485 | if (memberCount > 2) {
|
486 | return;
|
487 | }
|
488 |
|
489 | const hasHeroes = Array.isArray(this._summaryHeroes) && this._summaryHeroes.length;
|
490 |
|
491 | if (hasHeroes) {
|
492 | const availableMember = this._summaryHeroes.map(userId => {
|
493 | return this.getMember(userId);
|
494 | }).find(member => !!member);
|
495 |
|
496 | if (availableMember) {
|
497 | return availableMember;
|
498 | }
|
499 | }
|
500 |
|
501 | const members = this.currentState.getMembers();
|
502 |
|
503 |
|
504 | if (members.length <= 2) {
|
505 | const availableMember = members.find(m => {
|
506 | return m.userId !== this.myUserId;
|
507 | });
|
508 |
|
509 | if (availableMember) {
|
510 | return availableMember;
|
511 | }
|
512 | }
|
513 |
|
514 |
|
515 |
|
516 | if (hasHeroes) {
|
517 | const availableUser = this._summaryHeroes.map(userId => {
|
518 | return this._client.getUser(userId);
|
519 | }).find(user => !!user);
|
520 |
|
521 | if (availableUser) {
|
522 | const member = new _roomMember.RoomMember(this.roomId, availableUser.userId);
|
523 | member.user = availableUser;
|
524 | return member;
|
525 | }
|
526 | }
|
527 | };
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 | Room.prototype.updateMyMembership = function (membership) {
|
535 | const prevMembership = this._selfMembership;
|
536 | this._selfMembership = membership;
|
537 |
|
538 | if (prevMembership !== membership) {
|
539 | if (membership === "leave") {
|
540 | this._cleanupAfterLeaving();
|
541 | }
|
542 |
|
543 | this.emit("Room.myMembership", this, membership, prevMembership);
|
544 | }
|
545 | };
|
546 |
|
547 | Room.prototype._loadMembersFromServer = async function () {
|
548 | const lastSyncToken = this._client.store.getSyncToken();
|
549 |
|
550 | const queryString = utils.encodeParams({
|
551 | not_membership: "leave",
|
552 | at: lastSyncToken
|
553 | });
|
554 | const path = utils.encodeUri("/rooms/$roomId/members?" + queryString, {
|
555 | $roomId: this.roomId
|
556 | });
|
557 | const http = this._client._http;
|
558 | const response = await http.authedRequest(undefined, "GET", path);
|
559 | return response.chunk;
|
560 | };
|
561 |
|
562 | Room.prototype._loadMembers = async function () {
|
563 |
|
564 | let fromServer = false;
|
565 | let rawMembersEvents = await this._client.store.getOutOfBandMembers(this.roomId);
|
566 |
|
567 | if (rawMembersEvents === null) {
|
568 | fromServer = true;
|
569 | rawMembersEvents = await this._loadMembersFromServer();
|
570 |
|
571 | _logger.logger.log(`LL: got ${rawMembersEvents.length} ` + `members from server for room ${this.roomId}`);
|
572 | }
|
573 |
|
574 | const memberEvents = rawMembersEvents.map(this._client.getEventMapper());
|
575 | return {
|
576 | memberEvents,
|
577 | fromServer
|
578 | };
|
579 | };
|
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 | Room.prototype.loadMembersIfNeeded = function () {
|
591 | if (this._membersPromise) {
|
592 | return this._membersPromise;
|
593 | }
|
594 |
|
595 |
|
596 |
|
597 |
|
598 | this.currentState.markOutOfBandMembersStarted();
|
599 |
|
600 | const inMemoryUpdate = this._loadMembers().then(result => {
|
601 | this.currentState.setOutOfBandMembers(result.memberEvents);
|
602 |
|
603 | if (this._client.isCryptoEnabled() && this._client.isRoomEncrypted(this.roomId)) {
|
604 | this._client._crypto.trackRoomDevices(this.roomId);
|
605 | }
|
606 |
|
607 | return result.fromServer;
|
608 | }).catch(err => {
|
609 |
|
610 | this._membersPromise = null;
|
611 | this.currentState.markOutOfBandMembersFailed();
|
612 | throw err;
|
613 | });
|
614 |
|
615 |
|
616 | inMemoryUpdate.then(fromServer => {
|
617 | if (fromServer) {
|
618 | const oobMembers = this.currentState.getMembers().filter(m => m.isOutOfBand()).map(m => m.events.member.event);
|
619 |
|
620 | _logger.logger.log(`LL: telling store to write ${oobMembers.length}` + ` members for room ${this.roomId}`);
|
621 |
|
622 | const store = this._client.store;
|
623 | return store.setOutOfBandMembers(this.roomId, oobMembers)
|
624 |
|
625 | .catch(err => {
|
626 | _logger.logger.log("LL: storing OOB room members failed, oh well", err);
|
627 | });
|
628 | }
|
629 | }).catch(err => {
|
630 |
|
631 |
|
632 | _logger.logger.error(err);
|
633 | });
|
634 | this._membersPromise = inMemoryUpdate;
|
635 | return this._membersPromise;
|
636 | };
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | Room.prototype.clearLoadedMembersIfNeeded = async function () {
|
643 | if (this._opts.lazyLoadMembers && this._membersPromise) {
|
644 | await this.loadMembersIfNeeded();
|
645 | await this._client.store.clearOutOfBandMembers(this.roomId);
|
646 | this.currentState.clearOutOfBandMembers();
|
647 | this._membersPromise = null;
|
648 | }
|
649 | };
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 | Room.prototype._cleanupAfterLeaving = function () {
|
657 | this.clearLoadedMembersIfNeeded().catch(err => {
|
658 | _logger.logger.error(`error after clearing loaded members from ` + `room ${this.roomId} after leaving`);
|
659 |
|
660 | _logger.logger.log(err);
|
661 | });
|
662 | };
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 | Room.prototype.resetLiveTimeline = function (backPaginationToken, forwardPaginationToken) {
|
677 | for (let i = 0; i < this._timelineSets.length; i++) {
|
678 | this._timelineSets[i].resetLiveTimeline(backPaginationToken, forwardPaginationToken);
|
679 | }
|
680 |
|
681 | this._fixUpLegacyTimelineFields();
|
682 | };
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 | Room.prototype._fixUpLegacyTimelineFields = function () {
|
691 |
|
692 |
|
693 |
|
694 |
|
695 | this.timeline = this.getLiveTimeline().getEvents();
|
696 | this.oldState = this.getLiveTimeline().getState(_eventTimeline.EventTimeline.BACKWARDS);
|
697 | this.currentState = this.getLiveTimeline().getState(_eventTimeline.EventTimeline.FORWARDS);
|
698 | };
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 | Room.prototype.hasUnverifiedDevices = async function () {
|
711 | if (!this._client.isRoomEncrypted(this.roomId)) {
|
712 | return false;
|
713 | }
|
714 |
|
715 | const e2eMembers = await this.getEncryptionTargetMembers();
|
716 |
|
717 | for (const member of e2eMembers) {
|
718 | const devices = await this._client.getStoredDevicesForUser(member.userId);
|
719 |
|
720 | if (devices.some(device => device.isUnverified())) {
|
721 | return true;
|
722 | }
|
723 | }
|
724 |
|
725 | return false;
|
726 | };
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 | Room.prototype.getTimelineSets = function () {
|
734 | return this._timelineSets;
|
735 | };
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 | Room.prototype.getUnfilteredTimelineSet = function () {
|
743 | return this._timelineSets[0];
|
744 | };
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 | Room.prototype.getTimelineForEvent = function (eventId) {
|
755 | return this.getUnfilteredTimelineSet().getTimelineForEvent(eventId);
|
756 | };
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 | Room.prototype.addTimeline = function () {
|
765 | return this.getUnfilteredTimelineSet().addTimeline();
|
766 | };
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 | Room.prototype.findEventById = function (eventId) {
|
776 | return this.getUnfilteredTimelineSet().findEventById(eventId);
|
777 | };
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | Room.prototype.getUnreadNotificationCount = function (type) {
|
787 | type = type || 'total';
|
788 | return this._notificationCounts[type];
|
789 | };
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 | Room.prototype.setUnreadNotificationCount = function (type, count) {
|
798 | this._notificationCounts[type] = count;
|
799 | };
|
800 |
|
801 | Room.prototype.setSummary = function (summary) {
|
802 | const heroes = summary["m.heroes"];
|
803 | const joinedCount = summary["m.joined_member_count"];
|
804 | const invitedCount = summary["m.invited_member_count"];
|
805 |
|
806 | if (Number.isInteger(joinedCount)) {
|
807 | this.currentState.setJoinedMemberCount(joinedCount);
|
808 | }
|
809 |
|
810 | if (Number.isInteger(invitedCount)) {
|
811 | this.currentState.setInvitedMemberCount(invitedCount);
|
812 | }
|
813 |
|
814 | if (Array.isArray(heroes)) {
|
815 |
|
816 |
|
817 |
|
818 | this._summaryHeroes = heroes.filter(userId => {
|
819 | return userId !== this.myUserId;
|
820 | });
|
821 | }
|
822 | };
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 | Room.prototype.setBlacklistUnverifiedDevices = function (value) {
|
831 | this._blacklistUnverifiedDevices = value;
|
832 | };
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 | Room.prototype.getBlacklistUnverifiedDevices = function () {
|
841 | return this._blacklistUnverifiedDevices;
|
842 | };
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 | Room.prototype.getAvatarUrl = function (baseUrl, width, height, resizeMethod, allowDefault) {
|
858 | const roomAvatarEvent = this.currentState.getStateEvents("m.room.avatar", "");
|
859 |
|
860 | if (allowDefault === undefined) {
|
861 | allowDefault = true;
|
862 | }
|
863 |
|
864 | if (!roomAvatarEvent && !allowDefault) {
|
865 | return null;
|
866 | }
|
867 |
|
868 | const mainUrl = roomAvatarEvent ? roomAvatarEvent.getContent().url : null;
|
869 |
|
870 | if (mainUrl) {
|
871 | return (0, _contentRepo.getHttpUriForMxc)(baseUrl, mainUrl, width, height, resizeMethod);
|
872 | } else if (allowDefault) {
|
873 | return (0, _contentRepo.getIdenticonUri)(baseUrl, this.roomId, width, height);
|
874 | }
|
875 |
|
876 | return null;
|
877 | };
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 | Room.prototype.getAliases = function () {
|
887 | const aliasStrings = [];
|
888 | const aliasEvents = this.currentState.getStateEvents("m.room.aliases");
|
889 |
|
890 | if (aliasEvents) {
|
891 | for (let i = 0; i < aliasEvents.length; ++i) {
|
892 | const aliasEvent = aliasEvents[i];
|
893 |
|
894 | if (utils.isArray(aliasEvent.getContent().aliases)) {
|
895 | const filteredAliases = aliasEvent.getContent().aliases.filter(a => {
|
896 | if (typeof a !== "string") return false;
|
897 | if (a[0] !== '#') return false;
|
898 | if (!a.endsWith(`:${aliasEvent.getStateKey()}`)) return false;
|
899 |
|
900 | return true;
|
901 | });
|
902 | Array.prototype.push.apply(aliasStrings, filteredAliases);
|
903 | }
|
904 | }
|
905 | }
|
906 |
|
907 | return aliasStrings;
|
908 | };
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 | Room.prototype.getCanonicalAlias = function () {
|
918 | const canonicalAlias = this.currentState.getStateEvents("m.room.canonical_alias", "");
|
919 |
|
920 | if (canonicalAlias) {
|
921 | return canonicalAlias.getContent().alias;
|
922 | }
|
923 |
|
924 | return null;
|
925 | };
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 | Room.prototype.addEventsToTimeline = function (events, toStartOfTimeline, timeline, paginationToken) {
|
948 | timeline.getTimelineSet().addEventsToTimeline(events, toStartOfTimeline, timeline, paginationToken);
|
949 | };
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 | Room.prototype.getMember = function (userId) {
|
958 | return this.currentState.getMember(userId);
|
959 | };
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 | Room.prototype.getJoinedMembers = function () {
|
967 | return this.getMembersWithMembership("join");
|
968 | };
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 | Room.prototype.getJoinedMemberCount = function () {
|
979 | return this.currentState.getJoinedMemberCount();
|
980 | };
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 | Room.prototype.getInvitedMemberCount = function () {
|
988 | return this.currentState.getInvitedMemberCount();
|
989 | };
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 | Room.prototype.getInvitedAndJoinedMemberCount = function () {
|
997 | return this.getInvitedMemberCount() + this.getJoinedMemberCount();
|
998 | };
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 | Room.prototype.getMembersWithMembership = function (membership) {
|
1007 | return utils.filter(this.currentState.getMembers(), function (m) {
|
1008 | return m.membership === membership;
|
1009 | });
|
1010 | };
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 | Room.prototype.getEncryptionTargetMembers = async function () {
|
1019 | await this.loadMembersIfNeeded();
|
1020 | let members = this.getMembersWithMembership("join");
|
1021 |
|
1022 | if (this.shouldEncryptForInvitedMembers()) {
|
1023 | members = members.concat(this.getMembersWithMembership("invite"));
|
1024 | }
|
1025 |
|
1026 | return members;
|
1027 | };
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 | Room.prototype.shouldEncryptForInvitedMembers = function () {
|
1035 | const ev = this.currentState.getStateEvents("m.room.history_visibility", "");
|
1036 | return ev && ev.getContent() && ev.getContent().history_visibility !== "joined";
|
1037 | };
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 | Room.prototype.getDefaultRoomName = function (userId) {
|
1048 | return calculateRoomName(this, userId, true);
|
1049 | };
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 | Room.prototype.hasMembershipState = function (userId, membership) {
|
1059 | const member = this.getMember(userId);
|
1060 |
|
1061 | if (!member) {
|
1062 | return false;
|
1063 | }
|
1064 |
|
1065 | return member.membership === membership;
|
1066 | };
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 | Room.prototype.getOrCreateFilteredTimelineSet = function (filter) {
|
1075 | if (this._filteredTimelineSets[filter.filterId]) {
|
1076 | return this._filteredTimelineSets[filter.filterId];
|
1077 | }
|
1078 |
|
1079 | const opts = Object.assign({
|
1080 | filter: filter
|
1081 | }, this._opts);
|
1082 | const timelineSet = new _eventTimelineSet.EventTimelineSet(this, opts);
|
1083 | this.reEmitter.reEmit(timelineSet, ["Room.timeline", "Room.timelineReset"]);
|
1084 | this._filteredTimelineSets[filter.filterId] = timelineSet;
|
1085 |
|
1086 | this._timelineSets.push(timelineSet);
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 | const unfilteredLiveTimeline = this.getLiveTimeline();
|
1095 | unfilteredLiveTimeline.getEvents().forEach(function (event) {
|
1096 | timelineSet.addLiveEvent(event);
|
1097 | });
|
1098 |
|
1099 | let timeline = unfilteredLiveTimeline;
|
1100 |
|
1101 | while (timeline.getNeighbouringTimeline(_eventTimeline.EventTimeline.BACKWARDS)) {
|
1102 | timeline = timeline.getNeighbouringTimeline(_eventTimeline.EventTimeline.BACKWARDS);
|
1103 | }
|
1104 |
|
1105 | timelineSet.getLiveTimeline().setPaginationToken(timeline.getPaginationToken(_eventTimeline.EventTimeline.BACKWARDS), _eventTimeline.EventTimeline.BACKWARDS);
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 | return timelineSet;
|
1114 | };
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 | Room.prototype.removeFilteredTimelineSet = function (filter) {
|
1123 | const timelineSet = this._filteredTimelineSets[filter.filterId];
|
1124 | delete this._filteredTimelineSets[filter.filterId];
|
1125 |
|
1126 | const i = this._timelineSets.indexOf(timelineSet);
|
1127 |
|
1128 | if (i > -1) {
|
1129 | this._timelineSets.splice(i, 1);
|
1130 | }
|
1131 | };
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 | Room.prototype._addLiveEvent = function (event, duplicateStrategy) {
|
1144 | if (event.isRedaction()) {
|
1145 | const redactId = event.event.redacts;
|
1146 |
|
1147 | const redactedEvent = this.getUnfilteredTimelineSet().findEventById(redactId);
|
1148 |
|
1149 | if (redactedEvent) {
|
1150 | redactedEvent.makeRedacted(event);
|
1151 |
|
1152 | if (redactedEvent.getStateKey()) {
|
1153 | const currentStateEvent = this.currentState.getStateEvents(redactedEvent.getType(), redactedEvent.getStateKey());
|
1154 |
|
1155 | if (currentStateEvent.getId() === redactedEvent.getId()) {
|
1156 | this.currentState.setStateEvents([redactedEvent]);
|
1157 | }
|
1158 | }
|
1159 |
|
1160 | this.emit("Room.redaction", event, this);
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 | }
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 | }
|
1171 |
|
1172 | if (event.getUnsigned().transaction_id) {
|
1173 | const existingEvent = this._txnToEvent[event.getUnsigned().transaction_id];
|
1174 |
|
1175 | if (existingEvent) {
|
1176 |
|
1177 | this._handleRemoteEcho(event, existingEvent);
|
1178 |
|
1179 | return;
|
1180 | }
|
1181 | }
|
1182 |
|
1183 |
|
1184 | for (let i = 0; i < this._timelineSets.length; i++) {
|
1185 | this._timelineSets[i].addLiveEvent(event, duplicateStrategy);
|
1186 | }
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 | if (event.sender && event.getType() !== "m.room.redaction") {
|
1193 | this.addReceipt(synthesizeReceipt(event.sender.userId, event, "m.read"), true);
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 | }
|
1200 | };
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 | Room.prototype.addPendingEvent = function (event, txnId) {
|
1221 | if (event.status !== _event.EventStatus.SENDING) {
|
1222 | throw new Error("addPendingEvent called on an event with status " + event.status);
|
1223 | }
|
1224 |
|
1225 | if (this._txnToEvent[txnId]) {
|
1226 | throw new Error("addPendingEvent called on an event with known txnId " + txnId);
|
1227 | }
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 | _eventTimeline.EventTimeline.setEventMetadata(event, this.getLiveTimeline().getState(_eventTimeline.EventTimeline.FORWARDS), false);
|
1233 |
|
1234 | this._txnToEvent[txnId] = event;
|
1235 |
|
1236 | if (this._opts.pendingEventOrdering == "detached") {
|
1237 | if (this._pendingEventList.some(e => e.status === _event.EventStatus.NOT_SENT)) {
|
1238 | _logger.logger.warn("Setting event as NOT_SENT due to messages in the same state");
|
1239 |
|
1240 | event.setStatus(_event.EventStatus.NOT_SENT);
|
1241 | }
|
1242 |
|
1243 | this._pendingEventList.push(event);
|
1244 |
|
1245 | if (event.isRelation()) {
|
1246 |
|
1247 |
|
1248 |
|
1249 | this._aggregateNonLiveRelation(event);
|
1250 | }
|
1251 |
|
1252 | if (event.isRedaction()) {
|
1253 | const redactId = event.event.redacts;
|
1254 |
|
1255 | let redactedEvent = this._pendingEventList && this._pendingEventList.find(e => e.getId() === redactId);
|
1256 |
|
1257 | if (!redactedEvent) {
|
1258 | redactedEvent = this.getUnfilteredTimelineSet().findEventById(redactId);
|
1259 | }
|
1260 |
|
1261 | if (redactedEvent) {
|
1262 | redactedEvent.markLocallyRedacted(event);
|
1263 | this.emit("Room.redaction", event, this);
|
1264 | }
|
1265 | }
|
1266 | } else {
|
1267 | for (let i = 0; i < this._timelineSets.length; i++) {
|
1268 | const timelineSet = this._timelineSets[i];
|
1269 |
|
1270 | if (timelineSet.getFilter()) {
|
1271 | if (timelineSet.getFilter().filterRoomTimeline([event]).length) {
|
1272 | timelineSet.addEventToTimeline(event, timelineSet.getLiveTimeline(), false);
|
1273 | }
|
1274 | } else {
|
1275 | timelineSet.addEventToTimeline(event, timelineSet.getLiveTimeline(), false);
|
1276 | }
|
1277 | }
|
1278 | }
|
1279 |
|
1280 | this.emit("Room.localEchoUpdated", event, this, null, null);
|
1281 | };
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 | Room.prototype._aggregateNonLiveRelation = function (event) {
|
1295 |
|
1296 |
|
1297 | for (let i = 0; i < this._timelineSets.length; i++) {
|
1298 | const timelineSet = this._timelineSets[i];
|
1299 |
|
1300 | if (timelineSet.getFilter()) {
|
1301 | if (timelineSet.getFilter().filterRoomTimeline([event]).length) {
|
1302 | timelineSet.aggregateRelations(event);
|
1303 | }
|
1304 | } else {
|
1305 | timelineSet.aggregateRelations(event);
|
1306 | }
|
1307 | }
|
1308 | };
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 |
|
1322 |
|
1323 |
|
1324 |
|
1325 | Room.prototype._handleRemoteEcho = function (remoteEvent, localEvent) {
|
1326 | const oldEventId = localEvent.getId();
|
1327 | const newEventId = remoteEvent.getId();
|
1328 | const oldStatus = localEvent.status;
|
1329 |
|
1330 | delete this._txnToEvent[remoteEvent.getUnsigned().transaction_id];
|
1331 |
|
1332 | if (this._pendingEventList) {
|
1333 | utils.removeElement(this._pendingEventList, function (ev) {
|
1334 | return ev.getId() == oldEventId;
|
1335 | }, false);
|
1336 | }
|
1337 |
|
1338 |
|
1339 |
|
1340 | localEvent.handleRemoteEcho(remoteEvent.event);
|
1341 |
|
1342 | for (let i = 0; i < this._timelineSets.length; i++) {
|
1343 | const timelineSet = this._timelineSets[i];
|
1344 |
|
1345 | timelineSet.handleRemoteEcho(localEvent, oldEventId, newEventId);
|
1346 | }
|
1347 |
|
1348 | this.emit("Room.localEchoUpdated", localEvent, this, oldEventId, oldStatus);
|
1349 | };
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 | const ALLOWED_TRANSITIONS = {};
|
1355 | ALLOWED_TRANSITIONS[_event.EventStatus.ENCRYPTING] = [_event.EventStatus.SENDING, _event.EventStatus.NOT_SENT];
|
1356 | ALLOWED_TRANSITIONS[_event.EventStatus.SENDING] = [_event.EventStatus.ENCRYPTING, _event.EventStatus.QUEUED, _event.EventStatus.NOT_SENT, _event.EventStatus.SENT];
|
1357 | ALLOWED_TRANSITIONS[_event.EventStatus.QUEUED] = [_event.EventStatus.SENDING, _event.EventStatus.CANCELLED];
|
1358 | ALLOWED_TRANSITIONS[_event.EventStatus.SENT] = [];
|
1359 | ALLOWED_TRANSITIONS[_event.EventStatus.NOT_SENT] = [_event.EventStatus.SENDING, _event.EventStatus.QUEUED, _event.EventStatus.CANCELLED];
|
1360 | ALLOWED_TRANSITIONS[_event.EventStatus.CANCELLED] = [];
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 |
|
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 | Room.prototype.updatePendingEvent = function (event, newStatus, newEventId) {
|
1375 | _logger.logger.log(`setting pendingEvent status to ${newStatus} in ${event.getRoomId()}`);
|
1376 |
|
1377 |
|
1378 | if (newStatus == _event.EventStatus.SENT && !newEventId) {
|
1379 | throw new Error("updatePendingEvent called with status=SENT, " + "but no new event id");
|
1380 | }
|
1381 |
|
1382 |
|
1383 | if (newStatus == _event.EventStatus.SENT) {
|
1384 | const timeline = this.getUnfilteredTimelineSet().eventIdToTimeline(newEventId);
|
1385 |
|
1386 | if (timeline) {
|
1387 |
|
1388 |
|
1389 | return;
|
1390 | }
|
1391 | }
|
1392 |
|
1393 | const oldStatus = event.status;
|
1394 | const oldEventId = event.getId();
|
1395 |
|
1396 | if (!oldStatus) {
|
1397 | throw new Error("updatePendingEventStatus called on an event which is " + "not a local echo.");
|
1398 | }
|
1399 |
|
1400 | const allowed = ALLOWED_TRANSITIONS[oldStatus];
|
1401 |
|
1402 | if (!allowed || allowed.indexOf(newStatus) < 0) {
|
1403 | throw new Error("Invalid EventStatus transition " + oldStatus + "->" + newStatus);
|
1404 | }
|
1405 |
|
1406 | event.setStatus(newStatus);
|
1407 |
|
1408 | if (newStatus == _event.EventStatus.SENT) {
|
1409 |
|
1410 | event.replaceLocalEventId(newEventId);
|
1411 |
|
1412 |
|
1413 |
|
1414 | for (let i = 0; i < this._timelineSets.length; i++) {
|
1415 | this._timelineSets[i].replaceEventId(oldEventId, newEventId);
|
1416 | }
|
1417 | } else if (newStatus == _event.EventStatus.CANCELLED) {
|
1418 |
|
1419 | if (this._pendingEventList) {
|
1420 | const idx = this._pendingEventList.findIndex(ev => ev.getId() === oldEventId);
|
1421 |
|
1422 | if (idx !== -1) {
|
1423 | const [removedEvent] = this._pendingEventList.splice(idx, 1);
|
1424 |
|
1425 | if (removedEvent.isRedaction()) {
|
1426 | this._revertRedactionLocalEcho(removedEvent);
|
1427 | }
|
1428 | }
|
1429 | }
|
1430 |
|
1431 | this.removeEvent(oldEventId);
|
1432 | }
|
1433 |
|
1434 | this.emit("Room.localEchoUpdated", event, this, oldEventId, oldStatus);
|
1435 | };
|
1436 |
|
1437 | Room.prototype._revertRedactionLocalEcho = function (redactionEvent) {
|
1438 | const redactId = redactionEvent.event.redacts;
|
1439 |
|
1440 | if (!redactId) {
|
1441 | return;
|
1442 | }
|
1443 |
|
1444 | const redactedEvent = this.getUnfilteredTimelineSet().findEventById(redactId);
|
1445 |
|
1446 | if (redactedEvent) {
|
1447 | redactedEvent.unmarkLocallyRedacted();
|
1448 |
|
1449 | this.emit("Room.redactionCancelled", redactionEvent, this);
|
1450 |
|
1451 | if (redactedEvent.isRelation()) {
|
1452 | this._aggregateNonLiveRelation(redactedEvent);
|
1453 | }
|
1454 | }
|
1455 | };
|
1456 |
|
1457 |
|
1458 |
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 |
|
1474 | Room.prototype.addLiveEvents = function (events, duplicateStrategy) {
|
1475 | let i;
|
1476 |
|
1477 | if (duplicateStrategy && ["replace", "ignore"].indexOf(duplicateStrategy) === -1) {
|
1478 | throw new Error("duplicateStrategy MUST be either 'replace' or 'ignore'");
|
1479 | }
|
1480 |
|
1481 |
|
1482 | for (i = 0; i < this._timelineSets.length; i++) {
|
1483 | const liveTimeline = this._timelineSets[i].getLiveTimeline();
|
1484 |
|
1485 | if (liveTimeline.getPaginationToken(_eventTimeline.EventTimeline.FORWARDS)) {
|
1486 | throw new Error("live timeline " + i + " is no longer live - it has a pagination token " + "(" + liveTimeline.getPaginationToken(_eventTimeline.EventTimeline.FORWARDS) + ")");
|
1487 | }
|
1488 |
|
1489 | if (liveTimeline.getNeighbouringTimeline(_eventTimeline.EventTimeline.FORWARDS)) {
|
1490 | throw new Error("live timeline " + i + " is no longer live - " + "it has a neighbouring timeline");
|
1491 | }
|
1492 | }
|
1493 |
|
1494 | for (i = 0; i < events.length; i++) {
|
1495 |
|
1496 |
|
1497 | this._addLiveEvent(events[i], duplicateStrategy);
|
1498 | }
|
1499 | };
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 | Room.prototype.addEphemeralEvents = function (events) {
|
1507 | for (const event of events) {
|
1508 | if (event.getType() === 'm.typing') {
|
1509 | this.currentState.setTypingEvent(event);
|
1510 | } else if (event.getType() === 'm.receipt') {
|
1511 | this.addReceipt(event);
|
1512 | }
|
1513 |
|
1514 | }
|
1515 | };
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 |
|
1521 |
|
1522 | Room.prototype.removeEvents = function (eventIds) {
|
1523 | for (let i = 0; i < eventIds.length; ++i) {
|
1524 | this.removeEvent(eventIds[i]);
|
1525 | }
|
1526 | };
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 | Room.prototype.removeEvent = function (eventId) {
|
1537 | let removedAny = false;
|
1538 |
|
1539 | for (let i = 0; i < this._timelineSets.length; i++) {
|
1540 | const removed = this._timelineSets[i].removeEvent(eventId);
|
1541 |
|
1542 | if (removed) {
|
1543 | if (removed.isRedaction()) {
|
1544 | this._revertRedactionLocalEcho(removed);
|
1545 | }
|
1546 |
|
1547 | removedAny = true;
|
1548 | }
|
1549 | }
|
1550 |
|
1551 | return removedAny;
|
1552 | };
|
1553 |
|
1554 |
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 |
|
1561 | Room.prototype.recalculate = function () {
|
1562 |
|
1563 |
|
1564 | const self = this;
|
1565 | const membershipEvent = this.currentState.getStateEvents("m.room.member", this.myUserId);
|
1566 |
|
1567 | if (membershipEvent && membershipEvent.getContent().membership === "invite") {
|
1568 | const strippedStateEvents = membershipEvent.event.invite_room_state || [];
|
1569 | utils.forEach(strippedStateEvents, function (strippedEvent) {
|
1570 | const existingEvent = self.currentState.getStateEvents(strippedEvent.type, strippedEvent.state_key);
|
1571 |
|
1572 | if (!existingEvent) {
|
1573 |
|
1574 | self.currentState.setStateEvents([new _event.MatrixEvent({
|
1575 | type: strippedEvent.type,
|
1576 | state_key: strippedEvent.state_key,
|
1577 | content: strippedEvent.content,
|
1578 | event_id: "$fake" + Date.now(),
|
1579 | room_id: self.roomId,
|
1580 | user_id: self.myUserId
|
1581 |
|
1582 | })]);
|
1583 | }
|
1584 | });
|
1585 | }
|
1586 |
|
1587 | const oldName = this.name;
|
1588 | this.name = calculateRoomName(this, this.myUserId);
|
1589 | this.summary = new _roomSummary.RoomSummary(this.roomId, {
|
1590 | title: this.name
|
1591 | });
|
1592 |
|
1593 | if (oldName !== this.name) {
|
1594 | this.emit("Room.name", this);
|
1595 | }
|
1596 | };
|
1597 |
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 |
|
1604 | Room.prototype.getUsersReadUpTo = function (event) {
|
1605 | return this.getReceiptsForEvent(event).filter(function (receipt) {
|
1606 | return receipt.type === "m.read";
|
1607 | }).map(function (receipt) {
|
1608 | return receipt.userId;
|
1609 | });
|
1610 | };
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 |
|
1618 |
|
1619 |
|
1620 |
|
1621 |
|
1622 | Room.prototype.getEventReadUpTo = function (userId, ignoreSynthesized) {
|
1623 | let receipts = this._receipts;
|
1624 |
|
1625 | if (ignoreSynthesized) {
|
1626 | receipts = this._realReceipts;
|
1627 | }
|
1628 |
|
1629 | if (receipts["m.read"] === undefined || receipts["m.read"][userId] === undefined) {
|
1630 | return null;
|
1631 | }
|
1632 |
|
1633 | return receipts["m.read"][userId].eventId;
|
1634 | };
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 |
|
1641 |
|
1642 |
|
1643 |
|
1644 |
|
1645 | Room.prototype.hasUserReadEvent = function (userId, eventId) {
|
1646 | const readUpToId = this.getEventReadUpTo(userId, false);
|
1647 | if (readUpToId === eventId) return true;
|
1648 |
|
1649 | if (this.timeline.length && this.timeline[this.timeline.length - 1].getSender() && this.timeline[this.timeline.length - 1].getSender() === userId) {
|
1650 |
|
1651 |
|
1652 | return true;
|
1653 | }
|
1654 |
|
1655 | for (let i = this.timeline.length - 1; i >= 0; --i) {
|
1656 | const ev = this.timeline[i];
|
1657 |
|
1658 |
|
1659 |
|
1660 | if (ev.getId() === eventId) return false;
|
1661 | if (ev.getId() === readUpToId) return true;
|
1662 | }
|
1663 |
|
1664 |
|
1665 | return false;
|
1666 | };
|
1667 |
|
1668 |
|
1669 |
|
1670 |
|
1671 |
|
1672 |
|
1673 |
|
1674 |
|
1675 | Room.prototype.getReceiptsForEvent = function (event) {
|
1676 | return this._receiptCacheByEventId[event.getId()] || [];
|
1677 | };
|
1678 |
|
1679 |
|
1680 |
|
1681 |
|
1682 |
|
1683 |
|
1684 |
|
1685 | Room.prototype.addReceipt = function (event, fake) {
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 |
|
1693 |
|
1694 |
|
1695 |
|
1696 | if (fake === undefined) {
|
1697 | fake = false;
|
1698 | }
|
1699 |
|
1700 | if (!fake) {
|
1701 | this._addReceiptsToStructure(event, this._realReceipts);
|
1702 |
|
1703 |
|
1704 | }
|
1705 |
|
1706 | this._addReceiptsToStructure(event, this._receipts);
|
1707 |
|
1708 | this._receiptCacheByEventId = this._buildReceiptCache(this._receipts);
|
1709 |
|
1710 |
|
1711 | this.emit("Room.receipt", event, this);
|
1712 | };
|
1713 |
|
1714 |
|
1715 |
|
1716 |
|
1717 |
|
1718 |
|
1719 |
|
1720 | Room.prototype._addReceiptsToStructure = function (event, receipts) {
|
1721 | const self = this;
|
1722 | utils.keys(event.getContent()).forEach(function (eventId) {
|
1723 | utils.keys(event.getContent()[eventId]).forEach(function (receiptType) {
|
1724 | utils.keys(event.getContent()[eventId][receiptType]).forEach(function (userId) {
|
1725 | const receipt = event.getContent()[eventId][receiptType][userId];
|
1726 |
|
1727 | if (!receipts[receiptType]) {
|
1728 | receipts[receiptType] = {};
|
1729 | }
|
1730 |
|
1731 | const existingReceipt = receipts[receiptType][userId];
|
1732 |
|
1733 | if (!existingReceipt) {
|
1734 | receipts[receiptType][userId] = {};
|
1735 | } else {
|
1736 |
|
1737 |
|
1738 |
|
1739 |
|
1740 | const ordering = self.getUnfilteredTimelineSet().compareEventOrdering(existingReceipt.eventId, eventId);
|
1741 |
|
1742 | if (ordering !== null && ordering >= 0) {
|
1743 | return;
|
1744 | }
|
1745 | }
|
1746 |
|
1747 | receipts[receiptType][userId] = {
|
1748 | eventId: eventId,
|
1749 | data: receipt
|
1750 | };
|
1751 | });
|
1752 | });
|
1753 | });
|
1754 | };
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 |
|
1760 |
|
1761 |
|
1762 | Room.prototype._buildReceiptCache = function (receipts) {
|
1763 | const receiptCacheByEventId = {};
|
1764 | utils.keys(receipts).forEach(function (receiptType) {
|
1765 | utils.keys(receipts[receiptType]).forEach(function (userId) {
|
1766 | const receipt = receipts[receiptType][userId];
|
1767 |
|
1768 | if (!receiptCacheByEventId[receipt.eventId]) {
|
1769 | receiptCacheByEventId[receipt.eventId] = [];
|
1770 | }
|
1771 |
|
1772 | receiptCacheByEventId[receipt.eventId].push({
|
1773 | userId: userId,
|
1774 | type: receiptType,
|
1775 | data: receipt.data
|
1776 | });
|
1777 | });
|
1778 | });
|
1779 | return receiptCacheByEventId;
|
1780 | };
|
1781 |
|
1782 |
|
1783 |
|
1784 |
|
1785 |
|
1786 |
|
1787 |
|
1788 |
|
1789 |
|
1790 | Room.prototype._addLocalEchoReceipt = function (userId, e, receiptType) {
|
1791 | this.addReceipt(synthesizeReceipt(userId, e, receiptType), true);
|
1792 | };
|
1793 |
|
1794 |
|
1795 |
|
1796 |
|
1797 |
|
1798 |
|
1799 | Room.prototype.addTags = function (event) {
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 |
|
1808 | this.tags = event.getContent().tags || {};
|
1809 |
|
1810 |
|
1811 | this.emit("Room.tags", event, this);
|
1812 | };
|
1813 |
|
1814 |
|
1815 |
|
1816 |
|
1817 |
|
1818 |
|
1819 | Room.prototype.addAccountData = function (events) {
|
1820 | for (let i = 0; i < events.length; i++) {
|
1821 | const event = events[i];
|
1822 |
|
1823 | if (event.getType() === "m.tag") {
|
1824 | this.addTags(event);
|
1825 | }
|
1826 |
|
1827 | this.accountData[event.getType()] = event;
|
1828 | this.emit("Room.accountData", event, this);
|
1829 | }
|
1830 | };
|
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 | Room.prototype.getAccountData = function (type) {
|
1839 | return this.accountData[type];
|
1840 | };
|
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 |
|
1848 | Room.prototype.maySendMessage = function () {
|
1849 | return this.getMyMembership() === 'join' && this.currentState.maySendEvent('m.room.message', this.myUserId);
|
1850 | };
|
1851 |
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 |
|
1858 |
|
1859 |
|
1860 |
|
1861 |
|
1862 |
|
1863 | function calculateRoomName(room, userId, ignoreRoomNameEvent) {
|
1864 | if (!ignoreRoomNameEvent) {
|
1865 |
|
1866 |
|
1867 | const mRoomName = room.currentState.getStateEvents("m.room.name", "");
|
1868 |
|
1869 | if (mRoomName && mRoomName.getContent() && mRoomName.getContent().name) {
|
1870 | return mRoomName.getContent().name;
|
1871 | }
|
1872 | }
|
1873 |
|
1874 | let alias = room.getCanonicalAlias();
|
1875 |
|
1876 | if (!alias) {
|
1877 | const aliases = room.getAliases();
|
1878 |
|
1879 | if (aliases.length) {
|
1880 | alias = aliases[0];
|
1881 | }
|
1882 | }
|
1883 |
|
1884 | if (alias) {
|
1885 | return alias;
|
1886 | }
|
1887 |
|
1888 | const joinedMemberCount = room.currentState.getJoinedMemberCount();
|
1889 | const invitedMemberCount = room.currentState.getInvitedMemberCount();
|
1890 |
|
1891 | const inviteJoinCount = joinedMemberCount + invitedMemberCount - 1;
|
1892 |
|
1893 | let otherNames = null;
|
1894 |
|
1895 | if (room._summaryHeroes) {
|
1896 |
|
1897 |
|
1898 | otherNames = room._summaryHeroes.map(userId => {
|
1899 | const member = room.getMember(userId);
|
1900 | return member ? member.name : userId;
|
1901 | });
|
1902 | } else {
|
1903 | let otherMembers = room.currentState.getMembers().filter(m => {
|
1904 | return m.userId !== userId && (m.membership === "invite" || m.membership === "join");
|
1905 | });
|
1906 |
|
1907 | otherMembers.sort((a, b) => a.userId.localeCompare(b.userId));
|
1908 |
|
1909 | otherMembers = otherMembers.slice(0, 5);
|
1910 | otherNames = otherMembers.map(m => m.name);
|
1911 | }
|
1912 |
|
1913 | if (inviteJoinCount) {
|
1914 | return memberNamesToRoomName(otherNames, inviteJoinCount);
|
1915 | }
|
1916 |
|
1917 | const myMembership = room.getMyMembership();
|
1918 |
|
1919 |
|
1920 | if (myMembership == 'join') {
|
1921 | const thirdPartyInvites = room.currentState.getStateEvents("m.room.third_party_invite");
|
1922 |
|
1923 | if (thirdPartyInvites && thirdPartyInvites.length) {
|
1924 | const thirdPartyNames = thirdPartyInvites.map(i => {
|
1925 | return i.getContent().display_name;
|
1926 | });
|
1927 | return `Inviting ${memberNamesToRoomName(thirdPartyNames)}`;
|
1928 | }
|
1929 | }
|
1930 |
|
1931 |
|
1932 | let leftNames = otherNames;
|
1933 |
|
1934 | if (!leftNames.length) {
|
1935 | leftNames = room.currentState.getMembers().filter(m => {
|
1936 | return m.userId !== userId && m.membership !== "invite" && m.membership !== "join";
|
1937 | }).map(m => m.name);
|
1938 | }
|
1939 |
|
1940 | if (leftNames.length) {
|
1941 | return `Empty room (was ${memberNamesToRoomName(leftNames)})`;
|
1942 | } else {
|
1943 | return "Empty room";
|
1944 | }
|
1945 | }
|
1946 |
|
1947 | function memberNamesToRoomName(names, count = names.length + 1) {
|
1948 | const countWithoutMe = count - 1;
|
1949 |
|
1950 | if (!names.length) {
|
1951 | return "Empty room";
|
1952 | } else if (names.length === 1 && countWithoutMe <= 1) {
|
1953 | return names[0];
|
1954 | } else if (names.length === 2 && countWithoutMe <= 2) {
|
1955 | return `${names[0]} and ${names[1]}`;
|
1956 | } else {
|
1957 | const plural = countWithoutMe > 1;
|
1958 |
|
1959 | if (plural) {
|
1960 | return `${names[0]} and ${countWithoutMe} others`;
|
1961 | } else {
|
1962 | return `${names[0]} and 1 other`;
|
1963 | }
|
1964 | }
|
1965 | }
|
1966 |
|
1967 |
|
1968 |
|
1969 |
|
1970 |
|
1971 |
|
1972 |
|
1973 |
|
1974 |
|
1975 |
|
1976 |
|
1977 |
|
1978 |
|
1979 |
|
1980 |
|
1981 |
|
1982 |
|
1983 |
|
1984 |
|
1985 |
|
1986 |
|
1987 |
|
1988 |
|
1989 |
|
1990 |
|
1991 |
|
1992 |
|
1993 |
|
1994 |
|
1995 |
|
1996 |
|
1997 |
|
1998 |
|
1999 |
|
2000 |
|
2001 |
|
2002 |
|
2003 |
|
2004 |
|
2005 |
|
2006 |
|
2007 |
|
2008 |
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
2017 |
|
2018 |
|
2019 |
|
2020 |
|
2021 |
|
2022 |
|
2023 |
|
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 |
|
2035 |
|
2036 |
|
2037 |
|
2038 |
|
2039 |
|
2040 |
|
2041 |
|
2042 |
|
2043 |
|
2044 |
|
2045 |
|
2046 |
|
2047 |
|
2048 |
|
2049 |
|
2050 |
|
2051 |
|
2052 |
|
2053 |
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 |
|
2061 |
|
2062 |
|
2063 |
|
2064 |
|
2065 |
|
2066 |
|
2067 |
|
\ | No newline at end of file |