Consider own broadcasts from other device as a playback (#9821)

This commit is contained in:
Michael Weimann 2022-12-23 17:41:18 +01:00 committed by GitHub
parent 32140855fb
commit 6c40e2476a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 29 deletions

View file

@ -16,7 +16,7 @@ limitations under the License.
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { VoiceBroadcastInfoState } from ".."; import { VoiceBroadcastInfoEventContent, VoiceBroadcastInfoState } from "..";
export const shouldDisplayAsVoiceBroadcastRecordingTile = ( export const shouldDisplayAsVoiceBroadcastRecordingTile = (
state: VoiceBroadcastInfoState, state: VoiceBroadcastInfoState,
@ -24,5 +24,10 @@ export const shouldDisplayAsVoiceBroadcastRecordingTile = (
event: MatrixEvent, event: MatrixEvent,
): boolean => { ): boolean => {
const userId = client.getUserId(); 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
);
}; };

View file

@ -17,26 +17,30 @@ limitations under the License.
import { mocked } from "jest-mock"; import { mocked } from "jest-mock";
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { import { shouldDisplayAsVoiceBroadcastRecordingTile, VoiceBroadcastInfoState } from "../../../src/voice-broadcast";
shouldDisplayAsVoiceBroadcastRecordingTile, import { createTestClient } from "../../test-utils";
VoiceBroadcastInfoEventType, import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
VoiceBroadcastInfoState,
} from "../../../src/voice-broadcast";
import { createTestClient, mkEvent } 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", // own MXID
"@user1:example.com", // sender MXID "@user1:example.com", // sender MXID
"ABC123", // own device ID
"ABC123", // sender device ID
VoiceBroadcastInfoState.Started, VoiceBroadcastInfoState.Started,
true, // expected return value true, // expected return value
], ],
["@user1:example.com", "@user1:example.com", VoiceBroadcastInfoState.Paused, true], ["@user1:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Paused, true],
["@user1:example.com", "@user1:example.com", VoiceBroadcastInfoState.Resumed, true], ["@user1:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Resumed, true],
["@user1:example.com", "@user1:example.com", VoiceBroadcastInfoState.Stopped, false], ["@user1:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Stopped, false],
["@user2:example.com", "@user1:example.com", VoiceBroadcastInfoState.Started, false], ["@user2:example.com", "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Started, false],
[null, null, null, false], [null, "@user1:example.com", "ABC123", "ABC123", VoiceBroadcastInfoState.Started, false],
[undefined, undefined, undefined, 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", () => { describe("shouldDisplayAsVoiceBroadcastRecordingTile", () => {
@ -47,18 +51,13 @@ describe("shouldDisplayAsVoiceBroadcastRecordingTile", () => {
client = createTestClient(); client = createTestClient();
}); });
describe.each(testCases)( describe.each<TestTuple>(testCases)(
"when called with user »%s«, sender »%s«, state »%s«", "when called with user »%s«, sender »%s«, device »%s«, sender device »%s« state »%s«",
(userId: string, senderId: string, state: VoiceBroadcastInfoState, expected: boolean) => { (userId, senderId, deviceId, senderDeviceId, state, expected) => {
beforeEach(() => { beforeEach(() => {
event = mkEvent({ event = mkVoiceBroadcastInfoStateEvent("!room:example.com", state, senderId, senderDeviceId);
event: true,
type: VoiceBroadcastInfoEventType,
room: "!room:example.com",
user: senderId,
content: {},
});
mocked(client.getUserId).mockReturnValue(userId); mocked(client.getUserId).mockReturnValue(userId);
mocked(client.getDeviceId).mockReturnValue(deviceId);
}); });
it(`should return ${expected}`, () => { 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);
});
}); });

View file

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { Optional } from "matrix-events-sdk";
import { EventType, MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix"; import { EventType, MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix";
import { import {
@ -24,10 +25,10 @@ import {
import { mkEvent } from "../../test-utils"; import { mkEvent } from "../../test-utils";
export const mkVoiceBroadcastInfoStateEvent = ( export const mkVoiceBroadcastInfoStateEvent = (
roomId: string, roomId: Optional<string>,
state: VoiceBroadcastInfoState, state: Optional<VoiceBroadcastInfoState>,
senderId: string, senderId: Optional<string>,
senderDeviceId: string, senderDeviceId: Optional<string>,
startedInfoEvent?: MatrixEvent, startedInfoEvent?: MatrixEvent,
): MatrixEvent => { ): MatrixEvent => {
const relationContent = {}; const relationContent = {};
@ -41,9 +42,12 @@ export const mkVoiceBroadcastInfoStateEvent = (
return mkEvent({ return mkEvent({
event: true, event: true,
// @ts-ignore allow everything here for edge test cases
room: roomId, room: roomId,
// @ts-ignore allow everything here for edge test cases
user: senderId, user: senderId,
type: VoiceBroadcastInfoEventType, type: VoiceBroadcastInfoEventType,
// @ts-ignore allow everything here for edge test cases
skey: senderId, skey: senderId,
content: { content: {
state, state,