Stop voice broadcast from save device on resume (#9509)
This commit is contained in:
parent
ee61994c05
commit
e6a645dfae
6 changed files with 196 additions and 220 deletions
|
@ -14,43 +14,87 @@ 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 { ClientEvent, MatrixClient, MatrixEvent, RelationType, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { SyncState } from "matrix-js-sdk/src/sync";
|
||||
|
||||
import { VoiceBroadcastInfoEventContent, VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "..";
|
||||
import { IDestroyable } from "../../utils/IDestroyable";
|
||||
import { findRoomLiveVoiceBroadcastFromUserAndDevice } from "./findRoomLiveVoiceBroadcastFromUserAndDevice";
|
||||
import { resumeVoiceBroadcastInRoom } from "./resumeVoiceBroadcastInRoom";
|
||||
|
||||
/**
|
||||
* Handles voice broadcasts on app resume (after logging in, reload, crash…).
|
||||
*/
|
||||
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();
|
||||
if (client.isInitialSyncComplete()) {
|
||||
this.resume();
|
||||
} else {
|
||||
// wait for initial sync
|
||||
client.on(ClientEvent.Sync, this.onClientSync);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
private onClientSync = () => {
|
||||
if (this.client.getSyncState() === SyncState.Syncing) {
|
||||
this.client.off(ClientEvent.Sync, this.onClientSync);
|
||||
this.resume();
|
||||
}
|
||||
};
|
||||
|
||||
private resume(): void {
|
||||
const userId = this.client.getUserId();
|
||||
const deviceId = this.client.getDeviceId();
|
||||
|
||||
if (!userId || !deviceId) {
|
||||
// Resuming a voice broadcast only makes sense if there is a user.
|
||||
return;
|
||||
}
|
||||
|
||||
this.client.getRooms().forEach((room: Room) => {
|
||||
const infoEvent = findRoomLiveVoiceBroadcastFromUserAndDevice(room, userId, deviceId);
|
||||
|
||||
if (infoEvent) {
|
||||
// Found a live broadcast event from current device; stop it.
|
||||
// Stopping it is a temporary solution (see PSF-1669).
|
||||
this.sendStopVoiceBroadcastStateEvent(infoEvent);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private sendStopVoiceBroadcastStateEvent(infoEvent: MatrixEvent): void {
|
||||
const userId = this.client.getUserId();
|
||||
const deviceId = this.client.getDeviceId();
|
||||
const roomId = infoEvent.getRoomId();
|
||||
|
||||
if (!userId || !deviceId || !roomId) {
|
||||
// We can only send a state event if we know all the IDs.
|
||||
return;
|
||||
}
|
||||
|
||||
const content: VoiceBroadcastInfoEventContent = {
|
||||
device_id: deviceId,
|
||||
state: VoiceBroadcastInfoState.Stopped,
|
||||
};
|
||||
|
||||
// all events should reference the started event
|
||||
const referencedEventId = infoEvent.getContent()?.state === VoiceBroadcastInfoState.Started
|
||||
? infoEvent.getId()
|
||||
: infoEvent.getContent()?.["m.relates_to"]?.event_id;
|
||||
|
||||
if (referencedEventId) {
|
||||
content["m.relates_to"] = {
|
||||
rel_type: RelationType.Reference,
|
||||
event_id: referencedEventId,
|
||||
};
|
||||
}
|
||||
|
||||
this.client.sendStateEvent(roomId, VoiceBroadcastInfoEventType, content, userId);
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.client.off(ClientEvent.Room, this.onRoom);
|
||||
this.seenRooms = new Set<string>();
|
||||
this.client.off(ClientEvent.Sync, this.onClientSync);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue