Move space shortcut from roomlist to accountdata

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2022-02-27 17:02:03 +05:30
parent 7db674b65d
commit a62df536dd
10 changed files with 131 additions and 77 deletions

View file

@ -0,0 +1,79 @@
import EventEmitter from 'events';
import appDispatcher from '../dispatcher';
import cons from './cons';
class AccountData extends EventEmitter {
constructor(roomList) {
super();
this.matrixClient = roomList.matrixClient;
this.roomList = roomList;
this.spaces = roomList.spaces;
this.spaceShortcut = new Set();
this._populateSpaceShortcut();
this._listenEvents();
appDispatcher.register(this.roomActions.bind(this));
}
_populateSpaceShortcut() {
this.spaceShortcut.clear();
const spacesContent = this.matrixClient.getAccountData(cons.IN_CINNY_SPACES)?.getContent();
if (!spacesContent) return;
if (spacesContent?.shortcut === undefined) return;
spacesContent.shortcut.forEach((shortcut) => {
if (this.spaces.has(shortcut)) this.spaceShortcut.add(shortcut);
});
if (spacesContent.shortcut.length !== this.spaceShortcut.size) {
// update shortcut list from account data if shortcut space doesn't exist.
// TODO: we can wait for sync to complete or else we may end up removing valid space id
this._updateSpaceShortcutData([...this.spaceShortcut]);
}
}
_updateSpaceShortcutData(shortcutList) {
const spaceContent = this.matrixClient.getAccountData(cons.IN_CINNY_SPACES)?.getContent() || {};
spaceContent.shortcut = shortcutList;
this.matrixClient.setAccountData(cons.IN_CINNY_SPACES, spaceContent);
}
roomActions(action) {
const actions = {
[cons.actions.accountData.CREATE_SPACE_SHORTCUT]: () => {
if (this.spaceShortcut.has(action.roomId)) return;
this.spaceShortcut.add(action.roomId);
this._updateSpaceShortcutData([...this.spaceShortcut]);
this.emit(cons.events.accountData.SPACE_SHORTCUT_UPDATED, action.roomId);
},
[cons.actions.accountData.DELETE_SPACE_SHORTCUT]: () => {
if (!this.spaceShortcut.has(action.roomId)) return;
this.spaceShortcut.delete(action.roomId);
this._updateSpaceShortcutData([...this.spaceShortcut]);
this.emit(cons.events.accountData.SPACE_SHORTCUT_UPDATED, action.roomId);
},
};
actions[action.type]?.();
}
_listenEvents() {
this.matrixClient.on('accountData', (event) => {
if (event.getType() !== cons.IN_CINNY_SPACES) return;
this._populateSpaceShortcut();
this.emit(cons.events.accountData.SPACE_SHORTCUT_UPDATED);
});
this.roomList.on(cons.events.roomList.ROOM_LEAVED, (roomId) => {
if (this.spaceShortcut.has(roomId)) {
// if deleted space has shortcut remove it.
this.spaceShortcut.delete(roomId);
this._updateSpaceShortcutData([...this.spaceShortcut]);
this.emit(cons.events.accountData.SPACE_SHORTCUT_UPDATED, roomId);
}
});
}
}
export default AccountData;

View file

