2016-09-06 18:39:21 +03:00
|
|
|
/*
|
2021-01-13 17:49:23 +03:00
|
|
|
Copyright 2016, 2019, 2021 The Matrix.org Foundation C.I.C.
|
2016-09-06 18:39:21 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../MatrixClientPeg';
|
2020-08-28 20:53:43 +03:00
|
|
|
import {uniq} from "lodash";
|
2021-01-13 17:49:23 +03:00
|
|
|
import {Room} from "matrix-js-sdk/src/models/room";
|
|
|
|
import {Event} from "matrix-js-sdk/src/models/event";
|
|
|
|
import {MatrixClient} from "matrix-js-sdk/src/client";
|
2016-09-26 20:02:14 +03:00
|
|
|
|
2016-09-06 18:39:21 +03:00
|
|
|
/**
|
|
|
|
* Class that takes a Matrix Client and flips the m.direct map
|
|
|
|
* so the operation of mapping a room ID to which user it's a DM
|
|
|
|
* with can be performed efficiently.
|
2016-09-26 20:02:14 +03:00
|
|
|
*
|
|
|
|
* With 'start', this can also keep itself up to date over time.
|
2016-09-06 18:39:21 +03:00
|
|
|
*/
|
|
|
|
export default class DMRoomMap {
|
2021-01-13 17:49:23 +03:00
|
|
|
private static sharedInstance: DMRoomMap;
|
|
|
|
|
|
|
|
private matrixClient: MatrixClient;
|
|
|
|
// TODO: convert these to maps
|
|
|
|
private roomToUser: {[key: string]: string} = null;
|
|
|
|
private userToRooms: {[key: string]: string[]} = null;
|
|
|
|
private hasSentOutPatchDirectAccountDataPatch: boolean;
|
|
|
|
private mDirectEvent: Event;
|
|
|
|
|
2016-09-06 18:39:21 +03:00
|
|
|
constructor(matrixClient) {
|
2016-09-12 20:32:44 +03:00
|
|
|
this.matrixClient = matrixClient;
|
2021-01-13 17:49:23 +03:00
|
|
|
// see onAccountData
|
|
|
|
this.hasSentOutPatchDirectAccountDataPatch = false;
|
2016-09-26 20:02:14 +03:00
|
|
|
|
2016-09-06 18:39:21 +03:00
|
|
|
const mDirectEvent = matrixClient.getAccountData('m.direct');
|
2018-09-04 17:00:40 +03:00
|
|
|
this.mDirectEvent = mDirectEvent ? mDirectEvent.getContent() : {};
|
2016-09-06 18:39:21 +03:00
|
|
|
}
|
|
|
|
|
2016-09-27 11:56:31 +03:00
|
|
|
/**
|
|
|
|
* Makes and returns a new shared instance that can then be accessed
|
|
|
|
* with shared(). This returned instance is not automatically started.
|
|
|
|
*/
|
2021-01-13 18:37:49 +03:00
|
|
|
public static makeShared(): DMRoomMap {
|
2021-01-13 17:49:23 +03:00
|
|
|
DMRoomMap.sharedInstance = new DMRoomMap(MatrixClientPeg.get());
|
|
|
|
return DMRoomMap.sharedInstance;
|
2016-09-27 11:56:31 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 20:02:14 +03:00
|
|
|
/**
|
|
|
|
* Returns a shared instance of the class
|
|
|
|
* that uses the singleton matrix client
|
|
|
|
* The shared instance must be started before use.
|
|
|
|
*/
|
2021-01-13 18:37:49 +03:00
|
|
|
public static shared(): DMRoomMap {
|
2021-01-13 17:49:23 +03:00
|
|
|
return DMRoomMap.sharedInstance;
|
2016-09-26 20:02:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
public start() {
|
|
|
|
this.populateRoomToUser();
|
2021-01-13 17:49:23 +03:00
|
|
|
this.matrixClient.on("accountData", this.onAccountData);
|
2016-09-26 20:02:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
public stop() {
|
2021-01-13 17:49:23 +03:00
|
|
|
this.matrixClient.removeListener("accountData", this.onAccountData);
|
2016-09-26 20:02:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:49:23 +03:00
|
|
|
private onAccountData = (ev) => {
|
2016-09-26 20:02:14 +03:00
|
|
|
if (ev.getType() == 'm.direct') {
|
2018-09-04 17:00:40 +03:00
|
|
|
this.mDirectEvent = this.matrixClient.getAccountData('m.direct').getContent() || {};
|
|
|
|
this.userToRooms = null;
|
|
|
|
this.roomToUser = null;
|
2016-09-26 20:02:14 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-13 17:49:23 +03:00
|
|
|
|
2018-08-30 11:53:25 +03:00
|
|
|
/**
|
|
|
|
* some client bug somewhere is causing some DMs to be marked
|
|
|
|
* with ourself, not the other user. Fix it by guessing the other user and
|
|
|
|
* modifying userToRooms
|
|
|
|
*/
|
2021-01-13 18:37:49 +03:00
|
|
|
private patchUpSelfDMs(userToRooms) {
|
2018-08-30 11:53:25 +03:00
|
|
|
const myUserId = this.matrixClient.getUserId();
|
|
|
|
const selfRoomIds = userToRooms[myUserId];
|
|
|
|
if (selfRoomIds) {
|
2018-08-30 13:36:53 +03:00
|
|
|
// any self-chats that should not be self-chats?
|
|
|
|
const guessedUserIdsThatChanged = selfRoomIds.map((roomId) => {
|
2018-08-30 11:53:25 +03:00
|
|
|
const room = this.matrixClient.getRoom(roomId);
|
2018-08-30 13:36:53 +03:00
|
|
|
if (room) {
|
|
|
|
const userId = room.guessDMUserId();
|
|
|
|
if (userId && userId !== myUserId) {
|
|
|
|
return {userId, roomId};
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 06:50:35 +03:00
|
|
|
}).filter((ids) => !!ids); //filter out
|
2018-08-30 13:36:53 +03:00
|
|
|
// these are actually all legit self-chats
|
|
|
|
// bail out
|
|
|
|
if (!guessedUserIdsThatChanged.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
userToRooms[myUserId] = selfRoomIds.filter((roomId) => {
|
2018-09-04 14:06:27 +03:00
|
|
|
return !guessedUserIdsThatChanged
|
2018-08-30 13:36:53 +03:00
|
|
|
.some((ids) => ids.roomId === roomId);
|
2018-08-30 11:53:25 +03:00
|
|
|
});
|
2018-08-30 13:36:53 +03:00
|
|
|
guessedUserIdsThatChanged.forEach(({userId, roomId}) => {
|
2018-10-12 06:05:59 +03:00
|
|
|
const roomIds = userToRooms[userId];
|
2018-08-30 11:53:25 +03:00
|
|
|
if (!roomIds) {
|
2018-09-04 14:07:24 +03:00
|
|
|
userToRooms[userId] = [roomId];
|
|
|
|
} else {
|
|
|
|
roomIds.push(roomId);
|
2020-08-28 20:53:43 +03:00
|
|
|
userToRooms[userId] = uniq(roomIds);
|
2018-08-30 11:53:25 +03:00
|
|
|
}
|
|
|
|
});
|
2018-08-30 13:36:53 +03:00
|
|
|
return true;
|
2018-08-30 11:53:25 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 20:02:14 +03:00
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
public getDMRoomsForUserId(userId): string[] {
|
2016-09-09 19:35:35 +03:00
|
|
|
// Here, we return the empty list if there are no rooms,
|
|
|
|
// since the number of conversations you have with this user is zero.
|
2021-01-13 18:37:49 +03:00
|
|
|
return this.getUserToRooms()[userId] || [];
|
2016-09-06 18:39:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:32:00 +03:00
|
|
|
/**
|
|
|
|
* Gets the DM room which the given IDs share, if any.
|
|
|
|
* @param {string[]} ids The identifiers (user IDs and email addresses) to look for.
|
|
|
|
* @returns {Room} The DM room which all IDs given share, or falsey if no common room.
|
|
|
|
*/
|
2021-01-13 18:37:49 +03:00
|
|
|
public getDMRoomForIdentifiers(ids: string[]): Room {
|
2020-01-15 09:32:00 +03:00
|
|
|
// TODO: [Canonical DMs] Handle lookups for email addresses.
|
|
|
|
// For now we'll pretend we only get user IDs and end up returning nothing for email addresses
|
|
|
|
|
|
|
|
let commonRooms = this.getDMRoomsForUserId(ids[0]);
|
|
|
|
for (let i = 1; i < ids.length; i++) {
|
|
|
|
const userRooms = this.getDMRoomsForUserId(ids[i]);
|
|
|
|
commonRooms = commonRooms.filter(r => userRooms.includes(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
const joinedRooms = commonRooms.map(r => MatrixClientPeg.get().getRoom(r))
|
|
|
|
.filter(r => r && r.getMyMembership() === 'join');
|
|
|
|
|
|
|
|
return joinedRooms[0];
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
public getUserIdForRoomId(roomId: string) {
|
2016-09-09 18:15:01 +03:00
|
|
|
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.
|
2021-01-13 18:37:49 +03:00
|
|
|
this.populateRoomToUser();
|
2016-09-09 18:15:01 +03:00
|
|
|
}
|
2016-09-09 19:35:35 +03:00
|
|
|
// 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.
|
2016-09-12 20:32:44 +03:00
|
|
|
if (this.roomToUser[roomId] === undefined) {
|
|
|
|
// no entry? if the room is an invite, look for the is_direct hint.
|
|
|
|
const room = this.matrixClient.getRoom(roomId);
|
|
|
|
if (room) {
|
2018-08-14 12:43:03 +03:00
|
|
|
return room.getDMInviter();
|
2016-09-12 20:32:44 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:39:21 +03:00
|
|
|
return this.roomToUser[roomId];
|
|
|
|
}
|
2016-09-09 18:15:01 +03:00
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
public getUniqueRoomsWithIndividuals(): {[userId: string]: Room} {
|
2020-03-05 00:18:56 +03:00
|
|
|
if (!this.roomToUser) return {}; // No rooms means no map.
|
2020-01-03 03:40:18 +03:00
|
|
|
return Object.keys(this.roomToUser)
|
|
|
|
.map(r => ({userId: this.getUserIdForRoomId(r), room: this.matrixClient.getRoom(r)}))
|
|
|
|
.filter(r => r.userId && r.room && r.room.getInvitedAndJoinedMemberCount() === 2)
|
|
|
|
.reduce((obj, r) => (obj[r.userId] = r.room) && obj, {});
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
private getUserToRooms(): {[key: string]: string[]} {
|
2018-09-04 17:00:40 +03:00
|
|
|
if (!this.userToRooms) {
|
2021-01-13 17:49:23 +03:00
|
|
|
const userToRooms = this.mDirectEvent as {[key: string]: string[]};
|
2018-09-04 17:00:40 +03:00
|
|
|
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.`);
|
2021-01-13 17:49:23 +03:00
|
|
|
if (neededPatching && !this.hasSentOutPatchDirectAccountDataPatch) {
|
|
|
|
this.hasSentOutPatchDirectAccountDataPatch = true;
|
2018-09-04 17:00:40 +03:00
|
|
|
this.matrixClient.setAccountData('m.direct', userToRooms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.userToRooms = userToRooms;
|
|
|
|
}
|
|
|
|
return this.userToRooms;
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:37:49 +03:00
|
|
|
private populateRoomToUser() {
|
2016-09-09 18:15:01 +03:00
|
|
|
this.roomToUser = {};
|
2018-09-04 17:00:40 +03:00
|
|
|
for (const user of Object.keys(this._getUserToRooms())) {
|
2016-09-09 18:15:01 +03:00
|
|
|
for (const roomId of this.userToRooms[user]) {
|
|
|
|
this.roomToUser[roomId] = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:39:21 +03:00
|
|
|
}
|