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

Send extensible events structure and support on-demand parsing #2091

Merged
merged 8 commits into from
Jan 13, 2022
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"bs58": "^4.0.1",
"content-type": "^1.0.4",
"loglevel": "^1.7.1",
"matrix-events-sdk": "^0.0.1-beta.2",
"p-retry": "^4.5.0",
"qs": "^6.9.6",
"request": "^2.88.2",
Expand Down
2 changes: 1 addition & 1 deletion spec/integ/matrix-client-crypto.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function recvMessage(httpBackend, client, sender, message) {
return testUtils.awaitDecryption(event);
}).then((event) => {
expect(event.getType()).toEqual("m.room.message");
expect(event.getContent()).toEqual({
expect(event.getContent()).toMatchObject({
msgtype: "m.text",
body: "Hello, World",
});
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/room-state.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("RoomState", function() {
it("should return a single MatrixEvent if a state_key was specified",
function() {
const event = state.getStateEvents("m.room.member", userA);
expect(event.getContent()).toEqual({
expect(event.getContent()).toMatchObject({
membership: "join",
});
});
Expand Down
33 changes: 30 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
*/

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

import { ISyncStateData, SyncApi, SyncState } from "./sync";
import { EventStatus, IContent, IDecryptOptions, IEvent, MatrixEvent } from "./models/event";
Expand Down Expand Up @@ -86,7 +87,14 @@ import {
} from "./crypto/keybackup";
import { IIdentityServerProvider } from "./@types/IIdentityServerProvider";
import { MatrixScheduler } from "./scheduler";
import { IAuthData, ICryptoCallbacks, IMinimalEvent, IRoomEvent, IStateEvent, NotificationCountType } from "./matrix";
import {
IAuthData,
ICryptoCallbacks,
IMinimalEvent,
IRoomEvent,
IStateEvent,
NotificationCountType,
} from "./matrix";
import {
CrossSigningKey,
IAddSecretStorageKeyOpts,
Expand Down Expand Up @@ -3935,11 +3943,30 @@ export class MatrixClient extends EventEmitter {
callback = txnId as any as Callback; // for legacy
txnId = undefined;
}

// 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;
if (sendContent['msgtype'] === MsgType.Text) {
const serialized = MessageEvent.from(sendContent['body'], sendContent['formatted_body']).serialize();
eventType = serialized.type;
sendContent = serialized.content;
} else if (sendContent['msgtype'] === MsgType.Emote) {
const serialized = EmoteEvent.from(sendContent['body'], sendContent['formatted_body']).serialize();
eventType = serialized.type;
sendContent = serialized.content;
} else if (sendContent['msgtype'] === MsgType.Notice) {
const serialized = NoticeEvent.from(sendContent['body'], sendContent['formatted_body']).serialize();
eventType = serialized.type;
sendContent = serialized.content;
}

return this.sendEvent(
roomId,
threadId as (string | null),
EventType.RoomMessage,
content as IContent,
eventType,
sendContent,
txnId as string,
callback,
);
Expand Down
32 changes: 31 additions & 1 deletion src/models/event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
Copyright 2015 - 2022 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ limitations under the License.
*/

import { EventEmitter } from 'events';
import { ExtensibleEvent, ExtensibleEvents, Optional } from "matrix-events-sdk";

import { logger } from '../logger';
import { VerificationRequest } from "../crypto/verification/request/VerificationRequest";
Expand Down Expand Up @@ -210,6 +211,12 @@ export class MatrixEvent extends EventEmitter {
*/
private visibility: MessageVisibility = MESSAGE_VISIBLE;

// Not all events will be extensible-event compatible, so cache a flag in
// addition to a falsy cached event value. We check the flag later on in
// a public getter to decide if the cache is valid.
private _hasCachedExtEv = false;
private _cachedExtEv: Optional<ExtensibleEvent> = undefined;

/* curve25519 key which we believe belongs to the sender of the event. See
* getSenderKey()
*/
Expand Down Expand Up @@ -327,6 +334,25 @@ export class MatrixEvent extends EventEmitter {
this.reEmitter = new ReEmitter(this);
}

/**
* Unstable getter to try and get an extensible event. Note that this might
* return a falsy value if the event could not be parsed as an extensible
* event.
*
* @deprecated Use stable functions where possible.
*/
public get unstableExtensibleEvent(): Optional<ExtensibleEvent> {
if (!this._hasCachedExtEv) {
this._cachedExtEv = ExtensibleEvents.parse(this.getEffectiveEvent());
}
return this._cachedExtEv;
}

private invalidateExtensibleEvent() {
// just reset the flag - that'll trick the getter into parsing a new event
this._hasCachedExtEv = false;
}

/**
* Gets the event as though it would appear unencrypted. If the event is already not
* encrypted, it is simply returned as-is.
Expand Down Expand Up @@ -861,6 +887,7 @@ export class MatrixEvent extends EventEmitter {
this.forwardingCurve25519KeyChain =
decryptionResult.forwardingCurve25519KeyChain || [];
this.untrusted = decryptionResult.untrusted || false;
this.invalidateExtensibleEvent();
}

/**
Expand Down Expand Up @@ -1079,6 +1106,8 @@ export class MatrixEvent extends EventEmitter {
delete content[key];
}
}

this.invalidateExtensibleEvent();
}

/**
Expand Down Expand Up @@ -1282,6 +1311,7 @@ export class MatrixEvent extends EventEmitter {
if (this._replacingEvent !== newEvent) {
this._replacingEvent = newEvent;
this.emit("Event.replaced", this);
this.invalidateExtensibleEvent();
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5470,6 +5470,11 @@ marked@^2.0.3:
resolved "https://registry.yarnpkg.com/marked/-/marked-2.1.3.tgz#bd017cef6431724fd4b27e0657f5ceb14bff3753"
integrity sha512-/Q+7MGzaETqifOMWYEA7HVMaZb4XbcRfaOzcSsHZEith83KGlvaSG33u0SKu89Mj5h+T8V2hM+8O45Qc5XTgwA==

matrix-events-sdk@^0.0.1-beta.2:
version "0.0.1-beta.2"
resolved "https://registry.yarnpkg.com/matrix-events-sdk/-/matrix-events-sdk-0.0.1-beta.2.tgz#28efdcc3259152c4d53094cedb72b3843e5f772e"
integrity sha512-a3VIZeb9IxxxPrvFnUbt4pjP7A6irv7eWLv1GBoq+80m7v5n3QhzT/mmeUGJx2KNt7jLboFau4g1iIU82H3wEg==

matrix-mock-request@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/matrix-mock-request/-/matrix-mock-request-1.2.3.tgz#56b15d86e2601a9b48a854844396d18caab649c8"
Expand Down