From 62deafdaf0e81689da49007f08391b3d902a2c06 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Wed, 4 Jan 2023 10:54:36 +0100 Subject: [PATCH] Check connection before starting broadcast --- src/i18n/strings/en_EN.json | 2 ++ src/voice-broadcast/index.ts | 1 + ...owCantStartRecordingNoConnectionDialog.tsx | 29 +++++++++++++++++++ .../utils/startNewVoiceBroadcastRecording.ts | 8 +++++ ...artNewVoiceBroadcastRecording-test.ts.snap | 23 +++++++++++++++ .../startNewVoiceBroadcastRecording-test.ts | 13 +++++++++ 6 files changed, 76 insertions(+) create mode 100644 src/voice-broadcast/utils/showCantStartRecordingNoConnectionDialog.tsx diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index e481dfca6fb0..e22e20336562 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -652,6 +652,8 @@ "Someone else is already recording a voice broadcast. Wait for their voice broadcast to end to start a new one.": "Someone else is already recording a voice broadcast. Wait for their voice broadcast to end to start a new one.", "Can’t start a call": "Can’t start a call", "You can’t start a call as you are currently recording a live broadcast. Please end your live broadcast in order to start a call.": "You can’t start a call as you are currently recording a live broadcast. Please end your live broadcast in order to start a call.", + "Connection error": "Connection error", + "Unfortunately we're unable to start a recording right now. Please try again later.": "Unfortunately we're unable to start a recording right now. Please try again later.", "You ended a voice broadcast": "You ended a voice broadcast", "%(senderName)s ended a voice broadcast": "%(senderName)s ended a voice broadcast", "You ended a voice broadcast": "You ended a voice broadcast", diff --git a/src/voice-broadcast/index.ts b/src/voice-broadcast/index.ts index b6d859a925d1..4ff5c0d4ca85 100644 --- a/src/voice-broadcast/index.ts +++ b/src/voice-broadcast/index.ts @@ -55,6 +55,7 @@ export * from "./utils/retrieveStartedInfoEvent"; export * from "./utils/shouldDisplayAsVoiceBroadcastRecordingTile"; export * from "./utils/shouldDisplayAsVoiceBroadcastTile"; export * from "./utils/shouldDisplayAsVoiceBroadcastStoppedText"; +export * from "./utils/showCantStartRecordingNoConnectionDialog"; export * from "./utils/startNewVoiceBroadcastRecording"; export * from "./utils/textForVoiceBroadcastStoppedEvent"; export * from "./utils/textForVoiceBroadcastStoppedEventWithoutLink"; diff --git a/src/voice-broadcast/utils/showCantStartRecordingNoConnectionDialog.tsx b/src/voice-broadcast/utils/showCantStartRecordingNoConnectionDialog.tsx new file mode 100644 index 000000000000..cd7172b93904 --- /dev/null +++ b/src/voice-broadcast/utils/showCantStartRecordingNoConnectionDialog.tsx @@ -0,0 +1,29 @@ +/* +Copyright 2023 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 React from "react"; + +import InfoDialog from "../../components/views/dialogs/InfoDialog"; +import { _t } from "../../languageHandler"; +import Modal from "../../Modal"; + +export const showCantStartRecordingNoConnectionDialog = (): void => { + Modal.createDialog(InfoDialog, { + title: _t("Connection error"), + description:

{_t("Unfortunately we're unable to start a recording right now. Please try again later.")}

, + hasCloseButton: true, + }); +}; diff --git a/src/voice-broadcast/utils/startNewVoiceBroadcastRecording.ts b/src/voice-broadcast/utils/startNewVoiceBroadcastRecording.ts index 4c464f7dfa16..f5d43f210ec8 100644 --- a/src/voice-broadcast/utils/startNewVoiceBroadcastRecording.ts +++ b/src/voice-broadcast/utils/startNewVoiceBroadcastRecording.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { ISendEventResponse, MatrixClient, Room, RoomStateEvent } from "matrix-js-sdk/src/matrix"; +import { SyncState } from "matrix-js-sdk/src/sync"; import { defer } from "matrix-js-sdk/src/utils"; import { @@ -25,6 +26,7 @@ import { VoiceBroadcastRecording, getChunkLength, VoiceBroadcastPlaybacksStore, + showCantStartRecordingNoConnectionDialog, } from ".."; import { checkVoiceBroadcastPreConditions } from "./checkVoiceBroadcastPreConditions"; @@ -92,6 +94,12 @@ export const startNewVoiceBroadcastRecording = async ( return null; } + if (client.getSyncState() === SyncState.Error) { + console.log("wuhurst"); + showCantStartRecordingNoConnectionDialog(); + return null; + } + // pause and clear current playback (if any) playbacksStore.getCurrent()?.pause(); playbacksStore.clearCurrent(); diff --git a/test/voice-broadcast/utils/__snapshots__/startNewVoiceBroadcastRecording-test.ts.snap b/test/voice-broadcast/utils/__snapshots__/startNewVoiceBroadcastRecording-test.ts.snap index fdc7985c88b2..dd5aa15305ce 100644 --- a/test/voice-broadcast/utils/__snapshots__/startNewVoiceBroadcastRecording-test.ts.snap +++ b/test/voice-broadcast/utils/__snapshots__/startNewVoiceBroadcastRecording-test.ts.snap @@ -91,3 +91,26 @@ exports[`startNewVoiceBroadcastRecording when the current user is not allowed to ], } `; + +exports[`startNewVoiceBroadcastRecording when trying to start a broadcast if there is no connection should show an info dialog and not start a recording 1`] = ` +[MockFunction] { + "calls": [ + [ + [Function], + { + "description":

+ Unfortunately we're unable to start a recording right now. Please try again later. +

, + "hasCloseButton": true, + "title": "Connection error", + }, + ], + ], + "results": [ + { + "type": "return", + "value": undefined, + }, + ], +} +`; diff --git a/test/voice-broadcast/utils/startNewVoiceBroadcastRecording-test.ts b/test/voice-broadcast/utils/startNewVoiceBroadcastRecording-test.ts index afab8278df9b..5f2447d85836 100644 --- a/test/voice-broadcast/utils/startNewVoiceBroadcastRecording-test.ts +++ b/test/voice-broadcast/utils/startNewVoiceBroadcastRecording-test.ts @@ -16,6 +16,7 @@ limitations under the License. import { mocked } from "jest-mock"; import { EventType, ISendEventResponse, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; +import { SyncState } from "matrix-js-sdk/src/sync"; import Modal from "../../../src/Modal"; import { @@ -103,6 +104,18 @@ describe("startNewVoiceBroadcastRecording", () => { jest.clearAllMocks(); }); + describe("when trying to start a broadcast if there is no connection", () => { + beforeEach(async () => { + mocked(client.getSyncState).mockReturnValue(SyncState.Error); + result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore); + }); + + it("should show an info dialog and not start a recording", () => { + expect(result).toBeNull(); + expect(Modal.createDialog).toMatchSnapshot(); + }); + }); + describe("when the current user is allowed to send voice broadcast info state events", () => { beforeEach(() => { mocked(room.currentState.maySendStateEvent).mockReturnValue(true);