From 6c40e2476a1246899036007f482f9564ad1adb74 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Fri, 23 Dec 2022 17:41:18 +0100 Subject: [PATCH] Consider own broadcasts from other device as a playback (#9821) --- ...uldDisplayAsVoiceBroadcastRecordingTile.ts | 9 ++- ...splayAsVoiceBroadcastRecordingTile-test.ts | 57 +++++++++++-------- test/voice-broadcast/utils/test-utils.ts | 12 ++-- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile.ts b/src/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile.ts index c6891f3b77..6beecd11ce 100644 --- a/src/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile.ts +++ b/src/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile.ts @@ -16,7 +16,7 @@ limitations under the License. import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; -import { VoiceBroadcastInfoState } from ".."; +import { VoiceBroadcastInfoEventContent, VoiceBroadcastInfoState } from ".."; export const shouldDisplayAsVoiceBroadcastRecordingTile = ( state: VoiceBroadcastInfoState, @@ -24,5 +24,10 @@ export const shouldDisplayAsVoiceBroadcastRecordingTile = ( event: MatrixEvent, ): boolean => { const userId = client.getUserId(); - return !!userId && userId === event.getSender() && state !== VoiceBroadcastInfoState.Stopped; + return ( + !!userId && + userId === event.getSender() && + client.getDeviceId() === event.getContent()?.device_id && + state !== VoiceBroadcastInfoState.Stopped + ); }; diff --git a/test/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile-test.ts b/test/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile-test.ts index 6c99c6b7c0..89883c8ae8 100644 --- a/test/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile-test.ts +++ b/test/voice-broadcast/utils/shouldDisplayAsVoiceBroadcastRecordingTile-test.ts @@ -17,26 +17,30 @@ limitations under the License. import { mocked } from "jest-mock"; import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; -import { - shouldDisplayAsVoiceBroadcastRecordingTile, - VoiceBroadcastInfoEventType, - VoiceBroadcastInfoState, -} from "../../../src/voice-broadcast"; -import { createTestClient, mkEvent } from "../../test-utils"; +import { shouldDisplayAsVoiceBroadcastRecordingTile, VoiceBroadcastInfoState } from "../../../src/voice-broadcast"; +import { createTestClient } from "../../test-utils"; +import { mkVoiceBroadcastInfoStateEvent } from "./test-utils"; -const testCases = [ +type TestTuple = [string | null, string, string, string, VoiceBroadcastInfoState, boolean]; + +const testCases: TestTuple[] = [ [ "@user1:example.com", // own MXID "@user1:example.com", // sender MXID + "ABC123", // own device ID + "ABC123", // sender device ID VoiceBroadcastInfoState.Started, true, // expected return value ], - ["@user1:example.com", "@user1:example.com", VoiceBroadcastInfoState.Paused, true], - ["@user1:example.com", "@user1:example.com", VoiceBroadcastInfoState.Resumed, true], - ["@user1:example.com", "@user1:example.com", VoiceBroadcastInfoState.Stopped, false], - ["@user2:example.com", "@user1:example.com", VoiceBroadcastInfoState.Started, false], - [null, null, null, false], - [undefined, undefined, undefined, false], + ["@user1:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Paused, true], + ["@user1:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Resumed, true], + ["@user1:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Stopped, false], + ["@user2:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Started, false], + [null, "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Started, false], + // other device + ["@user1:example.com", "@user1:example.com", "ABC123", "JKL123", VoiceBroadcastInfoState.Started, false], + ["@user1:example.com", "@user1:example.com", "ABC123", "JKL123", VoiceBroadcastInfoState.Paused, false], + ["@user1:example.com", "@user1:example.com", "ABC123", "JKL123", VoiceBroadcastInfoState.Resumed, false], ]; describe("shouldDisplayAsVoiceBroadcastRecordingTile", () => { @@ -47,18 +51,13 @@ describe("shouldDisplayAsVoiceBroadcastRecordingTile", () => { client = createTestClient(); }); - describe.each(testCases)( - "when called with user »%s«, sender »%s«, state »%s«", - (userId: string, senderId: string, state: VoiceBroadcastInfoState, expected: boolean) => { + describe.each(testCases)( + "when called with user »%s«, sender »%s«, device »%s«, sender device »%s« state »%s«", + (userId, senderId, deviceId, senderDeviceId, state, expected) => { beforeEach(() => { - event = mkEvent({ - event: true, - type: VoiceBroadcastInfoEventType, - room: "!room:example.com", - user: senderId, - content: {}, - }); + event = mkVoiceBroadcastInfoStateEvent("!room:example.com", state, senderId, senderDeviceId); mocked(client.getUserId).mockReturnValue(userId); + mocked(client.getDeviceId).mockReturnValue(deviceId); }); it(`should return ${expected}`, () => { @@ -66,4 +65,16 @@ describe("shouldDisplayAsVoiceBroadcastRecordingTile", () => { }); }, ); + + it("should return false, when all params are null", () => { + event = mkVoiceBroadcastInfoStateEvent("!room:example.com", null, null, null); + // @ts-ignore Simulate null state received for any reason. + expect(shouldDisplayAsVoiceBroadcastRecordingTile(null, client, event)).toBe(false); + }); + + it("should return false, when all params are undefined", () => { + event = mkVoiceBroadcastInfoStateEvent("!room:example.com", undefined, undefined, undefined); + // @ts-ignore Simulate undefined state received for any reason. + expect(shouldDisplayAsVoiceBroadcastRecordingTile(undefined, client, event)).toBe(false); + }); }); diff --git a/test/voice-broadcast/utils/test-utils.ts b/test/voice-broadcast/utils/test-utils.ts index 152a5fc7f0..cacb1a9165 100644 --- a/test/voice-broadcast/utils/test-utils.ts +++ b/test/voice-broadcast/utils/test-utils.ts @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { Optional } from "matrix-events-sdk"; import { EventType, MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix"; import { @@ -24,10 +25,10 @@ import { import { mkEvent } from "../../test-utils"; export const mkVoiceBroadcastInfoStateEvent = ( - roomId: string, - state: VoiceBroadcastInfoState, - senderId: string, - senderDeviceId: string, + roomId: Optional, + state: Optional, + senderId: Optional, + senderDeviceId: Optional, startedInfoEvent?: MatrixEvent, ): MatrixEvent => { const relationContent = {}; @@ -41,9 +42,12 @@ export const mkVoiceBroadcastInfoStateEvent = ( return mkEvent({ event: true, + // @ts-ignore allow everything here for edge test cases room: roomId, + // @ts-ignore allow everything here for edge test cases user: senderId, type: VoiceBroadcastInfoEventType, + // @ts-ignore allow everything here for edge test cases skey: senderId, content: { state,