This commit is contained in:
Bruno Windels 2021-08-27 17:23:32 +02:00
parent 826de7e9cb
commit a61f052fe8
2 changed files with 31 additions and 57 deletions

View File

@ -191,32 +191,6 @@ export function tests() {
}; };
} }
function createRoomResponse(stateEvents, timelineEvents) {
if (!Array.isArray(timelineEvents)) {
timelineEvents = [timelineEvents];
}
if (!Array.isArray(stateEvents)) {
stateEvents = [stateEvents];
}
return {
timeline: {
limited: false,
events: timelineEvents,
},
state: {
events: stateEvents
}
};
}
function createTimelineResponse(timelineEvents) {
return createRoomResponse([], timelineEvents)
}
function createStateReponse(stateEvents) {
return createRoomResponse(stateEvents, []);
}
function createStorage(initialMembers = []) { function createStorage(initialMembers = []) {
const members = new Map(); const members = new Map();
for (const m of initialMembers) { for (const m of initialMembers) {

View File

@ -149,35 +149,37 @@ export class SyncWriter {
const entries = []; const entries = [];
const updatedEntries = []; const updatedEntries = [];
// only create a fragment when we will really write an event // only create a fragment when we will really write an event
currentKey = await this._ensureLiveFragment(currentKey, entries, timeline, txn, log); if (timelineEvents?.length) {
log.set("timelineEvents", timelineEvents.length); currentKey = await this._ensureLiveFragment(currentKey, entries, timeline, txn, log);
let timelineStateEventCount = 0; log.set("timelineEvents", timelineEvents.length);
for(const event of timelineEvents) { let timelineStateEventCount = 0;
// store event in timeline for(const event of timelineEvents) {
currentKey = currentKey.nextKey(); // store event in timeline
const storageEntry = createEventEntry(currentKey, this._roomId, event); currentKey = currentKey.nextKey();
let member = await memberSync.lookupMemberAtEvent(event.sender, event, txn); const storageEntry = createEventEntry(currentKey, this._roomId, event);
if (member) { let member = await memberSync.lookupMemberAtEvent(event.sender, event, txn);
storageEntry.displayName = member.displayName; if (member) {
storageEntry.avatarUrl = member.avatarUrl; storageEntry.displayName = member.displayName;
} storageEntry.avatarUrl = member.avatarUrl;
txn.timelineEvents.insert(storageEntry); }
const entry = new EventEntry(storageEntry, this._fragmentIdComparer); txn.timelineEvents.insert(storageEntry);
entries.push(entry); const entry = new EventEntry(storageEntry, this._fragmentIdComparer);
const updatedRelationTargetEntries = await this._relationWriter.writeRelation(entry, txn, log); entries.push(entry);
if (updatedRelationTargetEntries) { const updatedRelationTargetEntries = await this._relationWriter.writeRelation(entry, txn, log);
updatedEntries.push(...updatedRelationTargetEntries); if (updatedRelationTargetEntries) {
} updatedEntries.push(...updatedRelationTargetEntries);
// update state events after writing event, so for a member event, }
// we only update the member info after having written the member event // update state events after writing event, so for a member event,
// to the timeline, as we want that event to have the old profile info. // we only update the member info after having written the member event
// member events are written prior by MemberWriter. // to the timeline, as we want that event to have the old profile info.
if (typeof event.state_key === "string" && event.type !== MEMBER_EVENT_TYPE) { // member events are written prior by MemberWriter.
timelineStateEventCount += 1; if (typeof event.state_key === "string" && event.type !== MEMBER_EVENT_TYPE) {
txn.roomState.set(this._roomId, event); timelineStateEventCount += 1;
txn.roomState.set(this._roomId, event);
}
} }
log.set("timelineStateEventCount", timelineStateEventCount);
} }
log.set("timelineStateEventCount", timelineStateEventCount);
return {currentKey, entries, updatedEntries}; return {currentKey, entries, updatedEntries};
} }
@ -236,10 +238,8 @@ export class SyncWriter {
if (stateEvents) { if (stateEvents) {
await this._writeStateEvents(roomResponse, txn, log); await this._writeStateEvents(roomResponse, txn, log);
} }
if (timelineEvents?.length) { const {currentKey, entries, updatedEntries} =
const {currentKey, entries, updatedEntries} = await this._writeTimeline(timelineEvents, timeline, memberSync, this._lastLiveKey, txn, log);
await this._writeTimeline(timelineEvents, timeline, memberSync, this._lastLiveKey, txn, log);
}
const memberChanges = await memberSync.write(txn); const memberChanges = await memberSync.write(txn);
return {entries, updatedEntries, newLiveKey: currentKey, memberChanges}; return {entries, updatedEntries, newLiveKey: currentKey, memberChanges};
} }