@ -16,8 +16,6 @@ class RoomList extends EventEmitter {
// No matter if you have joined those children rooms or not.
this.roomIdToParents = new Map();
this.spaceShortcut = new Set();
this.inviteDirects = new Set();
this.inviteSpaces = new Set();
this.inviteRooms = new Set();
@ -29,18 +27,11 @@ class RoomList extends EventEmitter {
this.processingRooms = new Map();
this._populateRooms();
this._populateSpaceShortcut();
this._listenEvents();
appDispatcher.register(this.roomActions.bind(this));
}
_updateSpaceShortcutData(shortcutList) {
const spaceContent = this.matrixClient.getAccountData(cons['in.cinny.spaces'])?.getContent() || {};
spaceContent.shortcut = shortcutList;
this.matrixClient.setAccountData(cons['in.cinny.spaces'], spaceContent);
}
isOrphan(roomId) {
return !this.roomIdToParents.has(roomId);
}
@ -103,12 +94,6 @@ class RoomList extends EventEmitter {
spaceChildren?.forEach((childRoomId) => {
this.removeFromRoomIdToParents(childRoomId, roomId);
});
if (this.spaceShortcut.has(roomId)) {
// if delete space has shortcut remove it.
this.spaceShortcut.delete(roomId);
this._updateSpaceShortcutData([...this.spaceShortcut]);
}
}
roomActions(action) {
@ -151,18 +136,6 @@ class RoomList extends EventEmitter {
});
}
},
[cons.actions.room.CREATE_SPACE_SHORTCUT]: () => {
if (this.spaceShortcut.has(action.roomId)) return;
this.spaceShortcut.add(action.roomId);
this._updateSpaceShortcutData([...this.spaceShortcut]);
this.emit(cons.events.roomList.SPACE_SHORTCUT_UPDATED, action.roomId);
},
[cons.actions.room.DELETE_SPACE_SHORTCUT]: () => {
if (!this.spaceShortcut.has(action.roomId)) return;
this.spaceShortcut.delete(action.roomId);
this._updateSpaceShortcutData([...this.spaceShortcut]);
this.emit(cons.events.roomList.SPACE_SHORTCUT_UPDATED, action.roomId);
},
};
actions[action.type]?.();
}
@ -182,21 +155,6 @@ class RoomList extends EventEmitter {
return mDirectsId;
}
_populateSpaceShortcut() {
this.spaceShortcut.clear();
const spacesContent = this.matrixClient.getAccountData(cons['in.cinny.spaces'])?.getContent();
if (spacesContent && Array.isArray(spacesContent?.shortcut)) {
spacesContent.shortcut.forEach((shortcut) => {
if (this.spaces.has(shortcut)) this.spaceShortcut.add(shortcut);
});
if (spacesContent.shortcut.length !== this.spaceShortcut.size) {
// update shortcut list from account data if shortcut space doesn't exist.
this._updateSpaceShortcutData([...this.spaceShortcut]);
}
}
}
_populateRooms() {
this.directs.clear();
this.roomIdToParents.clear();
@ -238,12 +196,6 @@ class RoomList extends EventEmitter {
_listenEvents() {
// Update roomList when m.direct changes
this.matrixClient.on('accountData', (event) => {
if (event.getType() === cons['in.cinny.spaces']) {
this._populateSpaceShortcut();
this.emit(cons.events.roomList.SPACE_SHORTCUT_UPDATED);
return;
}
if (event.getType() !== 'm.direct') return;
const latestMDirects = this.getMDirects();

View file

@ -7,7 +7,7 @@ const cons = {
BASE_URL: 'cinny_hs_base_url',
},
DEVICE_DISPLAY_NAME: 'Cinny Web',
'in.cinny.spaces': 'in.cinny.spaces',
IN_CINNY_SPACES: 'in.cinny.spaces',
tabs: {
HOME: 'home',
DIRECTS: 'dm',
@ -51,6 +51,8 @@ const cons = {
JOIN: 'JOIN',
LEAVE: 'LEAVE',
CREATE: 'CREATE',
},
accountData: {
CREATE_SPACE_SHORTCUT: 'CREATE_SPACE_SHORTCUT',
DELETE_SPACE_SHORTCUT: 'DELETE_SPACE_SHORTCUT',
},
@ -91,9 +93,11 @@ const cons = {
ROOM_JOINED: 'ROOM_JOINED',
ROOM_LEAVED: 'ROOM_LEAVED',
ROOM_CREATED: 'ROOM_CREATED',
SPACE_SHORTCUT_UPDATED: 'SPACE_SHORTCUT_UPDATED',
ROOM_PROFILE_UPDATED: 'ROOM_PROFILE_UPDATED',
},
accountData: {
SPACE_SHORTCUT_UPDATED: 'SPACE_SHORTCUT_UPDATED',
},
notifications: {
NOTI_CHANGED: 'NOTI_CHANGED',
FULL_READ: 'FULL_READ',