Skip to content

Commit

Permalink
Implement MSC3912: Relation based redactions
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Dec 8, 2022
1 parent 7e2a9eb commit aa6cd9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/@types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ export const UNSTABLE_MSC3089_BRANCH = new UnstableValue("m.branch", "org.matrix
*/
export const UNSTABLE_MSC2716_MARKER = new UnstableValue("m.room.marker", "org.matrix.msc2716.marker");

/**
* Server-side feature to also redact related events by providing `with_relations` in the redaction event content.
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
*/
export const UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS = new UnstableValue(
"org.matrix.msc3912.stable",
"org.matrix.msc3912",
);

/**
* Functional members type for declaring a purpose of room members (e.g. helpful bots).
* Note that this reference is UNSTABLE and subject to breaking changes, including its
Expand Down
4 changes: 3 additions & 1 deletion src/@types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { IRoomEventFilter } from "../filter";
import { Direction } from "../models/event-timeline";
import { PushRuleAction } from "./PushRules";
import { IRoomEvent } from "../sync-accumulator";
import { EventType, RoomType } from "./event";
import { EventType, RelationType, RoomType } from "./event";

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

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

export interface ISendEventResponse {
Expand Down
25 changes: 21 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import {
UNSTABLE_MSC3088_ENABLED,
UNSTABLE_MSC3088_PURPOSE,
UNSTABLE_MSC3089_TREE_SUBTYPE,
UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS,
} from "./@types/event";
import { IdServerUnbindResult, IImageInfo, Preset, Visibility } from "./@types/partials";
import { EventMapper, eventMapperFor, MapperOpts } from "./event-mapper";
Expand Down Expand Up @@ -4355,20 +4356,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns Promise which resolves: TODO
* @returns Rejects: with an error response.
*/
public redactEvent(
public async redactEvent(
roomId: string,
eventId: string,
txnId?: string | undefined,
opts?: IRedactOpts,
): Promise<ISendEventResponse>;
public redactEvent(
public async redactEvent(
roomId: string,
threadId: string | null,
eventId: string,
txnId?: string | undefined,
opts?: IRedactOpts,
): Promise<ISendEventResponse>;
public redactEvent(
public async redactEvent(
roomId: string,
threadId: string | null,
eventId?: string,
Expand All @@ -4382,9 +4383,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
threadId = null;
}
const reason = opts?.reason;

if (opts?.with_relations && !await this.supportsRelationBasedRedactions()) {
throw new Error(
"Server does not support relation based redactions "
+ `roomId ${roomId} eventId ${eventId} txnId: ${txnId} threadId ${threadId}`,
);
}

return this.sendCompleteEvent(roomId, threadId, {
type: EventType.RoomRedaction,
content: { reason },
content: {
...(opts?.with_relations ? { with_relations: opts?.with_relations } : {}),
reason,
},
redacts: eventId,
}, txnId as string);
}
Expand Down Expand Up @@ -9265,6 +9277,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return this.clientOpts?.experimentalThreadSupport || false;
}

public async supportsRelationBasedRedactions(): Promise<boolean> {
return await this.doesServerSupportUnstableFeature(UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS.stable!)
|| await this.doesServerSupportUnstableFeature(UNSTABLE_MSC3912_RELATION_BASED_REDACTIONS.unstable!);
}

/**
* Fetches the summary of a room as defined by an initial version of MSC3266 and implemented in Synapse
* Proposed at https://github.com/matrix-org/matrix-doc/pull/3266
Expand Down

0 comments on commit aa6cd9c

Please sign in to comment.