Skip to content
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/32.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add experimental support for receiving ephemeral data from the homeserver (MSC2409)
7 changes: 7 additions & 0 deletions src/app-service-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class AppServiceRegistration {
reg.setSenderLocalpart(obj.sender_localpart);
reg.setRateLimited(obj.rate_limited);
reg.setProtocols(obj.protocols);
reg.pushEphemeral = obj["de.sorunome.msc2409.push_ephemeral"];
if (obj.namespaces) {
const kinds = ["users", "aliases", "rooms"];
kinds.forEach((kind) => {
Expand All @@ -62,6 +63,11 @@ export class AppServiceRegistration {
private asToken: string|null = null;
private senderLocalpart: string|null = null;
private rateLimited = true;
/**
* **Experimental**
* Signal to the homeserver that this appservice will accept ephemeral events.
*/
public pushEphemeral = false;
private namespaces: {
users: RegexObj[];
aliases: RegexObj[];
Expand Down Expand Up @@ -223,6 +229,7 @@ export class AppServiceRegistration {
sender_localpart: this.senderLocalpart,
rate_limited: this.rateLimited,
protocols: this.protocols,
"de.sorunome.msc2409.push_ephemeral": this.pushEphemeral,
};
}

Expand Down
27 changes: 23 additions & 4 deletions src/app-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ export declare interface AppService {
* });
*/
on(event: "event", cb: (event: Record<string, unknown>) => void): this;
/**
* Emitted when an ephemeral event is pushed to the appservice.
* The format of the event object is documented at
* https://github.com/matrix-org/matrix-doc/pull/2409
* @event
* @example
* appService.on("ephemeral", function(ev) {
* console.log("ID: %s", ev.type);
* });
*/
on(event: "ephemeral", cb: (event: Record<string, unknown>) => void): this;
/**
* Emitted when the HTTP listener logs some information.
* `access_tokens` are stripped from requests
Expand All @@ -42,7 +53,6 @@ export declare interface AppService {
* console.log("ID: %s", ev.content.body);
* });
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
on(event: string, cb: (event: Record<string, unknown>) => void): this;
}

Expand Down Expand Up @@ -194,6 +204,7 @@ export class AppService extends EventEmitter {
private isInvalidToken(req: Request, res: Response): boolean {
const providedToken = req.query.access_token;
if (providedToken !== this.config.homeserverToken) {
res.status(403);
res.send({
errcode: "M_FORBIDDEN",
error: "Bad token supplied,"
Expand Down Expand Up @@ -255,11 +266,13 @@ export class AppService extends EventEmitter {
res.send("Missing transaction ID.");
return;
}
if (!req.body || !req.body.events) {
res.send("Missing events body.");
if (!req.body) {
res.send("Missing body.");
return;
}
const events = req.body.events;

const events = req.body.events || [];
const ephemeral = req.body["de.sorunome.msc2409.ephemeral"] || [];

if (this.lastProcessedTxnId === txnId) {
res.send({}); // duplicate
Expand All @@ -271,6 +284,12 @@ export class AppService extends EventEmitter {
this.emit("type:" + event.type, event);
}
}
for (const event of ephemeral) {
this.emit("ephemeral", event);
if (event.type) {
this.emit("ephemeral_type:" + event.type, event);
}
}
this.lastProcessedTxnId = txnId;
res.send({});
}
Expand Down