Skip to content

Commit 2d7fdde

Browse files
authored
Update MSC3912 implementation to use with_rel_type instead of with_relations (#3420)
* Migrate MSC3912 * Fix doc blocks * Remove with_relations fallback * Implement PR feedback * Fix typo
1 parent cf34e90 commit 2d7fdde

File tree

4 files changed

+52
-58
lines changed

4 files changed

+52
-58
lines changed

spec/unit/matrix-client.spec.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
UNSTABLE_MSC3088_ENABLED,
2929
UNSTABLE_MSC3088_PURPOSE,
3030
UNSTABLE_MSC3089_TREE_SUBTYPE,
31-
MSC3912_RELATION_BASED_REDACTIONS_PROP,
3231
} from "../../src/@types/event";
3332
import { MEGOLM_ALGORITHM } from "../../src/crypto/olmlib";
3433
import { Crypto } from "../../src/crypto";
@@ -181,9 +180,7 @@ describe("MatrixClient", function () {
181180
data: SYNC_DATA,
182181
};
183182

184-
const unstableFeatures: Record<string, boolean> = {
185-
"org.matrix.msc3440.stable": true,
186-
};
183+
let unstableFeatures: Record<string, boolean> = {};
187184

188185
// items are popped off when processed and block if no items left.
189186
let httpLookups: HttpLookup[] = [];
@@ -342,6 +339,12 @@ describe("MatrixClient", function () {
342339
store.getClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
343340
store.storeClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
344341
store.isNewlyCreated = jest.fn().mockReturnValue(Promise.resolve(true));
342+
343+
// set unstableFeatures to a defined state before each test
344+
unstableFeatures = {
345+
"org.matrix.msc3440.stable": true,
346+
};
347+
345348
makeClient();
346349

347350
// set reasonable working defaults
@@ -1373,18 +1376,18 @@ describe("MatrixClient", function () {
13731376
await client.redactEvent(roomId, eventId, txnId, { reason });
13741377
});
13751378

1376-
describe("when calling with with_relations", () => {
1379+
describe("when calling with 'with_rel_types'", () => {
13771380
const eventId = "$event42:example.org";
13781381

1379-
it("should raise an error if server has no support for relation based redactions", async () => {
1382+
it("should raise an error if the server has no support for relation based redactions", async () => {
13801383
// load supported features
13811384
await client.getVersions();
13821385

13831386
const txnId = client.makeTxnId();
13841387

13851388
expect(() => {
13861389
client.redactEvent(roomId, eventId, txnId, {
1387-
with_relations: [RelationType.Reference],
1390+
with_rel_types: [RelationType.Reference],
13881391
});
13891392
}).toThrow(
13901393
new Error(
@@ -1394,34 +1397,30 @@ describe("MatrixClient", function () {
13941397
);
13951398
});
13961399

1397-
describe("and the server supports relation based redactions (unstable)", () => {
1398-
beforeEach(async () => {
1399-
unstableFeatures["org.matrix.msc3912"] = true;
1400-
// load supported features
1401-
await client.getVersions();
1402-
});
1400+
it("and the server has unstable support for relation based redactions, it should send 'org.matrix.msc3912.with_relations' in the request body", async () => {
1401+
unstableFeatures["org.matrix.msc3912"] = true;
1402+
// load supported features
1403+
await client.getVersions();
14031404

1404-
it("should send with_relations in the request body", async () => {
1405-
const txnId = client.makeTxnId();
1405+
const txnId = client.makeTxnId();
14061406

1407-
httpLookups = [
1408-
{
1409-
method: "PUT",
1410-
path:
1411-
`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}` +
1412-
`/${encodeURIComponent(txnId)}`,
1413-
expectBody: {
1414-
reason: "redaction test",
1415-
[MSC3912_RELATION_BASED_REDACTIONS_PROP.unstable!]: [RelationType.Reference],
1416-
},
1417-
data: { event_id: eventId },
1407+
httpLookups = [
1408+
{
1409+
method: "PUT",
1410+
path:
1411+
`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}` +
1412+
`/${encodeURIComponent(txnId)}`,
1413+
expectBody: {
1414+
reason: "redaction test",
1415+
["org.matrix.msc3912.with_relations"]: ["m.reference"],
14181416
},
1419-
];
1417+
data: { event_id: eventId },
1418+
},
1419+
];
14201420

1421-
await client.redactEvent(roomId, eventId, txnId, {
1422-
reason: "redaction test",
1423-
with_relations: [RelationType.Reference],
1424-
});
1421+
await client.redactEvent(roomId, eventId, txnId, {
1422+
reason: "redaction test",
1423+
with_rel_types: [RelationType.Reference],
14251424
});
14261425
});
14271426
});

src/@types/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ export const UNSTABLE_MSC3089_BRANCH = new UnstableValue("m.branch", "org.matrix
168168
export const UNSTABLE_MSC2716_MARKER = new UnstableValue("m.room.marker", "org.matrix.msc2716.marker");
169169

170170
/**
171-
* Name of the "with_relations" request property for relation based redactions.
171+
* Name of the request property for relation based redactions.
172172
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
173173
*/
174174
export const MSC3912_RELATION_BASED_REDACTIONS_PROP = new UnstableValue(
175-
"with_relations",
175+
"with_rel_types",
176176
"org.matrix.msc3912.with_relations",
177177
);
178178

src/@types/requests.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ export interface IJoinRoomOpts {
4848
export interface IRedactOpts {
4949
reason?: string;
5050
/**
51-
* Whether events related to the redacted event should be redacted.
52-
*
5351
* If specified, then any events which relate to the event being redacted with
5452
* any of the relationship types listed will also be redacted.
53+
* Provide a "*" list item to tell the server to redact relations of any type.
5554
*
5655
* <b>Raises an Error if the server does not support it.</b>
5756
* Check for server-side support before using this param with
5857
* <code>client.canSupport.get(Feature.RelationBasedRedactions)</code>.
5958
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
6059
*/
61-
with_relations?: Array<RelationType | string>;
60+
with_rel_types?: Array<RelationType | "*">;
6261
}
6362

6463
export interface ISendEventResponse {

src/client.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,10 +4593,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
45934593

45944594
/**
45954595
* @param txnId - transaction id. One will be made up if not supplied.
4596-
* @param opts - Options to pass on, may contain `reason` and `with_relations` (MSC3912)
4596+
* @param opts - Redact options
45974597
* @returns Promise which resolves: TODO
45984598
* @returns Rejects: with an error response.
4599-
* @throws Error if called with `with_relations` (MSC3912) but the server does not support it.
4599+
* @throws Error if called with `with_rel_types` (MSC3912) but the server does not support it.
46004600
* Callers should check whether the server supports MSC3912 via `MatrixClient.canSupport`.
46014601
*/
46024602
public redactEvent(
@@ -4626,34 +4626,30 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
46264626
threadId = null;
46274627
}
46284628
const reason = opts?.reason;
4629+
const content: IContent = { reason };
46294630

4630-
if (
4631-
opts?.with_relations &&
4632-
this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Unsupported
4633-
) {
4634-
throw new Error(
4635-
"Server does not support relation based redactions " +
4636-
`roomId ${roomId} eventId ${eventId} txnId: ${txnId} threadId ${threadId}`,
4637-
);
4638-
}
4631+
if (opts?.with_rel_types !== undefined) {
4632+
if (this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Unsupported) {
4633+
throw new Error(
4634+
"Server does not support relation based redactions " +
4635+
`roomId ${roomId} eventId ${eventId} txnId: ${txnId} threadId ${threadId}`,
4636+
);
4637+
}
46394638

4640-
const withRelations = opts?.with_relations
4641-
? {
4642-
[this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Stable
4643-
? MSC3912_RELATION_BASED_REDACTIONS_PROP.stable!
4644-
: MSC3912_RELATION_BASED_REDACTIONS_PROP.unstable!]: opts?.with_relations,
4645-
}
4646-
: {};
4639+
const withRelTypesPropName =
4640+
this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Stable
4641+
? MSC3912_RELATION_BASED_REDACTIONS_PROP.stable!
4642+
: MSC3912_RELATION_BASED_REDACTIONS_PROP.unstable!;
4643+
4644+
content[withRelTypesPropName] = opts.with_rel_types;
4645+
}
46474646

46484647
return this.sendCompleteEvent(
46494648
roomId,
46504649
threadId,
46514650
{
46524651
type: EventType.RoomRedaction,
4653-
content: {
4654-
...withRelations,
4655-
reason,
4656-
},
4652+
content,
46574653
redacts: eventId,
46584654
},
46594655
txnId as string,

0 commit comments

Comments
 (0)