Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extensible events v1 field population on legacy events #3040

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions spec/integ/matrix-client-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1177,11 +1177,10 @@ describe("MatrixClient", function () {
.when("PUT", "/send")
.check((req) => {
expect(req.data).toStrictEqual({
"msgtype": "m.emote",
"body": "Body",
"formatted_body": "<h1>Body</h1>",
"format": "org.matrix.custom.html",
"org.matrix.msc1767.message": expect.anything(),
msgtype: "m.emote",
body: "Body",
formatted_body: "<h1>Body</h1>",
format: "org.matrix.custom.html",
});
})
.respond(200, { event_id: "$foobar" });
Expand All @@ -1197,11 +1196,10 @@ describe("MatrixClient", function () {
.when("PUT", "/send")
.check((req) => {
expect(req.data).toStrictEqual({
"msgtype": "m.text",
"body": "Body",
"formatted_body": "<h1>Body</h1>",
"format": "org.matrix.custom.html",
"org.matrix.msc1767.message": expect.anything(),
msgtype: "m.text",
body: "Body",
formatted_body: "<h1>Body</h1>",
format: "org.matrix.custom.html",
});
})
.respond(200, { event_id: "$foobar" });
Expand Down
42 changes: 3 additions & 39 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
* This is an internal module. See {@link MatrixClient} for the public class.
*/

import { EmoteEvent, IPartialEvent, MessageEvent, NoticeEvent, Optional } from "matrix-events-sdk";
import { Optional } from "matrix-events-sdk";

import type { IMegolmSessionData } from "./@types/crypto";
import { ISyncStateData, SyncApi, SyncApiOptions, SyncState } from "./sync";
Expand Down Expand Up @@ -4545,44 +4545,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
threadId = null;
}

// Populate all outbound events with Extensible Events metadata to ensure there's a
// reasonably large pool of messages to parse.
let eventType: string = EventType.RoomMessage;
let sendContent: IContent = content as IContent;
const makeContentExtensible = (content: IContent = {}, recurse = true): IPartialEvent<object> | undefined => {
let newEvent: IPartialEvent<IContent> | undefined;

if (content["msgtype"] === MsgType.Text) {
newEvent = MessageEvent.from(content["body"], content["formatted_body"]).serialize();
} else if (content["msgtype"] === MsgType.Emote) {
newEvent = EmoteEvent.from(content["body"], content["formatted_body"]).serialize();
} else if (content["msgtype"] === MsgType.Notice) {
newEvent = NoticeEvent.from(content["body"], content["formatted_body"]).serialize();
}

if (newEvent && content["m.new_content"] && recurse) {
const newContent = makeContentExtensible(content["m.new_content"], false);
if (newContent) {
newEvent.content["m.new_content"] = newContent.content;
}
}

if (newEvent) {
// copy over all other fields we don't know about
for (const [k, v] of Object.entries(content)) {
if (!newEvent.content.hasOwnProperty(k)) {
newEvent.content[k] = v;
}
}
}

return newEvent;
};
const result = makeContentExtensible(sendContent);
if (result) {
eventType = result.type;
sendContent = result.content;
}
const eventType: string = EventType.RoomMessage;
const sendContent: IContent = content as IContent;

return this.sendEvent(roomId, threadId as string | null, eventType, sendContent, txnId);
}
Expand Down