clean up rejoin storage logic somewhat

This commit is contained in:
Bruno Windels 2021-05-06 15:26:48 +02:00
parent f16c08f13e
commit 8c2ae863fd
2 changed files with 11 additions and 12 deletions

View file

@ -249,12 +249,12 @@ export class Room extends EventEmitter {
/** @package */ /** @package */
async writeSync(roomResponse, isInitialSync, {summaryChanges, decryptChanges, roomEncryption, retryEntries}, txn, log) { async writeSync(roomResponse, isInitialSync, {summaryChanges, decryptChanges, roomEncryption, retryEntries}, txn, log) {
log.set("id", this.id); log.set("id", this.id);
const isRejoin = summaryChanges.membership === "join" && this.membership !== "join"; const isRejoin = summaryChanges.isNewJoin(this._summary.data);
if (isRejoin) { if (isRejoin) {
// remove all room state before calling syncWriter, // remove all room state before calling syncWriter,
// so no old state sticks around // so no old state sticks around
txn.roomState.removeAllForRoom(this.id); txn.roomState.removeAllForRoom(this.id);
this._summary.tryRemoveArchive(txn); txn.archivedRoomSummary.remove(this.id);
} }
const {entries: newEntries, newLiveKey, memberChanges} = const {entries: newEntries, newLiveKey, memberChanges} =
await log.wrap("syncWriter", log => this._syncWriter.writeSync(roomResponse, isRejoin, txn, log), log.level.Detail); await log.wrap("syncWriter", log => this._syncWriter.writeSync(roomResponse, isRejoin, txn, log), log.level.Detail);
@ -282,9 +282,11 @@ export class Room extends EventEmitter {
// also apply (decrypted) timeline entries to the summary changes // also apply (decrypted) timeline entries to the summary changes
summaryChanges = summaryChanges.applyTimelineEntries( summaryChanges = summaryChanges.applyTimelineEntries(
allEntries, isInitialSync, !this._isTimelineOpen, this._user.id); allEntries, isInitialSync, !this._isTimelineOpen, this._user.id);
// only archive a room if we had previously joined it // only archive a room if we had previously joined it
if (summaryChanges.membership === "leave" && this.membership === "join") { if (summaryChanges.membership === "leave" && this.membership === "join") {
summaryChanges = this._summary.removeAndWriteArchive(summaryChanges, txn); txn.roomSummary.remove(this.id);
summaryChanges = this._summary.writeArchivedData(summaryChanges, txn);
} else { } else {
// write summary changes, and unset if nothing was actually changed // write summary changes, and unset if nothing was actually changed
summaryChanges = this._summary.writeData(summaryChanges, txn); summaryChanges = this._summary.writeData(summaryChanges, txn);
@ -356,8 +358,7 @@ export class Room extends EventEmitter {
} }
let emitChange = false; let emitChange = false;
if (summaryChanges) { if (summaryChanges) {
// if we joined the room, we can't have an invite anymore if (summaryChanges.isNewJoin(this._summary.data)) {
if (summaryChanges.membership === "join" && this.membership !== "join") {
this._invite = null; this._invite = null;
} }
this._summary.applyChanges(summaryChanges); this._summary.applyChanges(summaryChanges);

View file

@ -261,6 +261,10 @@ export class SummaryData {
get needsHeroes() { get needsHeroes() {
return !this.name && !this.canonicalAlias && this.heroes && this.heroes.length > 0; return !this.name && !this.canonicalAlias && this.heroes && this.heroes.length > 0;
} }
isNewJoin(oldData) {
return this.membership === "join" && oldData.membership !== "join";
}
} }
export class RoomSummary { export class RoomSummary {
@ -304,19 +308,13 @@ export class RoomSummary {
} }
/** move summary to archived store when leaving the room */ /** move summary to archived store when leaving the room */
removeAndWriteArchive(data, txn) { writeArchivedData(data, txn) {
txn.roomSummary.remove(data.roomId);
if (data !== this._data) { if (data !== this._data) {
txn.archivedRoomSummary.set(data.serialize()); txn.archivedRoomSummary.set(data.serialize());
return data; return data;
} }
} }
/** delete archived summary when rejoining the room */
tryRemoveArchive(txn) {
txn.archivedRoomSummary.remove(this._data.roomId);
}
async writeAndApplyData(data, storage) { async writeAndApplyData(data, storage) {
if (data === this._data) { if (data === this._data) {
return false; return false;