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

Commit

Permalink
When deleting a voice broadcast, also delete related events
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Dec 12, 2022
1 parent 57e1822 commit 34b8a7b
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/components/views/dialogs/ConfirmRedactDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { Feature, ServerSupport } from 'matrix-js-sdk/src/feature';
import { MatrixEvent, RelationType } from 'matrix-js-sdk/src/matrix';
import React from 'react';

import { _t } from '../../../languageHandler';
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import Modal from '../../../Modal';
import { isVoiceBroadcastStartedEvent } from '../../../voice-broadcast';
import ErrorDialog from './ErrorDialog';
import TextInputDialog from "./TextInputDialog";

Expand Down Expand Up @@ -58,13 +60,29 @@ export function createRedactEventDialog({
if (!proceed) return;

const cli = MatrixClientPeg.get();
const withRelations: { with_relations?: RelationType[] } = {};

// redact related events if this is a voice broadcast started event and
// server has support for relation based redactions
if (isVoiceBroadcastStartedEvent(mxEvent)) {
const relationBasedRedactionsSupport = cli.canSupport.get(Feature.RelationBasedRedactions);
if (relationBasedRedactionsSupport && relationBasedRedactionsSupport !== ServerSupport.Unsupported) {
withRelations.with_relations = [RelationType.Reference];
}
}

try {
onCloseDialog?.();
await cli.redactEvent(
mxEvent.getRoomId(),
mxEvent.getId(),
undefined,
reason ? { reason } : {},
{
...(
reason ? { reason } : {}
),
...withRelations,
},
);
} catch (e) {
const code = e.errcode || e.statusCode;
Expand Down
1 change: 1 addition & 0 deletions src/voice-broadcast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from "./utils/doMaybeSetCurrentVoiceBroadcastPlayback";
export * from "./utils/getChunkLength";
export * from "./utils/getMaxBroadcastLength";
export * from "./utils/hasRoomLiveVoiceBroadcast";
export * from "./utils/isVoiceBroadcastStartedEvent";
export * from "./utils/findRoomLiveVoiceBroadcastFromUserAndDevice";
export * from "./utils/shouldDisplayAsVoiceBroadcastRecordingTile";
export * from "./utils/shouldDisplayAsVoiceBroadcastTile";
Expand Down
24 changes: 24 additions & 0 deletions src/voice-broadcast/utils/isVoiceBroadcastStartedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixEvent } from "matrix-js-sdk/src/matrix";

import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "..";

export const isVoiceBroadcastStartedEvent = (event: MatrixEvent): boolean => {
return event.getType() === VoiceBroadcastInfoEventType
&& event.getContent()?.state === VoiceBroadcastInfoState.Started;
};
98 changes: 98 additions & 0 deletions test/components/views/dialogs/ConfirmRedactDialog-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Feature, ServerSupport } from "matrix-js-sdk/src/feature";
import { MatrixClient, MatrixEvent, RelationType } from "matrix-js-sdk/src/matrix";
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

import { flushPromises, stubClient } from "../../../test-utils";
import { mkVoiceBroadcastInfoStateEvent } from "../../../voice-broadcast/utils/test-utils";
import { VoiceBroadcastInfoState } from "../../../../src/voice-broadcast";
import { createRedactEventDialog } from "../../../../src/components/views/dialogs/ConfirmRedactDialog";

describe("ConfirmRedactDialog", () => {
const roomId = "!room:example.com";
let client: MatrixClient;
let mxEvent: MatrixEvent;

const setUpVoiceBroadcastStartedEvent = () => {
mxEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Started,
client.getUserId()!,
client.deviceId!,
);
};

const confirmDeleteVoiceBroadcastStartedEvent = async () => {
setUpVoiceBroadcastStartedEvent();
createRedactEventDialog({ mxEvent });
// double-flush promises required for the dialog to show up
await flushPromises();
await flushPromises();

await userEvent.click(screen.getByTestId("dialog-primary-button"));
};

beforeEach(() => {
client = stubClient();
});

describe("when the server does not support relation based redactions", () => {
beforeEach(() => {
client.canSupport.set(Feature.RelationBasedRedactions, ServerSupport.Unsupported);
});

describe("and displaying and confirm the dialog for a voice broadcast", () => {
beforeEach(async () => {
await confirmDeleteVoiceBroadcastStartedEvent();
});

it("should call redact without `with_relations`", () => {
expect(client.redactEvent).toHaveBeenCalledWith(
roomId,
mxEvent.getId(),
undefined,
{},
);
});
});
});

describe("when the server supports relation based redactions", () => {
beforeEach(() => {
client.canSupport.set(Feature.RelationBasedRedactions, ServerSupport.Unstable);
});

describe("and displaying and confirm the dialog for a voice broadcast", () => {
beforeEach(async () => {
await confirmDeleteVoiceBroadcastStartedEvent();
});

it("should call redact with `with_relations`", () => {
expect(client.redactEvent).toHaveBeenCalledWith(
roomId,
mxEvent.getId(),
undefined,
{
with_relations: [RelationType.Reference],
},
);
});
});
});
});
1 change: 1 addition & 0 deletions test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export function createTestClient(): MatrixClient {
requestPasswordEmailToken: jest.fn().mockRejectedValue({}),
setPassword: jest.fn().mockRejectedValue({}),
groupCallEventHandler: { groupCalls: new Map<string, GroupCall>() },
redactEvent: jest.fn(),
} as unknown as MatrixClient;

client.reEmitter = new ReEmitter(client);
Expand Down

0 comments on commit 34b8a7b

Please sign in to comment.