mark room as DM based on synced state events,rather than just inviteData

as that does not work for rooms we create ourselves
This commit is contained in:
Bruno Windels 2022-02-08 14:34:34 +01:00
parent e04463c143
commit 45c8e3a793
4 changed files with 34 additions and 40 deletions

View file

@ -249,7 +249,7 @@ export class Sync {
if (!isRoomInResponse) { if (!isRoomInResponse) {
let room = this._session.rooms.get(roomId); let room = this._session.rooms.get(roomId);
if (room) { if (room) {
roomStates.push(new RoomSyncProcessState(room, false, null, {}, room.membership)); roomStates.push(new RoomSyncProcessState(room, false, {}, room.membership));
} }
} }
} }
@ -264,7 +264,7 @@ export class Sync {
await rs.room.load(null, prepareTxn, log); await rs.room.load(null, prepareTxn, log);
} }
return rs.room.prepareSync( return rs.room.prepareSync(
rs.roomResponse, rs.membership, rs.invite, newKeys, prepareTxn, log) rs.roomResponse, rs.membership, newKeys, prepareTxn, log)
}, log.level.Detail); }, log.level.Detail);
})); }));
@ -366,7 +366,7 @@ export class Sync {
if (invite) { if (invite) {
inviteStates.push(new InviteSyncProcessState(invite, false, null, membership)); inviteStates.push(new InviteSyncProcessState(invite, false, null, membership));
} }
const roomState = this._createRoomSyncState(roomId, invite, roomResponse, membership, isInitialSync); const roomState = this._createRoomSyncState(roomId, roomResponse, membership, isInitialSync);
if (roomState) { if (roomState) {
roomStates.push(roomState); roomStates.push(roomState);
} }
@ -381,7 +381,7 @@ export class Sync {
return {roomStates, archivedRoomStates}; return {roomStates, archivedRoomStates};
} }
_createRoomSyncState(roomId, invite, roomResponse, membership, isInitialSync) { _createRoomSyncState(roomId, roomResponse, membership, isInitialSync) {
let isNewRoom = false; let isNewRoom = false;
let room = this._session.rooms.get(roomId); let room = this._session.rooms.get(roomId);
// create room only either on new join, // create room only either on new join,
@ -397,7 +397,7 @@ export class Sync {
} }
if (room) { if (room) {
return new RoomSyncProcessState( return new RoomSyncProcessState(
room, isNewRoom, invite, roomResponse, membership); room, isNewRoom, roomResponse, membership);
} }
} }
@ -468,10 +468,9 @@ class SessionSyncProcessState {
} }
class RoomSyncProcessState { class RoomSyncProcessState {
constructor(room, isNewRoom, invite, roomResponse, membership) { constructor(room, isNewRoom, roomResponse, membership) {
this.room = room; this.room = room;
this.isNewRoom = isNewRoom; this.isNewRoom = isNewRoom;
this.invite = invite;
this.roomResponse = roomResponse; this.roomResponse = roomResponse;
this.membership = membership; this.membership = membership;
this.preparation = null; this.preparation = null;

View file

@ -188,7 +188,7 @@ export class Invite extends EventEmitter {
return { return {
roomId: this.id, roomId: this.id,
isEncrypted: !!summaryData.encryption, isEncrypted: !!summaryData.encryption,
isDirectMessage: this._isDirectMessage(myInvite), isDirectMessage: summaryData.isDirectMessage,
// type: // type:
name, name,
avatarUrl, avatarUrl,
@ -200,12 +200,8 @@ export class Invite extends EventEmitter {
}; };
} }
_isDirectMessage(myInvite) {
return !!(myInvite?.content?.is_direct);
}
_createSummaryData(inviteState) { _createSummaryData(inviteState) {
return inviteState.reduce(processStateEvent, new SummaryData(null, this.id)); return inviteState.reduce((data, event) => processStateEvent(data, event, this._user.id), new SummaryData(null, this.id));
} }
async _createHeroes(inviteState, log) { async _createHeroes(inviteState, log) {

View file

@ -54,15 +54,12 @@ export class Room extends BaseRoom {
return false; return false;
} }
async prepareSync(roomResponse, membership, invite, newKeys, txn, log) { async prepareSync(roomResponse, membership, newKeys, txn, log) {
log.set("id", this.id); log.set("id", this.id);
if (newKeys) { if (newKeys) {
log.set("newKeys", newKeys.length); log.set("newKeys", newKeys.length);
} }
let summaryChanges = this._summary.data.applySyncResponse(roomResponse, membership); let summaryChanges = this._summary.data.applySyncResponse(roomResponse, membership, this._user.id);
if (membership === "join" && invite) {
summaryChanges = summaryChanges.applyInvite(invite);
}
let roomEncryption = this._roomEncryption; let roomEncryption = this._roomEncryption;
// encryption is enabled in this sync // encryption is enabled in this sync
if (!roomEncryption && summaryChanges.encryption) { if (!roomEncryption && summaryChanges.encryption) {

View file

@ -46,7 +46,7 @@ export function reduceStateEvents(roomResponse, callback, value) {
return value; return value;
} }
function applySyncResponse(data, roomResponse, membership) { function applySyncResponse(data, roomResponse, membership, ownUserId) {
if (roomResponse.summary) { if (roomResponse.summary) {
data = updateSummary(data, roomResponse.summary); data = updateSummary(data, roomResponse.summary);
} }
@ -60,7 +60,7 @@ function applySyncResponse(data, roomResponse, membership) {
// process state events in state and in timeline. // process state events in state and in timeline.
// non-state events are handled by applyTimelineEntries // non-state events are handled by applyTimelineEntries
// so decryption is handled properly // so decryption is handled properly
data = reduceStateEvents(roomResponse, processStateEvent, data); data = reduceStateEvents(roomResponse, (data, event) => processStateEvent(data, event, ownUserId), data);
const unreadNotifications = roomResponse.unread_notifications; const unreadNotifications = roomResponse.unread_notifications;
if (unreadNotifications) { if (unreadNotifications) {
data = processNotificationCounts(data, unreadNotifications); data = processNotificationCounts(data, unreadNotifications);
@ -95,7 +95,7 @@ function processRoomAccountData(data, event) {
return data; return data;
} }
export function processStateEvent(data, event) { export function processStateEvent(data, event, ownUserId) {
if (event.type === "m.room.create") { if (event.type === "m.room.create") {
data = data.cloneIfNeeded(); data = data.cloneIfNeeded();
data.lastMessageTimestamp = event.origin_server_ts; data.lastMessageTimestamp = event.origin_server_ts;
@ -121,6 +121,25 @@ export function processStateEvent(data, event) {
const content = event.content; const content = event.content;
data = data.cloneIfNeeded(); data = data.cloneIfNeeded();
data.canonicalAlias = content.alias; data.canonicalAlias = content.alias;
} else if (event.type === "m.room.member") {
const content = event.content;
if (content.is_direct === true && content.membership === "invite" && !data.isDirectMessage) {
let other;
if (event.sender === ownUserId) {
other = event.state_key;
} else if (event.state_key === ownUserId) {
other = event.sender;
}
if (other) {
data = data.cloneIfNeeded();
data.isDirectMessage = true;
data.dmUserId = other;
}
} else if (content.membership === "leave" && data.isDirectMessage && data.dmUserId === event.state_key) {
data = data.cloneIfNeeded();
data.isDirectMessage = false;
data.dmUserId = null;
}
} }
return data; return data;
} }
@ -161,19 +180,6 @@ function updateSummary(data, summary) {
return data; return data;
} }
function applyInvite(data, invite) {
if (data.isDirectMessage !== invite.isDirectMessage) {
data = data.cloneIfNeeded();
data.isDirectMessage = invite.isDirectMessage;
if (invite.isDirectMessage) {
data.dmUserId = invite.inviter?.userId;
} else {
data.dmUserId = null;
}
}
return data;
}
export class SummaryData { export class SummaryData {
constructor(copy, roomId) { constructor(copy, roomId) {
this.roomId = copy ? copy.roomId : roomId; this.roomId = copy ? copy.roomId : roomId;
@ -230,12 +236,8 @@ export class SummaryData {
return applyTimelineEntries(this, timelineEntries, isInitialSync, canMarkUnread, ownUserId); return applyTimelineEntries(this, timelineEntries, isInitialSync, canMarkUnread, ownUserId);
} }
applySyncResponse(roomResponse, membership) { applySyncResponse(roomResponse, membership, ownUserId) {
return applySyncResponse(this, roomResponse, membership); return applySyncResponse(this, roomResponse, membership, ownUserId);
}
applyInvite(invite) {
return applyInvite(this, invite);
} }
get needsHeroes() { get needsHeroes() {