Merge remote-tracking branch 'origin/develop' into dbkr/dont_crash_if_no_dm_rooms
This commit is contained in:
commit
8e518af96c
5 changed files with 185 additions and 124 deletions
|
@ -21,18 +21,13 @@ limitations under the License.
|
|||
*/
|
||||
export default class DMRoomMap {
|
||||
constructor(matrixClient) {
|
||||
this.roomToUser = null;
|
||||
|
||||
const mDirectEvent = matrixClient.getAccountData('m.direct');
|
||||
if (!mDirectEvent) {
|
||||
this.userToRooms = {};
|
||||
this.roomToUser = {};
|
||||
} else {
|
||||
this.userToRooms = mDirectEvent.getContent();
|
||||
this.roomToUser = {};
|
||||
for (const user of Object.keys(this.userToRooms)) {
|
||||
for (const roomId of this.userToRooms[user]) {
|
||||
this.roomToUser[roomId] = user;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,8 +38,26 @@ export default class DMRoomMap {
|
|||
}
|
||||
|
||||
getUserIdForRoomId(roomId) {
|
||||
if (this.roomToUser == null) {
|
||||
// we lazily populate roomToUser so you can use
|
||||
// this class just to call getDMRoomsForUserId
|
||||
// which doesn't do very much, but is a fairly
|
||||
// convenient wrapper and there's no point
|
||||
// iterating through the map if getUserIdForRoomId()
|
||||
// is never called.
|
||||
this._populateRoomToUser();
|
||||
}
|
||||
// Here, we return undefined if the room is not in the map:
|
||||
// the room ID you gave is not a DM room for any user.
|
||||
return this.roomToUser[roomId];
|
||||
}
|
||||
|
||||
_populateRoomToUser() {
|
||||
this.roomToUser = {};
|
||||
for (const user of Object.keys(this.userToRooms)) {
|
||||
for (const roomId of this.userToRooms[user]) {
|
||||
this.roomToUser[roomId] = user;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue