Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Consider own broadcasts from other device as a playback #9821

Merged
merged 8 commits into from
Dec 23, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ 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,
client: MatrixClient,
event: MatrixEvent,
): boolean => {
const userId = client.getUserId();
return !!userId && userId === event.getSender() && state !== VoiceBroadcastInfoState.Stopped;
return (
!!userId &&
userId === event.getSender() &&
client.getDeviceId() === event.getContent<VoiceBroadcastInfoEventContent>()?.device_id &&
state !== VoiceBroadcastInfoState.Stopped
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -47,23 +51,30 @@ 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<TestTuple>(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}`, () => {
expect(shouldDisplayAsVoiceBroadcastRecordingTile(state, client, event)).toBe(expected);
});
},
);

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);
});
});
12 changes: 8 additions & 4 deletions test/voice-broadcast/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,10 +25,10 @@ import {
import { mkEvent } from "../../test-utils";

export const mkVoiceBroadcastInfoStateEvent = (
roomId: string,
state: VoiceBroadcastInfoState,
senderId: string,
senderDeviceId: string,
roomId: Optional<string>,
state: Optional<VoiceBroadcastInfoState>,
senderId: Optional<string>,
senderDeviceId: Optional<string>,
startedInfoEvent?: MatrixEvent,
): MatrixEvent => {
const relationContent = {};
Expand All @@ -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,
Expand Down