Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit fb0a0d5

Browse files
committed
allow self-chats
1 parent d8f15e1 commit fb0a0d5

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/utils/DMRoomMap.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ export default class DMRoomMap {
7272

7373
_onAccountData(ev) {
7474
if (ev.getType() == 'm.direct') {
75-
let userToRooms = this.matrixClient.getAccountData('m.direct').getContent();
75+
const userToRooms = this.matrixClient.getAccountData('m.direct').getContent() || {};
7676
const myUserId = this.matrixClient.getUserId();
7777
const selfDMs = userToRooms[myUserId];
7878
if (selfDMs && selfDMs.length) {
79-
this._patchUpSelfDMs(userToRooms);
79+
const neededPatching = this._patchUpSelfDMs(userToRooms);
8080
// to avoid multiple devices fighting to correct
8181
// the account data, only try to send the corrected
8282
// version once.
83-
if (!this._hasSentOutPatchDirectAccountDataPatch) {
83+
if (neededPatching && !this._hasSentOutPatchDirectAccountDataPatch) {
8484
this._hasSentOutPatchDirectAccountDataPatch = true;
8585
this.matrixClient.setAccountData('m.direct', userToRooms);
8686
}
@@ -98,25 +98,40 @@ export default class DMRoomMap {
9898
const myUserId = this.matrixClient.getUserId();
9999
const selfRoomIds = userToRooms[myUserId];
100100
if (selfRoomIds) {
101-
const guessedUserIds = selfRoomIds.map((roomId) => {
101+
// any self-chats that should not be self-chats?
102+
const guessedUserIdsThatChanged = selfRoomIds.map((roomId) => {
102103
const room = this.matrixClient.getRoom(roomId);
103-
return room && room.guessDMUserId();
104+
if (room) {
105+
const userId = room.guessDMUserId();
106+
if (userId && userId !== myUserId) {
107+
return {userId, roomId};
108+
}
109+
}
110+
}).filter((ids) => !!ids); //filter out
111+
// these are actually all legit self-chats
112+
// bail out
113+
if (!guessedUserIdsThatChanged.length) {
114+
return false;
115+
}
116+
userToRooms[myUserId] = selfRoomIds.filter((roomId) => {
117+
return guessedUserIdsThatChanged
118+
.some((ids) => ids.roomId === roomId);
104119
});
105-
delete userToRooms[myUserId];
106-
guessedUserIds.forEach((userId, i) => {
120+
121+
guessedUserIdsThatChanged.forEach(({userId, roomId}) => {
107122
if (!userId) {
108123
// if not able to guess the other user (unlikely)
109124
// still put it in the map so the room stays marked
110125
// as a DM, we just wont be able to show an avatar.
111126
userId = "";
112127
}
113-
const roomId = selfRoomIds[i];
114128
let roomIds = userToRooms[userId];
115129
if (!roomIds) {
116130
roomIds = userToRooms[userId] = [];
117131
}
118132
roomIds.push(roomId);
119133
});
134+
return true;
120135
}
121136
}
122137

0 commit comments

Comments
 (0)