@theokit/gateway-matrix 0.5.0 → 0.7.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.
- package/CHANGELOG.md +19 -0
- package/README.md +23 -0
- package/dist/index.cjs +201 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +70 -5
- package/dist/index.d.ts +70 -5
- package/dist/index.js +202 -3
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { BasePlatformAdapter, OutboundMessage, SendResult, MessageEvent, GatewayConfigurationError, GatewayConfigurationErrorOptions, MatrixMessageEvent } from '@theokit/gateway';
|
|
2
2
|
|
|
3
|
+
interface InviteEventLike {
|
|
4
|
+
getSender(): string | undefined;
|
|
5
|
+
}
|
|
6
|
+
interface InviteRoomLike {
|
|
7
|
+
readonly roomId: string;
|
|
8
|
+
getMyMembership?(): string;
|
|
9
|
+
/** The inviter is the sender of the bot's own member event. */
|
|
10
|
+
getMember?(userId: string): {
|
|
11
|
+
events?: {
|
|
12
|
+
member?: InviteEventLike;
|
|
13
|
+
};
|
|
14
|
+
} | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
3
17
|
/**
|
|
4
18
|
* Public option types + minimal Matrix shape narrowing (kept local so
|
|
5
19
|
* we don't force `matrix-js-sdk` types on consumers).
|
|
@@ -34,6 +48,12 @@ interface MatrixAdapterOptions {
|
|
|
34
48
|
* did before this option existed.
|
|
35
49
|
*/
|
|
36
50
|
readonly requireMention?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Decide whether to join a room the bot is invited to. Called with the inviter's user id and the
|
|
53
|
+
* room id; return `true` to join. Without it the bot joins nothing, and an invite stays pending,
|
|
54
|
+
* so a stranger cannot pull the bot into a room. A host applies its owner and allowlist rules here.
|
|
55
|
+
*/
|
|
56
|
+
readonly acceptInvite?: (inviter: string, roomId: string) => boolean | Promise<boolean>;
|
|
37
57
|
}
|
|
38
58
|
/**
|
|
39
59
|
* Minimal subset of `MatrixEvent` we read. Real events from
|
|
@@ -117,10 +137,6 @@ interface MatrixClientLike {
|
|
|
117
137
|
*/
|
|
118
138
|
declare function subscribeToTimeline(client: MatrixClientLike, handler: (event: MatrixEventLike, room: MatrixRoomLike) => void, opts: SyncFilterOpts): TimelineSubscription;
|
|
119
139
|
|
|
120
|
-
/**
|
|
121
|
-
* Lazy `matrix-js-sdk` loader.
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
140
|
interface MatrixSdkClient extends MatrixClientLike {
|
|
125
141
|
startClient(opts: {
|
|
126
142
|
initialSyncLimit: number;
|
|
@@ -140,9 +156,18 @@ interface MatrixSdkClient extends MatrixClientLike {
|
|
|
140
156
|
sendMessage?(roomId: string, content: Record<string, unknown>): Promise<{
|
|
141
157
|
event_id: string;
|
|
142
158
|
}>;
|
|
159
|
+
/** Send a room event of `eventType` (used for `m.reaction`). */
|
|
160
|
+
sendEvent?(roomId: string, eventType: string, content: Record<string, unknown>): Promise<{
|
|
161
|
+
event_id: string;
|
|
162
|
+
}>;
|
|
163
|
+
/** `PUT /rooms/{roomId}/typing/{userId}`; present on matrix-js-sdk's client. */
|
|
164
|
+
sendTyping?(roomId: string, isTyping: boolean, timeoutMs: number): Promise<unknown>;
|
|
143
165
|
getRoomIdForAlias(alias: string): Promise<{
|
|
144
166
|
room_id: string;
|
|
145
167
|
}>;
|
|
168
|
+
/** Join a room (used to accept an invite). */
|
|
169
|
+
joinRoom?(roomId: string): Promise<unknown>;
|
|
170
|
+
getRooms?(): InviteRoomLike[];
|
|
146
171
|
getRoom(roomId: string): MatrixRoomLike | null;
|
|
147
172
|
isRoomEncrypted?(roomId: string): boolean;
|
|
148
173
|
/** Best-effort current user id; matrix-js-sdk returns `string | null`. */
|
|
@@ -170,6 +195,7 @@ declare class MatrixAdapter extends BasePlatformAdapter {
|
|
|
170
195
|
private readonly opts;
|
|
171
196
|
private client;
|
|
172
197
|
private subscription;
|
|
198
|
+
private unsubscribeInvites;
|
|
173
199
|
private inboundHandler;
|
|
174
200
|
private readonly aliasCache;
|
|
175
201
|
private connected;
|
|
@@ -211,6 +237,11 @@ declare class MatrixAdapter extends BasePlatformAdapter {
|
|
|
211
237
|
*/
|
|
212
238
|
editMessage(messageId: string, out: OutboundMessage): Promise<SendResult>;
|
|
213
239
|
private editText;
|
|
240
|
+
/** Matrix expires a typing notice after its timeout; the runner renews it while the turn runs. */
|
|
241
|
+
startTyping(channelId: string): Promise<void>;
|
|
242
|
+
stopTyping(channelId: string): Promise<void>;
|
|
243
|
+
private setTyping;
|
|
244
|
+
react(channelId: string, messageId: string, emoji: string): Promise<SendResult>;
|
|
214
245
|
/** Whether the "no markdown mode here" warning has already been emitted. */
|
|
215
246
|
private warnedAboutFormat;
|
|
216
247
|
onInbound(handler: (event: MessageEvent) => Promise<void>): () => void;
|
|
@@ -269,6 +300,40 @@ declare class AliasCache {
|
|
|
269
300
|
size(): number;
|
|
270
301
|
}
|
|
271
302
|
|
|
303
|
+
/**
|
|
304
|
+
* One configuration field, shaped like the core's `ChannelConfigField` (typed locally so a published
|
|
305
|
+
* `.d.ts` never names a core type an older peer release lacks), and its constructor.
|
|
306
|
+
*/
|
|
307
|
+
interface ConfigField {
|
|
308
|
+
readonly key: string;
|
|
309
|
+
readonly required: boolean;
|
|
310
|
+
readonly secret: boolean;
|
|
311
|
+
readonly description: string;
|
|
312
|
+
readonly env?: string;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* `matrixChannel` — the Matrix channel described as data.
|
|
317
|
+
*
|
|
318
|
+
* Typed locally on purpose: the object is structurally a `ChannelDescriptor`, and the integration
|
|
319
|
+
* package's typecheck is where a drift between the two fails.
|
|
320
|
+
*/
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The Matrix channel: a sync loop the adapter opens itself, so it needs no webhook route.
|
|
324
|
+
*
|
|
325
|
+
* `create` validates each value with this package's `ConfigurationError` and makes no network call.
|
|
326
|
+
*
|
|
327
|
+
* @public
|
|
328
|
+
*/
|
|
329
|
+
declare const matrixChannel: {
|
|
330
|
+
platform: "matrix";
|
|
331
|
+
transport: "socket";
|
|
332
|
+
senderIdentity: "authenticated";
|
|
333
|
+
config: readonly ConfigField[];
|
|
334
|
+
create(config: Readonly<Record<string, unknown>>): MatrixAdapter;
|
|
335
|
+
};
|
|
336
|
+
|
|
272
337
|
/**
|
|
273
338
|
* Typed errors for `@theokit/gateway-matrix`.
|
|
274
339
|
*
|
|
@@ -327,4 +392,4 @@ declare function matrixEventToMessageEvent(event: MatrixEventLike, room: MatrixR
|
|
|
327
392
|
|
|
328
393
|
declare function detectChannelType(room: MatrixRoomLike | undefined): "dm" | "group";
|
|
329
394
|
|
|
330
|
-
export { AliasCache, ConfigurationError, EncryptedRoomError, MatrixAdapter, type MatrixAdapterOptions, type MatrixClientLike, type MatrixEventLike, type MatrixRoomLike, SDKNotInstalledError, type SyncFilterOpts, type TimelineSubscription, detectChannelType, matrixEventToMessageEvent, shouldDispatchSyncEvent, subscribeToTimeline };
|
|
395
|
+
export { AliasCache, ConfigurationError, EncryptedRoomError, MatrixAdapter, type MatrixAdapterOptions, type MatrixClientLike, type MatrixEventLike, type MatrixRoomLike, SDKNotInstalledError, type SyncFilterOpts, type TimelineSubscription, detectChannelType, matrixChannel, matrixEventToMessageEvent, shouldDispatchSyncEvent, subscribeToTimeline };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { BasePlatformAdapter, OutboundMessage, SendResult, MessageEvent, GatewayConfigurationError, GatewayConfigurationErrorOptions, MatrixMessageEvent } from '@theokit/gateway';
|
|
2
2
|
|
|
3
|
+
interface InviteEventLike {
|
|
4
|
+
getSender(): string | undefined;
|
|
5
|
+
}
|
|
6
|
+
interface InviteRoomLike {
|
|
7
|
+
readonly roomId: string;
|
|
8
|
+
getMyMembership?(): string;
|
|
9
|
+
/** The inviter is the sender of the bot's own member event. */
|
|
10
|
+
getMember?(userId: string): {
|
|
11
|
+
events?: {
|
|
12
|
+
member?: InviteEventLike;
|
|
13
|
+
};
|
|
14
|
+
} | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
3
17
|
/**
|
|
4
18
|
* Public option types + minimal Matrix shape narrowing (kept local so
|
|
5
19
|
* we don't force `matrix-js-sdk` types on consumers).
|
|
@@ -34,6 +48,12 @@ interface MatrixAdapterOptions {
|
|
|
34
48
|
* did before this option existed.
|
|
35
49
|
*/
|
|
36
50
|
readonly requireMention?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Decide whether to join a room the bot is invited to. Called with the inviter's user id and the
|
|
53
|
+
* room id; return `true` to join. Without it the bot joins nothing, and an invite stays pending,
|
|
54
|
+
* so a stranger cannot pull the bot into a room. A host applies its owner and allowlist rules here.
|
|
55
|
+
*/
|
|
56
|
+
readonly acceptInvite?: (inviter: string, roomId: string) => boolean | Promise<boolean>;
|
|
37
57
|
}
|
|
38
58
|
/**
|
|
39
59
|
* Minimal subset of `MatrixEvent` we read. Real events from
|
|
@@ -117,10 +137,6 @@ interface MatrixClientLike {
|
|
|
117
137
|
*/
|
|
118
138
|
declare function subscribeToTimeline(client: MatrixClientLike, handler: (event: MatrixEventLike, room: MatrixRoomLike) => void, opts: SyncFilterOpts): TimelineSubscription;
|
|
119
139
|
|
|
120
|
-
/**
|
|
121
|
-
* Lazy `matrix-js-sdk` loader.
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
140
|
interface MatrixSdkClient extends MatrixClientLike {
|
|
125
141
|
startClient(opts: {
|
|
126
142
|
initialSyncLimit: number;
|
|
@@ -140,9 +156,18 @@ interface MatrixSdkClient extends MatrixClientLike {
|
|
|
140
156
|
sendMessage?(roomId: string, content: Record<string, unknown>): Promise<{
|
|
141
157
|
event_id: string;
|
|
142
158
|
}>;
|
|
159
|
+
/** Send a room event of `eventType` (used for `m.reaction`). */
|
|
160
|
+
sendEvent?(roomId: string, eventType: string, content: Record<string, unknown>): Promise<{
|
|
161
|
+
event_id: string;
|
|
162
|
+
}>;
|
|
163
|
+
/** `PUT /rooms/{roomId}/typing/{userId}`; present on matrix-js-sdk's client. */
|
|
164
|
+
sendTyping?(roomId: string, isTyping: boolean, timeoutMs: number): Promise<unknown>;
|
|
143
165
|
getRoomIdForAlias(alias: string): Promise<{
|
|
144
166
|
room_id: string;
|
|
145
167
|
}>;
|
|
168
|
+
/** Join a room (used to accept an invite). */
|
|
169
|
+
joinRoom?(roomId: string): Promise<unknown>;
|
|
170
|
+
getRooms?(): InviteRoomLike[];
|
|
146
171
|
getRoom(roomId: string): MatrixRoomLike | null;
|
|
147
172
|
isRoomEncrypted?(roomId: string): boolean;
|
|
148
173
|
/** Best-effort current user id; matrix-js-sdk returns `string | null`. */
|
|
@@ -170,6 +195,7 @@ declare class MatrixAdapter extends BasePlatformAdapter {
|
|
|
170
195
|
private readonly opts;
|
|
171
196
|
private client;
|
|
172
197
|
private subscription;
|
|
198
|
+
private unsubscribeInvites;
|
|
173
199
|
private inboundHandler;
|
|
174
200
|
private readonly aliasCache;
|
|
175
201
|
private connected;
|
|
@@ -211,6 +237,11 @@ declare class MatrixAdapter extends BasePlatformAdapter {
|
|
|
211
237
|
*/
|
|
212
238
|
editMessage(messageId: string, out: OutboundMessage): Promise<SendResult>;
|
|
213
239
|
private editText;
|
|
240
|
+
/** Matrix expires a typing notice after its timeout; the runner renews it while the turn runs. */
|
|
241
|
+
startTyping(channelId: string): Promise<void>;
|
|
242
|
+
stopTyping(channelId: string): Promise<void>;
|
|
243
|
+
private setTyping;
|
|
244
|
+
react(channelId: string, messageId: string, emoji: string): Promise<SendResult>;
|
|
214
245
|
/** Whether the "no markdown mode here" warning has already been emitted. */
|
|
215
246
|
private warnedAboutFormat;
|
|
216
247
|
onInbound(handler: (event: MessageEvent) => Promise<void>): () => void;
|
|
@@ -269,6 +300,40 @@ declare class AliasCache {
|
|
|
269
300
|
size(): number;
|
|
270
301
|
}
|
|
271
302
|
|
|
303
|
+
/**
|
|
304
|
+
* One configuration field, shaped like the core's `ChannelConfigField` (typed locally so a published
|
|
305
|
+
* `.d.ts` never names a core type an older peer release lacks), and its constructor.
|
|
306
|
+
*/
|
|
307
|
+
interface ConfigField {
|
|
308
|
+
readonly key: string;
|
|
309
|
+
readonly required: boolean;
|
|
310
|
+
readonly secret: boolean;
|
|
311
|
+
readonly description: string;
|
|
312
|
+
readonly env?: string;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* `matrixChannel` — the Matrix channel described as data.
|
|
317
|
+
*
|
|
318
|
+
* Typed locally on purpose: the object is structurally a `ChannelDescriptor`, and the integration
|
|
319
|
+
* package's typecheck is where a drift between the two fails.
|
|
320
|
+
*/
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The Matrix channel: a sync loop the adapter opens itself, so it needs no webhook route.
|
|
324
|
+
*
|
|
325
|
+
* `create` validates each value with this package's `ConfigurationError` and makes no network call.
|
|
326
|
+
*
|
|
327
|
+
* @public
|
|
328
|
+
*/
|
|
329
|
+
declare const matrixChannel: {
|
|
330
|
+
platform: "matrix";
|
|
331
|
+
transport: "socket";
|
|
332
|
+
senderIdentity: "authenticated";
|
|
333
|
+
config: readonly ConfigField[];
|
|
334
|
+
create(config: Readonly<Record<string, unknown>>): MatrixAdapter;
|
|
335
|
+
};
|
|
336
|
+
|
|
272
337
|
/**
|
|
273
338
|
* Typed errors for `@theokit/gateway-matrix`.
|
|
274
339
|
*
|
|
@@ -327,4 +392,4 @@ declare function matrixEventToMessageEvent(event: MatrixEventLike, room: MatrixR
|
|
|
327
392
|
|
|
328
393
|
declare function detectChannelType(room: MatrixRoomLike | undefined): "dm" | "group";
|
|
329
394
|
|
|
330
|
-
export { AliasCache, ConfigurationError, EncryptedRoomError, MatrixAdapter, type MatrixAdapterOptions, type MatrixClientLike, type MatrixEventLike, type MatrixRoomLike, SDKNotInstalledError, type SyncFilterOpts, type TimelineSubscription, detectChannelType, matrixEventToMessageEvent, shouldDispatchSyncEvent, subscribeToTimeline };
|
|
395
|
+
export { AliasCache, ConfigurationError, EncryptedRoomError, MatrixAdapter, type MatrixAdapterOptions, type MatrixClientLike, type MatrixEventLike, type MatrixRoomLike, SDKNotInstalledError, type SyncFilterOpts, type TimelineSubscription, detectChannelType, matrixChannel, matrixEventToMessageEvent, shouldDispatchSyncEvent, subscribeToTimeline };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GatewayConfigurationError, BasePlatformAdapter } from '@theokit/gateway';
|
|
1
|
+
import { channelConfigReaders, GatewayConfigurationError, BasePlatformAdapter } from '@theokit/gateway';
|
|
2
2
|
|
|
3
3
|
// src/adapter.ts
|
|
4
4
|
var ConfigurationError = class extends GatewayConfigurationError {
|
|
@@ -63,6 +63,49 @@ async function loadMatrixSdk() {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
// src/invite.ts
|
|
67
|
+
function warn(msg) {
|
|
68
|
+
process.stderr.write(`[gateway-matrix] ${msg}
|
|
69
|
+
`);
|
|
70
|
+
}
|
|
71
|
+
async function handleInvite(client, opts, inviter, roomId, seen) {
|
|
72
|
+
if (seen.has(roomId)) return;
|
|
73
|
+
seen.add(roomId);
|
|
74
|
+
try {
|
|
75
|
+
if (!await opts.acceptInvite(inviter, roomId)) return;
|
|
76
|
+
await client.joinRoom(roomId);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
seen.delete(roomId);
|
|
79
|
+
warn(`join of ${roomId} failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function subscribeToInvites(client, opts) {
|
|
83
|
+
const seen = /* @__PURE__ */ new Set();
|
|
84
|
+
const run = (inviter, roomId) => {
|
|
85
|
+
void handleInvite(client, opts, inviter, roomId, seen).catch((err) => {
|
|
86
|
+
warn(`invite handling failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
const onMember = (event, member) => {
|
|
90
|
+
if (member.userId !== opts.botUserId || member.membership !== "invite") return;
|
|
91
|
+
run(event.getSender() ?? "", member.roomId);
|
|
92
|
+
};
|
|
93
|
+
const scanPending = () => {
|
|
94
|
+
for (const room of client.getRooms?.() ?? []) {
|
|
95
|
+
if (room.getMyMembership?.() !== "invite") continue;
|
|
96
|
+
const inviter = room.getMember?.(opts.botUserId)?.events?.member?.getSender() ?? "";
|
|
97
|
+
run(inviter, room.roomId);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
client.on("RoomMember.membership", onMember);
|
|
101
|
+
client.on("sync", scanPending);
|
|
102
|
+
scanPending();
|
|
103
|
+
return () => {
|
|
104
|
+
client.off("RoomMember.membership", onMember);
|
|
105
|
+
client.off("sync", scanPending);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
66
109
|
// src/room-state.ts
|
|
67
110
|
function detectChannelType(room) {
|
|
68
111
|
if (room === void 0) return "group";
|
|
@@ -148,6 +191,7 @@ var MatrixAdapter = class extends BasePlatformAdapter {
|
|
|
148
191
|
opts;
|
|
149
192
|
client;
|
|
150
193
|
subscription;
|
|
194
|
+
unsubscribeInvites;
|
|
151
195
|
inboundHandler;
|
|
152
196
|
aliasCache = new AliasCache();
|
|
153
197
|
connected = false;
|
|
@@ -219,7 +263,11 @@ var MatrixAdapter = class extends BasePlatformAdapter {
|
|
|
219
263
|
const msg = err instanceof Error ? err.message : String(err);
|
|
220
264
|
process.stderr.write(`[gateway-matrix] connect failed: ${msg}
|
|
221
265
|
`);
|
|
222
|
-
|
|
266
|
+
const rest = err;
|
|
267
|
+
if (rest?.httpStatus === 401 || rest?.httpStatus === 403 || rest?.errcode === "M_UNKNOWN_TOKEN") {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
throw err;
|
|
223
271
|
}
|
|
224
272
|
}
|
|
225
273
|
async disconnect() {
|
|
@@ -227,6 +275,8 @@ var MatrixAdapter = class extends BasePlatformAdapter {
|
|
|
227
275
|
this.tearingDown = true;
|
|
228
276
|
this.subscription?.unsubscribe();
|
|
229
277
|
this.subscription = void 0;
|
|
278
|
+
this.unsubscribeInvites?.();
|
|
279
|
+
this.unsubscribeInvites = void 0;
|
|
230
280
|
this.client?.stopClient();
|
|
231
281
|
this.client = void 0;
|
|
232
282
|
this.connected = false;
|
|
@@ -324,6 +374,52 @@ var MatrixAdapter = class extends BasePlatformAdapter {
|
|
|
324
374
|
}
|
|
325
375
|
return { ok: true, messageId };
|
|
326
376
|
}
|
|
377
|
+
/** Matrix expires a typing notice after its timeout; the runner renews it while the turn runs. */
|
|
378
|
+
async startTyping(channelId) {
|
|
379
|
+
await this.setTyping(channelId, true);
|
|
380
|
+
}
|
|
381
|
+
async stopTyping(channelId) {
|
|
382
|
+
await this.setTyping(channelId, false);
|
|
383
|
+
}
|
|
384
|
+
async setTyping(channelId, typing) {
|
|
385
|
+
const client = this.client;
|
|
386
|
+
if (channelId.length === 0 || client?.sendTyping === void 0) return;
|
|
387
|
+
try {
|
|
388
|
+
const roomId = await this.aliasCache.resolve(client, channelId);
|
|
389
|
+
await client.sendTyping(roomId, typing, 1e4);
|
|
390
|
+
} catch {
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async react(channelId, messageId, emoji) {
|
|
394
|
+
if (channelId.length === 0 || messageId.length === 0 || emoji.length === 0) {
|
|
395
|
+
return {
|
|
396
|
+
ok: false,
|
|
397
|
+
error: { code: "invalid_reaction", message: "room, event id and emoji are required" }
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
const client = this.client;
|
|
401
|
+
if (client === void 0) {
|
|
402
|
+
return {
|
|
403
|
+
ok: false,
|
|
404
|
+
error: { code: "not_connected", message: "MatrixAdapter.connect() not called" }
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
if (client.sendEvent === void 0) {
|
|
408
|
+
return {
|
|
409
|
+
ok: false,
|
|
410
|
+
error: { code: "react_unsupported", message: "matrix client has no sendEvent" }
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
const roomId = await this.aliasCache.resolve(client, channelId);
|
|
415
|
+
await client.sendEvent(roomId, "m.reaction", {
|
|
416
|
+
"m.relates_to": { rel_type: "m.annotation", event_id: messageId, key: emoji }
|
|
417
|
+
});
|
|
418
|
+
} catch (err) {
|
|
419
|
+
return mapMatrixError(err);
|
|
420
|
+
}
|
|
421
|
+
return { ok: true, messageId };
|
|
422
|
+
}
|
|
327
423
|
/**
|
|
328
424
|
* Send `out`, letting the caller's declared `format` decide the shape.
|
|
329
425
|
*
|
|
@@ -410,6 +506,14 @@ var MatrixAdapter = class extends BasePlatformAdapter {
|
|
|
410
506
|
botUserId: this.opts.userId,
|
|
411
507
|
freshnessWindowMs: this.opts.freshnessWindowMs ?? DEFAULT_FRESHNESS_MS
|
|
412
508
|
};
|
|
509
|
+
const acceptInvite = this.opts.acceptInvite;
|
|
510
|
+
if (acceptInvite !== void 0 && client.joinRoom !== void 0) {
|
|
511
|
+
this.unsubscribeInvites?.();
|
|
512
|
+
this.unsubscribeInvites = subscribeToInvites(
|
|
513
|
+
client,
|
|
514
|
+
{ botUserId: this.opts.userId, acceptInvite }
|
|
515
|
+
);
|
|
516
|
+
}
|
|
413
517
|
this.subscription = subscribeToTimeline(
|
|
414
518
|
client,
|
|
415
519
|
(ev, room) => {
|
|
@@ -522,6 +626,101 @@ function mapMatrixError(err) {
|
|
|
522
626
|
};
|
|
523
627
|
}
|
|
524
628
|
|
|
525
|
-
|
|
629
|
+
// ../../shared/channel-config-field.ts
|
|
630
|
+
var field = (key, env, description, { required = true, secret = false } = {}) => ({ key, required, secret, env, description });
|
|
631
|
+
|
|
632
|
+
// ../../shared/env-value-readers.ts
|
|
633
|
+
function readEnvNumber(invalid2, config, key) {
|
|
634
|
+
const value = config[key];
|
|
635
|
+
if (value === void 0) return void 0;
|
|
636
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
637
|
+
if (typeof value === "string" && /^[0-9]+$/.test(value.trim())) {
|
|
638
|
+
const parsed = Number(value.trim());
|
|
639
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
640
|
+
}
|
|
641
|
+
throw invalid2(key, "a number or a digit string");
|
|
642
|
+
}
|
|
643
|
+
function readEnvBoolean(invalid2, config, key) {
|
|
644
|
+
const value = config[key];
|
|
645
|
+
if (value === void 0) return void 0;
|
|
646
|
+
if (value === true || value === "true") return true;
|
|
647
|
+
if (value === false || value === "false") return false;
|
|
648
|
+
throw invalid2(key, 'a boolean or the string "true" or "false"');
|
|
649
|
+
}
|
|
650
|
+
function envValueReaders(invalid2) {
|
|
651
|
+
return {
|
|
652
|
+
number: (config, key) => readEnvNumber(invalid2, config, key),
|
|
653
|
+
boolean: (config, key) => readEnvBoolean(invalid2, config, key)
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// src/channel.ts
|
|
658
|
+
var readers = channelConfigReaders(
|
|
659
|
+
"gateway-matrix",
|
|
660
|
+
(options) => new ConfigurationError(options)
|
|
661
|
+
);
|
|
662
|
+
var { invalid } = readers;
|
|
663
|
+
var isAbsent = (value) => value === void 0;
|
|
664
|
+
var read = {
|
|
665
|
+
...envValueReaders(invalid),
|
|
666
|
+
string: readers.requiredString
|
|
667
|
+
};
|
|
668
|
+
function readAcceptInvite(config) {
|
|
669
|
+
const value = config.acceptInvite;
|
|
670
|
+
if (isAbsent(value)) return void 0;
|
|
671
|
+
if (typeof value !== "function") throw invalid("acceptInvite", "a function");
|
|
672
|
+
return value;
|
|
673
|
+
}
|
|
674
|
+
var CONFIG = [
|
|
675
|
+
field(
|
|
676
|
+
"homeserverUrl",
|
|
677
|
+
"MATRIX_HOMESERVER_URL",
|
|
678
|
+
"Base URL of the Matrix homeserver, e.g. https://matrix.example.org."
|
|
679
|
+
),
|
|
680
|
+
field("accessToken", "MATRIX_ACCESS_TOKEN", "Access token of the bot account.", { secret: true }),
|
|
681
|
+
field("userId", "MATRIX_USER_ID", "Matrix user id of the bot, e.g. @bot:example.org."),
|
|
682
|
+
field(
|
|
683
|
+
"freshnessWindowMs",
|
|
684
|
+
"MATRIX_FRESHNESS_WINDOW_MS",
|
|
685
|
+
"Events older than this many milliseconds are dropped.",
|
|
686
|
+
{ required: false }
|
|
687
|
+
),
|
|
688
|
+
field(
|
|
689
|
+
"requireMention",
|
|
690
|
+
"MATRIX_REQUIRE_MENTION",
|
|
691
|
+
"When true, group messages reach the handler only if they mention the bot.",
|
|
692
|
+
{ required: false }
|
|
693
|
+
),
|
|
694
|
+
{
|
|
695
|
+
key: "acceptInvite",
|
|
696
|
+
required: false,
|
|
697
|
+
secret: false,
|
|
698
|
+
description: "Programmatic callback deciding whether to accept a room invite; no env binding."
|
|
699
|
+
}
|
|
700
|
+
];
|
|
701
|
+
var matrixChannel = {
|
|
702
|
+
platform: "matrix",
|
|
703
|
+
transport: "socket",
|
|
704
|
+
senderIdentity: "authenticated",
|
|
705
|
+
config: CONFIG,
|
|
706
|
+
create(config) {
|
|
707
|
+
const homeserverUrl = read.string(config, "homeserverUrl");
|
|
708
|
+
const accessToken = read.string(config, "accessToken");
|
|
709
|
+
const userId = read.string(config, "userId");
|
|
710
|
+
const freshnessWindowMs = read.number(config, "freshnessWindowMs");
|
|
711
|
+
const requireMention = read.boolean(config, "requireMention");
|
|
712
|
+
const acceptInvite = readAcceptInvite(config);
|
|
713
|
+
return new MatrixAdapter({
|
|
714
|
+
homeserverUrl,
|
|
715
|
+
accessToken,
|
|
716
|
+
userId,
|
|
717
|
+
...freshnessWindowMs === void 0 ? {} : { freshnessWindowMs },
|
|
718
|
+
...requireMention === void 0 ? {} : { requireMention },
|
|
719
|
+
...acceptInvite === void 0 ? {} : { acceptInvite }
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
export { AliasCache, ConfigurationError, EncryptedRoomError, MatrixAdapter, SDKNotInstalledError, detectChannelType, matrixChannel, matrixEventToMessageEvent, shouldDispatchSyncEvent, subscribeToTimeline };
|
|
526
725
|
//# sourceMappingURL=index.js.map
|
|
527
726
|
//# sourceMappingURL=index.js.map
|