Reuse code in getOrLoadEntry

This commit is contained in:
RMidhunSuresh 2021-12-13 12:52:03 +05:30
parent f5fadf700e
commit d1818d2a57

View file

@ -290,7 +290,7 @@ export class Timeline {
const id = entry.contextEventId; const id = entry.contextEventId;
let contextEvent = this._getTrackedEvent(id); let contextEvent = this._getTrackedEvent(id);
if (!contextEvent) { if (!contextEvent) {
contextEvent = await this._timelineReader.readById(id) ?? await this._getEventFromHomeserver(id); contextEvent = await this._getEventFromStorage(id) ?? await this._getEventFromHomeserver(id);
// this entry was created from storage/hs, so it's not tracked by remoteEntries // this entry was created from storage/hs, so it's not tracked by remoteEntries
// we track them here so that we can update reply preview of dependents on redaction // we track them here so that we can update reply preview of dependents on redaction
this._fetchedEventEntries.set(id, contextEvent); this._fetchedEventEntries.set(id, contextEvent);
@ -308,6 +308,11 @@ export class Timeline {
return this.getByEventId(id) ?? this._fetchedEventEntries.get(id); return this.getByEventId(id) ?? this._fetchedEventEntries.get(id);
} }
async _getEventFromStorage(eventId) {
const entry = await this._timelineReader.readById(eventId);
return entry;
}
async _getEventFromHomeserver(eventId) { async _getEventFromHomeserver(eventId) {
const response = await this._hsApi.context(this._roomId, eventId, 0).response(); const response = await this._hsApi.context(this._roomId, eventId, 0).response();
const sender = response.event.sender; const sender = response.event.sender;
@ -364,18 +369,7 @@ export class Timeline {
} }
} }
if (eventId) { if (eventId) {
const loadedEntry = this.getByEventId(eventId); return this.getByEventId(eventId) ?? await this._getEventFromStorage(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; return null;
} }