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

Add support for ephemeral events from the AS API #238

Merged
merged 3 commits into from
Oct 8, 2020
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 changelog.d/238.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for ephemeral events from the AS api (MSC2409)
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"extend": "^3.0.2",
"is-my-json-valid": "^2.20.5",
"js-yaml": "^3.14.0",
"matrix-appservice": "^0.5.0",
"matrix-appservice": "^0.6.0",
"matrix-js-sdk": "^8.4.1",
"nedb": "^1.8.0",
"nopt": "^4.0.3",
Expand All @@ -44,16 +44,16 @@
"@types/express": "^4.17.8",
"@types/extend": "^3.0.1",
"@types/js-yaml": "^3.12.5",
"@types/nedb": "^1.8.10",
"@types/node": "^10",
"@types/nopt": "^3.0.29",
"@types/nedb": "^1.8.10",
"@typescript-eslint/eslint-plugin": "^4.1.0",
"@typescript-eslint/parser": "^4.1.0",
"eslint": "^7.9.0",
"jasmine": "^3.6.0",
"nyc": "^15.1.0",
"typedoc": "^0.19.0",
"typescript": "^4.0.2",
"winston-transport": "^4.4.0",
"typedoc": "^0.19.0"
"winston-transport": "^4.4.0"
}
}
1 change: 1 addition & 0 deletions spec/integ/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const registrationFileContent = {
},
rate_limited: false,
protocols: [],
"de.sorunome.msc2409.push_ephemeral": undefined,
};
async function writeRegistrationFile(content=registrationFileContent, filename="registration.yaml") {
const filePath = path.join(tempDir, filename);
Expand Down
6 changes: 5 additions & 1 deletion src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ export class Bridge {
this.membershipCache,
this.appServiceBot,
this.onEvent.bind(this),
this.onEphemeralEvent.bind(this),
// If the bridge supports pushEphemeral, don't use sync data.
!this.registration.pushEphemeral ? this.onEphemeralEvent.bind(this) : undefined,
this.getIntent.bind(this),
);
}
Expand Down Expand Up @@ -634,6 +635,9 @@ export class Bridge {
}
return undefined;
});
this.appservice.on("ephemeral", async (event) =>
this.onEphemeralEvent(event as unknown as EphemeralEvent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really guaranteed to be an EphemeralEvent or is as unknown as X the new any?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's guaranteed. matrix-appservice-node doesn't have types and will send us a Record<string, unknown> but that doesn't cast to a EphemeralEvent easily. The homeserver will only send us these events however.

);
this.appservice.on("http-log", (line) => {
this.onLog(line, false);
});
Expand Down
19 changes: 15 additions & 4 deletions src/components/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class EncryptedEventBroker {
private membership: MembershipCache,
private asBot: AppServiceBot,
private onEvent: (weakEvent: WeakEvent) => void,
private onEphemeralEvent: (event: EphemeralEvent) => void,
private onEphemeralEvent: ((event: EphemeralEvent) => void)|undefined,
private getIntent: (userId: string) => Intent
) {

Expand Down Expand Up @@ -203,20 +203,29 @@ export class EncryptedEventBroker {
}

private onTyping(syncUserId: string, event: any) {
if (!this.onEphemeralEvent) {
return;
}
if (this.userForRoom.get(event.getRoomId()) === syncUserId) {
// Ensure only the selected user for the room syncs this.
this.onEphemeralEvent(event.event);
}
}

private onReceipt(syncUserId: string, event: any) {
if (!this.onEphemeralEvent) {
return;
}
if (this.userForRoom.get(event.getRoomId()) === syncUserId) {
// Ensure only the user for the room syncs this.
this.onEphemeralEvent(event.event);
}
}

private onPresence(event: any) {
if (!this.onEphemeralEvent) {
return;
}
// Presence needs to be de-duplicated.
const now = Date.now();
const presenceEv = event.event as PresenceEvent;
Expand Down Expand Up @@ -258,9 +267,11 @@ export class EncryptedEventBroker {
matrixClient.on("error", (err: Error) => {
log.error(`${userId} client error:`, err);
});
matrixClient.on("RoomMember.typing", (event: TypingEvent) => this.onTyping(userId, event));
matrixClient.on("Room.receipt", (event: ReadReceiptEvent) => this.onReceipt(userId, event));
matrixClient.on("User.presence", (event: PresenceEvent) => this.onPresence(event));
if (this.onEphemeralEvent) {
matrixClient.on("RoomMember.typing", (event: TypingEvent) => this.onTyping(userId, event));
matrixClient.on("Room.receipt", (event: ReadReceiptEvent) => this.onReceipt(userId, event));
matrixClient.on("User.presence", (event: PresenceEvent) => this.onPresence(event));
}
const filter = new Filter(userId);
filter.setDefinition(SYNC_FILTER);
await matrixClient.startClient({
Expand Down