2021-07-28 16:15:52 +03:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import appDispatcher from '../dispatcher';
|
|
|
|
import cons from './cons';
|
|
|
|
|
2021-09-10 16:07:52 +03:00
|
|
|
function isMEventSpaceChild(mEvent) {
|
|
|
|
return mEvent.getType() === 'm.space.child' && Object.keys(mEvent.getContent()).length > 0;
|
|
|
|
}
|
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
/**
|
|
|
|
* @param {() => boolean} callback if return true wait will over else callback will be called again.
|
|
|
|
* @param {number} timeout timeout to callback
|
|
|
|
* @param {number} maxTry maximum callback try > 0. -1 means no limit
|
|
|
|
*/
|
|
|
|
async function waitFor(callback, timeout = 400, maxTry = -1) {
|
|
|
|
if (maxTry === 0) return false;
|
|
|
|
const isOver = async () => new Promise((resolve) => {
|
|
|
|
setTimeout(() => resolve(callback()), timeout);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (await isOver()) return true;
|
|
|
|
return waitFor(callback, timeout, maxTry - 1);
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
class RoomList extends EventEmitter {
|
|
|
|
constructor(matrixClient) {
|
|
|
|
super();
|
|
|
|
this.matrixClient = matrixClient;
|
|
|
|
this.mDirects = this.getMDirects();
|
2021-09-10 16:07:52 +03:00
|
|
|
|
|
|
|
// Contains roomId to parent spaces roomId mapping of all spaces children.
|
|
|
|
// No matter if you have joined those children rooms or not.
|
2021-09-03 15:28:01 +03:00
|
|
|
this.roomIdToParents = new Map();
|
2021-07-28 16:15:52 +03:00
|
|
|
|
|
|
|
this.inviteDirects = new Set();
|
|
|
|
this.inviteSpaces = new Set();
|
|
|
|
this.inviteRooms = new Set();
|
|
|
|
|
|
|
|
this.directs = new Set();
|
|
|
|
this.spaces = new Set();
|
|
|
|
this.rooms = new Set();
|
|
|
|
|
|
|
|
this.processingRooms = new Map();
|
|
|
|
|
|
|
|
this._populateRooms();
|
|
|
|
this._listenEvents();
|
|
|
|
|
|
|
|
appDispatcher.register(this.roomActions.bind(this));
|
|
|
|
}
|
|
|
|
|
2021-09-12 18:14:13 +03:00
|
|
|
isOrphan(roomId) {
|
|
|
|
return !this.roomIdToParents.has(roomId);
|
|
|
|
}
|
|
|
|
|
2022-03-14 14:04:34 +02:00
|
|
|
getOrphanSpaces() {
|
|
|
|
return [...this.spaces].filter((roomId) => !this.roomIdToParents.has(roomId));
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrphanRooms() {
|
|
|
|
return [...this.rooms].filter((roomId) => !this.roomIdToParents.has(roomId));
|
|
|
|
}
|
|
|
|
|
2021-09-12 18:14:13 +03:00
|
|
|
getOrphans() {
|
|
|
|
const rooms = [...this.spaces].concat([...this.rooms]);
|
|
|
|
return rooms.filter((roomId) => !this.roomIdToParents.has(roomId));
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:28:01 +03:00
|
|
|
getSpaceChildren(roomId) {
|
|
|
|
const space = this.matrixClient.getRoom(roomId);
|
2022-03-13 07:07:25 +02:00
|
|
|
if (space === null) return null;
|
2021-09-03 15:28:01 +03:00
|
|
|
const mSpaceChild = space?.currentState.getStateEvents('m.space.child');
|
2022-03-13 07:07:25 +02:00
|
|
|
|
|
|
|
const children = [];
|
|
|
|
mSpaceChild.forEach((mEvent) => {
|
2021-09-10 16:07:52 +03:00
|
|
|
const childId = mEvent.event.state_key;
|
2022-03-13 07:07:25 +02:00
|
|
|
if (isMEventSpaceChild(mEvent)) children.push(childId);
|
2021-09-03 15:28:01 +03:00
|
|
|
});
|
2022-03-13 07:07:25 +02:00
|
|
|
return children;
|
2021-09-03 15:28:01 +03:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:27:29 +02:00
|
|
|
getCategorizedSpaces(spaceIds) {
|
|
|
|
const categorized = new Map();
|
|
|
|
|
|
|
|
const categorizeSpace = (spaceId) => {
|
|
|
|
if (categorized.has(spaceId)) return;
|
|
|
|
const mappedChild = new Set();
|
|
|
|
categorized.set(spaceId, mappedChild);
|
|
|
|
|
|
|
|
const child = this.getSpaceChildren(spaceId);
|
|
|
|
|
|
|
|
child.forEach((childId) => {
|
|
|
|
const room = this.matrixClient.getRoom(childId);
|
|
|
|
if (room === null) return;
|
|
|
|
if (room.isSpaceRoom()) categorizeSpace(childId);
|
|
|
|
else mappedChild.add(childId);
|
|
|
|
});
|
|
|
|
};
|
2022-03-13 07:07:25 +02:00
|
|
|
spaceIds.forEach(categorizeSpace);
|
2022-03-02 17:27:29 +02:00
|
|
|
|
|
|
|
return categorized;
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:28:01 +03:00
|
|
|
addToRoomIdToParents(roomId, parentRoomId) {
|
|
|
|
if (!this.roomIdToParents.has(roomId)) {
|
|
|
|
this.roomIdToParents.set(roomId, new Set());
|
|
|
|
}
|
|
|
|
const parents = this.roomIdToParents.get(roomId);
|
|
|
|
parents.add(parentRoomId);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFromRoomIdToParents(roomId, parentRoomId) {
|
|
|
|
if (!this.roomIdToParents.has(roomId)) return;
|
|
|
|
const parents = this.roomIdToParents.get(roomId);
|
|
|
|
parents.delete(parentRoomId);
|
|
|
|
if (parents.size === 0) this.roomIdToParents.delete(roomId);
|
|
|
|
}
|
|
|
|
|
2022-03-14 14:01:57 +02:00
|
|
|
getAllParentSpaces(roomId) {
|
2022-03-13 12:06:15 +02:00
|
|
|
const allParents = new Set();
|
2022-03-14 14:01:57 +02:00
|
|
|
|
2022-03-13 12:06:15 +02:00
|
|
|
const addAllParentIds = (rId) => {
|
2022-03-14 14:01:57 +02:00
|
|
|
if (allParents.has(rId)) return;
|
|
|
|
allParents.add(rId);
|
2022-03-13 12:06:15 +02:00
|
|
|
|
|
|
|
const parents = this.roomIdToParents.get(rId);
|
|
|
|
if (parents === undefined) return;
|
|
|
|
|
|
|
|
parents.forEach((id) => addAllParentIds(id));
|
|
|
|
};
|
|
|
|
addAllParentIds(roomId);
|
2022-03-14 14:01:57 +02:00
|
|
|
allParents.delete(roomId);
|
2022-03-13 12:06:15 +02:00
|
|
|
return allParents;
|
2021-09-25 17:48:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-03 15:28:01 +03:00
|
|
|
addToSpaces(roomId) {
|
|
|
|
this.spaces.add(roomId);
|
2021-09-25 17:48:06 +03:00
|
|
|
|
2022-03-14 13:59:40 +02:00
|
|
|
const allParentSpaces = this.getAllParentSpaces(roomId);
|
2021-09-03 15:28:01 +03:00
|
|
|
const spaceChildren = this.getSpaceChildren(roomId);
|
2022-03-14 13:59:40 +02:00
|
|
|
spaceChildren?.forEach((childId) => {
|
|
|
|
if (allParentSpaces.has(childId)) return;
|
|
|
|
this.addToRoomIdToParents(childId, roomId);
|
2021-09-03 15:28:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteFromSpaces(roomId) {
|
|
|
|
this.spaces.delete(roomId);
|
2022-03-14 13:59:40 +02:00
|
|
|
|
2021-09-03 15:28:01 +03:00
|
|
|
const spaceChildren = this.getSpaceChildren(roomId);
|
2022-03-14 13:59:40 +02:00
|
|
|
spaceChildren?.forEach((childId) => {
|
|
|
|
this.removeFromRoomIdToParents(childId, roomId);
|
2021-09-03 15:28:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
roomActions(action) {
|
|
|
|
const addRoom = (roomId, isDM) => {
|
|
|
|
const myRoom = this.matrixClient.getRoom(roomId);
|
|
|
|
if (myRoom === null) return false;
|
|
|
|
|
|
|
|
if (isDM) this.directs.add(roomId);
|
2021-09-03 15:28:01 +03:00
|
|
|
else if (myRoom.isSpaceRoom()) this.addToSpaces(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
else this.rooms.add(roomId);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
const actions = {
|
|
|
|
[cons.actions.room.JOIN]: () => {
|
|
|
|
if (addRoom(action.roomId, action.isDM)) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.emit(cons.events.roomList.ROOM_JOINED, action.roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
}, 100);
|
|
|
|
} else {
|
|
|
|
this.processingRooms.set(action.roomId, {
|
|
|
|
roomId: action.roomId,
|
|
|
|
isDM: action.isDM,
|
|
|
|
task: 'JOIN',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[cons.actions.room.CREATE]: () => {
|
|
|
|
if (addRoom(action.roomId, action.isDM)) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.emit(cons.events.roomList.ROOM_CREATED, action.roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOM_JOINED, action.roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
}, 100);
|
|
|
|
} else {
|
|
|
|
this.processingRooms.set(action.roomId, {
|
|
|
|
roomId: action.roomId,
|
|
|
|
isDM: action.isDM,
|
|
|
|
task: 'CREATE',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
actions[action.type]?.();
|
|
|
|
}
|
|
|
|
|
|
|
|
getMDirects() {
|
|
|
|
const mDirectsId = new Set();
|
|
|
|
const mDirect = this.matrixClient
|
|
|
|
.getAccountData('m.direct')
|
|
|
|
?.getContent();
|
|
|
|
|
|
|
|
if (typeof mDirect === 'undefined') return mDirectsId;
|
|
|
|
|
|
|
|
Object.keys(mDirect).forEach((direct) => {
|
|
|
|
mDirect[direct].forEach((directId) => mDirectsId.add(directId));
|
|
|
|
});
|
|
|
|
|
|
|
|
return mDirectsId;
|
|
|
|
}
|
|
|
|
|
|
|
|
_populateRooms() {
|
|
|
|
this.directs.clear();
|
2021-09-03 15:28:01 +03:00
|
|
|
this.roomIdToParents.clear();
|
2021-07-28 16:15:52 +03:00
|
|
|
this.spaces.clear();
|
|
|
|
this.rooms.clear();
|
|
|
|
this.inviteDirects.clear();
|
|
|
|
this.inviteSpaces.clear();
|
|
|
|
this.inviteRooms.clear();
|
|
|
|
this.matrixClient.getRooms().forEach((room) => {
|
|
|
|
const { roomId } = room;
|
|
|
|
const tombstone = room.currentState.events.get('m.room.tombstone');
|
2022-04-16 18:23:24 +03:00
|
|
|
if (tombstone?.get('') !== undefined) {
|
2021-07-28 16:15:52 +03:00
|
|
|
const repRoomId = tombstone.get('').getContent().replacement_room;
|
|
|
|
const repRoomMembership = this.matrixClient.getRoom(repRoomId)?.getMyMembership();
|
|
|
|
if (repRoomMembership === 'join') return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (room.getMyMembership() === 'invite') {
|
|
|
|
if (this._isDMInvite(room)) this.inviteDirects.add(roomId);
|
|
|
|
else if (room.isSpaceRoom()) this.inviteSpaces.add(roomId);
|
|
|
|
else this.inviteRooms.add(roomId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (room.getMyMembership() !== 'join') return;
|
|
|
|
|
|
|
|
if (this.mDirects.has(roomId)) this.directs.add(roomId);
|
2021-09-03 15:28:01 +03:00
|
|
|
else if (room.isSpaceRoom()) this.addToSpaces(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
else this.rooms.add(roomId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_isDMInvite(room) {
|
2022-05-04 11:52:16 +03:00
|
|
|
if (this.mDirects.has(room.roomId)) return true;
|
2021-07-28 16:15:52 +03:00
|
|
|
const me = room.getMember(this.matrixClient.getUserId());
|
|
|
|
const myEventContent = me.events.member.getContent();
|
|
|
|
return myEventContent.membership === 'invite' && myEventContent.is_direct;
|
|
|
|
}
|
|
|
|
|
|
|
|
_listenEvents() {
|
|
|
|
// Update roomList when m.direct changes
|
|
|
|
this.matrixClient.on('accountData', (event) => {
|
|
|
|
if (event.getType() !== 'm.direct') return;
|
|
|
|
|
|
|
|
const latestMDirects = this.getMDirects();
|
|
|
|
|
|
|
|
latestMDirects.forEach((directId) => {
|
|
|
|
const myRoom = this.matrixClient.getRoom(directId);
|
|
|
|
if (this.mDirects.has(directId)) return;
|
|
|
|
this.mDirects.add(directId);
|
|
|
|
|
|
|
|
if (myRoom === null) return;
|
2022-05-04 11:52:16 +03:00
|
|
|
if (myRoom.getMyMembership() === 'join') {
|
2021-07-28 16:15:52 +03:00
|
|
|
this.directs.add(directId);
|
|
|
|
this.rooms.delete(directId);
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-23 06:33:20 +02:00
|
|
|
this.matrixClient.on('Room.name', (room) => {
|
2021-07-28 16:15:52 +03:00
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
2021-12-23 06:33:20 +02:00
|
|
|
this.emit(cons.events.roomList.ROOM_PROFILE_UPDATED, room.roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
});
|
|
|
|
|
2021-12-23 06:33:20 +02:00
|
|
|
this.matrixClient.on('RoomState.events', (mEvent, state) => {
|
2021-09-03 15:28:01 +03:00
|
|
|
if (mEvent.getType() === 'm.space.child') {
|
2022-03-14 13:59:40 +02:00
|
|
|
const roomId = mEvent.event.room_id;
|
|
|
|
const childId = mEvent.event.state_key;
|
2021-09-10 16:07:52 +03:00
|
|
|
if (isMEventSpaceChild(mEvent)) {
|
2022-03-14 13:59:40 +02:00
|
|
|
const allParentSpaces = this.getAllParentSpaces(roomId);
|
|
|
|
// only add if it doesn't make a cycle
|
|
|
|
if (!allParentSpaces.has(childId)) {
|
|
|
|
this.addToRoomIdToParents(childId, roomId);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.removeFromRoomIdToParents(childId, roomId);
|
|
|
|
}
|
2021-09-03 15:28:01 +03:00
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-23 06:33:20 +02:00
|
|
|
if (mEvent.getType() === 'm.room.join_rules') {
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (['m.room.avatar', 'm.room.topic'].includes(mEvent.getType())) {
|
|
|
|
if (mEvent.getType() === 'm.room.avatar') {
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
}
|
|
|
|
this.emit(cons.events.roomList.ROOM_PROFILE_UPDATED, state.roomId);
|
|
|
|
}
|
2021-07-28 16:15:52 +03:00
|
|
|
});
|
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
this.matrixClient.on('Room.myMembership', async (room, membership, prevMembership) => {
|
2021-07-28 16:15:52 +03:00
|
|
|
// room => prevMembership = null | invite | join | leave | kick | ban | unban
|
|
|
|
// room => membership = invite | join | leave | kick | ban | unban
|
|
|
|
const { roomId } = room;
|
2022-05-04 11:52:16 +03:00
|
|
|
const isRoomReady = () => this.matrixClient.getRoom(roomId) !== null;
|
|
|
|
if (['join', 'invite'].includes(membership) && isRoomReady() === false) {
|
|
|
|
if (await waitFor(isRoomReady, 200, 100) === false) return;
|
|
|
|
}
|
2021-07-28 16:15:52 +03:00
|
|
|
|
|
|
|
if (membership === 'unban') return;
|
|
|
|
|
|
|
|
if (membership === 'invite') {
|
|
|
|
if (this._isDMInvite(room)) this.inviteDirects.add(roomId);
|
|
|
|
else if (room.isSpaceRoom()) this.inviteSpaces.add(roomId);
|
|
|
|
else this.inviteRooms.add(roomId);
|
|
|
|
|
|
|
|
this.emit(cons.events.roomList.INVITELIST_UPDATED, roomId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
if (prevMembership === 'invite') {
|
|
|
|
if (this.inviteDirects.has(roomId)) this.inviteDirects.delete(roomId);
|
|
|
|
else if (this.inviteSpaces.has(roomId)) this.inviteSpaces.delete(roomId);
|
|
|
|
else this.inviteRooms.delete(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
this.emit(cons.events.roomList.INVITELIST_UPDATED, roomId);
|
|
|
|
}
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
if (['leave', 'kick', 'ban'].includes(membership)) {
|
|
|
|
if (this.directs.has(roomId)) this.directs.delete(roomId);
|
|
|
|
else if (this.spaces.has(roomId)) this.deleteFromSpaces(roomId);
|
|
|
|
else this.rooms.delete(roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOM_LEAVED, roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
// when user create room/DM OR accept room/dm invite from this client.
|
|
|
|
// we will update this.rooms/this.directs with user action
|
|
|
|
if (membership === 'join' && this.processingRooms.has(roomId)) {
|
|
|
|
const procRoomInfo = this.processingRooms.get(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
if (procRoomInfo.isDM) this.directs.add(roomId);
|
|
|
|
else if (room.isSpaceRoom()) this.addToSpaces(roomId);
|
|
|
|
else this.rooms.add(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
if (procRoomInfo.task === 'CREATE') this.emit(cons.events.roomList.ROOM_CREATED, roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOM_JOINED, roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
this.processingRooms.delete(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-04 11:52:16 +03:00
|
|
|
if (this.mDirects.has(roomId) && membership === 'join') {
|
|
|
|
this.directs.add(roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOM_JOINED, roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-04 11:52:16 +03:00
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
if (membership === 'join') {
|
2021-09-03 15:28:01 +03:00
|
|
|
if (room.isSpaceRoom()) this.addToSpaces(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
else this.rooms.add(roomId);
|
|
|
|
this.emit(cons.events.roomList.ROOM_JOINED, roomId);
|
2022-05-04 11:52:16 +03:00
|
|
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default RoomList;
|