From cb4e444a60686d2dcd00eabb5c8743f1bc7b5f25 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 3 Jan 2023 10:02:28 +0100 Subject: [PATCH] Mute broadcast chunk notifications --- src/Notifier.ts | 8 ++++++++ test/Notifier-test.ts | 41 +++++++++++++++++++++++++++++++++++++++ test/test-utils/client.ts | 1 + 3 files changed, 50 insertions(+) diff --git a/src/Notifier.ts b/src/Notifier.ts index dd624e2dc7..7e1f9eb0a4 100644 --- a/src/Notifier.ts +++ b/src/Notifier.ts @@ -50,6 +50,7 @@ import { localNotificationsAreSilenced, createLocalNotificationSettingsIfNeeded import { getIncomingCallToastKey, IncomingCallToast } from "./toasts/IncomingCallToast"; import ToastStore from "./stores/ToastStore"; import { ElementCall } from "./models/Call"; +import { VoiceBroadcastChunkEventType } from "./voice-broadcast"; /* * Dispatches: @@ -77,6 +78,13 @@ const msgTypeHandlers = { [M_LOCATION.altName]: (event: MatrixEvent) => { return TextForEvent.textForLocationEvent(event)(); }, + [MsgType.Audio]: (event: MatrixEvent): string | null => { + if (event.getContent()?.[VoiceBroadcastChunkEventType]) { + // mute broadcast chunks + return null; + } + return TextForEvent.textForEvent(event); + }, }; export const Notifier = { diff --git a/test/Notifier-test.ts b/test/Notifier-test.ts index bfa1e1d1f3..36021e6f6f 100644 --- a/test/Notifier-test.ts +++ b/test/Notifier-test.ts @@ -20,6 +20,7 @@ import { Room, RoomEvent } from "matrix-js-sdk/src/models/room"; import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event"; import { SyncState } from "matrix-js-sdk/src/sync"; import { waitFor } from "@testing-library/react"; +import { EventType, MsgType } from "matrix-js-sdk/src/matrix"; import BasePlatform from "../src/BasePlatform"; import { ElementCall } from "../src/models/Call"; @@ -39,6 +40,7 @@ import { mkThread } from "./test-utils/threads"; import dis from "../src/dispatcher/dispatcher"; import { ThreadPayload } from "../src/dispatcher/payloads/ThreadPayload"; import { Action } from "../src/dispatcher/actions"; +import { VoiceBroadcastChunkEventType } from "../src/voice-broadcast"; jest.mock("../src/utils/notifications", () => ({ // @ts-ignore @@ -73,6 +75,22 @@ describe("Notifier", () => { }); }; + const mkAudioEvent = (broadcastChunk = false): MatrixEvent => { + const chunkContent = broadcastChunk ? { [VoiceBroadcastChunkEventType]: {} } : {}; + + return mkEvent({ + event: true, + type: EventType.RoomMessage, + user: "@user:example.com", + room: "!room:example.com", + content: { + ...chunkContent, + msgtype: MsgType.Audio, + body: "test audio message", + }, + }); + }; + beforeEach(() => { accountDataStore = {}; mockClient = getMockClientWithEventEmitter({ @@ -256,6 +274,24 @@ describe("Notifier", () => { Notifier._displayPopupNotification(testEvent, testRoom); expect(MockPlatform.displayNotification).toHaveBeenCalledTimes(count); }); + + it("should display a notification for a voice message", () => { + const audioEvent = mkAudioEvent(); + Notifier._displayPopupNotification(audioEvent, testRoom); + expect(MockPlatform.displayNotification).toHaveBeenCalledWith( + "@user:example.com (!room1:server)", + "@user:example.com: test audio message", + "data:image/png;base64,00", + testRoom, + audioEvent, + ); + }); + + it("should not display a notification for a broadcast chunk", () => { + const audioEvent = mkAudioEvent(true); + Notifier._displayPopupNotification(audioEvent, testRoom); + expect(MockPlatform.displayNotification).not.toHaveBeenCalled(); + }); }); describe("getSoundForRoom", () => { @@ -456,6 +492,11 @@ describe("Notifier", () => { Notifier._evaluateEvent(events[1]); expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(1); }); + + it("should show a pop-up for an audio message", () => { + Notifier._evaluateEvent(mkAudioEvent()); + expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(1); + }); }); describe("setPromptHidden", () => { diff --git a/test/test-utils/client.ts b/test/test-utils/client.ts index 47e037b8fd..47040dcb4b 100644 --- a/test/test-utils/client.ts +++ b/test/test-utils/client.ts @@ -92,6 +92,7 @@ export const unmockClientPeg = () => jest.spyOn(MatrixClientPeg, "get").mockRest */ export const mockClientMethodsUser = (userId = "@alice:domain") => ({ getUserId: jest.fn().mockReturnValue(userId), + getSafeUserId: jest.fn().mockReturnValue(userId), getUser: jest.fn().mockReturnValue(new User(userId)), isGuest: jest.fn().mockReturnValue(false), mxcUrlToHttp: jest.fn().mockReturnValue("mock-mxcUrlToHttp"),