hydrogen-web/src/domain/session/room/CallViewModel.ts

86 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-03-17 17:34:14 +05:30
/*
2022-03-17 17:37:55 +05:30
Copyright 2022 The Matrix.org Foundation C.I.C.
2022-03-17 17:34:14 +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.
*/
2022-03-17 17:37:55 +05:30
import {ViewModel, Options as BaseOptions} from "../../ViewModel";
2022-03-17 17:34:14 +05:30
import type {GroupCall} from "../../../matrix/calls/group/GroupCall";
2022-03-17 17:37:55 +05:30
import type {Member} from "../../../matrix/calls/group/Member";
import type {BaseObservableList} from "../../../observable/list/BaseObservableList";
2022-04-13 22:04:01 +05:30
import type {Stream} from "../../../platform/types/MediaDevices";
2022-03-17 17:34:14 +05:30
2022-03-17 17:37:55 +05:30
type Options = BaseOptions & {call: GroupCall};
2022-03-17 17:34:14 +05:30
2022-03-17 17:37:55 +05:30
export class CallViewModel extends ViewModel<Options> {
public readonly memberViewModels: BaseObservableList<CallMemberViewModel>;
2022-03-17 17:34:14 +05:30
2022-03-17 17:37:55 +05:30
constructor(options: Options) {
2022-03-17 17:34:14 +05:30
super(options);
2022-03-17 17:37:55 +05:30
this.memberViewModels = this.getOption("call").members
2022-04-07 20:25:26 +05:30
.filterValues(member => member.isConnected)
2022-03-17 17:37:55 +05:30
.mapValues(member => new CallMemberViewModel(this.childOptions({member})))
2022-03-21 21:00:13 +05:30
.sortValues((a, b) => a.compare(b));
2022-03-17 17:34:14 +05:30
}
2022-03-24 18:22:19 +05:30
private get call(): GroupCall {
return this.getOption("call");
}
2022-03-17 17:34:14 +05:30
get name(): string {
2022-03-24 18:22:19 +05:30
return this.call.name;
2022-03-17 17:37:55 +05:30
}
2022-03-21 21:00:13 +05:30
get id(): string {
2022-03-24 18:22:19 +05:30
return this.call.id;
2022-03-21 21:00:13 +05:30
}
2022-04-13 22:04:01 +05:30
get localStream(): Stream | undefined {
return this.call.localMedia?.userMedia;
2022-03-24 18:22:19 +05:30
}
leave() {
if (this.call.hasJoined) {
this.call.leave();
}
2022-03-17 17:37:55 +05:30
}
2022-04-15 02:49:44 +05:30
async toggleVideo() {
const localMedia = this.call.localMedia!;
const toggledMedia = localMedia.withMuted(localMedia.microphoneMuted, !localMedia.cameraMuted);
await this.call.setMedia(toggledMedia);
}
2022-03-17 17:37:55 +05:30
}
type MemberOptions = BaseOptions & {member: Member};
2022-03-21 21:00:13 +05:30
export class CallMemberViewModel extends ViewModel<MemberOptions> {
2022-04-13 22:04:01 +05:30
get stream(): Stream | undefined {
return this.member.remoteMedia?.userMedia;
2022-03-24 18:22:19 +05:30
}
private get member(): Member {
return this.getOption("member");
2022-03-17 17:34:14 +05:30
}
2022-03-21 21:00:13 +05:30
compare(other: CallMemberViewModel): number {
2022-03-24 18:22:19 +05:30
const myUserId = this.member.member.userId;
const otherUserId = other.member.member.userId;
2022-03-21 21:00:13 +05:30
if(myUserId === otherUserId) {
return 0;
}
return myUserId < otherUserId ? -1 : 1;
}
2022-03-17 17:34:14 +05:30
}