mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-05 23:10:28 +03:00
Move space shortcut from roomlist to accountdata
Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
parent
7db674b65d
commit
a62df536dd
10 changed files with 131 additions and 77 deletions
79
src/client/state/AccountData.js
Normal file
79
src/client/state/AccountData.js
Normal 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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue