Merge pull request #2146 from matrix-org/bwindels/fixavatars-parttrois
Fix DM avatars, part 3
This commit is contained in:
commit
a6d241c29f
1 changed files with 35 additions and 32 deletions
|
@ -15,6 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import MatrixClientPeg from '../MatrixClientPeg';
|
import MatrixClientPeg from '../MatrixClientPeg';
|
||||||
|
import _uniq from 'lodash/uniq';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that takes a Matrix Client and flips the m.direct map
|
* Class that takes a Matrix Client and flips the m.direct map
|
||||||
|
@ -36,11 +37,8 @@ export default class DMRoomMap {
|
||||||
this._onAccountData = this._onAccountData.bind(this);
|
this._onAccountData = this._onAccountData.bind(this);
|
||||||
|
|
||||||
const mDirectEvent = matrixClient.getAccountData('m.direct');
|
const mDirectEvent = matrixClient.getAccountData('m.direct');
|
||||||
if (!mDirectEvent) {
|
this.mDirectEvent = mDirectEvent ? mDirectEvent.getContent() : {};
|
||||||
this.userToRooms = {};
|
this.userToRooms = null;
|
||||||
} else {
|
|
||||||
this.userToRooms = mDirectEvent.getContent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,21 +70,9 @@ export default class DMRoomMap {
|
||||||
|
|
||||||
_onAccountData(ev) {
|
_onAccountData(ev) {
|
||||||
if (ev.getType() == 'm.direct') {
|
if (ev.getType() == 'm.direct') {
|
||||||
const userToRooms = this.matrixClient.getAccountData('m.direct').getContent() || {};
|
this.mDirectEvent = this.matrixClient.getAccountData('m.direct').getContent() || {};
|
||||||
const myUserId = this.matrixClient.getUserId();
|
this.userToRooms = null;
|
||||||
const selfDMs = userToRooms[myUserId];
|
this.roomToUser = null;
|
||||||
if (selfDMs && selfDMs.length) {
|
|
||||||
const neededPatching = this._patchUpSelfDMs(userToRooms);
|
|
||||||
// to avoid multiple devices fighting to correct
|
|
||||||
// the account data, only try to send the corrected
|
|
||||||
// version once.
|
|
||||||
if (neededPatching && !this._hasSentOutPatchDirectAccountDataPatch) {
|
|
||||||
this._hasSentOutPatchDirectAccountDataPatch = true;
|
|
||||||
this.matrixClient.setAccountData('m.direct', userToRooms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.userToRooms = userToRooms;
|
|
||||||
this._populateRoomToUser();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -114,22 +100,17 @@ export default class DMRoomMap {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
userToRooms[myUserId] = selfRoomIds.filter((roomId) => {
|
userToRooms[myUserId] = selfRoomIds.filter((roomId) => {
|
||||||
return guessedUserIdsThatChanged
|
return !guessedUserIdsThatChanged
|
||||||
.some((ids) => ids.roomId === roomId);
|
.some((ids) => ids.roomId === roomId);
|
||||||
});
|
});
|
||||||
|
|
||||||
guessedUserIdsThatChanged.forEach(({userId, roomId}) => {
|
guessedUserIdsThatChanged.forEach(({userId, roomId}) => {
|
||||||
if (!userId) {
|
|
||||||
// if not able to guess the other user (unlikely)
|
|
||||||
// still put it in the map so the room stays marked
|
|
||||||
// as a DM, we just wont be able to show an avatar.
|
|
||||||
userId = "";
|
|
||||||
}
|
|
||||||
let roomIds = userToRooms[userId];
|
let roomIds = userToRooms[userId];
|
||||||
if (!roomIds) {
|
if (!roomIds) {
|
||||||
roomIds = userToRooms[userId] = [];
|
userToRooms[userId] = [roomId];
|
||||||
|
} else {
|
||||||
|
roomIds.push(roomId);
|
||||||
|
userToRooms[userId] = _uniq(roomIds);
|
||||||
}
|
}
|
||||||
roomIds.push(roomId);
|
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -138,7 +119,7 @@ export default class DMRoomMap {
|
||||||
getDMRoomsForUserId(userId) {
|
getDMRoomsForUserId(userId) {
|
||||||
// Here, we return the empty list if there are no rooms,
|
// Here, we return the empty list if there are no rooms,
|
||||||
// since the number of conversations you have with this user is zero.
|
// since the number of conversations you have with this user is zero.
|
||||||
return this.userToRooms[userId] || [];
|
return this._getUserToRooms()[userId] || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
getUserIdForRoomId(roomId) {
|
getUserIdForRoomId(roomId) {
|
||||||
|
@ -163,9 +144,31 @@ export default class DMRoomMap {
|
||||||
return this.roomToUser[roomId];
|
return this.roomToUser[roomId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getUserToRooms() {
|
||||||
|
if (!this.userToRooms) {
|
||||||
|
const userToRooms = this.mDirectEvent;
|
||||||
|
const myUserId = this.matrixClient.getUserId();
|
||||||
|
const selfDMs = userToRooms[myUserId];
|
||||||
|
if (selfDMs && selfDMs.length) {
|
||||||
|
const neededPatching = this._patchUpSelfDMs(userToRooms);
|
||||||
|
// to avoid multiple devices fighting to correct
|
||||||
|
// the account data, only try to send the corrected
|
||||||
|
// version once.
|
||||||
|
console.warn(`Invalid m.direct account data detected ` +
|
||||||
|
`(self-chats that shouldn't be), patching it up.`);
|
||||||
|
if (neededPatching && !this._hasSentOutPatchDirectAccountDataPatch) {
|
||||||
|
this._hasSentOutPatchDirectAccountDataPatch = true;
|
||||||
|
this.matrixClient.setAccountData('m.direct', userToRooms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.userToRooms = userToRooms;
|
||||||
|
}
|
||||||
|
return this.userToRooms;
|
||||||
|
}
|
||||||
|
|
||||||
_populateRoomToUser() {
|
_populateRoomToUser() {
|
||||||
this.roomToUser = {};
|
this.roomToUser = {};
|
||||||
for (const user of Object.keys(this.userToRooms)) {
|
for (const user of Object.keys(this._getUserToRooms())) {
|
||||||
for (const roomId of this.userToRooms[user]) {
|
for (const roomId of this.userToRooms[user]) {
|
||||||
this.roomToUser[roomId] = user;
|
this.roomToUser[roomId] = user;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue