Resume voice broadcast on load (#9475)

This commit is contained in:
Michael Weimann 2022-10-21 09:30:02 +02:00 committed by GitHub
parent 493ee425b7
commit 5f59ce182e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 554 additions and 22 deletions

View file

@ -139,6 +139,7 @@ import { ValidatedServerConfig } from '../../utils/ValidatedServerConfig';
import { isLocalRoom } from '../../utils/localRoom/isLocalRoom';
import { SdkContextClass, SDKContext } from '../../contexts/SDKContext';
import { viewUserDeviceSettings } from '../../actions/handlers/viewUserDeviceSettings';
import { VoiceBroadcastResumer } from '../../voice-broadcast';
// legacy export
export { default as Views } from "../../Views";
@ -234,6 +235,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
private focusComposer: boolean;
private subTitleStatus: string;
private prevWindowWidth: number;
private voiceBroadcastResumer: VoiceBroadcastResumer;
private readonly loggedInView: React.RefObject<LoggedInViewType>;
private readonly dispatcherRef: string;
@ -433,6 +435,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
window.removeEventListener("resize", this.onWindowResized);
if (this.accountPasswordTimer !== null) clearTimeout(this.accountPasswordTimer);
if (this.voiceBroadcastResumer) this.voiceBroadcastResumer.destroy();
}
private onWindowResized = (): void => {
@ -1618,6 +1621,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
});
}
});
this.voiceBroadcastResumer = new VoiceBroadcastResumer(cli);
}
/**

View file

@ -36,9 +36,12 @@ export * from "./stores/VoiceBroadcastPlaybacksStore";
export * from "./stores/VoiceBroadcastRecordingsStore";
export * from "./utils/getChunkLength";
export * from "./utils/hasRoomLiveVoiceBroadcast";
export * from "./utils/findRoomLiveVoiceBroadcastFromUserAndDevice";
export * from "./utils/resumeVoiceBroadcastInRoom";
export * from "./utils/shouldDisplayAsVoiceBroadcastRecordingTile";
export * from "./utils/shouldDisplayAsVoiceBroadcastTile";
export * from "./utils/startNewVoiceBroadcastRecording";
export * from "./utils/VoiceBroadcastResumer";
export const VoiceBroadcastInfoEventType = "io.element.voice_broadcast_info";
export const VoiceBroadcastChunkEventType = "io.element.voice_broadcast_chunk";

View file

@ -52,9 +52,23 @@ export class VoiceBroadcastRecording
public constructor(
public readonly infoEvent: MatrixEvent,
private client: MatrixClient,
initialState?: VoiceBroadcastInfoState,
) {
super();
if (initialState) {
this.state = initialState;
} else {
this.setInitialStateFromInfoEvent();
}
// TODO Michael W: listen for state updates
//
this.infoEvent.on(MatrixEventEvent.BeforeRedaction, this.onBeforeRedaction);
this.dispatcherRef = dis.register(this.onAction);
}
private setInitialStateFromInfoEvent(): void {
const room = this.client.getRoom(this.infoEvent.getRoomId());
const relations = room?.getUnfilteredTimelineSet()?.relations?.getChildEventsForEvent(
this.infoEvent.getId(),
@ -65,10 +79,6 @@ export class VoiceBroadcastRecording
this.state = !relatedEvents?.find((event: MatrixEvent) => {
return event.getContent()?.state === VoiceBroadcastInfoState.Stopped;
}) ? VoiceBroadcastInfoState.Started : VoiceBroadcastInfoState.Stopped;
// TODO Michael W: add listening for updates
this.infoEvent.on(MatrixEventEvent.BeforeRedaction, this.onBeforeRedaction);
this.dispatcherRef = dis.register(this.onAction);
}
public async start(): Promise<void> {

View file

@ -0,0 +1,56 @@
/*
Copyright 2022 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.
*/
import { ClientEvent, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { IDestroyable } from "../../utils/IDestroyable";
import { findRoomLiveVoiceBroadcastFromUserAndDevice } from "./findRoomLiveVoiceBroadcastFromUserAndDevice";
import { resumeVoiceBroadcastInRoom } from "./resumeVoiceBroadcastInRoom";
export class VoiceBroadcastResumer implements IDestroyable {
private seenRooms = new Set<string>();
private userId: string;
private deviceId: string;
public constructor(
private client: MatrixClient,
) {
this.client.on(ClientEvent.Room, this.onRoom);
this.userId = this.client.getUserId();
this.deviceId = this.client.getDeviceId();
}
private onRoom = (room: Room): void => {
if (this.seenRooms.has(room.roomId)) return;
this.seenRooms.add(room.roomId);
const infoEvent = findRoomLiveVoiceBroadcastFromUserAndDevice(
room,
this.userId,
this.deviceId,
);
if (infoEvent) {
resumeVoiceBroadcastInRoom(infoEvent, room, this.client);
}
};
destroy(): void {
this.client.off(ClientEvent.Room, this.onRoom);
this.seenRooms = new Set<string>();
}
}

View file

@ -0,0 +1,37 @@
/*
Copyright 2022 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.
*/
import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "..";
export const findRoomLiveVoiceBroadcastFromUserAndDevice = (
room: Room,
userId: string,
deviceId: string,
): MatrixEvent | null => {
const stateEvent = room.currentState.getStateEvents(VoiceBroadcastInfoEventType, userId);
// no broadcast from that user
if (!stateEvent) return null;
const content = stateEvent.getContent() || {};
// stopped broadcast
if (content.state === VoiceBroadcastInfoState.Stopped) return null;
return content.device_id === deviceId ? stateEvent : null;
};

View file

@ -0,0 +1,34 @@
/*
Copyright 2022 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.
*/
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { VoiceBroadcastInfoState, VoiceBroadcastRecording } from "..";
import { VoiceBroadcastRecordingsStore } from "../stores/VoiceBroadcastRecordingsStore";
export const resumeVoiceBroadcastInRoom = (latestInfoEvent: MatrixEvent, room: Room, client: MatrixClient) => {
// voice broadcasts are based on their started event, try to find it
const infoEvent = latestInfoEvent.getContent()?.state === VoiceBroadcastInfoState.Started
? latestInfoEvent
: room.findEventById(latestInfoEvent.getRelation()?.event_id);
if (!infoEvent) {
return;
}
const recording = new VoiceBroadcastRecording(infoEvent, client, VoiceBroadcastInfoState.Paused);
VoiceBroadcastRecordingsStore.instance().setCurrent(recording);
};