Improved roomList
This commit is contained in:
parent
c2faa605d3
commit
6b53b78ee3
1 changed files with 14 additions and 6 deletions
|
@ -2,11 +2,18 @@ import EventEmitter from 'events';
|
||||||
import appDispatcher from '../dispatcher';
|
import appDispatcher from '../dispatcher';
|
||||||
import cons from './cons';
|
import cons from './cons';
|
||||||
|
|
||||||
|
function isMEventSpaceChild(mEvent) {
|
||||||
|
return mEvent.getType() === 'm.space.child' && Object.keys(mEvent.getContent()).length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
class RoomList extends EventEmitter {
|
class RoomList extends EventEmitter {
|
||||||
constructor(matrixClient) {
|
constructor(matrixClient) {
|
||||||
super();
|
super();
|
||||||
this.matrixClient = matrixClient;
|
this.matrixClient = matrixClient;
|
||||||
this.mDirects = this.getMDirects();
|
this.mDirects = this.getMDirects();
|
||||||
|
|
||||||
|
// Contains roomId to parent spaces roomId mapping of all spaces children.
|
||||||
|
// No matter if you have joined those children rooms or not.
|
||||||
this.roomIdToParents = new Map();
|
this.roomIdToParents = new Map();
|
||||||
|
|
||||||
this.spaceShortcut = new Set();
|
this.spaceShortcut = new Set();
|
||||||
|
@ -38,10 +45,11 @@ class RoomList extends EventEmitter {
|
||||||
const space = this.matrixClient.getRoom(roomId);
|
const space = this.matrixClient.getRoom(roomId);
|
||||||
const mSpaceChild = space?.currentState.getStateEvents('m.space.child');
|
const mSpaceChild = space?.currentState.getStateEvents('m.space.child');
|
||||||
const children = mSpaceChild?.map((mEvent) => {
|
const children = mSpaceChild?.map((mEvent) => {
|
||||||
if (Object.keys(mEvent.event.content).length === 0) return null;
|
const childId = mEvent.event.state_key;
|
||||||
return mEvent.event.state_key;
|
if (isMEventSpaceChild(mEvent)) return childId;
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
return children?.filter((child) => child !== null);
|
return children?.filter((childId) => childId !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
addToRoomIdToParents(roomId, parentRoomId) {
|
addToRoomIdToParents(roomId, parentRoomId) {
|
||||||
|
@ -259,9 +267,9 @@ class RoomList extends EventEmitter {
|
||||||
this.matrixClient.on('RoomState.events', (mEvent) => {
|
this.matrixClient.on('RoomState.events', (mEvent) => {
|
||||||
if (mEvent.getType() === 'm.space.child') {
|
if (mEvent.getType() === 'm.space.child') {
|
||||||
const { event } = mEvent;
|
const { event } = mEvent;
|
||||||
const isRoomAdded = Object.keys(event.content).length > 0;
|
if (isMEventSpaceChild(mEvent)) {
|
||||||
if (isRoomAdded) this.addToRoomIdToParents(event.state_key, event.room_id);
|
this.addToRoomIdToParents(event.state_key, event.room_id);
|
||||||
else this.removeFromRoomIdToParents(event.state_key, event.room_id);
|
} else this.removeFromRoomIdToParents(event.state_key, event.room_id);
|
||||||
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
this.emit(cons.events.roomList.ROOMLIST_UPDATED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue