Skip to content

Commit aa6cd9c

Browse files
committed
Implement MSC3912: Relation based redactions
1 parent 7e2a9eb commit aa6cd9c

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/@types/event.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ export const UNSTABLE_MSC3089_BRANCH = new UnstableValue("m.branch", "org.matrix
165165
*/
166166
export const UNSTABLE_MSC2716_MARKER = new UnstableValue("m.room.marker", "org.matrix.msc2716.marker");
167167

168+
/**
169+
* Server-side feature to also redact related events by providing `with_relations` in the redaction event content.
170+
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
171+
*/
172+
export const UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS = new UnstableValue(
173+
"org.matrix.msc3912.stable",
174+
"org.matrix.msc3912",
175+
);
176+
168177
/**
169178
* Functional members type for declaring a purpose of room members (e.g. helpful bots).
170179
* Note that this reference is UNSTABLE and subject to breaking changes, including its

src/@types/requests.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { IRoomEventFilter } from "../filter";
2121
import { Direction } from "../models/event-timeline";
2222
import { PushRuleAction } from "./PushRules";
2323
import { IRoomEvent } from "../sync-accumulator";
24-
import { EventType, RoomType } from "./event";
24+
import { EventType, RelationType, RoomType } from "./event";
2525

2626
// allow camelcase as these are things that go onto the wire
2727
/* eslint-disable camelcase */
@@ -47,6 +47,8 @@ export interface IJoinRoomOpts {
4747

4848
export interface IRedactOpts {
4949
reason?: string;
50+
// also delete related events, if the server supports it (MSC3912)
51+
with_relations?: Array<RelationType | string>;
5052
}
5153

5254
export interface ISendEventResponse {

src/client.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ import {
154154
UNSTABLE_MSC3088_ENABLED,
155155
UNSTABLE_MSC3088_PURPOSE,
156156
UNSTABLE_MSC3089_TREE_SUBTYPE,
157+
UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS,
157158
} from "./@types/event";
158159
import { IdServerUnbindResult, IImageInfo, Preset, Visibility } from "./@types/partials";
159160
import { EventMapper, eventMapperFor, MapperOpts } from "./event-mapper";
@@ -4355,20 +4356,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
43554356
* @returns Promise which resolves: TODO
43564357
* @returns Rejects: with an error response.
43574358
*/
4358-
public redactEvent(
4359+
public async redactEvent(
43594360
roomId: string,
43604361
eventId: string,
43614362
txnId?: string | undefined,
43624363
opts?: IRedactOpts,
43634364
): Promise<ISendEventResponse>;
4364-
public redactEvent(
4365+
public async redactEvent(
43654366
roomId: string,
43664367
threadId: string | null,
43674368
eventId: string,
43684369
txnId?: string | undefined,
43694370
opts?: IRedactOpts,
43704371
): Promise<ISendEventResponse>;
4371-
public redactEvent(
4372+
public async redactEvent(
43724373
roomId: string,
43734374
threadId: string | null,
43744375
eventId?: string,
@@ -4382,9 +4383,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
43824383
threadId = null;
43834384
}
43844385
const reason = opts?.reason;
4386+
4387+
if (opts?.with_relations && !await this.supportsRelationBasedRedactions()) {
4388+
throw new Error(
4389+
"Server does not support relation based redactions "
4390+
+ `roomId ${roomId} eventId ${eventId} txnId: ${txnId} threadId ${threadId}`,
4391+
);
4392+
}
4393+
43854394
return this.sendCompleteEvent(roomId, threadId, {
43864395
type: EventType.RoomRedaction,
4387-
content: { reason },
4396+
content: {
4397+
...(opts?.with_relations ? { with_relations: opts?.with_relations } : {}),
4398+
reason,
4399+
},
43884400
redacts: eventId,
43894401
}, txnId as string);
43904402
}
@@ -9265,6 +9277,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
92659277
return this.clientOpts?.experimentalThreadSupport || false;
92669278
}
92679279

9280+
public async supportsRelationBasedRedactions(): Promise<boolean> {
9281+
return await this.doesServerSupportUnstableFeature(UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS.stable!)
9282+
|| await this.doesServerSupportUnstableFeature(UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS.unstable!);
9283+
}
9284+
92689285
/**
92699286
* Fetches the summary of a room as defined by an initial version of MSC3266 and implemented in Synapse
92709287
* Proposed at https://github.com/matrix-org/matrix-doc/pull/3266

0 commit comments

Comments
 (0)