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

Commit 75a2668

Browse files
authored
Merge pull request #2141 from matrix-org/bwindels/fixdmavatar
Fix DM avatar
2 parents 6d158d3 + fb0a0d5 commit 75a2668

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

src/components/views/avatars/RoomAvatar.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,7 @@ module.exports = React.createClass({
119119
} else {
120120
// if the room is not marked as a 1:1, but only has max 2 members
121121
// then still try to show any avatar (pref. other member)
122-
const totalMemberCount = room.getJoinedMemberCount() +
123-
room.getInvitedMemberCount();
124-
const members = room.currentState.getMembers();
125-
if (totalMemberCount == 2) {
126-
const myUserId = MatrixClientPeg.get().getUserId();
127-
otherMember = members.find(m => m.userId !== myUserId);
128-
} else if (totalMemberCount == 1) {
129-
otherMember = members[0];
130-
}
122+
otherMember = room.getAvatarFallbackMember();
131123
}
132124
if (otherMember) {
133125
return otherMember.getAvatarUrl(

src/utils/DMRoomMap.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default class DMRoomMap {
2727
constructor(matrixClient) {
2828
this.matrixClient = matrixClient;
2929
this.roomToUser = null;
30+
// see _onAccountData
31+
this._hasSentOutPatchDirectAccountDataPatch = false;
3032

3133
// XXX: Force-bind the event handler method because it
3234
// doesn't call it with our object as the 'this'
@@ -70,10 +72,68 @@ export default class DMRoomMap {
7072

7173
_onAccountData(ev) {
7274
if (ev.getType() == 'm.direct') {
73-
this.userToRooms = this.matrixClient.getAccountData('m.direct').getContent();
75+
const userToRooms = this.matrixClient.getAccountData('m.direct').getContent() || {};
76+
const myUserId = this.matrixClient.getUserId();
77+
const selfDMs = userToRooms[myUserId];
78+
if (selfDMs && selfDMs.length) {
79+
const neededPatching = this._patchUpSelfDMs(userToRooms);
80+
// to avoid multiple devices fighting to correct
81+
// the account data, only try to send the corrected
82+
// version once.
83+
if (neededPatching && !this._hasSentOutPatchDirectAccountDataPatch) {
84+
this._hasSentOutPatchDirectAccountDataPatch = true;
85+
this.matrixClient.setAccountData('m.direct', userToRooms);
86+
}
87+
}
88+
this.userToRooms = userToRooms;
7489
this._populateRoomToUser();
7590
}
7691
}
92+
/**
93+
* some client bug somewhere is causing some DMs to be marked
94+
* with ourself, not the other user. Fix it by guessing the other user and
95+
* modifying userToRooms
96+
*/
97+
_patchUpSelfDMs(userToRooms) {
98+
const myUserId = this.matrixClient.getUserId();
99+
const selfRoomIds = userToRooms[myUserId];
100+
if (selfRoomIds) {
101+
// any self-chats that should not be self-chats?
102+
const guessedUserIdsThatChanged = selfRoomIds.map((roomId) => {
103+
const room = this.matrixClient.getRoom(roomId);
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);
119+
});
120+
121+
guessedUserIdsThatChanged.forEach(({userId, roomId}) => {
122+
if (!userId) {
123+
// if not able to guess the other user (unlikely)
124+
// still put it in the map so the room stays marked
125+
// as a DM, we just wont be able to show an avatar.
126+
userId = "";
127+
}
128+
let roomIds = userToRooms[userId];
129+
if (!roomIds) {
130+
roomIds = userToRooms[userId] = [];
131+
}
132+
roomIds.push(roomId);
133+
});
134+
return true;
135+
}
136+
}
77137

78138
getDMRoomsForUserId(userId) {
79139
// Here, we return the empty list if there are no rooms,

0 commit comments

Comments
 (0)