Fix unwanted ringing of other devices even though the user is already connected to the call. (#12742)

* Fix call ringing on other device when already joined.
This is done by checking if a user is already connected to the call on
another device before playing the ring sound.

* Add test
This commit is contained in:
Timo 2024-07-25 11:34:19 +02:00 committed by GitHub
parent 72e7df0f13
commit 2e0716cc59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 1 deletions

View file

@ -32,6 +32,8 @@ import {
} from "matrix-js-sdk/src/matrix"; } from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { PermissionChanged as PermissionChangedEvent } from "@matrix-org/analytics-events/types/typescript/PermissionChanged"; import { PermissionChanged as PermissionChangedEvent } from "@matrix-org/analytics-events/types/typescript/PermissionChanged";
// eslint-disable-next-line no-restricted-imports
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
import { MatrixClientPeg } from "./MatrixClientPeg"; import { MatrixClientPeg } from "./MatrixClientPeg";
import { PosthogAnalytics } from "./PosthogAnalytics"; import { PosthogAnalytics } from "./PosthogAnalytics";
@ -505,10 +507,16 @@ class NotifierClass {
* Some events require special handling such as showing in-app toasts * Some events require special handling such as showing in-app toasts
*/ */
private performCustomEventHandling(ev: MatrixEvent): void { private performCustomEventHandling(ev: MatrixEvent): void {
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(ev.getRoomId());
const thisUserHasConnectedDevice =
room && MatrixRTCSession.callMembershipsForRoom(room).some((m) => m.sender === cli.getUserId());
if ( if (
EventType.CallNotify === ev.getType() && EventType.CallNotify === ev.getType() &&
SettingsStore.getValue("feature_group_calls") && SettingsStore.getValue("feature_group_calls") &&
(ev.getAge() ?? 0) < 10000 (ev.getAge() ?? 0) < 10000 &&
!thisUserHasConnectedDevice
) { ) {
const content = ev.getContent(); const content = ev.getContent();
const roomId = ev.getRoomId(); const roomId = ev.getRoomId();

View file

@ -26,6 +26,10 @@ import {
SyncState, SyncState,
} from "matrix-js-sdk/src/matrix"; } from "matrix-js-sdk/src/matrix";
import { waitFor } from "@testing-library/react"; import { waitFor } from "@testing-library/react";
// eslint-disable-next-line no-restricted-imports
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
// eslint-disable-next-line no-restricted-imports
import { CallMembership } from "matrix-js-sdk/src/matrixrtc/CallMembership";
import BasePlatform from "../src/BasePlatform"; import BasePlatform from "../src/BasePlatform";
import Notifier from "../src/Notifier"; import Notifier from "../src/Notifier";
@ -139,6 +143,11 @@ describe("Notifier", () => {
getRoom: jest.fn(), getRoom: jest.fn(),
getPushActionsForEvent: jest.fn(), getPushActionsForEvent: jest.fn(),
supportsThreads: jest.fn().mockReturnValue(false), supportsThreads: jest.fn().mockReturnValue(false),
matrixRTC: {
on: jest.fn(),
off: jest.fn(),
getRoomSession: jest.fn(),
},
}); });
mockClient.pushRules = { mockClient.pushRules = {
@ -455,6 +464,35 @@ describe("Notifier", () => {
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled(); expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
}); });
it("should not show toast when group call is already connected", () => {
setGroupCallsEnabled(true);
const spyCallMemberships = jest.spyOn(MatrixRTCSession, "callMembershipsForRoom").mockReturnValue([
new CallMembership(
mkEvent({
event: true,
room: testRoom.roomId,
user: userId,
type: EventType.GroupCallMemberPrefix,
content: {},
}),
{
call_id: "123",
application: "m.call",
focus_active: { type: "livekit" },
foci_preferred: [],
device_id: "DEVICE",
},
),
]);
const roomSession = MatrixRTCSession.roomSessionForRoom(mockClient, testRoom);
mockClient.matrixRTC.getRoomSession.mockReturnValue(roomSession);
emitCallNotifyEvent();
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
spyCallMemberships.mockRestore();
});
it("should not show toast when calling with non-group call event", () => { it("should not show toast when calling with non-group call event", () => {
setGroupCallsEnabled(true); setGroupCallsEnabled(true);