2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-10-06 12:22:06 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
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 12:22:06 +02:00
|
|
|
import {LeftPanelViewModel} from "./leftpanel/LeftPanelViewModel.js";
|
2021-06-03 17:33:02 +02:00
|
|
|
import {RoomViewModel} from "./room/RoomViewModel.js";
|
2021-05-18 11:07:19 +02:00
|
|
|
import {UnknownRoomViewModel} from "./room/UnknownRoomViewModel.js";
|
2021-04-21 15:47:39 +02:00
|
|
|
import {InviteViewModel} from "./room/InviteViewModel.js";
|
2020-10-28 17:42:18 +01:00
|
|
|
import {LightboxViewModel} from "./room/LightboxViewModel.js";
|
2020-05-05 23:17:27 +02:00
|
|
|
import {SessionStatusViewModel} from "./SessionStatusViewModel.js";
|
2020-10-07 12:25:03 +02:00
|
|
|
import {RoomGridViewModel} from "./RoomGridViewModel.js";
|
2020-10-20 12:10:41 +02:00
|
|
|
import {SettingsViewModel} from "./settings/SettingsViewModel.js";
|
2020-05-04 19:23:11 +02:00
|
|
|
import {ViewModel} from "../ViewModel.js";
|
2021-05-07 13:10:35 +02:00
|
|
|
import {RoomViewModelObservable} from "./RoomViewModelObservable.js";
|
2021-06-11 20:26:07 +02:00
|
|
|
import {MemberListViewModel} from "./rightpanel/MemberListViewModel.js";
|
2021-06-17 10:49:55 +02:00
|
|
|
import { RightPanelViewModel } from "./rightpanel/RightPanelViewModel.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
2020-05-04 19:23:11 +02:00
|
|
|
export class SessionViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-05-05 23:17:27 +02:00
|
|
|
const {sessionContainer} = options;
|
2020-10-09 17:01:22 +02:00
|
|
|
this._sessionContainer = this.track(sessionContainer);
|
2020-05-05 23:16:51 +02:00
|
|
|
this._sessionStatusViewModel = this.track(new SessionStatusViewModel(this.childOptions({
|
2020-05-07 00:05:21 +02:00
|
|
|
sync: sessionContainer.sync,
|
2020-09-17 18:00:00 +02:00
|
|
|
reconnector: sessionContainer.reconnector,
|
|
|
|
session: sessionContainer.session,
|
2020-05-05 23:16:51 +02:00
|
|
|
})));
|
2020-10-14 11:34:34 +02:00
|
|
|
this._leftPanelViewModel = this.track(new LeftPanelViewModel(this.childOptions({
|
2021-04-21 17:26:34 +02:00
|
|
|
invites: this._sessionContainer.session.invites,
|
2020-10-12 17:49:06 +02:00
|
|
|
rooms: this._sessionContainer.session.rooms
|
2020-10-14 11:34:34 +02:00
|
|
|
})));
|
2020-10-16 18:06:20 +02:00
|
|
|
this._settingsViewModel = null;
|
2021-05-07 13:10:35 +02:00
|
|
|
this._roomViewModelObservable = null;
|
2020-10-07 12:25:03 +02:00
|
|
|
this._gridViewModel = null;
|
2020-10-12 17:49:06 +02:00
|
|
|
this._setupNavigation();
|
|
|
|
}
|
2020-10-09 19:43:11 +02:00
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
_setupNavigation() {
|
|
|
|
const gridRooms = this.navigation.observe("rooms");
|
2020-10-09 19:43:11 +02:00
|
|
|
// this gives us a set of room ids in the grid
|
2020-10-12 17:49:06 +02:00
|
|
|
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 18:06:20 +02:00
|
|
|
this._updateRoom(roomId);
|
2020-10-09 19:43:11 +02:00
|
|
|
}
|
2021-06-17 10:49:55 +02:00
|
|
|
this._updateRightPanel();
|
2020-10-09 19:43:11 +02:00
|
|
|
}));
|
2020-10-16 18:06:20 +02:00
|
|
|
if (!this._gridViewModel) {
|
|
|
|
this._updateRoom(currentRoomId.get());
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
2020-10-16 18:06:20 +02:00
|
|
|
|
|
|
|
const settings = this.navigation.observe("settings");
|
|
|
|
this.track(settings.subscribe(settingsOpen => {
|
|
|
|
this._updateSettings(settingsOpen);
|
|
|
|
}));
|
|
|
|
this._updateSettings(settings.get());
|
2020-10-28 17:42:18 +01:00
|
|
|
|
|
|
|
const lightbox = this.navigation.observe("lightbox");
|
|
|
|
this.track(lightbox.subscribe(eventId => {
|
|
|
|
this._updateLightbox(eventId);
|
|
|
|
}));
|
|
|
|
this._updateLightbox(lightbox.get());
|
2021-05-23 13:47:24 +02:00
|
|
|
|
2021-06-17 10:49:55 +02:00
|
|
|
// const members = this.navigation.observe("members");
|
|
|
|
// this.track(members.subscribe(() => this._toggleMemberListPanel()));
|
|
|
|
// this._toggleMemberListPanel();
|
2021-06-11 20:26:07 +02:00
|
|
|
|
2021-06-17 10:49:55 +02:00
|
|
|
const rightpanel = this.navigation.observe("rightpanel");
|
|
|
|
this.track(rightpanel.subscribe(() => this._updateRightPanel()));
|
|
|
|
this._updateRightPanel();
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 14:43:31 +02:00
|
|
|
get id() {
|
|
|
|
return this._sessionContainer.sessionId;
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
start() {
|
|
|
|
this._sessionStatusViewModel.start();
|
|
|
|
}
|
|
|
|
|
2021-04-21 15:47:39 +02:00
|
|
|
get activeMiddleViewModel() {
|
2021-05-07 13:10:35 +02:00
|
|
|
return this._roomViewModelObservable?.get() || this._gridViewModel || this._settingsViewModel;
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get roomGridViewModel() {
|
|
|
|
return this._gridViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-06 12:22:06 +02:00
|
|
|
get leftPanelViewModel() {
|
|
|
|
return this._leftPanelViewModel;
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
get sessionStatusViewModel() {
|
|
|
|
return this._sessionStatusViewModel;
|
2019-06-16 10:54:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 18:06:20 +02:00
|
|
|
get settingsViewModel() {
|
|
|
|
return this._settingsViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:43:11 +02:00
|
|
|
get currentRoomViewModel() {
|
2021-05-07 13:10:35 +02:00
|
|
|
return this._roomViewModelObservable?.get();
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 20:26:07 +02:00
|
|
|
get memberListViewModel() {
|
|
|
|
return this._memberListViewModel;
|
|
|
|
}
|
|
|
|
|
2021-06-17 10:49:55 +02:00
|
|
|
get rightPanelViewModel() {
|
|
|
|
return this._rightPanelViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
_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 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: roomId => new RoomViewModelObservable(this, roomId),
|
2020-10-12 17:49:06 +02:00
|
|
|
})));
|
2021-05-07 13:10:35 +02:00
|
|
|
// 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 17:49:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._gridViewModel.setRoomIds(roomIds);
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
} else if (this._gridViewModel && !roomIds) {
|
2021-04-21 15:47:39 +02:00
|
|
|
// closing grid, try to show focused room in grid
|
2020-10-12 17:49:06 +02:00
|
|
|
if (currentRoomId) {
|
2021-05-07 13:10:35 +02:00
|
|
|
const vmo = this._gridViewModel.releaseRoomViewModel(currentRoomId.value);
|
|
|
|
if (vmo) {
|
|
|
|
this._roomViewModelObservable = this.track(vmo);
|
|
|
|
this._roomViewModelObservable.subscribe(() => {
|
|
|
|
this.emitChange("activeMiddleViewModel");
|
|
|
|
});
|
2020-10-13 13:12:30 +02:00
|
|
|
}
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
this._gridViewModel = this.disposeTracked(this._gridViewModel);
|
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
if (changed) {
|
2021-04-21 15:47:39 +02:00
|
|
|
this.emitChange("activeMiddleViewModel");
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 13:10:35 +02:00
|
|
|
_createRoomViewModel(roomId) {
|
|
|
|
const room = this._sessionContainer.session.rooms.get(roomId);
|
|
|
|
if (room) {
|
2021-05-31 15:18:44 +02:00
|
|
|
const roomVM = new RoomViewModel(this.childOptions({room}));
|
2021-05-07 13:10:35 +02:00
|
|
|
roomVM.load();
|
|
|
|
return roomVM;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:07:19 +02:00
|
|
|
_createUnknownRoomViewModel(roomIdOrAlias) {
|
|
|
|
return new UnknownRoomViewModel(this.childOptions({
|
|
|
|
roomIdOrAlias,
|
|
|
|
session: this._sessionContainer.session,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-05-07 13:10:35 +02:00
|
|
|
async _createArchivedRoomViewModel(roomId) {
|
|
|
|
const room = await this._sessionContainer.session.loadArchivedRoom(roomId);
|
|
|
|
if (room) {
|
2021-05-31 15:18:44 +02:00
|
|
|
const roomVM = new RoomViewModel(this.childOptions({room}));
|
2021-05-07 13:10:35 +02:00
|
|
|
roomVM.load();
|
|
|
|
return roomVM;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createInviteViewModel(roomId) {
|
2021-04-21 15:47:39 +02:00
|
|
|
const invite = this._sessionContainer.session.invites.get(roomId);
|
2021-04-23 18:06:38 +02:00
|
|
|
if (invite) {
|
|
|
|
return new InviteViewModel(this.childOptions({
|
|
|
|
invite,
|
|
|
|
mediaRepository: this._sessionContainer.session.mediaRepository,
|
|
|
|
}));
|
2021-04-21 15:47:39 +02:00
|
|
|
}
|
2021-04-23 18:06:38 +02:00
|
|
|
return null;
|
2021-04-21 15:47:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 18:06:20 +02:00
|
|
|
_updateRoom(roomId) {
|
2021-04-23 18:06:38 +02:00
|
|
|
// opening a room and already open?
|
2021-05-07 13:10:35 +02:00
|
|
|
if (this._roomViewModelObservable?.id === roomId) {
|
2020-10-12 17:49:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-23 18:06:38 +02:00
|
|
|
// close if needed
|
2021-05-07 13:10:35 +02:00
|
|
|
if (this._roomViewModelObservable) {
|
|
|
|
this._roomViewModelObservable = this.disposeTracked(this._roomViewModelObservable);
|
2021-04-23 18:06:38 +02:00
|
|
|
}
|
2021-05-11 13:10:21 +02:00
|
|
|
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 13:10:35 +02:00
|
|
|
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-27 22:50:08 +01:00
|
|
|
}
|
2020-10-16 18:06:20 +02:00
|
|
|
|
|
|
|
_updateSettings(settingsOpen) {
|
|
|
|
if (this._settingsViewModel) {
|
|
|
|
this._settingsViewModel = this.disposeTracked(this._settingsViewModel);
|
|
|
|
}
|
|
|
|
if (settingsOpen) {
|
|
|
|
this._settingsViewModel = this.track(new SettingsViewModel(this.childOptions({
|
2020-10-20 17:50:43 +02:00
|
|
|
session: this._sessionContainer.session,
|
2020-10-16 18:06:20 +02:00
|
|
|
})));
|
2020-10-20 17:50:43 +02:00
|
|
|
this._settingsViewModel.load();
|
2020-10-16 18:06:20 +02:00
|
|
|
}
|
2021-04-21 15:47:39 +02:00
|
|
|
this.emitChange("activeMiddleViewModel");
|
2020-10-16 18:06:20 +02:00
|
|
|
}
|
2020-10-28 17:42:18 +01:00
|
|
|
|
|
|
|
_updateLightbox(eventId) {
|
|
|
|
if (this._lightboxViewModel) {
|
|
|
|
this._lightboxViewModel = this.disposeTracked(this._lightboxViewModel);
|
|
|
|
}
|
|
|
|
if (eventId) {
|
2021-05-24 13:31:29 +02:00
|
|
|
const room = this._roomFromNavigation();
|
2020-10-30 15:20:11 +01:00
|
|
|
this._lightboxViewModel = this.track(new LightboxViewModel(this.childOptions({eventId, room})));
|
2020-10-28 17:42:18 +01:00
|
|
|
}
|
|
|
|
this.emitChange("lightboxViewModel");
|
|
|
|
}
|
|
|
|
|
|
|
|
get lightboxViewModel() {
|
|
|
|
return this._lightboxViewModel;
|
|
|
|
}
|
2021-05-23 13:47:24 +02:00
|
|
|
|
2021-05-24 13:31:29 +02:00
|
|
|
_roomFromNavigation() {
|
2021-05-23 16:54:39 +02:00
|
|
|
const roomId = this.navigation.path.get("room")?.value;
|
2021-05-23 13:47:24 +02:00
|
|
|
const room = this._sessionContainer.session.rooms.get(roomId);
|
2021-05-24 13:31:29 +02:00
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2021-06-11 20:26:07 +02:00
|
|
|
async _toggleMemberListPanel() {
|
|
|
|
this._memberListViewModel = this.disposeTracked(this._memberListViewModel);
|
|
|
|
const enable = !!this.navigation.path.get("members")?.value;
|
|
|
|
if (enable) {
|
|
|
|
const room = this._roomFromNavigation();
|
|
|
|
const list = await room.loadMemberList();
|
|
|
|
const members = list.members;
|
|
|
|
this._memberListViewModel = this.track(
|
|
|
|
new MemberListViewModel(this.childOptions({members}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.emitChange("memberListViewModel");
|
|
|
|
}
|
2021-06-17 10:49:55 +02:00
|
|
|
|
|
|
|
_updateRightPanel() {
|
|
|
|
this._rightPanelViewModel = this.disposeTracked(this._rightPanelViewModel);
|
|
|
|
const enable = !!this.navigation.path.get("rightpanel")?.value;
|
|
|
|
if (enable) {
|
|
|
|
const room = this._roomFromNavigation();
|
|
|
|
this._rightPanelViewModel = this.track(new RightPanelViewModel(this.childOptions({room})));
|
|
|
|
}
|
|
|
|
this.emitChange("rightPanelViewModel");
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|