This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/room/timeline/Timeline.js

394 lines
16 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2021 The Matrix.org Foundation C.I.C.
2020-08-05 22:08:55 +05:30
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.
*/
import {SortedArray, MappedList, ConcatList, ObservableArray} from "../../../observable/index.js";
2020-09-10 20:10:30 +05:30
import {Disposables} from "../../../utils/Disposables.js";
import {Direction} from "./Direction.js";
import {TimelineReader} from "./persistence/TimelineReader.js";
import {PendingEventEntry} from "./entries/PendingEventEntry.js";
import {RoomMember} from "../members/RoomMember.js";
import {PowerLevels} from "./PowerLevels.js";
2021-06-08 20:26:17 +05:30
import {getRelationFromContent, getRelation, ANNOTATION_RELATION_TYPE} from "./relations.js";
2021-06-09 20:22:30 +05:30
import {REDACTION_TYPE} from "../common.js";
export class Timeline {
constructor({roomId, storage, closeCallback, fragmentIdComparer, pendingEvents, clock}) {
this._roomId = roomId;
this._storage = storage;
this._closeCallback = closeCallback;
this._fragmentIdComparer = fragmentIdComparer;
2020-09-10 20:10:30 +05:30
this._disposables = new Disposables();
this._pendingEvents = pendingEvents;
this._clock = clock;
// constructing this early avoid some problem while sync and openTimeline race
this._remoteEntries = new SortedArray((a, b) => a.compare(b));
this._ownMember = null;
2019-05-20 00:19:46 +05:30
this._timelineReader = new TimelineReader({
roomId: this._roomId,
storage: this._storage,
fragmentIdComparer: this._fragmentIdComparer
});
2020-09-10 20:10:30 +05:30
this._readerRequest = null;
this._allEntries = null;
this._powerLevels = null;
}
/** @package */
async load(user, membership, log) {
const txn = await this._storage.readTxn(this._timelineReader.readTxnStores.concat(
this._storage.storeNames.roomMembers,
this._storage.storeNames.roomState
));
const memberData = await txn.roomMembers.get(this._roomId, user.id);
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);
}
// 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
this._powerLevels = await this._loadPowerLevels(txn);
2020-09-10 20:10:30 +05:30
// 30 seems to be a good amount to fill the entire screen
const readerRequest = this._disposables.track(this._timelineReader.readFromEnd(30, txn, log));
2020-09-10 20:10:30 +05:30
try {
const entries = await readerRequest.complete();
this._setupEntries(entries);
2020-09-10 20:10:30 +05:30
} finally {
this._disposables.disposeTracked(readerRequest);
}
// txn should be assumed to have finished here, as decryption will close it.
}
async _loadPowerLevels(txn) {
// TODO: update power levels as state is updated
const powerLevelsState = await txn.roomState.get(this._roomId, "m.room.power_levels", "");
if (powerLevelsState) {
return new PowerLevels({
powerLevelEvent: powerLevelsState.event,
ownUserId: this._ownMember.userId
});
}
const createState = await txn.roomState.get(this._roomId, "m.room.create", "");
if (createState) {
return new PowerLevels({
createEvent: createState.event,
ownUserId: this._ownMember.userId
});
} else {
return new PowerLevels({ownUserId: this._ownMember.userId});
}
}
_setupEntries(timelineEntries) {
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});
2021-06-08 16:50:55 +05:30
this._onAddPendingEvent(pee);
return pee;
}, (pee, params) => {
// is sending but redacted, who do we detect that here to remove the relation?
pee.notifyUpdate(params);
2021-06-08 16:50:55 +05:30
}, pee => this._onRemovePendingEvent(pee));
} else {
this._localEntries = new ObservableArray();
}
this._allEntries = new ConcatList(this._remoteEntries, this._localEntries);
}
2021-06-08 16:50:55 +05:30
_onAddPendingEvent(pee) {
let redactedEntry;
this._applyAndEmitLocalRelationChange(pee.pendingEvent, target => {
const wasRedacted = target.isRedacted;
const params = target.addLocalRelation(pee);
if (!wasRedacted && target.isRedacted) {
redactedEntry = target;
}
return params;
});
if (redactedEntry) {
2021-06-09 20:22:30 +05:30
this._addLocallyRedactedRelationToTarget(redactedEntry);
}
}
_addLocallyRedactedRelationToTarget(redactedEntry) {
const redactedRelation = getRelationFromContent(redactedEntry.content);
if (redactedRelation?.event_id) {
const found = this._remoteEntries.findAndUpdate(
e => e.id === redactedRelation.event_id,
relationTarget => relationTarget.addLocalRelation(redactedEntry) || false
);
2021-06-08 16:50:55 +05:30
}
}
_onRemovePendingEvent(pee) {
let unredactedEntry;
this._applyAndEmitLocalRelationChange(pee.pendingEvent, target => {
const wasRedacted = target.isRedacted;
const params = target.removeLocalRelation(pee);
if (wasRedacted && !target.isRedacted) {
unredactedEntry = target;
}
return params;
});
if (unredactedEntry) {
const redactedRelation = getRelationFromContent(unredactedEntry.content);
if (redactedRelation?.event_id) {
this._remoteEntries.findAndUpdate(
e => e.id === redactedRelation.event_id,
relationTarget => relationTarget.removeLocalRelation(unredactedEntry) || false
);
}
}
}
_applyAndEmitLocalRelationChange(pe, updater) {
const updateOrFalse = e => {
const params = updater(e);
return params ? params : false;
};
// first, look in local entries based on txn id
if (pe.relatedTxnId) {
const found = this._localEntries.findAndUpdate(
e => e.id === pe.relatedTxnId,
updateOrFalse,
);
if (found) {
return;
}
}
// now look in remote entries based on event id
if (pe.relatedEventId) {
this._remoteEntries.findAndUpdate(
e => e.id === pe.relatedEventId,
updateOrFalse
);
}
}
2021-06-08 20:26:17 +05:30
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);
if (annotation.event.sender === this._ownMember.userId && getRelation(annotation.event).key === key) {
const eventEntry = new EventEntry(annotation, this._fragmentIdComparer);
this._addLocalRelationsToNewRemoteEntries([eventEntry]);
return eventEntry;
}
}
return null;
}
updateOwnMember(member) {
this._ownMember = member;
}
replaceEntries(entries) {
this._addLocalRelationsToNewRemoteEntries(entries);
for (const entry of entries) {
this._remoteEntries.update(entry);
}
}
_addLocalRelationsToNewRemoteEntries(entries) {
2021-06-02 22:12:46 +05:30
// 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.
//
// _localEntries can also not yet exist
if (!this._localEntries?.hasSubscriptions) {
2021-06-02 22:12:46 +05:30
return;
}
// 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-06-09 20:22:30 +05:30
const relationTarget = entries.find(e => e.id === pee.relatedEventId);
2021-06-09 20:22:30 +05:30
if (relationTarget) {
const wasRedacted = relationTarget.isRedacted;
// no need to emit here as this entry is about to be added
relationTarget.addLocalRelation(pee);
if (!wasRedacted && relationTarget.isRedacted) {
this._addLocallyRedactedRelationToTarget(relationTarget);
}
} else if (pee.eventType === REDACTION_TYPE) {
// if pee is a redaction, we need to lookup the event it is redacting,
// and see if that is a relation of one of the entries
const redactedEntry = this.getByEventId(pee.relatedEventId);
if (redactedEntry) {
const relation = getRelation(redactedEntry);
if (relation) {
const redactedRelationTarget = entries.find(e => e.id === relation.event_id);
redactedRelationTarget?.addLocalRelation(redactedEntry);
}
}
} else {
// TODO: errors are swallowed here
// console.log(`could not find target for pee ${pee.relatedEventId} ` + entries.filter(e => !["m.reaction", "m.room.redaction"].includes(e.eventType)).map(e => `${e.id}: ${e.content?.body}`).join(","));
// console.log(`could not find target for pee ${pee.relatedEventId} ` + entries.filter(e => "m.reaction" === e.eventType).map(e => `${e.id}: ${getRelation(e)?.key}`).join(","));
// console.log(`could not find target for pee ${pee.relatedEventId} ` + entries.map(e => `${e.id}: ${e._eventEntry.key.substr(e._eventEntry.key.lastIndexOf("|") + 1)}`).join(","));
}
}
}
}
/** @package */
addOrReplaceEntries(newEntries) {
this._addLocalRelationsToNewRemoteEntries(newEntries);
this._remoteEntries.setManySorted(newEntries);
2019-03-09 05:11:06 +05:30
}
2020-03-22 04:10:40 +05:30
// tries to prepend `amount` entries to the `entries` list.
/**
* [loadAtTop description]
* @param {[type]} amount [description]
* @return {boolean} true if the top of the timeline has been reached
*
*/
2019-03-09 05:11:06 +05:30
async loadAtTop(amount) {
if (this._disposables.isDisposed) {
return true;
}
const firstEventEntry = this._remoteEntries.array.find(e => !!e.eventType);
2019-06-16 20:59:33 +05:30
if (!firstEventEntry) {
return true;
2019-03-09 05:11:06 +05:30
}
2020-09-10 20:10:30 +05:30
const readerRequest = this._disposables.track(this._timelineReader.readFrom(
2019-06-16 20:59:33 +05:30
firstEventEntry.asEventKey(),
Direction.Backward,
amount
2020-09-10 20:10:30 +05:30
));
try {
const entries = await readerRequest.complete();
this.addOrReplaceEntries(entries);
return entries.length < amount;
2020-09-10 20:10:30 +05:30
} finally {
this._disposables.disposeTracked(readerRequest);
}
}
getByEventId(eventId) {
for (let i = 0; i < this._remoteEntries.length; i += 1) {
const entry = this._remoteEntries.get(i);
if (entry.id === eventId) {
return entry;
}
}
return null;
}
/** @public */
get entries() {
return this._allEntries;
}
/**
* @internal
* @return {Array<EventEntry>} remote event entries, should not be modified
*/
get remoteEntries() {
return this._remoteEntries.array;
}
/** @public */
dispose() {
2020-05-05 01:51:56 +05:30
if (this._closeCallback) {
this._disposables.dispose();
2020-05-05 01:51:56 +05:30
this._closeCallback();
this._closeCallback = null;
}
}
/** @internal */
enableEncryption(decryptEntries) {
this._timelineReader.enableEncryption(decryptEntries);
}
get powerLevels() {
return this._powerLevels;
}
get me() {
return this._ownMember;
}
}
import {FragmentIdComparer} from "./FragmentIdComparer.js";
import {Clock as MockClock} from "../../../mocks/Clock.js";
import {createMockStorage} from "../../../mocks/Storage.js";
import {createEvent, withTextBody, withSender} from "../../../mocks/event.js";
import {NullLogItem} from "../../../logging/NullLogger.js";
import {EventEntry} from "./entries/EventEntry.js";
import {User} from "../../User.js";
import {PendingEvent} from "../sending/PendingEvent.js";
export function tests() {
const fragmentIdComparer = new FragmentIdComparer([]);
const roomId = "$abc";
return {
"adding or replacing entries before subscribing to entries does not loose local relations": async assert => {
const pendingEvents = new ObservableArray();
const timeline = new Timeline({
roomId,
storage: await createMockStorage(),
closeCallback: () => {},
fragmentIdComparer,
pendingEvents,
clock: new MockClock(),
});
// 1. load timeline
await timeline.load(new User("@alice:hs.tld"), "join", new NullLogItem());
// 2. test replaceEntries and addOrReplaceEntries don't fail
const event1 = withTextBody("hi!", withSender("@bob:hs.tld", createEvent("m.room.message", "!abc")));
const entry1 = new EventEntry({event: event1, fragmentId: 1, eventIndex: 1}, fragmentIdComparer);
timeline.replaceEntries([entry1]);
const event2 = withTextBody("hi bob!", withSender("@alice:hs.tld", createEvent("m.room.message", "!def")));
const entry2 = new EventEntry({event: event2, fragmentId: 1, eventIndex: 2}, fragmentIdComparer);
timeline.addOrReplaceEntries([entry2]);
// 3. add local relation (redaction)
pendingEvents.append(new PendingEvent({data: {
roomId,
queueIndex: 1,
eventType: "m.room.redaction",
txnId: "t123",
content: {},
relatedEventId: event2.event_id
}}));
// 4. subscribe (it's now safe to iterate timeline.entries)
timeline.entries.subscribe({});
// 5. check the local relation got correctly aggregated
assert.equal(Array.from(timeline.entries)[0].isRedacting, true);
}
}
}