You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-30 04:23:07 +03:00
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
This commit is contained in:
@ -28,7 +28,6 @@ import {
|
|||||||
UNSTABLE_MSC3088_ENABLED,
|
UNSTABLE_MSC3088_ENABLED,
|
||||||
UNSTABLE_MSC3088_PURPOSE,
|
UNSTABLE_MSC3088_PURPOSE,
|
||||||
UNSTABLE_MSC3089_TREE_SUBTYPE,
|
UNSTABLE_MSC3089_TREE_SUBTYPE,
|
||||||
MSC3912_RELATION_BASED_REDACTIONS_PROP,
|
|
||||||
} from "../../src/@types/event";
|
} from "../../src/@types/event";
|
||||||
import { MEGOLM_ALGORITHM } from "../../src/crypto/olmlib";
|
import { MEGOLM_ALGORITHM } from "../../src/crypto/olmlib";
|
||||||
import { Crypto } from "../../src/crypto";
|
import { Crypto } from "../../src/crypto";
|
||||||
@ -181,9 +180,7 @@ describe("MatrixClient", function () {
|
|||||||
data: SYNC_DATA,
|
data: SYNC_DATA,
|
||||||
};
|
};
|
||||||
|
|
||||||
const unstableFeatures: Record<string, boolean> = {
|
let unstableFeatures: Record<string, boolean> = {};
|
||||||
"org.matrix.msc3440.stable": true,
|
|
||||||
};
|
|
||||||
|
|
||||||
// items are popped off when processed and block if no items left.
|
// items are popped off when processed and block if no items left.
|
||||||
let httpLookups: HttpLookup[] = [];
|
let httpLookups: HttpLookup[] = [];
|
||||||
@ -342,6 +339,12 @@ describe("MatrixClient", function () {
|
|||||||
store.getClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
|
store.getClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||||
store.storeClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
|
store.storeClientOptions = jest.fn().mockReturnValue(Promise.resolve(null));
|
||||||
store.isNewlyCreated = jest.fn().mockReturnValue(Promise.resolve(true));
|
store.isNewlyCreated = jest.fn().mockReturnValue(Promise.resolve(true));
|
||||||
|
|
||||||
|
// set unstableFeatures to a defined state before each test
|
||||||
|
unstableFeatures = {
|
||||||
|
"org.matrix.msc3440.stable": true,
|
||||||
|
};
|
||||||
|
|
||||||
makeClient();
|
makeClient();
|
||||||
|
|
||||||
// set reasonable working defaults
|
// set reasonable working defaults
|
||||||
@ -1373,10 +1376,10 @@ describe("MatrixClient", function () {
|
|||||||
await client.redactEvent(roomId, eventId, txnId, { reason });
|
await client.redactEvent(roomId, eventId, txnId, { reason });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when calling with with_relations", () => {
|
describe("when calling with 'with_rel_types'", () => {
|
||||||
const eventId = "$event42:example.org";
|
const eventId = "$event42:example.org";
|
||||||
|
|
||||||
it("should raise an error if server has no support for relation based redactions", async () => {
|
it("should raise an error if the server has no support for relation based redactions", async () => {
|
||||||
// load supported features
|
// load supported features
|
||||||
await client.getVersions();
|
await client.getVersions();
|
||||||
|
|
||||||
@ -1384,7 +1387,7 @@ describe("MatrixClient", function () {
|
|||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
client.redactEvent(roomId, eventId, txnId, {
|
client.redactEvent(roomId, eventId, txnId, {
|
||||||
with_relations: [RelationType.Reference],
|
with_rel_types: [RelationType.Reference],
|
||||||
});
|
});
|
||||||
}).toThrow(
|
}).toThrow(
|
||||||
new Error(
|
new Error(
|
||||||
@ -1394,34 +1397,30 @@ describe("MatrixClient", function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("and the server supports relation based redactions (unstable)", () => {
|
it("and the server has unstable support for relation based redactions, it should send 'org.matrix.msc3912.with_relations' in the request body", async () => {
|
||||||
beforeEach(async () => {
|
unstableFeatures["org.matrix.msc3912"] = true;
|
||||||
unstableFeatures["org.matrix.msc3912"] = true;
|
// load supported features
|
||||||
// load supported features
|
await client.getVersions();
|
||||||
await client.getVersions();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should send with_relations in the request body", async () => {
|
const txnId = client.makeTxnId();
|
||||||
const txnId = client.makeTxnId();
|
|
||||||
|
|
||||||
httpLookups = [
|
httpLookups = [
|
||||||
{
|
{
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
path:
|
path:
|
||||||
`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}` +
|
`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}` +
|
||||||
`/${encodeURIComponent(txnId)}`,
|
`/${encodeURIComponent(txnId)}`,
|
||||||
expectBody: {
|
expectBody: {
|
||||||
reason: "redaction test",
|
reason: "redaction test",
|
||||||
[MSC3912_RELATION_BASED_REDACTIONS_PROP.unstable!]: [RelationType.Reference],
|
["org.matrix.msc3912.with_relations"]: ["m.reference"],
|
||||||
},
|
|
||||||
data: { event_id: eventId },
|
|
||||||
},
|
},
|
||||||
];
|
data: { event_id: eventId },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
await client.redactEvent(roomId, eventId, txnId, {
|
await client.redactEvent(roomId, eventId, txnId, {
|
||||||
reason: "redaction test",
|
reason: "redaction test",
|
||||||
with_relations: [RelationType.Reference],
|
with_rel_types: [RelationType.Reference],
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -168,11 +168,11 @@ 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");
|
export const UNSTABLE_MSC2716_MARKER = new UnstableValue("m.room.marker", "org.matrix.msc2716.marker");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the "with_relations" request property for relation based redactions.
|
* Name of the request property for relation based redactions.
|
||||||
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
|
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
|
||||||
*/
|
*/
|
||||||
export const MSC3912_RELATION_BASED_REDACTIONS_PROP = new UnstableValue(
|
export const MSC3912_RELATION_BASED_REDACTIONS_PROP = new UnstableValue(
|
||||||
"with_relations",
|
"with_rel_types",
|
||||||
"org.matrix.msc3912.with_relations",
|
"org.matrix.msc3912.with_relations",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -48,17 +48,16 @@ export interface IJoinRoomOpts {
|
|||||||
export interface IRedactOpts {
|
export interface IRedactOpts {
|
||||||
reason?: string;
|
reason?: string;
|
||||||
/**
|
/**
|
||||||
* Whether events related to the redacted event should be redacted.
|
|
||||||
*
|
|
||||||
* If specified, then any events which relate to the event being redacted with
|
* If specified, then any events which relate to the event being redacted with
|
||||||
* any of the relationship types listed will also be redacted.
|
* any of the relationship types listed will also be redacted.
|
||||||
|
* Provide a "*" list item to tell the server to redact relations of any type.
|
||||||
*
|
*
|
||||||
* <b>Raises an Error if the server does not support it.</b>
|
* <b>Raises an Error if the server does not support it.</b>
|
||||||
* Check for server-side support before using this param with
|
* Check for server-side support before using this param with
|
||||||
* <code>client.canSupport.get(Feature.RelationBasedRedactions)</code>.
|
* <code>client.canSupport.get(Feature.RelationBasedRedactions)</code>.
|
||||||
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
|
* {@link https://github.com/matrix-org/matrix-spec-proposals/pull/3912}
|
||||||
*/
|
*/
|
||||||
with_relations?: Array<RelationType | string>;
|
with_rel_types?: Array<RelationType | "*">;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ISendEventResponse {
|
export interface ISendEventResponse {
|
||||||
|
@ -4593,10 +4593,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param txnId - transaction id. One will be made up if not supplied.
|
* @param txnId - transaction id. One will be made up if not supplied.
|
||||||
* @param opts - Options to pass on, may contain `reason` and `with_relations` (MSC3912)
|
* @param opts - Redact options
|
||||||
* @returns Promise which resolves: TODO
|
* @returns Promise which resolves: TODO
|
||||||
* @returns Rejects: with an error response.
|
* @returns Rejects: with an error response.
|
||||||
* @throws Error if called with `with_relations` (MSC3912) but the server does not support it.
|
* @throws Error if called with `with_rel_types` (MSC3912) but the server does not support it.
|
||||||
* Callers should check whether the server supports MSC3912 via `MatrixClient.canSupport`.
|
* Callers should check whether the server supports MSC3912 via `MatrixClient.canSupport`.
|
||||||
*/
|
*/
|
||||||
public redactEvent(
|
public redactEvent(
|
||||||
@ -4626,34 +4626,30 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
threadId = null;
|
threadId = null;
|
||||||
}
|
}
|
||||||
const reason = opts?.reason;
|
const reason = opts?.reason;
|
||||||
|
const content: IContent = { reason };
|
||||||
|
|
||||||
if (
|
if (opts?.with_rel_types !== undefined) {
|
||||||
opts?.with_relations &&
|
if (this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Unsupported) {
|
||||||
this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Unsupported
|
throw new Error(
|
||||||
) {
|
"Server does not support relation based redactions " +
|
||||||
throw new Error(
|
`roomId ${roomId} eventId ${eventId} txnId: ${txnId} threadId ${threadId}`,
|
||||||
"Server does not support relation based redactions " +
|
);
|
||||||
`roomId ${roomId} eventId ${eventId} txnId: ${txnId} threadId ${threadId}`,
|
}
|
||||||
);
|
|
||||||
|
const withRelTypesPropName =
|
||||||
|
this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Stable
|
||||||
|
? MSC3912_RELATION_BASED_REDACTIONS_PROP.stable!
|
||||||
|
: MSC3912_RELATION_BASED_REDACTIONS_PROP.unstable!;
|
||||||
|
|
||||||
|
content[withRelTypesPropName] = opts.with_rel_types;
|
||||||
}
|
}
|
||||||
|
|
||||||
const withRelations = opts?.with_relations
|
|
||||||
? {
|
|
||||||
[this.canSupport.get(Feature.RelationBasedRedactions) === ServerSupport.Stable
|
|
||||||
? MSC3912_RELATION_BASED_REDACTIONS_PROP.stable!
|
|
||||||
: MSC3912_RELATION_BASED_REDACTIONS_PROP.unstable!]: opts?.with_relations,
|
|
||||||
}
|
|
||||||
: {};
|
|
||||||
|
|
||||||
return this.sendCompleteEvent(
|
return this.sendCompleteEvent(
|
||||||
roomId,
|
roomId,
|
||||||
threadId,
|
threadId,
|
||||||
{
|
{
|
||||||
type: EventType.RoomRedaction,
|
type: EventType.RoomRedaction,
|
||||||
content: {
|
content,
|
||||||
...withRelations,
|
|
||||||
reason,
|
|
||||||
},
|
|
||||||
redacts: eventId,
|
redacts: eventId,
|
||||||
},
|
},
|
||||||
txnId as string,
|
txnId as string,
|
||||||
|
Reference in New Issue
Block a user