2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-10-06 15:52:06 +05:30
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 22:08:55 +05:30
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-10-06 15:52:06 +05:30
|
|
|
import {LeftPanelViewModel} from "./leftpanel/LeftPanelViewModel.js";
|
2021-06-03 21:03:02 +05:30
|
|
|
import {RoomViewModel} from "./room/RoomViewModel.js";
|
2021-05-18 14:37:19 +05:30
|
|
|
import {UnknownRoomViewModel} from "./room/UnknownRoomViewModel.js";
|
2021-04-21 19:17:39 +05:30
|
|
|
import {InviteViewModel} from "./room/InviteViewModel.js";
|
2022-02-03 22:27:35 +05:30
|
|
|
import {RoomBeingCreatedViewModel} from "./room/RoomBeingCreatedViewModel.js";
|
2020-10-28 22:12:18 +05:30
|
|
|
import {LightboxViewModel} from "./room/LightboxViewModel.js";
|
2020-05-06 02:47:27 +05:30
|
|
|
import {SessionStatusViewModel} from "./SessionStatusViewModel.js";
|
2020-10-07 15:55:03 +05:30
|
|
|
import {RoomGridViewModel} from "./RoomGridViewModel.js";
|
2020-10-20 15:40:41 +05:30
|
|
|
import {SettingsViewModel} from "./settings/SettingsViewModel.js";
|
2022-02-09 23:30:41 +05:30
|
|
|
import {CreateRoomViewModel} from "./CreateRoomViewModel.js";
|
2022-02-14 22:23:59 +05:30
|
|
|
import {ViewModel} from "../ViewModel";
|
2021-05-07 16:40:35 +05:30
|
|
|
import {RoomViewModelObservable} from "./RoomViewModelObservable.js";
|
2021-06-17 15:16:34 +05:30
|
|
|
import {RightPanelViewModel} from "./rightpanel/RightPanelViewModel.js";
|
2019-02-28 03:20:08 +05:30
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
export class SessionViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2021-12-22 21:39:52 +05:30
|
|
|
const {client} = options;
|
|
|
|
this._client = this.track(client);
|
2020-05-06 02:46:51 +05:30
|
|
|
this._sessionStatusViewModel = this.track(new SessionStatusViewModel(this.childOptions({
|
2021-12-22 21:39:52 +05:30
|
|
|
sync: client.sync,
|
|
|
|
reconnector: client.reconnector,
|
|
|
|
session: client.session,
|
2020-05-06 02:46:51 +05:30
|
|
|
})));
|
2022-02-03 22:27:35 +05:30
|
|
|
this._leftPanelViewModel = this.track(new LeftPanelViewModel(this.childOptions({session: this._client.session})));
|
2020-10-16 21:36:20 +05:30
|
|
|
this._settingsViewModel = null;
|
2021-05-07 16:40:35 +05:30
|
|
|
this._roomViewModelObservable = null;
|
2020-10-07 15:55:03 +05:30
|
|
|
this._gridViewModel = null;
|
2022-02-09 23:30:41 +05:30
|
|
|
this._createRoomViewModel = null;
|
2020-10-12 21:19:06 +05:30
|
|
|
this._setupNavigation();
|
|
|
|
}
|
2020-10-09 23:13:11 +05:30
|
|
|
|
2020-10-12 21:19:06 +05:30
|
|
|
_setupNavigation() {
|
|
|
|
const gridRooms = this.navigation.observe("rooms");
|
2020-10-09 23:13:11 +05:30
|
|
|
// this gives us a set of room ids in the grid
|
2020-10-12 21:19:06 +05:30
|
|
|
this.track(gridRooms.subscribe(roomIds => {
|
|
|
|
this._updateGrid(roomIds);
|
|
|
|
}));
|
|
|
|
if (gridRooms.get()) {
|
|
|
|
this._updateGrid(gridRooms.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentRoomId = this.navigation.observe("room");
|
|
|
|
// this gives us the active room
|
|
|
|
this.track(currentRoomId.subscribe(roomId => {
|
|
|
|
if (!this._gridViewModel) {
|
2020-10-16 21:36:20 +05:30
|
|
|
this._updateRoom(roomId);
|
2020-10-09 23:13:11 +05:30
|
|
|
}
|
2021-06-17 14:19:55 +05:30
|
|
|
this._updateRightPanel();
|
2020-10-09 23:13:11 +05:30
|
|
|
}));
|
2020-10-16 21:36:20 +05:30
|
|
|
if (!this._gridViewModel) {
|
|
|
|
this._updateRoom(currentRoomId.get());
|
2020-10-12 21:19:06 +05:30
|
|
|
}
|
2020-10-16 21:36:20 +05:30
|
|
|
|
|
|
|
const settings = this.navigation.observe("settings");
|
|
|
|
this.track(settings.subscribe(settingsOpen => {
|
|
|
|
this._updateSettings(settingsOpen);
|
|
|
|
}));
|
|
|
|
this._updateSettings(settings.get());
|
2020-10-28 22:12:18 +05:30
|
|
|
|
2022-02-09 23:30:41 +05:30
|
|
|
const createRoom = this.navigation.observe("create-room");
|
|
|
|
this.track(createRoom.subscribe(createRoomOpen => {
|
|
|
|
this._updateCreateRoom(createRoomOpen);
|
|
|
|
}));
|
|
|
|
this._updateCreateRoom(createRoom.get());
|
|
|
|
|
2020-10-28 22:12:18 +05:30
|
|
|
const lightbox = this.navigation.observe("lightbox");
|
|
|
|
this.track(lightbox.subscribe(eventId => {
|
|
|
|
this._updateLightbox(eventId);
|
|
|
|
}));
|
|
|
|
this._updateLightbox(lightbox.get());
|
2021-05-23 17:17:24 +05:30
|
|
|
|
2021-06-11 23:56:07 +05:30
|
|
|
|
2021-06-30 14:22:39 +05:30
|
|
|
const rightpanel = this.navigation.observe("right-panel");
|
2021-06-17 14:19:55 +05:30
|
|
|
this.track(rightpanel.subscribe(() => this._updateRightPanel()));
|
|
|
|
this._updateRightPanel();
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2020-10-13 18:13:31 +05:30
|
|
|
get id() {
|
2021-12-22 21:39:52 +05:30
|
|
|
return this._client.sessionId;
|
2020-10-13 18:13:31 +05:30
|
|
|
}
|
|
|
|
|
2020-05-06 02:46:51 +05:30
|
|
|
start() {
|
|
|
|
this._sessionStatusViewModel.start();
|
2022-04-07 14:02:23 +05:30
|
|
|
this._client.session.callHandler.loadCalls("m.ring");
|
2022-04-07 20:25:10 +05:30
|
|
|
// TODO: only do this when opening the room
|
|
|
|
this._client.session.callHandler.loadCalls("m.prompt");
|
2020-05-06 02:46:51 +05:30
|
|
|
}
|
|
|
|
|
2021-04-21 19:17:39 +05:30
|
|
|
get activeMiddleViewModel() {
|
2022-02-09 23:30:41 +05:30
|
|
|
return this._roomViewModelObservable?.get() || this._gridViewModel || this._settingsViewModel || this._createRoomViewModel;
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get roomGridViewModel() {
|
|
|
|
return this._gridViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:52:06 +05:30
|
|
|
get leftPanelViewModel() {
|
|
|
|
return this._leftPanelViewModel;
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:46:51 +05:30
|
|
|
get sessionStatusViewModel() {
|
|
|
|
return this._sessionStatusViewModel;
|
2019-06-16 14:24:16 +05:30
|
|
|
}
|
|
|
|
|
2020-10-16 21:36:20 +05:30
|
|
|
get settingsViewModel() {
|
|
|
|
return this._settingsViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:13:11 +05:30
|
|
|
get currentRoomViewModel() {
|
2021-05-07 16:40:35 +05:30
|
|
|
return this._roomViewModelObservable?.get();
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2021-06-17 14:19:55 +05:30
|
|
|
get rightPanelViewModel() {
|
|
|
|
return this._rightPanelViewModel;
|
|
|
|
}
|
|
|
|
|
2022-02-09 23:30:41 +05:30
|
|
|
get createRoomViewModel() {
|
|
|
|
return this._createRoomViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:19:06 +05:30
|
|
|
_updateGrid(roomIds) {
|
|
|
|
const changed = !(this._gridViewModel && roomIds);
|
|
|
|
const currentRoomId = this.navigation.path.get("room");
|
|
|
|
if (roomIds) {
|
|
|
|
if (!this._gridViewModel) {
|
|
|
|
this._gridViewModel = this.track(new RoomGridViewModel(this.childOptions({
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
2021-05-07 16:40:35 +05:30
|
|
|
createRoomViewModelObservable: roomId => new RoomViewModelObservable(this, roomId),
|
2020-10-12 21:19:06 +05:30
|
|
|
})));
|
2021-05-07 16:40:35 +05:30
|
|
|
// try to transfer the current room view model, so we don't have to reload the timeline
|
|
|
|
this._roomViewModelObservable?.unsubscribeAll();
|
|
|
|
if (this._gridViewModel.initializeRoomIdsAndTransferVM(roomIds, this._roomViewModelObservable)) {
|
|
|
|
this._roomViewModelObservable = this.untrack(this._roomViewModelObservable);
|
|
|
|
} else if (this._roomViewModelObservable) {
|
|
|
|
this._roomViewModelObservable = this.disposeTracked(this._roomViewModelObservable);
|
2020-10-12 21:19:06 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._gridViewModel.setRoomIds(roomIds);
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
2020-10-12 21:19:06 +05:30
|
|
|
} else if (this._gridViewModel && !roomIds) {
|
2021-04-21 19:17:39 +05:30
|
|
|
// closing grid, try to show focused room in grid
|
2020-10-12 21:19:06 +05:30
|
|
|
if (currentRoomId) {
|
2021-05-07 16:40:35 +05:30
|
|
|
const vmo = this._gridViewModel.releaseRoomViewModel(currentRoomId.value);
|
|
|
|
if (vmo) {
|
|
|
|
this._roomViewModelObservable = this.track(vmo);
|
|
|
|
this._roomViewModelObservable.subscribe(() => {
|
|
|
|
this.emitChange("activeMiddleViewModel");
|
|
|
|
});
|
2020-10-13 16:42:30 +05:30
|
|
|
}
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
|
|
|
this._gridViewModel = this.disposeTracked(this._gridViewModel);
|
|
|
|
}
|
2020-10-12 21:19:06 +05:30
|
|
|
if (changed) {
|
2021-04-21 19:17:39 +05:30
|
|
|
this.emitChange("activeMiddleViewModel");
|
2020-10-12 21:19:06 +05:30
|
|
|
}
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
|
|
|
|
2022-02-09 23:30:41 +05:30
|
|
|
_createRoomViewModelInstance(roomId) {
|
2021-12-22 21:39:52 +05:30
|
|
|
const room = this._client.session.rooms.get(roomId);
|
2021-05-07 16:40:35 +05:30
|
|
|
if (room) {
|
2022-03-17 17:34:14 +05:30
|
|
|
const roomVM = new RoomViewModel(this.childOptions({room, session: this._client.session}));
|
2021-05-07 16:40:35 +05:30
|
|
|
roomVM.load();
|
|
|
|
return roomVM;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 14:37:19 +05:30
|
|
|
_createUnknownRoomViewModel(roomIdOrAlias) {
|
|
|
|
return new UnknownRoomViewModel(this.childOptions({
|
|
|
|
roomIdOrAlias,
|
2021-12-22 21:39:52 +05:30
|
|
|
session: this._client.session,
|
2021-05-18 14:37:19 +05:30
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:40:35 +05:30
|
|
|
async _createArchivedRoomViewModel(roomId) {
|
2021-12-22 21:39:52 +05:30
|
|
|
const room = await this._client.session.loadArchivedRoom(roomId);
|
2021-05-07 16:40:35 +05:30
|
|
|
if (room) {
|
2022-03-25 19:13:22 +05:30
|
|
|
const roomVM = new RoomViewModel(this.childOptions({room, session: this._client.session}));
|
2021-05-07 16:40:35 +05:30
|
|
|
roomVM.load();
|
|
|
|
return roomVM;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createInviteViewModel(roomId) {
|
2021-12-22 21:39:52 +05:30
|
|
|
const invite = this._client.session.invites.get(roomId);
|
2021-04-23 21:36:38 +05:30
|
|
|
if (invite) {
|
|
|
|
return new InviteViewModel(this.childOptions({
|
|
|
|
invite,
|
2021-12-22 21:39:52 +05:30
|
|
|
mediaRepository: this._client.session.mediaRepository,
|
2021-04-23 21:36:38 +05:30
|
|
|
}));
|
2021-04-21 19:17:39 +05:30
|
|
|
}
|
2021-04-23 21:36:38 +05:30
|
|
|
return null;
|
2021-04-21 19:17:39 +05:30
|
|
|
}
|
|
|
|
|
2022-02-03 22:27:35 +05:30
|
|
|
_createRoomBeingCreatedViewModel(localId) {
|
|
|
|
const roomBeingCreated = this._client.session.roomsBeingCreated.get(localId);
|
|
|
|
if (roomBeingCreated) {
|
|
|
|
return new RoomBeingCreatedViewModel(this.childOptions({
|
|
|
|
roomBeingCreated,
|
|
|
|
mediaRepository: this._client.session.mediaRepository,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:36:20 +05:30
|
|
|
_updateRoom(roomId) {
|
2021-04-23 21:36:38 +05:30
|
|
|
// opening a room and already open?
|
2021-05-07 16:40:35 +05:30
|
|
|
if (this._roomViewModelObservable?.id === roomId) {
|
2020-10-12 21:19:06 +05:30
|
|
|
return;
|
|
|
|
}
|
2021-04-23 21:36:38 +05:30
|
|
|
// close if needed
|
2021-05-07 16:40:35 +05:30
|
|
|
if (this._roomViewModelObservable) {
|
|
|
|
this._roomViewModelObservable = this.disposeTracked(this._roomViewModelObservable);
|
2021-04-23 21:36:38 +05:30
|
|
|
}
|
2021-05-11 16:40:21 +05:30
|
|
|
if (!roomId) {
|
|
|
|
// if clearing the activeMiddleViewModel rather than changing to a different one,
|
|
|
|
// emit so the view picks it up and show the placeholder
|
|
|
|
this.emitChange("activeMiddleViewModel");
|
|
|
|
return;
|
|
|
|
}
|
2021-05-07 16:40:35 +05:30
|
|
|
const vmo = new RoomViewModelObservable(this, roomId);
|
|
|
|
this._roomViewModelObservable = this.track(vmo);
|
|
|
|
// subscription is unsubscribed in RoomViewModelObservable.dispose, and thus handled by track
|
|
|
|
this._roomViewModelObservable.subscribe(() => {
|
|
|
|
this.emitChange("activeMiddleViewModel");
|
|
|
|
});
|
|
|
|
vmo.initialize();
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
2020-10-16 21:36:20 +05:30
|
|
|
|
|
|
|
_updateSettings(settingsOpen) {
|
|
|
|
if (this._settingsViewModel) {
|
|
|
|
this._settingsViewModel = this.disposeTracked(this._settingsViewModel);
|
|
|
|
}
|
|
|
|
if (settingsOpen) {
|
|
|
|
this._settingsViewModel = this.track(new SettingsViewModel(this.childOptions({
|
2021-12-22 21:39:52 +05:30
|
|
|
client: this._client,
|
2020-10-16 21:36:20 +05:30
|
|
|
})));
|
2020-10-20 21:20:43 +05:30
|
|
|
this._settingsViewModel.load();
|
2020-10-16 21:36:20 +05:30
|
|
|
}
|
2021-04-21 19:17:39 +05:30
|
|
|
this.emitChange("activeMiddleViewModel");
|
2020-10-16 21:36:20 +05:30
|
|
|
}
|
2020-10-28 22:12:18 +05:30
|
|
|
|
2022-02-09 23:30:41 +05:30
|
|
|
_updateCreateRoom(createRoomOpen) {
|
|
|
|
if (this._createRoomViewModel) {
|
|
|
|
this._createRoomViewModel = this.disposeTracked(this._createRoomViewModel);
|
|
|
|
}
|
|
|
|
if (createRoomOpen) {
|
|
|
|
this._createRoomViewModel = this.track(new CreateRoomViewModel(this.childOptions({session: this._client.session})));
|
|
|
|
}
|
|
|
|
this.emitChange("activeMiddleViewModel");
|
|
|
|
}
|
|
|
|
|
2020-10-28 22:12:18 +05:30
|
|
|
_updateLightbox(eventId) {
|
|
|
|
if (this._lightboxViewModel) {
|
|
|
|
this._lightboxViewModel = this.disposeTracked(this._lightboxViewModel);
|
|
|
|
}
|
|
|
|
if (eventId) {
|
2021-05-24 17:01:29 +05:30
|
|
|
const room = this._roomFromNavigation();
|
2020-10-30 19:50:11 +05:30
|
|
|
this._lightboxViewModel = this.track(new LightboxViewModel(this.childOptions({eventId, room})));
|
2020-10-28 22:12:18 +05:30
|
|
|
}
|
|
|
|
this.emitChange("lightboxViewModel");
|
|
|
|
}
|
|
|
|
|
|
|
|
get lightboxViewModel() {
|
|
|
|
return this._lightboxViewModel;
|
|
|
|
}
|
2021-05-23 17:17:24 +05:30
|
|
|
|
2021-05-24 17:01:29 +05:30
|
|
|
_roomFromNavigation() {
|
2021-05-23 20:24:39 +05:30
|
|
|
const roomId = this.navigation.path.get("room")?.value;
|
2021-12-22 21:39:52 +05:30
|
|
|
const room = this._client.session.rooms.get(roomId);
|
2021-05-24 17:01:29 +05:30
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2021-06-17 14:19:55 +05:30
|
|
|
_updateRightPanel() {
|
|
|
|
this._rightPanelViewModel = this.disposeTracked(this._rightPanelViewModel);
|
2021-06-30 14:22:39 +05:30
|
|
|
const enable = !!this.navigation.path.get("right-panel")?.value;
|
2021-06-17 14:19:55 +05:30
|
|
|
if (enable) {
|
|
|
|
const room = this._roomFromNavigation();
|
2022-02-03 22:27:35 +05:30
|
|
|
this._rightPanelViewModel = this.track(new RightPanelViewModel(this.childOptions({room, session: this._client.session})));
|
2021-06-17 14:19:55 +05:30
|
|
|
}
|
|
|
|
this.emitChange("rightPanelViewModel");
|
|
|
|
}
|
|
|
|
|
2022-02-10 19:27:48 +05:30
|
|
|
notifyRoomReplaced(oldId, newId) {
|
|
|
|
this.navigation.push("room", newId);
|
|
|
|
}
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|