Skip to content

Commit f5ed117

Browse files
authored
Merge pull request #32 from matrix-org/hs/ephemeral
Add support for MSC2409
2 parents c0ba3b0 + 7bc00ed commit f5ed117

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

changelog.d/32.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add experimental support for receiving ephemeral data from the homeserver (MSC2409)

src/app-service-registration.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class AppServiceRegistration {
3636
reg.setSenderLocalpart(obj.sender_localpart);
3737
reg.setRateLimited(obj.rate_limited);
3838
reg.setProtocols(obj.protocols);
39+
reg.pushEphemeral = obj["de.sorunome.msc2409.push_ephemeral"];
3940
if (obj.namespaces) {
4041
const kinds = ["users", "aliases", "rooms"];
4142
kinds.forEach((kind) => {
@@ -62,6 +63,11 @@ export class AppServiceRegistration {
6263
private asToken: string|null = null;
6364
private senderLocalpart: string|null = null;
6465
private rateLimited = true;
66+
/**
67+
* **Experimental**
68+
* Signal to the homeserver that this appservice will accept ephemeral events.
69+
*/
70+
public pushEphemeral = false;
6571
private namespaces: {
6672
users: RegexObj[];
6773
aliases: RegexObj[];
@@ -223,6 +229,7 @@ export class AppServiceRegistration {
223229
sender_localpart: this.senderLocalpart,
224230
rate_limited: this.rateLimited,
225231
protocols: this.protocols,
232+
"de.sorunome.msc2409.push_ephemeral": this.pushEphemeral,
226233
};
227234
}
228235

src/app-service.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ export declare interface AppService {
2121
* });
2222
*/
2323
on(event: "event", cb: (event: Record<string, unknown>) => void): this;
24+
/**
25+
* Emitted when an ephemeral event is pushed to the appservice.
26+
* The format of the event object is documented at
27+
* https://github.com/matrix-org/matrix-doc/pull/2409
28+
* @event
29+
* @example
30+
* appService.on("ephemeral", function(ev) {
31+
* console.log("ID: %s", ev.type);
32+
* });
33+
*/
34+
on(event: "ephemeral", cb: (event: Record<string, unknown>) => void): this;
2435
/**
2536
* Emitted when the HTTP listener logs some information.
2637
* `access_tokens` are stripped from requests
@@ -42,7 +53,6 @@ export declare interface AppService {
4253
* console.log("ID: %s", ev.content.body);
4354
* });
4455
*/
45-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4656
on(event: string, cb: (event: Record<string, unknown>) => void): this;
4757
}
4858

@@ -194,6 +204,7 @@ export class AppService extends EventEmitter {
194204
private isInvalidToken(req: Request, res: Response): boolean {
195205
const providedToken = req.query.access_token;
196206
if (providedToken !== this.config.homeserverToken) {
207+
res.status(403);
197208
res.send({
198209
errcode: "M_FORBIDDEN",
199210
error: "Bad token supplied,"
@@ -255,11 +266,13 @@ export class AppService extends EventEmitter {
255266
res.send("Missing transaction ID.");
256267
return;
257268
}
258-
if (!req.body || !req.body.events) {
259-
res.send("Missing events body.");
269+
if (!req.body) {
270+
res.send("Missing body.");
260271
return;
261272
}
262-
const events = req.body.events;
273+
274+
const events = req.body.events || [];
275+
const ephemeral = req.body["de.sorunome.msc2409.ephemeral"] || [];
263276

264277
if (this.lastProcessedTxnId === txnId) {
265278
res.send({}); // duplicate
@@ -271,6 +284,12 @@ export class AppService extends EventEmitter {
271284
this.emit("type:" + event.type, event);
272285
}
273286
}
287+
for (const event of ephemeral) {
288+
this.emit("ephemeral", event);
289+
if (event.type) {
290+
this.emit("ephemeral_type:" + event.type, event);
291+
}
292+
}
274293
this.lastProcessedTxnId = txnId;
275294
res.send({});
276295
}

0 commit comments

Comments
 (0)