2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2021-05-21 16:59:29 +02:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-05-07 16:13:49 +02:00
|
|
|
import {SortedArray, MappedList, ConcatList, ObservableArray} from "../../../observable/index.js";
|
2020-09-10 16:40:30 +02:00
|
|
|
import {Disposables} from "../../../utils/Disposables.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {Direction} from "./Direction.js";
|
|
|
|
import {TimelineReader} from "./persistence/TimelineReader.js";
|
|
|
|
import {PendingEventEntry} from "./entries/PendingEventEntry.js";
|
2021-03-03 14:53:22 +01:00
|
|
|
import {RoomMember} from "../members/RoomMember.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class Timeline {
|
2021-03-03 14:53:22 +01:00
|
|
|
constructor({roomId, storage, closeCallback, fragmentIdComparer, pendingEvents, clock}) {
|
2019-02-27 22:50:08 +01:00
|
|
|
this._roomId = roomId;
|
|
|
|
this._storage = storage;
|
|
|
|
this._closeCallback = closeCallback;
|
2019-05-12 20:26:03 +02:00
|
|
|
this._fragmentIdComparer = fragmentIdComparer;
|
2020-09-10 16:40:30 +02:00
|
|
|
this._disposables = new Disposables();
|
2021-05-21 16:59:29 +02:00
|
|
|
this._pendingEvents = pendingEvents;
|
|
|
|
this._clock = clock;
|
|
|
|
this._remoteEntries = null;
|
2021-03-03 14:53:22 +01:00
|
|
|
this._ownMember = null;
|
2019-05-19 20:49:46 +02:00
|
|
|
this._timelineReader = new TimelineReader({
|
|
|
|
roomId: this._roomId,
|
|
|
|
storage: this._storage,
|
|
|
|
fragmentIdComparer: this._fragmentIdComparer
|
|
|
|
});
|
2020-09-10 16:40:30 +02:00
|
|
|
this._readerRequest = null;
|
2021-05-21 16:59:29 +02:00
|
|
|
this._allEntries = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
2021-04-08 16:30:46 +02:00
|
|
|
async load(user, membership, log) {
|
2021-03-04 19:47:02 +01:00
|
|
|
const txn = await this._storage.readTxn(this._timelineReader.readTxnStores.concat(this._storage.storeNames.roomMembers));
|
2021-03-03 14:53:22 +01:00
|
|
|
const memberData = await txn.roomMembers.get(this._roomId, user.id);
|
2021-04-08 16:30:46 +02:00
|
|
|
if (memberData) {
|
|
|
|
this._ownMember = new RoomMember(memberData);
|
|
|
|
} else {
|
|
|
|
// this should never happen, as our own join into the room would have
|
|
|
|
// made us receive our own member event, but just to be on the safe side and not crash,
|
|
|
|
// fall back to bare user id
|
|
|
|
this._ownMember = RoomMember.fromUserId(this._roomId, user.id, membership);
|
|
|
|
}
|
2021-03-03 14:53:22 +01:00
|
|
|
// it should be fine to not update the local entries,
|
|
|
|
// as they should only populate once the view subscribes to it
|
|
|
|
// if they are populated already, the sender profile would be empty
|
|
|
|
|
2020-09-10 16:40:30 +02:00
|
|
|
// 30 seems to be a good amount to fill the entire screen
|
2021-03-15 12:56:40 +01:00
|
|
|
const readerRequest = this._disposables.track(this._timelineReader.readFromEnd(30, txn, log));
|
2020-09-10 16:40:30 +02:00
|
|
|
try {
|
|
|
|
const entries = await readerRequest.complete();
|
2021-05-21 16:59:29 +02:00
|
|
|
this._setupEntries(entries);
|
2020-09-10 16:40:30 +02:00
|
|
|
} finally {
|
|
|
|
this._disposables.disposeTracked(readerRequest);
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:59:29 +02:00
|
|
|
_setupEntries(timelineEntries) {
|
|
|
|
this._remoteEntries = new SortedArray((a, b) => a.compare(b));
|
|
|
|
this._remoteEntries.setManySorted(timelineEntries);
|
|
|
|
if (this._pendingEvents) {
|
|
|
|
this._localEntries = new MappedList(this._pendingEvents, pe => {
|
|
|
|
const pee = new PendingEventEntry({pendingEvent: pe, member: this._ownMember, clock: this._clock});
|
|
|
|
this._applyAndEmitLocalRelationChange(pee.pendingEvent, target => target.addLocalRelation(pee));
|
|
|
|
return pee;
|
|
|
|
}, (pee, params) => {
|
|
|
|
// is sending but redacted, who do we detect that here to remove the relation?
|
|
|
|
pee.notifyUpdate(params);
|
|
|
|
}, pee => {
|
|
|
|
this._applyAndEmitLocalRelationChange(pee.pendingEvent, target => target.removeLocalRelation(pee));
|
|
|
|
});
|
|
|
|
// we need a hook for when a pee is removed, so we can remove the local relation
|
|
|
|
} else {
|
|
|
|
this._localEntries = new ObservableArray();
|
|
|
|
}
|
|
|
|
this._allEntries = new ConcatList(this._remoteEntries, this._localEntries);
|
|
|
|
}
|
|
|
|
|
|
|
|
_applyAndEmitLocalRelationChange(pe, updater) {
|
|
|
|
// first, look in local entries (separately, as it has its own update mechanism)
|
|
|
|
const foundInLocalEntries = this._localEntries.findAndUpdate(
|
|
|
|
e => e.id === pe.relatedTxnId,
|
|
|
|
e => {
|
|
|
|
const params = updater(e);
|
|
|
|
return params ? params : false;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
// now look in remote entries
|
|
|
|
if (!foundInLocalEntries && pe.relatedEventId) {
|
|
|
|
// TODO: ideally iterate in reverse as target is likely to be most recent,
|
|
|
|
// but not easy through ObservableList contract
|
|
|
|
for (const entry of this._allEntries) {
|
|
|
|
if (pe.relatedEventId === entry.id) {
|
|
|
|
const params = updater(entry);
|
|
|
|
if (params) {
|
|
|
|
this._remoteEntries.update(entry, params);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 14:53:22 +01:00
|
|
|
updateOwnMember(member) {
|
|
|
|
this._ownMember = member;
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:28:22 +02:00
|
|
|
replaceEntries(entries) {
|
|
|
|
for (const entry of entries) {
|
2021-04-08 18:36:09 +02:00
|
|
|
// this will use the comparator and thus
|
|
|
|
// check for equality using the compare method in BaseEntry
|
2021-05-26 13:10:19 +02:00
|
|
|
this._remoteEntries.findAndUpdate(entry, (previousEntry, entry) => {
|
|
|
|
entry.transferLocalEchoState(previousEntry);
|
|
|
|
});
|
2020-09-04 15:28:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
/** @package */
|
2021-03-03 11:27:18 +01:00
|
|
|
addOrReplaceEntries(newEntries) {
|
2021-05-21 16:59:29 +02:00
|
|
|
// find any local relations to this new remote event
|
|
|
|
for (const pee of this._localEntries) {
|
|
|
|
// this will work because we set relatedEventId when removing remote echos
|
|
|
|
if (pee.relatedEventId) {
|
|
|
|
const relationTarget = newEntries.find(e => e.id === pee.relatedEventId);
|
|
|
|
// no need to emit here as this entry is about to be added
|
|
|
|
relationTarget?.addLocalRelation(pee);
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 19:53:58 +02:00
|
|
|
this._remoteEntries.setManySorted(newEntries);
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
2020-03-21 23:40:40 +01:00
|
|
|
|
2019-06-01 17:39:23 +02:00
|
|
|
// tries to prepend `amount` entries to the `entries` list.
|
2021-03-02 19:29:55 +01:00
|
|
|
/**
|
|
|
|
* [loadAtTop description]
|
|
|
|
* @param {[type]} amount [description]
|
|
|
|
* @return {boolean} true if the top of the timeline has been reached
|
|
|
|
*
|
|
|
|
*/
|
2019-03-09 00:41:06 +01:00
|
|
|
async loadAtTop(amount) {
|
2020-10-13 13:10:35 +02:00
|
|
|
if (this._disposables.isDisposed) {
|
2021-03-02 19:29:55 +01:00
|
|
|
return true;
|
2020-10-13 13:10:35 +02:00
|
|
|
}
|
2019-07-29 19:58:35 +02:00
|
|
|
const firstEventEntry = this._remoteEntries.array.find(e => !!e.eventType);
|
2019-06-16 17:29:33 +02:00
|
|
|
if (!firstEventEntry) {
|
2021-03-02 19:29:55 +01:00
|
|
|
return true;
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
2020-09-10 16:40:30 +02:00
|
|
|
const readerRequest = this._disposables.track(this._timelineReader.readFrom(
|
2019-06-16 17:29:33 +02:00
|
|
|
firstEventEntry.asEventKey(),
|
2019-06-02 15:15:26 +02:00
|
|
|
Direction.Backward,
|
|
|
|
amount
|
2020-09-10 16:40:30 +02:00
|
|
|
));
|
|
|
|
try {
|
|
|
|
const entries = await readerRequest.complete();
|
|
|
|
this._remoteEntries.setManySorted(entries);
|
2021-03-02 19:29:55 +01:00
|
|
|
return entries.length < amount;
|
2020-09-10 16:40:30 +02:00
|
|
|
} finally {
|
|
|
|
this._disposables.disposeTracked(readerRequest);
|
|
|
|
}
|
2019-03-08 20:03:18 +01:00
|
|
|
}
|
|
|
|
|
2020-10-30 15:19:51 +01:00
|
|
|
getByEventId(eventId) {
|
|
|
|
for (let i = 0; i < this._remoteEntries.length; i += 1) {
|
|
|
|
const entry = this._remoteEntries.get(i);
|
|
|
|
if (entry.id === eventId) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 16:59:29 +02:00
|
|
|
return null;
|
2020-10-30 15:19:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
/** @public */
|
|
|
|
get entries() {
|
2019-07-29 19:53:58 +02:00
|
|
|
return this._allEntries;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 22:30:33 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @return {Array<EventEntry>} remote event entries, should not be modified
|
|
|
|
*/
|
|
|
|
get remoteEntries() {
|
|
|
|
return this._remoteEntries.array;
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
/** @public */
|
2020-09-10 17:43:01 +02:00
|
|
|
dispose() {
|
2020-05-04 22:21:56 +02:00
|
|
|
if (this._closeCallback) {
|
2020-09-10 17:43:01 +02:00
|
|
|
this._disposables.dispose();
|
2020-05-04 22:21:56 +02:00
|
|
|
this._closeCallback();
|
|
|
|
this._closeCallback = null;
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
2020-09-04 15:28:22 +02:00
|
|
|
|
|
|
|
enableEncryption(decryptEntries) {
|
|
|
|
this._timelineReader.enableEncryption(decryptEntries);
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|