@im-more/dsh-matrix 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +14 -0
  3. package/lib/index.js +2401 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/as.d.ts +33 -0
  6. package/lib/types/as.d.ts.map +1 -0
  7. package/lib/types/as.js +237 -0
  8. package/lib/types/as.js.map +1 -0
  9. package/lib/types/bot.d.ts +109 -0
  10. package/lib/types/bot.d.ts.map +1 -0
  11. package/lib/types/bot.js +234 -0
  12. package/lib/types/bot.js.map +1 -0
  13. package/lib/types/config.d.ts +59 -0
  14. package/lib/types/config.d.ts.map +1 -0
  15. package/lib/types/config.js +67 -0
  16. package/lib/types/config.js.map +1 -0
  17. package/lib/types/decode.d.ts +56 -0
  18. package/lib/types/decode.d.ts.map +1 -0
  19. package/lib/types/decode.js +298 -0
  20. package/lib/types/decode.js.map +1 -0
  21. package/lib/types/errors.d.ts +45 -0
  22. package/lib/types/errors.d.ts.map +1 -0
  23. package/lib/types/errors.js +120 -0
  24. package/lib/types/errors.js.map +1 -0
  25. package/lib/types/html.d.ts +44 -0
  26. package/lib/types/html.d.ts.map +1 -0
  27. package/lib/types/html.js +286 -0
  28. package/lib/types/html.js.map +1 -0
  29. package/lib/types/identity.d.ts +20 -0
  30. package/lib/types/identity.d.ts.map +1 -0
  31. package/lib/types/identity.js +136 -0
  32. package/lib/types/identity.js.map +1 -0
  33. package/lib/types/index.d.ts +5 -0
  34. package/lib/types/index.d.ts.map +1 -0
  35. package/lib/types/index.js +5 -0
  36. package/lib/types/index.js.map +1 -0
  37. package/lib/types/media.d.ts +54 -0
  38. package/lib/types/media.d.ts.map +1 -0
  39. package/lib/types/media.js +94 -0
  40. package/lib/types/media.js.map +1 -0
  41. package/lib/types/mentions.d.ts +58 -0
  42. package/lib/types/mentions.d.ts.map +1 -0
  43. package/lib/types/mentions.js +94 -0
  44. package/lib/types/mentions.js.map +1 -0
  45. package/lib/types/modes/html.d.ts +13 -0
  46. package/lib/types/modes/html.d.ts.map +1 -0
  47. package/lib/types/modes/html.js +242 -0
  48. package/lib/types/modes/html.js.map +1 -0
  49. package/lib/types/modes/shared.d.ts +164 -0
  50. package/lib/types/modes/shared.d.ts.map +1 -0
  51. package/lib/types/modes/shared.js +312 -0
  52. package/lib/types/modes/shared.js.map +1 -0
  53. package/lib/types/request.d.ts +82 -0
  54. package/lib/types/request.d.ts.map +1 -0
  55. package/lib/types/request.js +206 -0
  56. package/lib/types/request.js.map +1 -0
  57. package/lib/types/rooms.d.ts +83 -0
  58. package/lib/types/rooms.d.ts.map +1 -0
  59. package/lib/types/rooms.js +177 -0
  60. package/lib/types/rooms.js.map +1 -0
  61. package/lib/types/send.d.ts +76 -0
  62. package/lib/types/send.d.ts.map +1 -0
  63. package/lib/types/send.js +172 -0
  64. package/lib/types/send.js.map +1 -0
  65. package/lib/types/types.d.ts +418 -0
  66. package/lib/types/types.d.ts.map +1 -0
  67. package/lib/types/types.js +134 -0
  68. package/lib/types/types.js.map +1 -0
  69. package/package.json +65 -3
  70. package/src/as.ts +293 -0
  71. package/src/bot.ts +338 -0
  72. package/src/config.ts +100 -0
  73. package/src/decode.ts +392 -0
  74. package/src/errors.ts +149 -0
  75. package/src/html.ts +325 -0
  76. package/src/identity.ts +165 -0
  77. package/src/index.ts +14 -0
  78. package/src/media.ts +132 -0
  79. package/src/mentions.ts +134 -0
  80. package/src/modes/html.ts +346 -0
  81. package/src/modes/shared.ts +485 -0
  82. package/src/request.ts +295 -0
  83. package/src/rooms.ts +232 -0
  84. package/src/send.ts +275 -0
  85. package/src/types.ts +633 -0
  86. package/README.md +0 -3
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Matrix Client-Server transport: one HTTP path for every endpoint the adapter calls, the standard error envelope
3
+ * (`errcode` / `error`), rate-limit fields, upload bytes and authenticated media download.
4
+ *
5
+ * `Internal` (the migrated upstream client) keeps its upstream method bodies and calls this class through
6
+ * `bot.http`; the adapter's own send/media paths call it directly, so both cannot diverge on base URL or auth.
7
+ */
8
+ import { MatrixApiError } from "./errors.js";
9
+ /** Delay asked for by a `Retry-After` response header (seconds or HTTP-date). */
10
+ function retryAfterHeader(response) {
11
+ const header = response.headers.get('retry-after');
12
+ if (header === null)
13
+ return undefined;
14
+ const seconds = Number(header);
15
+ if (Number.isFinite(seconds) && seconds >= 0)
16
+ return seconds * 1_000;
17
+ const date = Date.parse(header);
18
+ if (!Number.isNaN(date))
19
+ return Math.max(0, date - Date.now());
20
+ return undefined;
21
+ }
22
+ /** The retry delay of one failed answer: the body field first, then the standard header. */
23
+ function retryAfterOf(body, response) {
24
+ const bodyDelay = body?.retry_after_ms;
25
+ if (typeof bodyDelay === 'number' &&
26
+ Number.isFinite(bodyDelay) &&
27
+ bodyDelay >= 0)
28
+ return bodyDelay;
29
+ return retryAfterHeader(response);
30
+ }
31
+ /** Whether a caught value is an abort error, including the `DOMException` a signal produces. */
32
+ function isAbortError(value) {
33
+ return ((value instanceof Error || value instanceof DOMException) &&
34
+ value.name === 'AbortError');
35
+ }
36
+ /** The abort reason carried by a failure, unwrapping a transport error's `cause` chain. */
37
+ function abortReasonOf(error) {
38
+ if (isAbortError(error))
39
+ return error;
40
+ if (typeof error === 'object' && error !== null && 'cause' in error) {
41
+ return abortReasonOf(error.cause);
42
+ }
43
+ return undefined;
44
+ }
45
+ /** A message for a failed call, naming status and the server's own error text when it sent one. */
46
+ function failureMessage(method, path, status, body) {
47
+ const code = typeof body?.errcode === 'string' ? body.errcode : undefined;
48
+ const detail = typeof body?.error === 'string' ? body.error : undefined;
49
+ const suffix = code === undefined && detail === undefined
50
+ ? ''
51
+ : `: ${[code, detail].filter((part) => part !== undefined).join(' ')}`;
52
+ return `matrix ${method} ${path} answered ${status}${suffix}`;
53
+ }
54
+ /**
55
+ * The Client-Server client of one bot.
56
+ *
57
+ * Requests are relative to the homeserver root with `/_matrix` already appended. The access token is set after
58
+ * `register`/`login`; `register`/`login` themselves go through `ctx.http` with the application service token.
59
+ */
60
+ export class MatrixClient {
61
+ ctx;
62
+ endpoint;
63
+ /** The user access token every Client-Server call carries; empty until `connect` logged in. */
64
+ token;
65
+ /** @param ctx - context whose `http` service performs requests. @param endpoint - homeserver root plus `/_matrix`. */
66
+ constructor(ctx, endpoint) {
67
+ this.ctx = ctx;
68
+ this.endpoint = endpoint;
69
+ }
70
+ /** Set the user access token minted by `m.login.application_service`. */
71
+ setToken(token) {
72
+ this.token = token;
73
+ }
74
+ /** The absolute URL of one endpoint path. */
75
+ url(path) {
76
+ return `${this.endpoint}${path.startsWith('/') ? path : `/${path}`}`;
77
+ }
78
+ /**
79
+ * Call one GET endpoint.
80
+ * @param path - endpoint below the Matrix root.
81
+ * @param options - query parameters, headers and cancellation.
82
+ * @returns the parsed JSON answer.
83
+ */
84
+ async get(path, options = {}) {
85
+ return await this.decode(await this.send('GET', path, undefined, options), 'GET', path);
86
+ }
87
+ /**
88
+ * Call one PUT endpoint.
89
+ * @param path - endpoint below the Matrix root.
90
+ * @param data - JSON body (`t.rn` events, profile writes).
91
+ * @param options - query parameters, headers and cancellation.
92
+ * @returns the parsed JSON answer.
93
+ */
94
+ async put(path, data, options = {}) {
95
+ return await this.decode(await this.send('PUT', path, data, options), 'PUT', path);
96
+ }
97
+ /**
98
+ * Call one POST endpoint.
99
+ * @param path - endpoint below the Matrix root.
100
+ * @param data - JSON body or raw bytes (upload).
101
+ * @param options - query parameters, headers and cancellation.
102
+ * @returns the parsed JSON answer.
103
+ */
104
+ async post(path, data, options = {}) {
105
+ return await this.decode(await this.send('POST', path, data, options), 'POST', path);
106
+ }
107
+ /**
108
+ * Upload bytes to the media repository.
109
+ * @param name - file name sent as the `filename` query parameter.
110
+ * @param data - the bytes.
111
+ * @param mimetype - `content-type` of the bytes, when known.
112
+ * @param signal - cancellation.
113
+ * @returns the upload answer (`content_uri` on success).
114
+ */
115
+ async upload(name, data, mimetype, signal) {
116
+ return await this.post(`/media/v3/upload?filename=${encodeURIComponent(name)}`, data, {
117
+ ...(mimetype === undefined
118
+ ? {}
119
+ : { headers: { 'content-type': mimetype } }),
120
+ ...(signal === undefined ? {} : { signal }),
121
+ });
122
+ }
123
+ /**
124
+ * Download one media item through the authenticated endpoint (v1.11's recommended path).
125
+ * @param serverName - the server name of the `mxc://` URI.
126
+ * @param mediaId - the media id of the `mxc://` URI.
127
+ * @param fileName - optional file name path segment.
128
+ * @param signal - cancellation.
129
+ * @returns the platform's answer, body included.
130
+ */
131
+ async download(serverName, mediaId, fileName, signal) {
132
+ const suffix = fileName === undefined ? '' : `/${fileName}`;
133
+ return await this.send('GET', `/client/v1/media/download/${serverName}/${mediaId}${suffix}`, undefined, signal === undefined ? {} : { signal });
134
+ }
135
+ /** Perform one request and return the raw answer; transport failures become `MatrixApiError`s. */
136
+ async send(method, path, data, options) {
137
+ const headers = { ...options.headers };
138
+ if (this.token !== undefined)
139
+ headers.authorization = `Bearer ${this.token}`;
140
+ let response;
141
+ try {
142
+ response = await this.ctx.http(this.url(path), {
143
+ method,
144
+ ...(data === undefined ? {} : { data }),
145
+ ...(options.params === undefined ? {} : { params: options.params }),
146
+ headers,
147
+ ...(options.signal === undefined ? {} : { signal: options.signal }),
148
+ });
149
+ }
150
+ catch (cause) {
151
+ if (options.signal?.aborted === true)
152
+ throw options.signal.reason;
153
+ const abort = abortReasonOf(cause);
154
+ if (abort !== undefined)
155
+ throw abort;
156
+ throw new MatrixApiError(`matrix ${method} ${path} request failed`, {
157
+ method,
158
+ path,
159
+ cause,
160
+ });
161
+ }
162
+ if (options.signal?.aborted === true)
163
+ throw options.signal.reason;
164
+ return response;
165
+ }
166
+ /** Decode one answer: JSON object on success, `MatrixApiError` for every refusal. */
167
+ async decode(response, method, path) {
168
+ const status = response.status;
169
+ let text;
170
+ try {
171
+ text = await response.text();
172
+ }
173
+ catch (cause) {
174
+ throw new MatrixApiError('matrix answered without a readable body', {
175
+ status,
176
+ cause,
177
+ });
178
+ }
179
+ let parsed;
180
+ if (text !== '') {
181
+ try {
182
+ parsed = JSON.parse(text);
183
+ }
184
+ catch {
185
+ parsed = undefined;
186
+ }
187
+ }
188
+ const body = typeof parsed === 'object' && parsed !== null
189
+ ? parsed
190
+ : undefined;
191
+ if (status >= 400) {
192
+ const retryAfterMs = retryAfterOf(body, response);
193
+ throw new MatrixApiError(failureMessage(method, path, status, body), {
194
+ status,
195
+ ...(typeof body?.errcode === 'string' ? { errcode: body.errcode } : {}),
196
+ ...(typeof body?.error === 'string' ? { error: body.error } : {}),
197
+ ...(retryAfterMs === undefined ? {} : { retryAfterMs }),
198
+ });
199
+ }
200
+ if (parsed === undefined) {
201
+ throw new MatrixApiError(`matrix answered ${status} without a JSON body`, { status });
202
+ }
203
+ return parsed;
204
+ }
205
+ }
206
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAkB5C,iFAAiF;AACjF,SAAS,gBAAgB,CAAC,QAAkB;IAC1C,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAA;IAClD,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,SAAS,CAAA;IACrC,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;IAC9B,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,IAAI,CAAC;QAAE,OAAO,OAAO,GAAG,KAAK,CAAA;IACpE,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;IAC/B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,GAAG,IAAI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C,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACjE,GAAG,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC;aACxD,CAAC,CAAA;QACJ,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,MAAM,IAAI,cAAc,CACtB,mBAAmB,MAAM,sBAAsB,EAC/C,EAAE,MAAM,EAAE,CACX,CAAA;QACH,CAAC;QACD,OAAO,MAAW,CAAA;IACpB,CAAC;CACF"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The room cache and the one-shot full-state `/sync` that fills it.
3
+ *
4
+ * Application Service pushes carry events only: no room list, name, member set or summary. `connect` therefore asks the
5
+ * homeserver for one full-state sync (`full_state=true` makes the answer immediate, so it is a query, not a poll),
6
+ * uses the joined rooms for names/members/summaries/`m.direct`/encryption markers, and joins the invited rooms when
7
+ * `autoJoin` is on. The same query runs once more after the bot joins a room on its own.
8
+ *
9
+ * A room is reported as direct when the bot's own `m.direct` names it, else when the summary says two joined members,
10
+ * else when the observed joined members are exactly the bot and one other user. Everything else is a group.
11
+ */
12
+ import type { Sync } from './types.ts';
13
+ /** One member the cache has seen, with the display name its state events carried. */
14
+ export interface CachedMember {
15
+ joined: boolean;
16
+ displayname?: string;
17
+ }
18
+ /** The cached state of one room. */
19
+ export interface CachedRoom {
20
+ name?: string;
21
+ members: Map<string, CachedMember>;
22
+ /** `summary['m.joined_member_count']` of the full-state sync. */
23
+ joinedMemberCount?: number;
24
+ encrypted: boolean;
25
+ }
26
+ /** A minimal client surface the enumeration needs (the migrated `Internal` satisfies it). */
27
+ export interface RoomEnumerator {
28
+ sync(fullState?: boolean): Promise<Sync>;
29
+ }
30
+ /** In-memory room state shared by decode, enumeration and member reads. */
31
+ export declare class RoomCache {
32
+ private readonly rooms;
33
+ private readonly directRooms;
34
+ /** The cached state of one room, created on first use. */
35
+ room(roomId: string): CachedRoom;
36
+ /** The room name last seen, if any. */
37
+ nameOf(roomId: string): string | undefined;
38
+ /** The display name last seen for one member of a room. */
39
+ displayNameOf(roomId: string, userId: string): string | undefined;
40
+ /** Whether a room's state says it enabled end-to-end encryption. */
41
+ isEncrypted(roomId: string): boolean;
42
+ /**
43
+ * Record that a room is encrypted.
44
+ * @param roomId - the room.
45
+ * @returns true when this is the first observation, so the caller logs its warning once.
46
+ */
47
+ markEncrypted(roomId: string): boolean;
48
+ /** Record one `m.room.name` event. */
49
+ setName(roomId: string, name: string): void;
50
+ /** Record one `m.room.member` event. */
51
+ applyMember(roomId: string, userId: string, membership: string | undefined, displayname: string | undefined): void;
52
+ /** The joined members of one room, in insertion order. */
53
+ joinedMembers(roomId: string): string[];
54
+ /**
55
+ * Whether a room is a direct chat.
56
+ * @param roomId - the room.
57
+ * @param selfId - the bot's Matrix user id.
58
+ * @returns true for a room the judgement chain reports as direct.
59
+ */
60
+ isDirect(roomId: string, selfId: string): boolean;
61
+ /** Record one state event that arrived by push. */
62
+ applyStateEvent(roomId: string, type: string, stateKey: string | undefined, content: Record<string, unknown>): void;
63
+ /**
64
+ * Apply one full-state sync answer.
65
+ * @param sync - the answer of `GET /client/v3/sync?full_state=true`.
66
+ * @param selfId - the bot's Matrix user id.
67
+ * @returns the rooms that invited the bot, in answer order.
68
+ */
69
+ applySync(sync: Sync, selfId: string): string[];
70
+ }
71
+ /**
72
+ * Run the one-shot full-state sync and apply it.
73
+ *
74
+ * The response's `next_batch` is deliberately dropped: an Application Service has no cursor, and the timeline of this
75
+ * query is history that the host's store already owns.
76
+ *
77
+ * @param internal - the room enumerator (the migrated `Internal`).
78
+ * @param cache - the room cache to fill.
79
+ * @param selfId - the bot's Matrix user id.
80
+ * @returns the rooms that invited the bot.
81
+ */
82
+ export declare function enumerateRooms(internal: RoomEnumerator, cache: RoomCache, selfId: string): Promise<string[]>;
83
+ //# sourceMappingURL=rooms.d.ts.map
@@ -0,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AM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,WAAW,EAAE,MAAM,GAAG,SAAS,GAC9B,IAAI;IAQP,0DAA0D;IAC1D,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;IAQvC;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IASjD,mDAAmD;IACnD,eAAe,CACb,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,IAAI;IAyBP;;;;;OAKG;IACH,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;CAuChD;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,cAAc,EACxB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,EAAE,CAAC,CAGnB"}
@@ -0,0 +1,177 @@
1
+ /**
2
+ * The room cache and the one-shot full-state `/sync` that fills it.
3
+ *
4
+ * Application Service pushes carry events only: no room list, name, member set or summary. `connect` therefore asks the
5
+ * homeserver for one full-state sync (`full_state=true` makes the answer immediate, so it is a query, not a poll),
6
+ * uses the joined rooms for names/members/summaries/`m.direct`/encryption markers, and joins the invited rooms when
7
+ * `autoJoin` is on. The same query runs once more after the bot joins a room on its own.
8
+ *
9
+ * A room is reported as direct when the bot's own `m.direct` names it, else when the summary says two joined members,
10
+ * else when the observed joined members are exactly the bot and one other user. Everything else is a group.
11
+ */
12
+ /** The rooms the bot's own `m.direct` account data names. */
13
+ function directRoomIds(sync) {
14
+ const out = [];
15
+ for (const event of sync.account_data?.events ?? []) {
16
+ if (event.type !== 'm.direct')
17
+ continue;
18
+ const content = event.content;
19
+ for (const value of Object.values(content)) {
20
+ if (!Array.isArray(value))
21
+ continue;
22
+ for (const roomId of value) {
23
+ if (typeof roomId === 'string')
24
+ out.push(roomId);
25
+ }
26
+ }
27
+ }
28
+ return out;
29
+ }
30
+ /** In-memory room state shared by decode, enumeration and member reads. */
31
+ export class RoomCache {
32
+ rooms = new Map();
33
+ directRooms = new Set();
34
+ /** The cached state of one room, created on first use. */
35
+ room(roomId) {
36
+ let room = this.rooms.get(roomId);
37
+ if (room === undefined) {
38
+ room = { members: new Map(), encrypted: false };
39
+ this.rooms.set(roomId, room);
40
+ }
41
+ return room;
42
+ }
43
+ /** The room name last seen, if any. */
44
+ nameOf(roomId) {
45
+ return this.rooms.get(roomId)?.name;
46
+ }
47
+ /** The display name last seen for one member of a room. */
48
+ displayNameOf(roomId, userId) {
49
+ return this.rooms.get(roomId)?.members.get(userId)?.displayname;
50
+ }
51
+ /** Whether a room's state says it enabled end-to-end encryption. */
52
+ isEncrypted(roomId) {
53
+ return this.rooms.get(roomId)?.encrypted === true;
54
+ }
55
+ /**
56
+ * Record that a room is encrypted.
57
+ * @param roomId - the room.
58
+ * @returns true when this is the first observation, so the caller logs its warning once.
59
+ */
60
+ markEncrypted(roomId) {
61
+ const room = this.room(roomId);
62
+ if (room.encrypted)
63
+ return false;
64
+ room.encrypted = true;
65
+ return true;
66
+ }
67
+ /** Record one `m.room.name` event. */
68
+ setName(roomId, name) {
69
+ this.room(roomId).name = name;
70
+ }
71
+ /** Record one `m.room.member` event. */
72
+ applyMember(roomId, userId, membership, displayname) {
73
+ const room = this.room(roomId);
74
+ const member = room.members.get(userId) ?? { joined: false };
75
+ member.joined = membership === 'join';
76
+ if (displayname !== undefined)
77
+ member.displayname = displayname;
78
+ room.members.set(userId, member);
79
+ }
80
+ /** The joined members of one room, in insertion order. */
81
+ joinedMembers(roomId) {
82
+ const room = this.rooms.get(roomId);
83
+ if (room === undefined)
84
+ return [];
85
+ return [...room.members]
86
+ .filter(([, member]) => member.joined)
87
+ .map(([userId]) => userId);
88
+ }
89
+ /**
90
+ * Whether a room is a direct chat.
91
+ * @param roomId - the room.
92
+ * @param selfId - the bot's Matrix user id.
93
+ * @returns true for a room the judgement chain reports as direct.
94
+ */
95
+ isDirect(roomId, selfId) {
96
+ if (this.directRooms.has(roomId))
97
+ return true;
98
+ const room = this.rooms.get(roomId);
99
+ if (room === undefined)
100
+ return false;
101
+ if (room.joinedMemberCount === 2)
102
+ return true;
103
+ const joined = [...room.members.values()].filter((member) => member.joined);
104
+ return joined.length === 2 && room.members.get(selfId)?.joined === true;
105
+ }
106
+ /** Record one state event that arrived by push. */
107
+ applyStateEvent(roomId, type, stateKey, content) {
108
+ switch (type) {
109
+ case 'm.room.name':
110
+ if (typeof content.name === 'string')
111
+ this.setName(roomId, content.name);
112
+ return;
113
+ case 'm.room.member':
114
+ if (stateKey !== undefined) {
115
+ this.applyMember(roomId, stateKey, typeof content.membership === 'string'
116
+ ? content.membership
117
+ : undefined, typeof content.displayname === 'string'
118
+ ? content.displayname
119
+ : undefined);
120
+ }
121
+ return;
122
+ case 'm.room.encryption':
123
+ this.markEncrypted(roomId);
124
+ return;
125
+ }
126
+ }
127
+ /**
128
+ * Apply one full-state sync answer.
129
+ * @param sync - the answer of `GET /client/v3/sync?full_state=true`.
130
+ * @param selfId - the bot's Matrix user id.
131
+ * @returns the rooms that invited the bot, in answer order.
132
+ */
133
+ applySync(sync, selfId) {
134
+ for (const roomId of directRoomIds(sync))
135
+ this.directRooms.add(roomId);
136
+ for (const [roomId, joined] of Object.entries(sync.rooms?.join ?? {})) {
137
+ const room = this.room(roomId);
138
+ for (const event of joined.state?.events ?? []) {
139
+ this.applyStateEvent(roomId, event.type, event.state_key, event.content);
140
+ }
141
+ const count = joined.summary?.['m.joined_member_count'];
142
+ if (typeof count === 'number')
143
+ room.joinedMemberCount = count;
144
+ }
145
+ const invites = [];
146
+ for (const [roomId, invited] of Object.entries(sync.rooms?.invite ?? {})) {
147
+ let invitedSelf = false;
148
+ for (const event of invited.invite_state?.events ?? []) {
149
+ this.applyStateEvent(roomId, event.type, event.state_key, event.content);
150
+ if (event.type === 'm.room.member' &&
151
+ event.state_key === selfId &&
152
+ event.content.membership === 'invite') {
153
+ invitedSelf = true;
154
+ }
155
+ }
156
+ if (invitedSelf)
157
+ invites.push(roomId);
158
+ }
159
+ return invites;
160
+ }
161
+ }
162
+ /**
163
+ * Run the one-shot full-state sync and apply it.
164
+ *
165
+ * The response's `next_batch` is deliberately dropped: an Application Service has no cursor, and the timeline of this
166
+ * query is history that the host's store already owns.
167
+ *
168
+ * @param internal - the room enumerator (the migrated `Internal`).
169
+ * @param cache - the room cache to fill.
170
+ * @param selfId - the bot's Matrix user id.
171
+ * @returns the rooms that invited the bot.
172
+ */
173
+ export async function enumerateRooms(internal, cache, selfId) {
174
+ const sync = await internal.sync(true);
175
+ return cache.applySync(sync, selfId);
176
+ }
177
+ //# sourceMappingURL=rooms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rooms.js","sourceRoot":"","sources":["../../src/rooms.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAwBH,6DAA6D;AAC7D,SAAS,aAAa,CAAC,IAAU;IAC/B,MAAM,GAAG,GAAa,EAAE,CAAA;IACxB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,YAAY,EAAE,MAAM,IAAI,EAAE,EAAE,CAAC;QACpD,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU;YAAE,SAAQ;QACvC,MAAM,OAAO,GAAG,KAAK,CAAC,OAAkC,CAAA;QACxD,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC3C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBAAE,SAAQ;YACnC,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE,CAAC;gBAC3B,IAAI,OAAO,MAAM,KAAK,QAAQ;oBAAE,GAAG,CAAC,IAAI,CA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,QAAQ,EAClE,CAAC;oBACD,WAAW,GAAG,IAAI,CAAA;gBACpB,CAAC;YACH,CAAC;YACD,IAAI,WAAW;gBAAE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACvC,CAAC;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAwB,EACxB,KAAgB,EAChB,MAAc;IAEd,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACtC,OAAO,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AACtC,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Outbound hooks: send, delete, reaction, members and message reads.
3
+ *
4
+ * Every write is one `PUT /rooms/{roomId}/send|redact/...` with a transaction id; `txtId` is derived from the caller's
5
+ * idempotency key when there is one (Matrix deduplicates a retransmission with the same id), else from a per-process
6
+ * random base plus a counter. Matrix returns only an `event_id`, so a published part carries no platform timestamp.
7
+ */
8
+ import type { ChatRef, InboundMessage, MembersPage, MessageHandle, MessageRef, SendContext, SentPart, Target } from '@deepseek-ai/cordis-plugin-im-core';
9
+ import type { RoomCache } from './rooms.ts';
10
+ import type { MatrixPayload } from './modes/shared.ts';
11
+ import type { MatrixClient } from './request.ts';
12
+ import type { ClientEvent } from './types.ts';
13
+ /** Everything the outbound hooks need from the adapter. */
14
+ export interface MatrixSendDeps {
15
+ readonly client: MatrixClient;
16
+ /** The bot's Matrix user id. */
17
+ readonly selfId: string;
18
+ /** A fresh transaction id (random process base plus a counter). */
19
+ nextTxn(): number;
20
+ readonly rooms: RoomCache;
21
+ /** Whether a user id is a bot of this process. */
22
+ isBot(userId: string): boolean;
23
+ /** Decode one event fetched by id for the requested chat. */
24
+ decode(chat: ChatRef, event: ClientEvent): InboundMessage;
25
+ }
26
+ /** The transaction id of one request: idempotency key first, counter otherwise. */
27
+ export declare function txnIdOf(deps: MatrixSendDeps, ctx: SendContext): string;
28
+ /**
29
+ * Whether a retry of one send is deduplicated by Matrix. Only a call with an idempotency key is: the spec returns the
30
+ * first answer for a retransmission of the same `(endpoint, txnId)` pair.
31
+ */
32
+ export declare function idempotentSend(_target: Target, ctx: SendContext): boolean;
33
+ /**
34
+ * Send one serialized payload.
35
+ * @param deps - adapter services.
36
+ * @param payload - the serialized payload.
37
+ * @param _target - the chat being published to (the payload already names it).
38
+ * @param ctx - core send context (signal, part index, idempotency key).
39
+ * @returns the published part.
40
+ */
41
+ export declare function sendPayload(deps: MatrixSendDeps, payload: MatrixPayload, _target: Target, ctx: SendContext): Promise<SentPart>;
42
+ /**
43
+ * Redact one published event.
44
+ * @param deps - adapter services.
45
+ * @param part - the part to delete.
46
+ * @param handle - the message the part belongs to (its target names the room).
47
+ */
48
+ export declare function deletePart(deps: MatrixSendDeps, part: SentPart, handle: MessageHandle): Promise<void>;
49
+ /**
50
+ * Add an annotation (`m.reaction`) to one message.
51
+ * @param deps - adapter services.
52
+ * @param message - the message to react to.
53
+ * @param emoji - the annotation key, passed through as written (`:shortcode:` included).
54
+ */
55
+ export declare function createReaction(deps: MatrixSendDeps, message: MessageRef, emoji: string): Promise<void>;
56
+ /**
57
+ * List the joined members of a room.
58
+ *
59
+ * The room state endpoint returns the whole member set at once, so there is no cursor; `limit` truncates the page.
60
+ *
61
+ * @param deps - adapter services.
62
+ * @param chat - the room.
63
+ * @param _cursor - ignored; the state read is one full page.
64
+ * @param limit - page size bound.
65
+ * @returns the joined members, each with the display name its membership event carried.
66
+ */
67
+ export declare function members(deps: MatrixSendDeps, chat: ChatRef, _cursor?: string, limit?: number): Promise<MembersPage>;
68
+ /**
69
+ * Read one message by id.
70
+ * @param deps - adapter services.
71
+ * @param chat - the chat the caller asked about; the decoded message reports it unchanged.
72
+ * @param id - the event id.
73
+ * @returns the decoded message.
74
+ */
75
+ export declare function getMessage(deps: MatrixSendDeps, chat: ChatRef, id: string): Promise<InboundMessage>;
76
+ //# sourceMappingURL=send.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../src/send.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,WAAW,EACX,aAAa,EACb,UAAU,EACV,WAAW,EACX,QAAQ,EACR,MAAM,EAEP,MAAM,oCAAoC,CAAA;AAI3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,KAAK,EACV,aAAa,EAGd,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,YAAY,CAAA;AAE5D,2DAA2D;AAC3D,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAA;IAC7B,gCAAgC;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,mEAAmE;IACnE,OAAO,IAAI,MAAM,CAAA;IACjB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;IACzB,kDAAkD;IAClD,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAA;IAC9B,6DAA6D;IAC7D,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,GAAG,cAAc,CAAA;CAC1D;AAED,mFAAmF;AACnF,wBAAgB,OAAO,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,EAAE,WAAW,GAAG,MAAM,CAStE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAEzE;AA6DD;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,MAAM,EACf,GAAG,EAA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