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-06-10 18:29:10 +02:00
|
|
|
import {SortedArray, AsyncMappedList, 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";
|
2021-05-31 13:52:03 +02:00
|
|
|
import {PowerLevels} from "./PowerLevels.js";
|
2021-06-16 12:46:44 +02:00
|
|
|
import {getRelation, ANNOTATION_RELATION_TYPE} from "./relations.js";
|
2021-06-09 16:52:30 +02:00
|
|
|
import {REDACTION_TYPE} from "../common.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;
|
2021-06-04 16:28:08 +02:00
|
|
|
// constructing this early avoid some problem while sync and openTimeline race
|
|
|
|
this._remoteEntries = new SortedArray((a, b) => a.compare(b));
|
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;
|
2021-05-31 13:52:03 +02:00
|
|
|
this._powerLevels = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
2021-04-08 16:30:46 +02:00
|
|
|
async load(user, membership, log) {
|
2021-05-31 13:52:03 +02:00
|
|
|
const txn = await this._storage.readTxn(this._timelineReader.readTxnStores.concat(
|
|
|
|
this._storage.storeNames.roomMembers,
|
|
|
|
this._storage.storeNames.roomState
|
|
|
|
));
|
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
|
|
|
|
|
2021-06-24 14:24:22 +02:00
|
|
|
this._powerLevels = await this._loadPowerLevels(membership, txn);
|
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);
|
|
|
|
}
|
2021-06-02 15:06:30 +02:00
|
|
|
// txn should be assumed to have finished here, as decryption will close it.
|
2021-05-31 13:52:03 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 14:24:22 +02:00
|
|
|
async _loadPowerLevels(membership, txn) {
|
2021-06-02 18:43:47 +02:00
|
|
|
// TODO: update power levels as state is updated
|
2021-05-31 13:52:03 +02:00
|
|
|
const powerLevelsState = await txn.roomState.get(this._roomId, "m.room.power_levels", "");
|
|
|
|
if (powerLevelsState) {
|
|
|
|
return new PowerLevels({
|
|
|
|
powerLevelEvent: powerLevelsState.event,
|
2021-06-24 14:24:22 +02:00
|
|
|
ownUserId: this._ownMember.userId,
|
|
|
|
membership
|
2021-05-31 13:52:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const createState = await txn.roomState.get(this._roomId, "m.room.create", "");
|
2021-06-02 18:41:52 +02:00
|
|
|
if (createState) {
|
|
|
|
return new PowerLevels({
|
|
|
|
createEvent: createState.event,
|
2021-06-24 14:24:22 +02:00
|
|
|
ownUserId: this._ownMember.userId,
|
|
|
|
membership
|
2021-06-02 18:41:52 +02:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-24 14:24:22 +02:00
|
|
|
return new PowerLevels({ownUserId: this._ownMember.userId, membership});
|
2021-06-02 18:41:52 +02:00
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:59:29 +02:00
|
|
|
_setupEntries(timelineEntries) {
|
|
|
|
this._remoteEntries.setManySorted(timelineEntries);
|
|
|
|
if (this._pendingEvents) {
|
2021-06-10 18:29:10 +02:00
|
|
|
this._localEntries = new AsyncMappedList(this._pendingEvents,
|
|
|
|
pe => this._mapPendingEventToEntry(pe),
|
|
|
|
(pee, params) => {
|
|
|
|
// is sending but redacted, who do we detect that here to remove the relation?
|
|
|
|
pee.notifyUpdate(params);
|
|
|
|
},
|
|
|
|
pee => this._applyAndEmitLocalRelationChange(pee, target => target.removeLocalRelation(pee))
|
|
|
|
);
|
2021-05-21 16:59:29 +02:00
|
|
|
} else {
|
|
|
|
this._localEntries = new ObservableArray();
|
|
|
|
}
|
|
|
|
this._allEntries = new ConcatList(this._remoteEntries, this._localEntries);
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:29:10 +02:00
|
|
|
async _mapPendingEventToEntry(pe) {
|
2021-06-15 19:06:41 +02:00
|
|
|
// we load the redaction target for pending events,
|
2021-06-10 18:29:10 +02:00
|
|
|
// so if we are redacting a relation, we can pass the redaction
|
|
|
|
// to the relation target and the removal of the relation can
|
|
|
|
// be taken into account for local echo.
|
2021-06-16 12:46:44 +02:00
|
|
|
let redactingEntry;
|
2021-06-15 19:06:41 +02:00
|
|
|
if (pe.eventType === REDACTION_TYPE) {
|
2021-06-16 12:46:44 +02:00
|
|
|
redactingEntry = await this._getOrLoadEntry(pe.relatedTxnId, pe.relatedEventId);
|
2021-06-09 16:52:30 +02:00
|
|
|
}
|
2021-06-15 19:06:41 +02:00
|
|
|
const pee = new PendingEventEntry({
|
|
|
|
pendingEvent: pe, member: this._ownMember,
|
2021-06-16 12:46:44 +02:00
|
|
|
clock: this._clock, redactingEntry
|
2021-06-15 19:06:41 +02:00
|
|
|
});
|
2021-06-10 18:29:10 +02:00
|
|
|
this._applyAndEmitLocalRelationChange(pee, target => target.addLocalRelation(pee));
|
|
|
|
return pee;
|
2021-06-09 16:52:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 18:29:10 +02:00
|
|
|
_applyAndEmitLocalRelationChange(pee, updater) {
|
2021-06-16 12:46:44 +02:00
|
|
|
// this is the contract of findAndUpdate, used in _findAndUpdateRelatedEntry
|
2021-05-31 10:47:32 +02:00
|
|
|
const updateOrFalse = e => {
|
|
|
|
const params = updater(e);
|
|
|
|
return params ? params : false;
|
|
|
|
};
|
2021-06-16 12:46:44 +02:00
|
|
|
this._findAndUpdateRelatedEntry(pee.pendingEvent.relatedTxnId, pee.relatedEventId, updateOrFalse);
|
|
|
|
// also look for a relation target to update with this redaction
|
2021-06-17 10:03:32 +02:00
|
|
|
if (pee.redactingEntry) {
|
2021-06-16 12:46:44 +02:00
|
|
|
// redactingEntry might be a PendingEventEntry or an EventEntry, so don't assume pendingEvent
|
2021-06-17 10:03:32 +02:00
|
|
|
const relatedTxnId = pee.redactingEntry.pendingEvent?.relatedTxnId;
|
|
|
|
this._findAndUpdateRelatedEntry(relatedTxnId, pee.redactingEntry.relatedEventId, updateOrFalse);
|
2021-06-16 12:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_findAndUpdateRelatedEntry(relatedTxnId, relatedEventId, updateOrFalse) {
|
2021-06-10 18:29:10 +02:00
|
|
|
let found = false;
|
2021-05-31 10:47:32 +02:00
|
|
|
// first, look in local entries based on txn id
|
2021-06-10 18:29:10 +02:00
|
|
|
if (relatedTxnId) {
|
|
|
|
found = this._localEntries.findAndUpdate(
|
|
|
|
e => e.id === relatedTxnId,
|
2021-06-02 18:43:16 +02:00
|
|
|
updateOrFalse,
|
|
|
|
);
|
|
|
|
}
|
2021-06-16 10:28:17 +02:00
|
|
|
// if not found here, look in remote entries based on event id
|
2021-06-16 12:46:44 +02:00
|
|
|
if (!found && relatedEventId) {
|
2021-06-11 11:30:11 +02:00
|
|
|
this._remoteEntries.findAndUpdate(
|
2021-06-16 12:46:44 +02:00
|
|
|
e => e.id === relatedEventId,
|
2021-05-31 10:47:32 +02:00
|
|
|
updateOrFalse
|
|
|
|
);
|
2021-05-21 16:59:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:56:17 +02:00
|
|
|
async getOwnAnnotationEntry(targetId, key) {
|
|
|
|
const txn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.timelineEvents,
|
|
|
|
this._storage.storeNames.timelineRelations,
|
|
|
|
]);
|
|
|
|
const relations = await txn.timelineRelations.getForTargetAndType(this._roomId, targetId, ANNOTATION_RELATION_TYPE);
|
|
|
|
for (const relation of relations) {
|
|
|
|
const annotation = await txn.timelineEvents.getByEventId(this._roomId, relation.sourceEventId);
|
2021-06-18 11:51:02 +02:00
|
|
|
if (annotation && annotation.event.sender === this._ownMember.userId && getRelation(annotation.event).key === key) {
|
2021-06-08 16:56:17 +02:00
|
|
|
const eventEntry = new EventEntry(annotation, this._fragmentIdComparer);
|
|
|
|
this._addLocalRelationsToNewRemoteEntries([eventEntry]);
|
|
|
|
return eventEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-23 17:38:52 +02:00
|
|
|
/** @package */
|
2021-03-03 14:53:22 +01:00
|
|
|
updateOwnMember(member) {
|
|
|
|
this._ownMember = member;
|
|
|
|
}
|
|
|
|
|
2021-05-31 16:24:42 +02:00
|
|
|
_addLocalRelationsToNewRemoteEntries(entries) {
|
2021-06-02 18:42:46 +02:00
|
|
|
// because it is not safe to iterate a derived observable collection
|
|
|
|
// before it has any subscriptions, we bail out if this isn't
|
|
|
|
// the case yet. This can happen when sync adds or replaces entries
|
|
|
|
// before load has finished and the view has subscribed to the timeline.
|
|
|
|
//
|
|
|
|
// Once the subscription is setup, MappedList will set up the local
|
|
|
|
// relations as needed with _applyAndEmitLocalRelationChange,
|
|
|
|
// so we're not missing anything by bailing out.
|
2021-06-04 16:05:28 +02:00
|
|
|
//
|
|
|
|
// _localEntries can also not yet exist
|
|
|
|
if (!this._localEntries?.hasSubscriptions) {
|
2021-06-02 18:42:46 +02:00
|
|
|
return;
|
|
|
|
}
|
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) {
|
2021-05-31 16:24:42 +02:00
|
|
|
const relationTarget = entries.find(e => e.id === pee.relatedEventId);
|
2021-06-17 10:12:45 +02:00
|
|
|
// no need to emit here as this entry is about to be added
|
|
|
|
relationTarget?.addLocalRelation(pee);
|
2021-06-10 18:29:10 +02:00
|
|
|
}
|
2021-06-16 12:46:44 +02:00
|
|
|
if (pee.redactingEntry) {
|
|
|
|
const eventId = pee.redactingEntry.relatedEventId;
|
2021-06-10 18:29:10 +02:00
|
|
|
const relationTarget = entries.find(e => e.id === eventId);
|
2021-06-17 10:12:45 +02:00
|
|
|
relationTarget?.addLocalRelation(pee);
|
2021-05-21 16:59:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 16:24:42 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:38:52 +02:00
|
|
|
// used in replaceEntries
|
|
|
|
static _entryUpdater(existingEntry, entry) {
|
|
|
|
entry.updateFrom(existingEntry);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
replaceEntries(entries) {
|
|
|
|
this._addLocalRelationsToNewRemoteEntries(entries);
|
|
|
|
for (const entry of entries) {
|
|
|
|
this._remoteEntries.getAndUpdate(entry, Timeline._entryUpdater);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 16:24:42 +02:00
|
|
|
/** @package */
|
2021-06-23 17:38:52 +02:00
|
|
|
addEntries(newEntries) {
|
2021-05-31 16:24:42 +02:00
|
|
|
this._addLocalRelationsToNewRemoteEntries(newEntries);
|
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();
|
2021-06-23 17:38:52 +02:00
|
|
|
this.addEntries(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
|
|
|
}
|
|
|
|
|
2021-06-16 12:46:44 +02:00
|
|
|
async _getOrLoadEntry(txnId, eventId) {
|
|
|
|
if (txnId) {
|
|
|
|
// also look for redacting relation in pending events, in case the target is already being sent
|
|
|
|
for (const p of this._localEntries) {
|
|
|
|
if (p.id === txnId) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (eventId) {
|
|
|
|
const loadedEntry = this.getByEventId(eventId);
|
|
|
|
if (loadedEntry) {
|
|
|
|
return loadedEntry;
|
|
|
|
} else {
|
|
|
|
const txn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.timelineEvents,
|
|
|
|
]);
|
|
|
|
const redactionTargetEntry = await txn.timelineEvents.getByEventId(this._roomId, eventId);
|
|
|
|
if (redactionTargetEntry) {
|
|
|
|
return new EventEntry(redactionTargetEntry, this._fragmentIdComparer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-05-31 13:52:03 +02:00
|
|
|
/** @internal */
|
2020-09-04 15:28:22 +02:00
|
|
|
enableEncryption(decryptEntries) {
|
|
|
|
this._timelineReader.enableEncryption(decryptEntries);
|
|
|
|
}
|
2021-05-31 13:52:03 +02:00
|
|
|
|
|
|
|
get powerLevels() {
|
|
|
|
return this._powerLevels;
|
|
|
|
}
|
2021-05-31 15:18:44 +02:00
|
|
|
|
|
|
|
get me() {
|
|
|
|
return this._ownMember;
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
2021-06-02 18:41:03 +02:00
|
|
|
|
|
|
|
import {FragmentIdComparer} from "./FragmentIdComparer.js";
|
2021-06-10 18:29:10 +02:00
|
|
|
import {poll} from "../../../mocks/poll.js";
|
2021-06-02 18:41:03 +02:00
|
|
|
import {Clock as MockClock} from "../../../mocks/Clock.js";
|
|
|
|
import {createMockStorage} from "../../../mocks/Storage.js";
|
2021-06-18 14:39:54 +02:00
|
|
|
import {ListObserver} from "../../../mocks/ListObserver.js";
|
2021-06-18 11:51:02 +02:00
|
|
|
import {createEvent, withTextBody, withContent, withSender} from "../../../mocks/event.js";
|
2021-06-02 18:41:03 +02:00
|
|
|
import {NullLogItem} from "../../../logging/NullLogger.js";
|
|
|
|
import {EventEntry} from "./entries/EventEntry.js";
|
|
|
|
import {User} from "../../User.js";
|
|
|
|
import {PendingEvent} from "../sending/PendingEvent.js";
|
2021-06-18 11:51:02 +02:00
|
|
|
import {createAnnotation} from "./relations.js";
|
2021-06-02 18:41:03 +02:00
|
|
|
|
|
|
|
export function tests() {
|
|
|
|
const fragmentIdComparer = new FragmentIdComparer([]);
|
2021-06-18 11:51:02 +02:00
|
|
|
const roomId = "$abc";
|
|
|
|
const alice = "@alice:hs.tld";
|
|
|
|
const bob = "@bob:hs.tld";
|
|
|
|
|
|
|
|
function getIndexFromIterable(it, n) {
|
|
|
|
let i = 0;
|
|
|
|
for (const item of it) {
|
|
|
|
if (i === n) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
throw new Error("not enough items in iterable");
|
|
|
|
}
|
|
|
|
|
2021-06-02 18:41:03 +02:00
|
|
|
return {
|
2021-06-17 16:07:32 +02:00
|
|
|
"adding or replacing entries before subscribing to entries does not lose local relations": async assert => {
|
2021-06-02 18:41:03 +02:00
|
|
|
const pendingEvents = new ObservableArray();
|
2021-06-18 14:39:54 +02:00
|
|
|
const timeline = new Timeline({roomId, storage: await createMockStorage(),
|
|
|
|
closeCallback: () => {}, fragmentIdComparer, pendingEvents, clock: new MockClock()});
|
2021-06-02 18:41:03 +02:00
|
|
|
// 1. load timeline
|
2021-06-18 11:51:02 +02:00
|
|
|
await timeline.load(new User(alice), "join", new NullLogItem());
|
2021-06-23 17:38:52 +02:00
|
|
|
// 2. test replaceEntries and addEntries don't fail
|
2021-06-18 11:51:02 +02:00
|
|
|
const event1 = withTextBody("hi!", withSender(bob, createEvent("m.room.message", "!abc")));
|
2021-06-02 18:41:03 +02:00
|
|
|
const entry1 = new EventEntry({event: event1, fragmentId: 1, eventIndex: 1}, fragmentIdComparer);
|
|
|
|
timeline.replaceEntries([entry1]);
|
2021-06-18 11:51:02 +02:00
|
|
|
const event2 = withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!def")));
|
2021-06-02 18:41:03 +02:00
|
|
|
const entry2 = new EventEntry({event: event2, fragmentId: 1, eventIndex: 2}, fragmentIdComparer);
|
2021-06-23 17:38:52 +02:00
|
|
|
timeline.addEntries([entry2]);
|
2021-06-02 18:41:03 +02:00
|
|
|
// 3. add local relation (redaction)
|
|
|
|
pendingEvents.append(new PendingEvent({data: {
|
|
|
|
roomId,
|
|
|
|
queueIndex: 1,
|
|
|
|
eventType: "m.room.redaction",
|
|
|
|
txnId: "t123",
|
|
|
|
content: {},
|
|
|
|
relatedEventId: event2.event_id
|
|
|
|
}}));
|
2021-06-10 18:29:10 +02:00
|
|
|
// 4. subscribe (it's now safe to iterate timeline.entries)
|
2021-06-18 14:39:54 +02:00
|
|
|
timeline.entries.subscribe(new ListObserver());
|
2021-06-02 18:41:03 +02:00
|
|
|
// 5. check the local relation got correctly aggregated
|
2021-06-10 18:29:10 +02:00
|
|
|
const locallyRedacted = await poll(() => Array.from(timeline.entries)[0].isRedacting);
|
|
|
|
assert.equal(locallyRedacted, true);
|
2021-06-18 11:51:02 +02:00
|
|
|
},
|
2021-06-18 14:39:54 +02:00
|
|
|
"add and remove local reaction, and cancel again": async assert => {
|
|
|
|
// 1. setup timeline with message
|
2021-06-18 11:51:02 +02:00
|
|
|
const pendingEvents = new ObservableArray();
|
2021-06-18 14:39:54 +02:00
|
|
|
const timeline = new Timeline({roomId, storage: await createMockStorage(),
|
|
|
|
closeCallback: () => {}, fragmentIdComparer, pendingEvents, clock: new MockClock()});
|
2021-06-18 11:51:02 +02:00
|
|
|
await timeline.load(new User(bob), "join", new NullLogItem());
|
2021-06-18 14:39:54 +02:00
|
|
|
timeline.entries.subscribe(new ListObserver());
|
2021-06-18 11:51:02 +02:00
|
|
|
const event = withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!abc")));
|
2021-06-23 17:38:52 +02:00
|
|
|
timeline.addEntries([new EventEntry({event, fragmentId: 1, eventIndex: 2}, fragmentIdComparer)]);
|
2021-06-18 11:51:02 +02:00
|
|
|
let entry = getIndexFromIterable(timeline.entries, 0);
|
2021-06-18 14:39:54 +02:00
|
|
|
// 2. add local reaction
|
2021-06-18 11:51:02 +02:00
|
|
|
pendingEvents.append(new PendingEvent({data: {
|
|
|
|
roomId,
|
|
|
|
queueIndex: 1,
|
|
|
|
eventType: "m.reaction",
|
|
|
|
txnId: "t123",
|
|
|
|
content: entry.annotate("👋"),
|
|
|
|
relatedEventId: entry.id
|
|
|
|
}}));
|
2021-06-18 14:39:54 +02:00
|
|
|
await poll(() => timeline.entries.length === 2);
|
2021-06-24 12:26:38 +02:00
|
|
|
assert.equal(entry.pendingAnnotations.get("👋").count, 1);
|
2021-06-18 14:39:54 +02:00
|
|
|
const reactionEntry = getIndexFromIterable(timeline.entries, 1);
|
|
|
|
// 3. add redaction to timeline
|
|
|
|
pendingEvents.append(new PendingEvent({data: {
|
|
|
|
roomId,
|
|
|
|
queueIndex: 2,
|
|
|
|
eventType: "m.room.redaction",
|
|
|
|
txnId: "t456",
|
|
|
|
content: {},
|
|
|
|
relatedTxnId: reactionEntry.id
|
|
|
|
}}));
|
2021-06-18 15:06:49 +02:00
|
|
|
// TODO: await nextUpdate here with ListObserver, to ensure entry emits an update when pendingAnnotations changes
|
2021-06-18 14:39:54 +02:00
|
|
|
await poll(() => timeline.entries.length === 3);
|
2021-06-24 12:26:38 +02:00
|
|
|
assert.equal(entry.pendingAnnotations.get("👋").count, 0);
|
2021-06-18 14:39:54 +02:00
|
|
|
// 4. cancel redaction
|
|
|
|
pendingEvents.remove(1);
|
|
|
|
await poll(() => timeline.entries.length === 2);
|
2021-06-24 12:26:38 +02:00
|
|
|
assert.equal(entry.pendingAnnotations.get("👋").count, 1);
|
2021-06-18 14:39:54 +02:00
|
|
|
// 5. cancel reaction
|
|
|
|
pendingEvents.remove(0);
|
|
|
|
await poll(() => timeline.entries.length === 1);
|
|
|
|
assert(!entry.pendingAnnotations);
|
2021-06-18 11:51:02 +02:00
|
|
|
},
|
2021-06-18 14:39:54 +02:00
|
|
|
"getOwnAnnotationEntry": async assert => {
|
|
|
|
const messageId = "!abc";
|
|
|
|
const reactionId = "!def";
|
2021-06-18 11:51:02 +02:00
|
|
|
// 1. put event and reaction into storage
|
|
|
|
const storage = await createMockStorage();
|
2021-06-18 14:39:54 +02:00
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
|
|
|
|
txn.timelineEvents.insert({
|
|
|
|
event: withContent(createAnnotation(messageId, "👋"), createEvent("m.reaction", reactionId, bob)),
|
|
|
|
fragmentId: 1, eventIndex: 1, roomId
|
|
|
|
});
|
|
|
|
txn.timelineRelations.add(roomId, messageId, ANNOTATION_RELATION_TYPE, reactionId);
|
|
|
|
await txn.complete();
|
|
|
|
// 2. setup the timeline
|
|
|
|
const timeline = new Timeline({roomId, storage, closeCallback: () => {},
|
|
|
|
fragmentIdComparer, pendingEvents: new ObservableArray(), clock: new MockClock()});
|
|
|
|
await timeline.load(new User(bob), "join", new NullLogItem());
|
|
|
|
// 3. get the own annotation out
|
|
|
|
const reactionEntry = await timeline.getOwnAnnotationEntry(messageId, "👋");
|
|
|
|
assert.equal(reactionEntry.id, reactionId);
|
|
|
|
assert.equal(reactionEntry.relation.key, "👋");
|
|
|
|
},
|
|
|
|
"remote reaction": async assert => {
|
|
|
|
const messageEntry = new EventEntry({
|
2021-06-18 11:51:02 +02:00
|
|
|
event: withTextBody("hi bob!", createEvent("m.room.message", "!abc", alice)),
|
|
|
|
fragmentId: 1, eventIndex: 2, roomId,
|
|
|
|
annotations: { // aggregated like RelationWriter would
|
|
|
|
"👋": {count: 1, me: true, firstTimestamp: 0}
|
|
|
|
},
|
2021-06-18 14:39:54 +02:00
|
|
|
}, fragmentIdComparer);
|
2021-06-18 11:51:02 +02:00
|
|
|
// 2. setup timeline
|
|
|
|
const pendingEvents = new ObservableArray();
|
2021-06-18 14:39:54 +02:00
|
|
|
const timeline = new Timeline({roomId, storage: await createMockStorage(),
|
|
|
|
closeCallback: () => {}, fragmentIdComparer, pendingEvents, clock: new MockClock()});
|
2021-06-18 11:51:02 +02:00
|
|
|
await timeline.load(new User(bob), "join", new NullLogItem());
|
2021-06-18 14:39:54 +02:00
|
|
|
timeline.entries.subscribe(new ListObserver());
|
2021-06-18 11:51:02 +02:00
|
|
|
// 3. add message to timeline
|
2021-06-23 17:38:52 +02:00
|
|
|
timeline.addEntries([messageEntry]);
|
2021-06-18 11:51:02 +02:00
|
|
|
const entry = getIndexFromIterable(timeline.entries, 0);
|
|
|
|
assert.equal(entry, messageEntry);
|
|
|
|
assert.equal(entry.annotations["👋"].count, 1);
|
2021-06-18 14:39:54 +02:00
|
|
|
},
|
|
|
|
"remove remote reaction": async assert => {
|
|
|
|
// 1. setup timeline
|
|
|
|
const pendingEvents = new ObservableArray();
|
|
|
|
const timeline = new Timeline({roomId, storage: await createMockStorage(),
|
|
|
|
closeCallback: () => {}, fragmentIdComparer, pendingEvents, clock: new MockClock()});
|
|
|
|
await timeline.load(new User(bob), "join", new NullLogItem());
|
|
|
|
timeline.entries.subscribe(new ListObserver());
|
|
|
|
// 2. add message and reaction to timeline
|
|
|
|
const messageEntry = new EventEntry({
|
|
|
|
event: withTextBody("hi bob!", createEvent("m.room.message", "!abc", alice)),
|
|
|
|
fragmentId: 1, eventIndex: 2, roomId,
|
|
|
|
}, fragmentIdComparer);
|
|
|
|
const reactionEntry = new EventEntry({
|
|
|
|
event: withContent(createAnnotation(messageEntry.id, "👋"), createEvent("m.reaction", "!def", bob)),
|
|
|
|
fragmentId: 1, eventIndex: 3, roomId
|
|
|
|
}, fragmentIdComparer);
|
2021-06-23 17:38:52 +02:00
|
|
|
timeline.addEntries([messageEntry, reactionEntry]);
|
2021-06-18 14:39:54 +02:00
|
|
|
// 3. redact reaction
|
2021-06-18 11:51:02 +02:00
|
|
|
pendingEvents.append(new PendingEvent({data: {
|
|
|
|
roomId,
|
|
|
|
queueIndex: 1,
|
|
|
|
eventType: "m.room.redaction",
|
|
|
|
txnId: "t123",
|
|
|
|
content: {},
|
|
|
|
relatedEventId: reactionEntry.id
|
|
|
|
}}));
|
2021-06-18 14:39:54 +02:00
|
|
|
await poll(() => timeline.entries.length >= 3);
|
2021-06-24 12:26:38 +02:00
|
|
|
assert.equal(messageEntry.pendingAnnotations.get("👋").count, -1);
|
2021-06-18 11:51:02 +02:00
|
|
|
},
|
2021-06-18 15:09:14 +02:00
|
|
|
"local reaction gets applied after remote echo is added to timeline": async assert => {
|
|
|
|
const messageEntry = new EventEntry({event: withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!abc"))),
|
|
|
|
fragmentId: 1, eventIndex: 2}, fragmentIdComparer);
|
|
|
|
// 1. setup timeline
|
|
|
|
const pendingEvents = new ObservableArray();
|
|
|
|
const timeline = new Timeline({roomId, storage: await createMockStorage(),
|
|
|
|
closeCallback: () => {}, fragmentIdComparer, pendingEvents, clock: new MockClock()});
|
|
|
|
await timeline.load(new User(bob), "join", new NullLogItem());
|
|
|
|
timeline.entries.subscribe(new ListObserver());
|
|
|
|
// 2. add local reaction
|
|
|
|
pendingEvents.append(new PendingEvent({data: {
|
|
|
|
roomId,
|
|
|
|
queueIndex: 1,
|
|
|
|
eventType: "m.reaction",
|
|
|
|
txnId: "t123",
|
|
|
|
content: messageEntry.annotate("👋"),
|
|
|
|
relatedEventId: messageEntry.id
|
|
|
|
}}));
|
|
|
|
await poll(() => timeline.entries.length === 1);
|
|
|
|
// 3. add remote reaction target
|
2021-06-23 17:38:52 +02:00
|
|
|
timeline.addEntries([messageEntry]);
|
2021-06-18 15:09:14 +02:00
|
|
|
await poll(() => timeline.entries.length === 2);
|
|
|
|
const entry = getIndexFromIterable(timeline.entries, 0);
|
|
|
|
assert.equal(entry, messageEntry);
|
2021-06-24 12:26:38 +02:00
|
|
|
assert.equal(entry.pendingAnnotations.get("👋").count, 1);
|
2021-06-18 15:09:14 +02:00
|
|
|
},
|
|
|
|
"local reaction removal gets applied after remote echo is added to timeline with reaction not loaded": async assert => {
|
|
|
|
const messageId = "!abc";
|
|
|
|
const reactionId = "!def";
|
|
|
|
// 1. put reaction in storage
|
|
|
|
const storage = await createMockStorage();
|
|
|
|
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
|
|
|
|
txn.timelineEvents.insert({
|
|
|
|
event: withContent(createAnnotation(messageId, "👋"), createEvent("m.reaction", reactionId, bob)),
|
|
|
|
fragmentId: 1, eventIndex: 3, roomId
|
|
|
|
});
|
|
|
|
await txn.complete();
|
|
|
|
// 2. setup timeline
|
|
|
|
const pendingEvents = new ObservableArray();
|
|
|
|
const timeline = new Timeline({roomId, storage, closeCallback: () => {},
|
|
|
|
fragmentIdComparer, pendingEvents, clock: new MockClock()});
|
|
|
|
await timeline.load(new User(bob), "join", new NullLogItem());
|
|
|
|
timeline.entries.subscribe(new ListObserver());
|
|
|
|
// 3. add local redaction for reaction
|
|
|
|
pendingEvents.append(new PendingEvent({data: {
|
|
|
|
roomId,
|
|
|
|
queueIndex: 1,
|
|
|
|
eventType: "m.room.redaction",
|
|
|
|
txnId: "t123",
|
|
|
|
content: {},
|
|
|
|
relatedEventId: reactionId
|
|
|
|
}}));
|
|
|
|
await poll(() => timeline.entries.length === 1);
|
|
|
|
// 4. add reaction target
|
2021-06-23 17:38:52 +02:00
|
|
|
timeline.addEntries([new EventEntry({
|
2021-06-18 15:09:14 +02:00
|
|
|
event: withTextBody("hi bob!", createEvent("m.room.message", messageId, alice)),
|
|
|
|
fragmentId: 1, eventIndex: 2}, fragmentIdComparer)
|
|
|
|
]);
|
|
|
|
await poll(() => timeline.entries.length === 2);
|
|
|
|
// 5. check that redaction was linked to reaction target
|
|
|
|
const entry = getIndexFromIterable(timeline.entries, 0);
|
2021-06-24 12:26:38 +02:00
|
|
|
assert.equal(entry.pendingAnnotations.get("👋").count, -1);
|
2021-06-18 15:09:14 +02:00
|
|
|
},
|
2021-06-23 17:38:52 +02:00
|
|
|
"decrypted entry preserves content when receiving other update without decryption": async assert => {
|
|
|
|
// 1. create encrypted and decrypted entry
|
|
|
|
const encryptedEntry = new EventEntry({
|
|
|
|
event: withContent({ciphertext: "abc"}, createEvent("m.room.encrypted", "!abc", alice)),
|
|
|
|
fragmentId: 1, eventIndex: 1, roomId
|
|
|
|
}, fragmentIdComparer);
|
|
|
|
const decryptedEntry = encryptedEntry.clone();
|
|
|
|
decryptedEntry.setDecryptionResult({
|
|
|
|
event: withTextBody("hi bob!", createEvent("m.room.message", encryptedEntry.id, encryptedEntry.sender))
|
|
|
|
});
|
|
|
|
// 2. setup the timeline
|
|
|
|
const timeline = new Timeline({roomId, storage: await createMockStorage(), closeCallback: () => {},
|
|
|
|
fragmentIdComparer, pendingEvents: new ObservableArray(), clock: new MockClock()});
|
|
|
|
await timeline.load(new User(alice), "join", new NullLogItem());
|
|
|
|
timeline.addEntries([decryptedEntry]);
|
|
|
|
const observer = new ListObserver();
|
|
|
|
timeline.entries.subscribe(observer);
|
|
|
|
// 3. replace the entry with one that is not decrypted
|
|
|
|
// (as would happen when receiving a reaction,
|
|
|
|
// as it does not rerun the decryption)
|
|
|
|
// and check that the decrypted content is preserved
|
|
|
|
timeline.replaceEntries([encryptedEntry]);
|
|
|
|
const {value, type} = await observer.next();
|
|
|
|
assert.equal(type, "update");
|
|
|
|
assert.equal(value.eventType, "m.room.message");
|
|
|
|
assert.equal(value.content.body, "hi bob!");
|
|
|
|
}
|
2021-06-18 14:39:54 +02:00
|
|
|
};
|
2021-06-04 16:05:28 +02:00
|
|
|
}
|