2021-05-07 16:40:35 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-09-30 06:00:21 +05:30
|
|
|
import {ObservableValue} from "../../observable/ObservableValue";
|
2022-02-10 21:09:54 +05:30
|
|
|
import {RoomStatus} from "../../matrix/room/common";
|
2021-05-07 16:40:35 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
Depending on the status of a room (invited, joined, archived, or none),
|
|
|
|
we want to show a different view with a different view model
|
|
|
|
when showing a room. Furthermore, this logic is needed both in the
|
|
|
|
single room view and in the grid view. So this logic is extracted here,
|
|
|
|
and this observable updates with the right view model as the status for
|
|
|
|
a room changes.
|
|
|
|
|
|
|
|
To not have to track the subscription manually in the SessionViewModel and
|
|
|
|
the RoomGridViewModel, all subscriptions are removed in the dispose method.
|
|
|
|
Only when transferring a RoomViewModelObservable between the SessionViewModel
|
|
|
|
and RoomGridViewModel, unsubscribeAll should be called prior to doing
|
|
|
|
the transfer, so either parent view model don't keep getting updates for
|
|
|
|
the now transferred child view model.
|
|
|
|
|
|
|
|
This is also why there is an explicit initialize method, see comment there.
|
|
|
|
*/
|
|
|
|
export class RoomViewModelObservable extends ObservableValue {
|
2022-02-03 22:27:35 +05:30
|
|
|
constructor(sessionViewModel, roomIdOrLocalId) {
|
2021-05-07 16:40:35 +05:30
|
|
|
super(null);
|
2021-05-18 14:36:48 +05:30
|
|
|
this._statusSubscription = null;
|
2021-05-07 16:40:35 +05:30
|
|
|
this._sessionViewModel = sessionViewModel;
|
2022-02-03 22:27:35 +05:30
|
|
|
this.id = roomIdOrLocalId;
|
2021-05-07 16:40:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Separate initialize method rather than doing this onSubscribeFirst because
|
|
|
|
we don't want to run this again when transferring this value between
|
|
|
|
SessionViewModel and RoomGridViewModel, as onUnsubscribeLast and onSubscribeFirst
|
|
|
|
are called in that case.
|
|
|
|
*/
|
|
|
|
async initialize() {
|
2021-12-22 21:39:52 +05:30
|
|
|
const {session} = this._sessionViewModel._client;
|
2021-05-18 14:36:48 +05:30
|
|
|
const statusObservable = await session.observeRoomStatus(this.id);
|
|
|
|
this.set(await this._statusToViewModel(statusObservable.get()));
|
|
|
|
this._statusSubscription = statusObservable.subscribe(async status => {
|
2021-05-11 20:09:33 +05:30
|
|
|
// first dispose existing VM, if any
|
|
|
|
this.get()?.dispose();
|
2021-05-07 16:40:35 +05:30
|
|
|
this.set(await this._statusToViewModel(status));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _statusToViewModel(status) {
|
2022-02-03 22:27:35 +05:30
|
|
|
if (status & RoomStatus.Replaced) {
|
|
|
|
if (status & RoomStatus.BeingCreated) {
|
|
|
|
const {session} = this._sessionViewModel._client;
|
|
|
|
const roomBeingCreated = session.roomsBeingCreated.get(this.id);
|
2022-02-10 19:27:48 +05:30
|
|
|
this._sessionViewModel.notifyRoomReplaced(roomBeingCreated.id, roomBeingCreated.roomId);
|
2022-02-03 22:27:35 +05:30
|
|
|
} else {
|
|
|
|
throw new Error("Don't know how to replace a room with this status: " + (status ^ RoomStatus.Replaced));
|
|
|
|
}
|
|
|
|
} else if (status & RoomStatus.BeingCreated) {
|
|
|
|
return this._sessionViewModel._createRoomBeingCreatedViewModel(this.id);
|
|
|
|
} else if (status & RoomStatus.Invited) {
|
2021-05-07 16:40:35 +05:30
|
|
|
return this._sessionViewModel._createInviteViewModel(this.id);
|
2022-02-03 22:27:35 +05:30
|
|
|
} else if (status & RoomStatus.Joined) {
|
2022-02-09 23:32:18 +05:30
|
|
|
return this._sessionViewModel._createRoomViewModelInstance(this.id);
|
2022-02-03 22:27:35 +05:30
|
|
|
} else if (status & RoomStatus.Archived) {
|
2021-05-11 16:41:38 +05:30
|
|
|
return await this._sessionViewModel._createArchivedRoomViewModel(this.id);
|
2021-05-18 14:37:19 +05:30
|
|
|
} else {
|
|
|
|
return this._sessionViewModel._createUnknownRoomViewModel(this.id);
|
2021-05-07 16:40:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
if (this._statusSubscription) {
|
|
|
|
this._statusSubscription = this._statusSubscription();
|
|
|
|
}
|
|
|
|
this.unsubscribeAll();
|
|
|
|
this.get()?.dispose();
|
|
|
|
}
|
2021-09-30 06:00:21 +05:30
|
|
|
}
|