@colibri-social/bridge-core 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.
- package/README.md +19 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/auth.d.ts +12 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +29 -0
- package/dist/auth.js.map +1 -0
- package/dist/bridge.d.ts +102 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +1209 -0
- package/dist/bridge.js.map +1 -0
- package/dist/client.d.ts +186 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +133 -0
- package/dist/client.js.map +1 -0
- package/dist/connector.d.ts +202 -0
- package/dist/connector.d.ts.map +1 -0
- package/dist/connector.js +12 -0
- package/dist/connector.js.map +1 -0
- package/dist/events.d.ts +32 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +102 -0
- package/dist/events.js.map +1 -0
- package/dist/identity.d.ts +12 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +13 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +47 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +742 -0
- package/dist/markdown.js.map +1 -0
- package/dist/store.d.ts +93 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +165 -0
- package/dist/store.js.map +1 -0
- package/dist/text.d.ts +18 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +58 -0
- package/dist/text.js.map +1 -0
- package/package.json +46 -0
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,1209 @@
|
|
|
1
|
+
import { ColibriRequestError, } from "./client.js";
|
|
2
|
+
import { canDelete, canEdit, canImportHistory, canModerate, canReact, canThread, } from "./connector.js";
|
|
3
|
+
import { EventStream, frameKind } from "./events.js";
|
|
4
|
+
import { colibriToMarkdown, markdownToColibri, } from "./text.js";
|
|
5
|
+
const DEFAULT_MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024;
|
|
6
|
+
const DEFAULT_BACKFILL_BATCH_SIZE = 25;
|
|
7
|
+
const DEFAULT_BACKFILL_REPORT_INTERVAL_MS = 5000;
|
|
8
|
+
const BACKFILL_RETRIES = 8;
|
|
9
|
+
const INVALID_HANDLE = "handle.invalid";
|
|
10
|
+
const MAX_THREAD_NAME = 128;
|
|
11
|
+
const FALLBACK_THREAD_NAME = "Thread";
|
|
12
|
+
const CHANNEL_FEATURE = "social.colibri.beta.richtext.facet#channel";
|
|
13
|
+
const BRIDGED_MENTION_FEATURE = "social.colibri.beta.richtext.facet#bridgedMention";
|
|
14
|
+
const skeyOf = (space) => space.split("/").at(-1) ?? space;
|
|
15
|
+
const threadName = (name) => [...name.trim()].slice(0, MAX_THREAD_NAME).join("") || FALLBACK_THREAD_NAME;
|
|
16
|
+
const quoted = (markdown) => markdown
|
|
17
|
+
.split("\n")
|
|
18
|
+
.map((line) => `> ${line}`)
|
|
19
|
+
.join("\n");
|
|
20
|
+
const silentLogger = {
|
|
21
|
+
debug: () => { },
|
|
22
|
+
info: () => { },
|
|
23
|
+
warn: () => { },
|
|
24
|
+
error: () => { },
|
|
25
|
+
};
|
|
26
|
+
export const registrationKey = (community, registration) => `${community} ${registration}`;
|
|
27
|
+
export const colibriRecordKey = (key) => `${key.channel} ${key.did} ${key.rkey}`;
|
|
28
|
+
export const parseColibriRecordKey = (value) => {
|
|
29
|
+
const [channel, did, rkey] = value.split(" ");
|
|
30
|
+
return channel && did && rkey ? { channel, did, rkey } : null;
|
|
31
|
+
};
|
|
32
|
+
const remoteLocationKey = (remoteSpace, remoteRoom) => `${remoteSpace} ${remoteRoom}`;
|
|
33
|
+
class BackfillStopped extends Error {
|
|
34
|
+
}
|
|
35
|
+
class SerialQueues {
|
|
36
|
+
tails = new Map();
|
|
37
|
+
run(key, task) {
|
|
38
|
+
const next = (this.tails.get(key) ?? Promise.resolve()).then(task, task);
|
|
39
|
+
const settled = next.catch(() => undefined);
|
|
40
|
+
this.tails.set(key, settled);
|
|
41
|
+
void settled.then(() => {
|
|
42
|
+
if (this.tails.get(key) === settled)
|
|
43
|
+
this.tails.delete(key);
|
|
44
|
+
});
|
|
45
|
+
return settled;
|
|
46
|
+
}
|
|
47
|
+
async drain() {
|
|
48
|
+
await Promise.all([...this.tails.values()]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class Bridge {
|
|
52
|
+
options;
|
|
53
|
+
log;
|
|
54
|
+
queues = new SerialQueues();
|
|
55
|
+
stream;
|
|
56
|
+
sleep;
|
|
57
|
+
registrations = [];
|
|
58
|
+
byChannel = new Map();
|
|
59
|
+
byRemote = new Map();
|
|
60
|
+
threads = new Map();
|
|
61
|
+
backfills = new Map();
|
|
62
|
+
refreshing = null;
|
|
63
|
+
streamOpened = false;
|
|
64
|
+
stopped = false;
|
|
65
|
+
constructor(options) {
|
|
66
|
+
this.options = options;
|
|
67
|
+
this.log = options.log ?? silentLogger;
|
|
68
|
+
this.sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
69
|
+
const streamOptions = {
|
|
70
|
+
client: options.client,
|
|
71
|
+
onFrame: (frame) => this.onFrame(frame),
|
|
72
|
+
onOpen: () => this.onStreamOpen(),
|
|
73
|
+
onClose: (reason) => this.log.warn({ reason }, "bridge.stream.closed"),
|
|
74
|
+
};
|
|
75
|
+
this.stream = options.createEventStream
|
|
76
|
+
? options.createEventStream(streamOptions)
|
|
77
|
+
: new EventStream(streamOptions);
|
|
78
|
+
}
|
|
79
|
+
get context() {
|
|
80
|
+
return {
|
|
81
|
+
did: this.options.client.did,
|
|
82
|
+
log: this.log,
|
|
83
|
+
emit: (event) => this.emit(event),
|
|
84
|
+
pair: (remote) => this.options.client.createPairing({ platform: this.platform, ...remote }),
|
|
85
|
+
linkedRooms: (remoteSpace) => [...this.byRemote.values()]
|
|
86
|
+
.flat()
|
|
87
|
+
.filter((portal) => portal.remoteSpace === remoteSpace)
|
|
88
|
+
.map(({ community, registration, channel, remoteRoom, remoteName }) => ({
|
|
89
|
+
community,
|
|
90
|
+
registration,
|
|
91
|
+
channel,
|
|
92
|
+
remoteRoom,
|
|
93
|
+
remoteName,
|
|
94
|
+
})),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
get platform() {
|
|
98
|
+
return this.options.connector.platform;
|
|
99
|
+
}
|
|
100
|
+
async start() {
|
|
101
|
+
this.stopped = false;
|
|
102
|
+
await this.options.connector.start(this.context);
|
|
103
|
+
await this.loadWhenReachable();
|
|
104
|
+
this.stream.start();
|
|
105
|
+
}
|
|
106
|
+
async stop() {
|
|
107
|
+
this.stopped = true;
|
|
108
|
+
this.stream.stop();
|
|
109
|
+
await Promise.all([...this.backfills.values()]);
|
|
110
|
+
await this.queues.drain();
|
|
111
|
+
await this.options.connector.stop();
|
|
112
|
+
}
|
|
113
|
+
async idle() {
|
|
114
|
+
await this.refreshing;
|
|
115
|
+
await Promise.all([...this.backfills.values()]);
|
|
116
|
+
await this.queues.drain();
|
|
117
|
+
}
|
|
118
|
+
async loadWhenReachable() {
|
|
119
|
+
for (let attempt = 0; !this.stopped; attempt++) {
|
|
120
|
+
try {
|
|
121
|
+
await this.refresh();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
const delay = Math.min(1000 * 2 ** attempt, 30_000);
|
|
126
|
+
this.log.warn({ error: error instanceof Error ? error.message : String(error), retryInMs: delay }, "bridge.configuration.unavailable");
|
|
127
|
+
await this.sleep(delay);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
onStreamOpen() {
|
|
132
|
+
this.log.info({}, "bridge.stream.open");
|
|
133
|
+
if (this.streamOpened) {
|
|
134
|
+
void this.refresh().catch((error) => this.log.warn({ error: String(error) }, "bridge.refreshFailed"));
|
|
135
|
+
}
|
|
136
|
+
this.streamOpened = true;
|
|
137
|
+
}
|
|
138
|
+
refresh() {
|
|
139
|
+
this.refreshing ??= this.loadConfiguration().finally(() => {
|
|
140
|
+
this.refreshing = null;
|
|
141
|
+
});
|
|
142
|
+
return this.refreshing;
|
|
143
|
+
}
|
|
144
|
+
async loadConfiguration() {
|
|
145
|
+
const all = await this.options.client.getConfiguration();
|
|
146
|
+
this.registrations = all.filter((registration) => registration.platform === this.platform);
|
|
147
|
+
const byChannel = new Map();
|
|
148
|
+
const byRemote = new Map();
|
|
149
|
+
for (const registration of this.registrations) {
|
|
150
|
+
if (!registration.enabled)
|
|
151
|
+
continue;
|
|
152
|
+
for (const link of registration.links) {
|
|
153
|
+
const portal = {
|
|
154
|
+
key: `${registrationKey(registration.community, registration.id)} ${link.channel}`,
|
|
155
|
+
community: registration.community,
|
|
156
|
+
registration: registration.id,
|
|
157
|
+
channel: link.channel,
|
|
158
|
+
parentChannel: link.channel,
|
|
159
|
+
remoteRoom: link.remoteRoom,
|
|
160
|
+
remoteName: link.remoteName,
|
|
161
|
+
remoteSpace: registration.remoteSpace,
|
|
162
|
+
platform: registration.platform,
|
|
163
|
+
mirrorModeration: registration.mirrorModeration ?? false,
|
|
164
|
+
links: registration.links,
|
|
165
|
+
...(link.backfill
|
|
166
|
+
? {
|
|
167
|
+
backfill: {
|
|
168
|
+
...(link.backfill.since ? { since: link.backfill.since } : {}),
|
|
169
|
+
requestedAt: link.backfill.requestedAt,
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
: {}),
|
|
173
|
+
};
|
|
174
|
+
byChannel.set(link.channel, [...(byChannel.get(link.channel) ?? []), portal]);
|
|
175
|
+
const remote = remoteLocationKey(registration.remoteSpace, link.remoteRoom);
|
|
176
|
+
byRemote.set(remote, [...(byRemote.get(remote) ?? []), portal]);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const threads = new Map();
|
|
180
|
+
for (const registration of this.registrations) {
|
|
181
|
+
if (!registration.enabled)
|
|
182
|
+
continue;
|
|
183
|
+
const mappings = await this.options.store.list({
|
|
184
|
+
registration: registrationKey(registration.community, registration.id),
|
|
185
|
+
kind: "thread",
|
|
186
|
+
});
|
|
187
|
+
for (const mapping of mappings) {
|
|
188
|
+
const parent = byChannel
|
|
189
|
+
.get(mapping.channel)
|
|
190
|
+
?.find((portal) => portal.registration === registration.id);
|
|
191
|
+
if (!parent)
|
|
192
|
+
continue;
|
|
193
|
+
threads.set(mapping.rkey, [
|
|
194
|
+
...(threads.get(mapping.rkey) ?? []),
|
|
195
|
+
this.threadPortal(parent, mapping.rkey, mapping.remoteId),
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
this.byChannel = byChannel;
|
|
200
|
+
this.byRemote = byRemote;
|
|
201
|
+
this.threads = threads;
|
|
202
|
+
this.subscribe();
|
|
203
|
+
this.startBackfills();
|
|
204
|
+
for (const registration of this.registrations) {
|
|
205
|
+
await this.pushRooms(registration).catch((error) => this.log.warn({ registration: registration.id, error: String(error) }, "bridge.rooms.pushFailed"));
|
|
206
|
+
}
|
|
207
|
+
this.log.info({ registrations: this.registrations.length, channels: byChannel.size }, "bridge.configured");
|
|
208
|
+
}
|
|
209
|
+
async pushRooms(registration) {
|
|
210
|
+
const rooms = await this.options.connector.listRooms(registration.remoteSpace);
|
|
211
|
+
await this.options.client.putRemoteRooms({ community: registration.community, registration: registration.id }, rooms.map((room) => ({
|
|
212
|
+
id: room.id,
|
|
213
|
+
name: room.name,
|
|
214
|
+
...(room.kind ? { kind: room.kind } : {}),
|
|
215
|
+
...(room.parent ? { parent: room.parent } : {}),
|
|
216
|
+
})));
|
|
217
|
+
}
|
|
218
|
+
subscribe() {
|
|
219
|
+
this.stream.setChannels([...this.byChannel.keys(), ...this.threads.keys()]);
|
|
220
|
+
}
|
|
221
|
+
threadPortal(parent, space, remoteThread) {
|
|
222
|
+
return { ...parent, channel: space, parentChannel: parent.channel, remoteThread };
|
|
223
|
+
}
|
|
224
|
+
addThread(parent, space, remoteThread) {
|
|
225
|
+
const held = this.threads.get(space) ?? [];
|
|
226
|
+
const existing = held.find((portal) => portal.key === parent.key);
|
|
227
|
+
if (existing)
|
|
228
|
+
return existing;
|
|
229
|
+
const portal = this.threadPortal(parent, space, remoteThread);
|
|
230
|
+
this.threads.set(space, [...held, portal]);
|
|
231
|
+
this.subscribe();
|
|
232
|
+
return portal;
|
|
233
|
+
}
|
|
234
|
+
dropThread(space, portal) {
|
|
235
|
+
const rest = (this.threads.get(space) ?? []).filter((held) => held.key !== portal.key);
|
|
236
|
+
if (rest.length > 0)
|
|
237
|
+
this.threads.set(space, rest);
|
|
238
|
+
else
|
|
239
|
+
this.threads.delete(space);
|
|
240
|
+
this.subscribe();
|
|
241
|
+
}
|
|
242
|
+
portalsFor(space) {
|
|
243
|
+
return this.byChannel.get(space) ?? this.threads.get(space) ?? [];
|
|
244
|
+
}
|
|
245
|
+
location(portal) {
|
|
246
|
+
return {
|
|
247
|
+
remoteSpace: portal.remoteSpace,
|
|
248
|
+
remoteRoom: portal.remoteRoom,
|
|
249
|
+
...(portal.remoteThread ? { remoteThread: portal.remoteThread } : {}),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
resolver(portal) {
|
|
253
|
+
return (reference) => {
|
|
254
|
+
if (reference.kind === "room") {
|
|
255
|
+
const link = portal.links.find((held) => held.remoteRoom === reference.id);
|
|
256
|
+
return link ? { $type: CHANNEL_FEATURE, channel: skeyOf(link.channel) } : null;
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
$type: BRIDGED_MENTION_FEATURE,
|
|
260
|
+
registration: portal.registration,
|
|
261
|
+
platform: portal.platform,
|
|
262
|
+
remoteId: reference.id,
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
referrer(portal) {
|
|
267
|
+
return (feature) => {
|
|
268
|
+
const value = feature;
|
|
269
|
+
if (value.$type === CHANNEL_FEATURE) {
|
|
270
|
+
const link = portal.links.find((held) => skeyOf(held.channel) === value.channel);
|
|
271
|
+
return link ? { kind: "room", id: link.remoteRoom } : null;
|
|
272
|
+
}
|
|
273
|
+
if (value.$type === BRIDGED_MENTION_FEATURE && value.registration === portal.registration) {
|
|
274
|
+
return { kind: "user", id: String(value.remoteId) };
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
emit(event) {
|
|
280
|
+
if (event.type === "roomsChanged") {
|
|
281
|
+
for (const registration of this.registrations) {
|
|
282
|
+
if (registration.remoteSpace !== event.remoteSpace)
|
|
283
|
+
continue;
|
|
284
|
+
void this.pushRooms(registration).catch((error) => this.log.warn({ error: String(error) }, "bridge.rooms.pushFailed"));
|
|
285
|
+
}
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
for (const portal of this.byRemote.get(remoteLocationKey(event.remoteSpace, event.remoteRoom)) ?? []) {
|
|
289
|
+
void this.queues.run(portal.key, () => this.guard(portal, event.type, () => this.inbound(portal, event)));
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
onFrame(frame) {
|
|
293
|
+
switch (frameKind(frame)) {
|
|
294
|
+
case "bridgeEvent":
|
|
295
|
+
void this.refresh().catch((error) => this.log.warn({ error: String(error) }, "bridge.refreshFailed"));
|
|
296
|
+
return;
|
|
297
|
+
case "messageEvent":
|
|
298
|
+
this.onMessageFrame(frame);
|
|
299
|
+
return;
|
|
300
|
+
case "reactionEvent":
|
|
301
|
+
this.onReactionFrame(frame);
|
|
302
|
+
return;
|
|
303
|
+
case "threadEvent":
|
|
304
|
+
this.onThreadFrame(frame);
|
|
305
|
+
return;
|
|
306
|
+
case "labelEvent":
|
|
307
|
+
this.onLabelFrame(frame);
|
|
308
|
+
return;
|
|
309
|
+
case "error":
|
|
310
|
+
this.log.warn({ frame }, "bridge.stream.error");
|
|
311
|
+
return;
|
|
312
|
+
default:
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
onMessageFrame(frame) {
|
|
317
|
+
if (frame.imported === true)
|
|
318
|
+
return;
|
|
319
|
+
const channel = frame.channel;
|
|
320
|
+
for (const portal of this.portalsFor(channel)) {
|
|
321
|
+
void this.queues.run(portal.key, () => this.guard(portal, `colibri.message.${String(frame.event)}`, async () => {
|
|
322
|
+
if (frame.event === "delete") {
|
|
323
|
+
const subject = frame.subject;
|
|
324
|
+
await this.outboundDelete(portal, subject);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
await this.outboundMessage(portal, frame.message);
|
|
328
|
+
}));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
onReactionFrame(frame) {
|
|
332
|
+
const channel = frame.channel;
|
|
333
|
+
for (const portal of this.portalsFor(channel)) {
|
|
334
|
+
void this.queues.run(portal.key, () => this.guard(portal, `colibri.reaction.${String(frame.event)}`, () => this.outboundReaction(portal, frame)));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
onThreadFrame(frame) {
|
|
338
|
+
const action = `colibri.thread.${String(frame.event)}`;
|
|
339
|
+
if (frame.event === "create" || frame.event === "update") {
|
|
340
|
+
const thread = frame.thread;
|
|
341
|
+
if (!thread)
|
|
342
|
+
return;
|
|
343
|
+
for (const parent of this.byChannel.get(thread.channel) ?? []) {
|
|
344
|
+
void this.queues.run(parent.key, () => this.guard(parent, action, () => this.outboundThread(parent, thread)));
|
|
345
|
+
}
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (frame.event !== "delete" || typeof frame.space !== "string")
|
|
349
|
+
return;
|
|
350
|
+
const space = frame.space;
|
|
351
|
+
for (const portal of this.threads.get(space) ?? []) {
|
|
352
|
+
void this.queues.run(portal.key, () => this.guard(portal, action, () => this.outboundThreadDelete(space, portal)));
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
onLabelFrame(frame) {
|
|
356
|
+
if (frame.event !== "create" || frame.val !== "hidden")
|
|
357
|
+
return;
|
|
358
|
+
const subject = frame.subject;
|
|
359
|
+
if (subject.collection !== "social.colibri.beta.message" || !subject.did || !subject.rkey) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const target = { did: subject.did, rkey: subject.rkey };
|
|
363
|
+
for (const portal of this.portalsFor(frame.space)) {
|
|
364
|
+
void this.queues.run(portal.key, () => this.guard(portal, "colibri.label.hidden", () => this.outboundHide(portal, target)));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
async guard(portal, action, work) {
|
|
368
|
+
const attempts = (this.options.maxRetries ?? 3) + 1;
|
|
369
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
370
|
+
try {
|
|
371
|
+
await work();
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
const retryable = error instanceof ColibriRequestError && (error.status === 429 || error.status >= 500);
|
|
376
|
+
if (retryable && attempt < attempts) {
|
|
377
|
+
await this.sleep((error.retryAfterSeconds ?? 2 ** attempt) * 1000);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
this.log.warn({
|
|
381
|
+
action,
|
|
382
|
+
channel: portal.channel,
|
|
383
|
+
registration: portal.registration,
|
|
384
|
+
error: error instanceof Error ? error.message : String(error),
|
|
385
|
+
...(error instanceof ColibriRequestError ? { code: error.error } : {}),
|
|
386
|
+
}, "bridge.action.failed");
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
ref(portal) {
|
|
392
|
+
return { community: portal.community, registration: portal.registration };
|
|
393
|
+
}
|
|
394
|
+
mappingRegistration(portal) {
|
|
395
|
+
return registrationKey(portal.community, portal.registration);
|
|
396
|
+
}
|
|
397
|
+
async inbound(parent, event) {
|
|
398
|
+
switch (event.type) {
|
|
399
|
+
case "threadCreate":
|
|
400
|
+
await this.ensureThread(parent, event, event.author);
|
|
401
|
+
return;
|
|
402
|
+
case "threadUpdate":
|
|
403
|
+
await this.inboundThreadUpdate(parent, event.id, event.name);
|
|
404
|
+
return;
|
|
405
|
+
case "threadDelete":
|
|
406
|
+
await this.inboundThreadDelete(parent, event.id);
|
|
407
|
+
return;
|
|
408
|
+
case "roomsChanged":
|
|
409
|
+
return;
|
|
410
|
+
default:
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
const thread = event.type === "message" ? event.thread : undefined;
|
|
414
|
+
const remoteThread = thread?.id ?? event.remoteThread;
|
|
415
|
+
const portal = !remoteThread
|
|
416
|
+
? parent
|
|
417
|
+
: thread && event.type === "message"
|
|
418
|
+
? await this.ensureThread(parent, thread, event.author)
|
|
419
|
+
: await this.knownThread(parent, remoteThread);
|
|
420
|
+
if (!portal)
|
|
421
|
+
return;
|
|
422
|
+
switch (event.type) {
|
|
423
|
+
case "message":
|
|
424
|
+
await this.inboundMessage(portal, event);
|
|
425
|
+
return;
|
|
426
|
+
case "messageEdit":
|
|
427
|
+
await this.inboundEdit(portal, event.id, event.markdown);
|
|
428
|
+
return;
|
|
429
|
+
case "messageDelete":
|
|
430
|
+
await this.inboundDelete(portal, event.id);
|
|
431
|
+
return;
|
|
432
|
+
case "reactionAdd":
|
|
433
|
+
await this.inboundReactionAdd(portal, event.messageId, event.author, event.emoji);
|
|
434
|
+
return;
|
|
435
|
+
case "reactionRemove":
|
|
436
|
+
await this.inboundReactionRemove(portal, event.messageId, event.authorId, event.emoji);
|
|
437
|
+
return;
|
|
438
|
+
default:
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async avatarFor(portal, author) {
|
|
443
|
+
if (!author.avatarUrl)
|
|
444
|
+
return undefined;
|
|
445
|
+
const cached = await this.options.store.avatar(portal.community, author.avatarUrl);
|
|
446
|
+
if (cached)
|
|
447
|
+
return cached;
|
|
448
|
+
try {
|
|
449
|
+
const { bytes, mimeType } = await this.options.client.fetchBytes(author.avatarUrl);
|
|
450
|
+
const blob = await this.options.client.uploadBlob(this.ref(portal), bytes, mimeType);
|
|
451
|
+
await this.options.store.putAvatar(portal.community, author.avatarUrl, blob);
|
|
452
|
+
return blob;
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
this.log.debug({ error: String(error) }, "bridge.avatar.failed");
|
|
456
|
+
return undefined;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async attachmentsFor(portal, attachments) {
|
|
460
|
+
const out = [];
|
|
461
|
+
const limit = this.options.maxAttachmentBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES;
|
|
462
|
+
for (const attachment of attachments ?? []) {
|
|
463
|
+
if (attachment.size !== undefined && attachment.size > limit)
|
|
464
|
+
continue;
|
|
465
|
+
try {
|
|
466
|
+
const { bytes, mimeType } = await this.options.client.fetchBytes(attachment.url);
|
|
467
|
+
if (bytes.byteLength > limit)
|
|
468
|
+
continue;
|
|
469
|
+
const blob = await this.options.client.uploadBlob(this.ref(portal), bytes, mimeType);
|
|
470
|
+
out.push({ blob, ...(attachment.name ? { name: attachment.name } : {}) });
|
|
471
|
+
}
|
|
472
|
+
catch (error) {
|
|
473
|
+
this.log.debug({ error: String(error) }, "bridge.attachment.failed");
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return out;
|
|
477
|
+
}
|
|
478
|
+
async ensureThread(parent, thread, author, createdAt) {
|
|
479
|
+
const registration = this.mappingRegistration(parent);
|
|
480
|
+
const known = await this.options.store.byRemote({
|
|
481
|
+
registration,
|
|
482
|
+
kind: "thread",
|
|
483
|
+
remoteId: thread.id,
|
|
484
|
+
});
|
|
485
|
+
if (known)
|
|
486
|
+
return this.addThread(parent, known.rkey, thread.id);
|
|
487
|
+
const anchorMapping = thread.anchor
|
|
488
|
+
? await this.options.store.byRemote({
|
|
489
|
+
registration,
|
|
490
|
+
kind: "message",
|
|
491
|
+
remoteId: thread.anchor,
|
|
492
|
+
})
|
|
493
|
+
: null;
|
|
494
|
+
const anchorKey = anchorMapping ? parseColibriRecordKey(anchorMapping.rkey) : null;
|
|
495
|
+
const anchor = anchorKey?.channel === parent.channel ? anchorKey : null;
|
|
496
|
+
const avatar = await this.avatarFor(parent, author);
|
|
497
|
+
const { thread: space } = await this.options.client.createThread({
|
|
498
|
+
...this.ref(parent),
|
|
499
|
+
channel: parent.channel,
|
|
500
|
+
name: threadName(thread.name),
|
|
501
|
+
author: { id: author.id, name: author.name, ...(avatar ? { avatar } : {}) },
|
|
502
|
+
...(anchor ? { anchor: { did: anchor.did, rkey: anchor.rkey } } : {}),
|
|
503
|
+
remoteThread: thread.id,
|
|
504
|
+
...(createdAt ? { createdAt } : {}),
|
|
505
|
+
});
|
|
506
|
+
await this.options.store.put({
|
|
507
|
+
registration,
|
|
508
|
+
channel: parent.channel,
|
|
509
|
+
kind: "thread",
|
|
510
|
+
remoteId: thread.id,
|
|
511
|
+
rkey: space,
|
|
512
|
+
origin: "remote",
|
|
513
|
+
});
|
|
514
|
+
return this.addThread(parent, space, thread.id);
|
|
515
|
+
}
|
|
516
|
+
async knownThread(parent, remoteThread) {
|
|
517
|
+
const mapping = await this.options.store.byRemote({
|
|
518
|
+
registration: this.mappingRegistration(parent),
|
|
519
|
+
kind: "thread",
|
|
520
|
+
remoteId: remoteThread,
|
|
521
|
+
});
|
|
522
|
+
return mapping ? this.addThread(parent, mapping.rkey, remoteThread) : null;
|
|
523
|
+
}
|
|
524
|
+
async inboundThreadUpdate(parent, id, name) {
|
|
525
|
+
const mapping = await this.options.store.byRemote({
|
|
526
|
+
registration: this.mappingRegistration(parent),
|
|
527
|
+
kind: "thread",
|
|
528
|
+
remoteId: id,
|
|
529
|
+
});
|
|
530
|
+
if (mapping?.origin !== "remote")
|
|
531
|
+
return;
|
|
532
|
+
await this.options.client.updateThread({
|
|
533
|
+
...this.ref(parent),
|
|
534
|
+
thread: mapping.rkey,
|
|
535
|
+
name: threadName(name),
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
async inboundThreadDelete(parent, id) {
|
|
539
|
+
const registration = this.mappingRegistration(parent);
|
|
540
|
+
const mapping = await this.options.store.byRemote({
|
|
541
|
+
registration,
|
|
542
|
+
kind: "thread",
|
|
543
|
+
remoteId: id,
|
|
544
|
+
});
|
|
545
|
+
if (!mapping)
|
|
546
|
+
return;
|
|
547
|
+
if (mapping.origin === "remote") {
|
|
548
|
+
await this.options.client.deleteThread({ ...this.ref(parent), thread: mapping.rkey });
|
|
549
|
+
}
|
|
550
|
+
await this.options.store.remove({ registration, kind: "thread", rkey: mapping.rkey });
|
|
551
|
+
this.dropThread(mapping.rkey, parent);
|
|
552
|
+
}
|
|
553
|
+
async forwardFor(portal, forward) {
|
|
554
|
+
const attachments = await this.attachmentsFor(portal, forward.attachments);
|
|
555
|
+
const source = forward.remoteMessage
|
|
556
|
+
? await this.options.store.byRemote({
|
|
557
|
+
registration: this.mappingRegistration(portal),
|
|
558
|
+
kind: "message",
|
|
559
|
+
remoteId: forward.remoteMessage,
|
|
560
|
+
})
|
|
561
|
+
: null;
|
|
562
|
+
const key = source ? parseColibriRecordKey(source.rkey) : null;
|
|
563
|
+
if (!key) {
|
|
564
|
+
return {
|
|
565
|
+
...(forward.markdown.trim() ? { quote: quoted(forward.markdown) } : {}),
|
|
566
|
+
attachments,
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
const { text, facets } = markdownToColibri(forward.markdown, this.resolver(portal));
|
|
570
|
+
return {
|
|
571
|
+
forward: {
|
|
572
|
+
source: { space: key.channel, did: key.did, rkey: key.rkey },
|
|
573
|
+
createdAt: forward.createdAt ?? new Date().toISOString(),
|
|
574
|
+
text,
|
|
575
|
+
...(facets.length > 0 ? { facets } : {}),
|
|
576
|
+
...(attachments.length > 0 ? { attachments } : {}),
|
|
577
|
+
},
|
|
578
|
+
attachments: [],
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
async prepared(portal, message) {
|
|
582
|
+
const registration = this.mappingRegistration(portal);
|
|
583
|
+
const forwarded = message.forward ? await this.forwardFor(portal, message.forward) : null;
|
|
584
|
+
const markdown = [message.markdown, forwarded?.quote]
|
|
585
|
+
.filter((part) => Boolean(part?.trim()))
|
|
586
|
+
.join("\n\n");
|
|
587
|
+
const { text, facets } = markdownToColibri(markdown, this.resolver(portal));
|
|
588
|
+
const attachments = [
|
|
589
|
+
...(await this.attachmentsFor(portal, message.attachments)),
|
|
590
|
+
...(forwarded?.attachments ?? []),
|
|
591
|
+
];
|
|
592
|
+
const forward = forwarded?.forward;
|
|
593
|
+
if (text.trim().length === 0 && attachments.length === 0 && !forward)
|
|
594
|
+
return null;
|
|
595
|
+
const parentMapping = message.replyTo
|
|
596
|
+
? await this.options.store.byRemote({
|
|
597
|
+
registration,
|
|
598
|
+
kind: "message",
|
|
599
|
+
remoteId: message.replyTo,
|
|
600
|
+
})
|
|
601
|
+
: null;
|
|
602
|
+
const parent = parentMapping ? parseColibriRecordKey(parentMapping.rkey) : null;
|
|
603
|
+
const avatar = await this.avatarFor(portal, message.author);
|
|
604
|
+
return {
|
|
605
|
+
author: {
|
|
606
|
+
id: message.author.id,
|
|
607
|
+
name: message.author.name,
|
|
608
|
+
...(avatar ? { avatar } : {}),
|
|
609
|
+
},
|
|
610
|
+
text,
|
|
611
|
+
...(facets.length > 0 ? { facets } : {}),
|
|
612
|
+
...(parent ? { parent: { did: parent.did, rkey: parent.rkey } } : {}),
|
|
613
|
+
...(attachments.length > 0 ? { attachments } : {}),
|
|
614
|
+
...(forward ? { forward } : {}),
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
async mapMessage(portal, remoteId, rkey) {
|
|
618
|
+
await this.options.store.put({
|
|
619
|
+
registration: this.mappingRegistration(portal),
|
|
620
|
+
channel: portal.channel,
|
|
621
|
+
kind: "message",
|
|
622
|
+
remoteId,
|
|
623
|
+
rkey: colibriRecordKey({ channel: portal.channel, did: portal.community, rkey }),
|
|
624
|
+
origin: "remote",
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
async inboundMessage(portal, event) {
|
|
628
|
+
const existing = await this.options.store.byRemote({
|
|
629
|
+
registration: this.mappingRegistration(portal),
|
|
630
|
+
kind: "message",
|
|
631
|
+
remoteId: event.id,
|
|
632
|
+
});
|
|
633
|
+
if (existing)
|
|
634
|
+
return;
|
|
635
|
+
const message = await this.prepared(portal, event);
|
|
636
|
+
if (!message)
|
|
637
|
+
return;
|
|
638
|
+
const { rkey } = await this.options.client.postMessage({
|
|
639
|
+
...this.ref(portal),
|
|
640
|
+
channel: portal.channel,
|
|
641
|
+
...message,
|
|
642
|
+
remoteMessage: event.id,
|
|
643
|
+
...(event.createdAt ? { createdAt: event.createdAt } : {}),
|
|
644
|
+
});
|
|
645
|
+
await this.mapMessage(portal, event.id, rkey);
|
|
646
|
+
}
|
|
647
|
+
startBackfills() {
|
|
648
|
+
const connector = this.options.connector;
|
|
649
|
+
if (!canImportHistory(connector))
|
|
650
|
+
return;
|
|
651
|
+
for (const portal of [...this.byChannel.values()].flat()) {
|
|
652
|
+
const request = portal.backfill;
|
|
653
|
+
if (!request)
|
|
654
|
+
continue;
|
|
655
|
+
const key = `${portal.key} ${request.requestedAt}`;
|
|
656
|
+
if (this.backfills.has(key))
|
|
657
|
+
continue;
|
|
658
|
+
const run = this.backfill(connector, portal, key).finally(() => this.backfills.delete(key));
|
|
659
|
+
this.backfills.set(key, run);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
async backfill(connector, parent, key) {
|
|
663
|
+
const request = parent.backfill;
|
|
664
|
+
if (!request)
|
|
665
|
+
return;
|
|
666
|
+
const store = this.options.store;
|
|
667
|
+
const stored = await store.backfill(key);
|
|
668
|
+
if (stored && stored.state !== "running")
|
|
669
|
+
return;
|
|
670
|
+
const job = {
|
|
671
|
+
connector,
|
|
672
|
+
parent,
|
|
673
|
+
key,
|
|
674
|
+
...(request.since ? { since: request.since } : {}),
|
|
675
|
+
until: request.requestedAt,
|
|
676
|
+
progress: stored ?? {
|
|
677
|
+
state: "running",
|
|
678
|
+
from: request.since ?? request.requestedAt,
|
|
679
|
+
until: request.requestedAt,
|
|
680
|
+
threadsDone: [],
|
|
681
|
+
imported: 0,
|
|
682
|
+
},
|
|
683
|
+
reportedAt: 0,
|
|
684
|
+
};
|
|
685
|
+
try {
|
|
686
|
+
if (!stored) {
|
|
687
|
+
if (!request.since) {
|
|
688
|
+
job.progress.from = await this.retrying(() => connector.roomCreatedAt(this.location(parent)));
|
|
689
|
+
}
|
|
690
|
+
await store.putBackfill(key, job.progress);
|
|
691
|
+
}
|
|
692
|
+
await this.report(job, true);
|
|
693
|
+
await this.backfillRoom(job);
|
|
694
|
+
job.progress.state = "done";
|
|
695
|
+
}
|
|
696
|
+
catch (error) {
|
|
697
|
+
if (error instanceof BackfillStopped)
|
|
698
|
+
return;
|
|
699
|
+
job.progress.state = "failed";
|
|
700
|
+
this.log.warn({
|
|
701
|
+
channel: parent.channel,
|
|
702
|
+
registration: parent.registration,
|
|
703
|
+
error: error instanceof Error ? error.message : String(error),
|
|
704
|
+
...(error instanceof ColibriRequestError ? { code: error.error } : {}),
|
|
705
|
+
}, "bridge.backfill.failed");
|
|
706
|
+
}
|
|
707
|
+
await store.putBackfill(key, job.progress);
|
|
708
|
+
await this.report(job, true);
|
|
709
|
+
}
|
|
710
|
+
async backfillRoom(job) {
|
|
711
|
+
const { connector, parent, progress } = job;
|
|
712
|
+
const threads = (await this.retrying(() => connector.listThreads({
|
|
713
|
+
...this.location(parent),
|
|
714
|
+
...(job.since ? { since: job.since } : {}),
|
|
715
|
+
until: job.until,
|
|
716
|
+
})))
|
|
717
|
+
.filter((thread) => thread.createdAt <= job.until && !progress.threadsDone.includes(thread.id))
|
|
718
|
+
.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
719
|
+
await this.walk(job, parent, {
|
|
720
|
+
cursor: () => progress.cursor,
|
|
721
|
+
advance: (last) => {
|
|
722
|
+
progress.cursor = last.id;
|
|
723
|
+
progress.reached = last.createdAt;
|
|
724
|
+
},
|
|
725
|
+
before: async (message, flush) => {
|
|
726
|
+
while (threads[0] && threads[0].createdAt <= message.createdAt) {
|
|
727
|
+
await flush();
|
|
728
|
+
const thread = threads.shift();
|
|
729
|
+
if (thread)
|
|
730
|
+
await this.backfillThread(job, thread);
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
});
|
|
734
|
+
for (const thread of threads)
|
|
735
|
+
await this.backfillThread(job, thread);
|
|
736
|
+
}
|
|
737
|
+
async backfillThread(job, thread) {
|
|
738
|
+
const { parent, progress } = job;
|
|
739
|
+
if (progress.thread?.id !== thread.id)
|
|
740
|
+
progress.thread = { id: thread.id };
|
|
741
|
+
const portal = await this.onQueue(parent.key, () => this.retrying(() => this.ensureThread(parent, thread, thread.author, thread.createdAt)));
|
|
742
|
+
if (portal) {
|
|
743
|
+
await this.walk(job, portal, {
|
|
744
|
+
cursor: () => progress.thread?.cursor,
|
|
745
|
+
advance: (last) => {
|
|
746
|
+
progress.thread = { id: thread.id, cursor: last.id };
|
|
747
|
+
},
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
progress.threadsDone.push(thread.id);
|
|
751
|
+
delete progress.thread;
|
|
752
|
+
await this.options.store.putBackfill(job.key, progress);
|
|
753
|
+
}
|
|
754
|
+
async walk(job, portal, steps) {
|
|
755
|
+
const size = this.options.backfillBatchSize ?? DEFAULT_BACKFILL_BATCH_SIZE;
|
|
756
|
+
let batch = [];
|
|
757
|
+
const flush = async () => {
|
|
758
|
+
const held = batch;
|
|
759
|
+
batch = [];
|
|
760
|
+
await this.importBatch(job, portal, held, steps.advance);
|
|
761
|
+
};
|
|
762
|
+
for (;;) {
|
|
763
|
+
const after = steps.cursor();
|
|
764
|
+
const page = await this.retrying(() => job.connector.fetchHistory({
|
|
765
|
+
...this.location(portal),
|
|
766
|
+
...(job.since && !portal.remoteThread ? { since: job.since } : {}),
|
|
767
|
+
until: job.until,
|
|
768
|
+
...(after ? { after } : {}),
|
|
769
|
+
}));
|
|
770
|
+
const due = page.filter((message) => message.createdAt <= job.until);
|
|
771
|
+
if (due.length === 0)
|
|
772
|
+
return;
|
|
773
|
+
for (const message of due) {
|
|
774
|
+
if (this.stopped)
|
|
775
|
+
throw new BackfillStopped();
|
|
776
|
+
await steps.before?.(message, flush);
|
|
777
|
+
const needsEarlier = batch.some((held) => held.id === message.replyTo || held.id === message.forward?.remoteMessage);
|
|
778
|
+
if (batch.length >= size || needsEarlier)
|
|
779
|
+
await flush();
|
|
780
|
+
batch.push(message);
|
|
781
|
+
}
|
|
782
|
+
await flush();
|
|
783
|
+
if (due.length < page.length)
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
async importBatch(job, portal, batch, advance) {
|
|
788
|
+
const last = batch.at(-1);
|
|
789
|
+
if (!last)
|
|
790
|
+
return;
|
|
791
|
+
if (this.stopped || !this.requested(job))
|
|
792
|
+
throw new BackfillStopped();
|
|
793
|
+
await this.onQueue(portal.key, async () => {
|
|
794
|
+
const registration = this.mappingRegistration(portal);
|
|
795
|
+
const messages = [];
|
|
796
|
+
for (const message of batch) {
|
|
797
|
+
const known = await this.options.store.byRemote({
|
|
798
|
+
registration,
|
|
799
|
+
kind: "message",
|
|
800
|
+
remoteId: message.id,
|
|
801
|
+
});
|
|
802
|
+
if (known)
|
|
803
|
+
continue;
|
|
804
|
+
const prepared = await this.retrying(() => this.prepared(portal, message));
|
|
805
|
+
if (prepared) {
|
|
806
|
+
messages.push({ ...prepared, remoteMessage: message.id, createdAt: message.createdAt });
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
if (messages.length > 0) {
|
|
810
|
+
const results = await this.retrying(() => this.options.client.importMessages({
|
|
811
|
+
...this.ref(portal),
|
|
812
|
+
channel: portal.channel,
|
|
813
|
+
messages,
|
|
814
|
+
}));
|
|
815
|
+
for (const result of results)
|
|
816
|
+
await this.mapMessage(portal, result.remoteMessage, result.rkey);
|
|
817
|
+
job.progress.imported += results.length;
|
|
818
|
+
}
|
|
819
|
+
advance(last);
|
|
820
|
+
await this.options.store.putBackfill(job.key, job.progress);
|
|
821
|
+
});
|
|
822
|
+
await this.report(job, false);
|
|
823
|
+
}
|
|
824
|
+
async report(job, force) {
|
|
825
|
+
const now = Date.now();
|
|
826
|
+
const interval = this.options.backfillReportIntervalMs ?? DEFAULT_BACKFILL_REPORT_INTERVAL_MS;
|
|
827
|
+
if (!force && now - job.reportedAt < interval)
|
|
828
|
+
return;
|
|
829
|
+
job.reportedAt = now;
|
|
830
|
+
const { progress, parent } = job;
|
|
831
|
+
await this.options.client
|
|
832
|
+
.reportBackfill({
|
|
833
|
+
...this.ref(parent),
|
|
834
|
+
channel: parent.channel,
|
|
835
|
+
requestedAt: job.until,
|
|
836
|
+
state: progress.state,
|
|
837
|
+
imported: progress.imported,
|
|
838
|
+
from: progress.from,
|
|
839
|
+
until: progress.until,
|
|
840
|
+
...(progress.reached ? { reached: progress.reached } : {}),
|
|
841
|
+
})
|
|
842
|
+
.catch((error) => this.log.debug({ error: String(error) }, "bridge.backfill.reportFailed"));
|
|
843
|
+
}
|
|
844
|
+
requested(job) {
|
|
845
|
+
return (this.byChannel.get(job.parent.channel) ?? []).some((portal) => portal.key === job.parent.key && portal.backfill?.requestedAt === job.until);
|
|
846
|
+
}
|
|
847
|
+
onQueue(key, work) {
|
|
848
|
+
return new Promise((resolve, reject) => {
|
|
849
|
+
void this.queues.run(key, () => work().then(resolve, reject));
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
async retrying(work) {
|
|
853
|
+
for (let attempt = 1;; attempt++) {
|
|
854
|
+
try {
|
|
855
|
+
return await work();
|
|
856
|
+
}
|
|
857
|
+
catch (error) {
|
|
858
|
+
const retryable = error instanceof ColibriRequestError && (error.status === 429 || error.status >= 500);
|
|
859
|
+
if (!retryable || attempt > BACKFILL_RETRIES || this.stopped)
|
|
860
|
+
throw error;
|
|
861
|
+
await this.sleep((error.retryAfterSeconds ?? Math.min(2 ** attempt, 60)) * 1000);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
async inboundEdit(portal, id, markdown) {
|
|
866
|
+
const mapping = await this.options.store.byRemote({
|
|
867
|
+
registration: this.mappingRegistration(portal),
|
|
868
|
+
kind: "message",
|
|
869
|
+
remoteId: id,
|
|
870
|
+
});
|
|
871
|
+
const target = mapping?.origin === "remote" ? parseColibriRecordKey(mapping.rkey) : null;
|
|
872
|
+
if (!target)
|
|
873
|
+
return;
|
|
874
|
+
const { text, facets } = markdownToColibri(markdown, this.resolver(portal));
|
|
875
|
+
await this.options.client.editMessage({
|
|
876
|
+
...this.ref(portal),
|
|
877
|
+
channel: portal.channel,
|
|
878
|
+
rkey: target.rkey,
|
|
879
|
+
text,
|
|
880
|
+
...(facets.length > 0 ? { facets } : {}),
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
async inboundDelete(portal, id) {
|
|
884
|
+
const registration = this.mappingRegistration(portal);
|
|
885
|
+
const mapping = await this.options.store.byRemote({
|
|
886
|
+
registration,
|
|
887
|
+
kind: "message",
|
|
888
|
+
remoteId: id,
|
|
889
|
+
});
|
|
890
|
+
if (!mapping)
|
|
891
|
+
return;
|
|
892
|
+
const target = parseColibriRecordKey(mapping.rkey);
|
|
893
|
+
if (mapping.origin === "remote" && target) {
|
|
894
|
+
await this.options.client.deleteMessage({
|
|
895
|
+
...this.ref(portal),
|
|
896
|
+
channel: portal.channel,
|
|
897
|
+
rkey: target.rkey,
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
if (mapping.origin === "colibri" && target && portal.mirrorModeration) {
|
|
901
|
+
await this.options.client.hideMessage({
|
|
902
|
+
...this.ref(portal),
|
|
903
|
+
channel: portal.channel,
|
|
904
|
+
subject: { did: target.did, rkey: target.rkey },
|
|
905
|
+
reason: `Removed by a moderator on ${this.platform}`,
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
await this.options.store.remove({ registration, kind: "message", rkey: mapping.rkey });
|
|
909
|
+
}
|
|
910
|
+
async inboundReactionAdd(portal, messageId, author, emoji) {
|
|
911
|
+
const registration = this.mappingRegistration(portal);
|
|
912
|
+
const message = await this.options.store.byRemote({
|
|
913
|
+
registration,
|
|
914
|
+
kind: "message",
|
|
915
|
+
remoteId: messageId,
|
|
916
|
+
});
|
|
917
|
+
const target = message ? parseColibriRecordKey(message.rkey) : null;
|
|
918
|
+
if (!target)
|
|
919
|
+
return;
|
|
920
|
+
const remoteId = `${messageId} ${author.id} ${emoji}`;
|
|
921
|
+
if (await this.options.store.byRemote({ registration, kind: "reaction", remoteId }))
|
|
922
|
+
return;
|
|
923
|
+
let rkey;
|
|
924
|
+
try {
|
|
925
|
+
({ rkey } = await this.options.client.addReaction({
|
|
926
|
+
...this.ref(portal),
|
|
927
|
+
channel: portal.channel,
|
|
928
|
+
author: { id: author.id, name: author.name },
|
|
929
|
+
target: { did: target.did, rkey: target.rkey },
|
|
930
|
+
emoji,
|
|
931
|
+
}));
|
|
932
|
+
}
|
|
933
|
+
catch (error) {
|
|
934
|
+
if (error instanceof ColibriRequestError && error.error === "AlreadyReacted")
|
|
935
|
+
return;
|
|
936
|
+
throw error;
|
|
937
|
+
}
|
|
938
|
+
await this.options.store.put({
|
|
939
|
+
registration,
|
|
940
|
+
channel: portal.channel,
|
|
941
|
+
kind: "reaction",
|
|
942
|
+
remoteId,
|
|
943
|
+
rkey: colibriRecordKey({ channel: portal.channel, did: portal.community, rkey }),
|
|
944
|
+
origin: "remote",
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
async inboundReactionRemove(portal, messageId, authorId, emoji) {
|
|
948
|
+
const registration = this.mappingRegistration(portal);
|
|
949
|
+
const mapping = await this.options.store.byRemote({
|
|
950
|
+
registration,
|
|
951
|
+
kind: "reaction",
|
|
952
|
+
remoteId: `${messageId} ${authorId} ${emoji}`,
|
|
953
|
+
});
|
|
954
|
+
const target = mapping ? parseColibriRecordKey(mapping.rkey) : null;
|
|
955
|
+
if (!mapping || !target)
|
|
956
|
+
return;
|
|
957
|
+
await this.options.client.removeReaction({
|
|
958
|
+
...this.ref(portal),
|
|
959
|
+
channel: portal.channel,
|
|
960
|
+
rkey: target.rkey,
|
|
961
|
+
});
|
|
962
|
+
await this.options.store.remove({ registration, kind: "reaction", rkey: mapping.rkey });
|
|
963
|
+
}
|
|
964
|
+
isOwnEcho(portal, author) {
|
|
965
|
+
return author.did === portal.community && author.bridgeRegistration === portal.registration;
|
|
966
|
+
}
|
|
967
|
+
async outboundMessage(portal, message) {
|
|
968
|
+
if (this.isOwnEcho(portal, {
|
|
969
|
+
did: message.author.did,
|
|
970
|
+
bridgeRegistration: message.author.bridge?.registration,
|
|
971
|
+
})) {
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
const registration = this.mappingRegistration(portal);
|
|
975
|
+
const key = colibriRecordKey({
|
|
976
|
+
channel: portal.channel,
|
|
977
|
+
did: message.author.did,
|
|
978
|
+
rkey: message.rkey,
|
|
979
|
+
});
|
|
980
|
+
const existing = await this.options.store.byColibri({
|
|
981
|
+
registration,
|
|
982
|
+
kind: "message",
|
|
983
|
+
rkey: key,
|
|
984
|
+
});
|
|
985
|
+
const author = {
|
|
986
|
+
did: message.author.did,
|
|
987
|
+
name: message.author.displayName,
|
|
988
|
+
...(message.author.handle && message.author.handle !== INVALID_HANDLE
|
|
989
|
+
? { handle: message.author.handle }
|
|
990
|
+
: {}),
|
|
991
|
+
...(message.author.avatar ? { avatarUrl: message.author.avatar } : {}),
|
|
992
|
+
};
|
|
993
|
+
const markdown = colibriToMarkdown(message.text, message.facets ?? [], this.referrer(portal));
|
|
994
|
+
if (existing) {
|
|
995
|
+
if (!message.updatedAt || existing.origin !== "colibri")
|
|
996
|
+
return;
|
|
997
|
+
if (!canEdit(this.options.connector))
|
|
998
|
+
return;
|
|
999
|
+
await this.options.connector.editMessage({
|
|
1000
|
+
...this.location(portal),
|
|
1001
|
+
id: existing.remoteId,
|
|
1002
|
+
author,
|
|
1003
|
+
markdown,
|
|
1004
|
+
});
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
const parent = message.parent;
|
|
1008
|
+
const replyMapping = parent?.author?.did && parent.rkey
|
|
1009
|
+
? await this.options.store.byColibri({
|
|
1010
|
+
registration,
|
|
1011
|
+
kind: "message",
|
|
1012
|
+
rkey: colibriRecordKey({
|
|
1013
|
+
channel: portal.channel,
|
|
1014
|
+
did: parent.author.did,
|
|
1015
|
+
rkey: parent.rkey,
|
|
1016
|
+
}),
|
|
1017
|
+
})
|
|
1018
|
+
: null;
|
|
1019
|
+
const forward = message.forward ? await this.outboundForward(portal, message.forward) : null;
|
|
1020
|
+
const remoteId = await this.options.connector.sendMessage({
|
|
1021
|
+
...this.location(portal),
|
|
1022
|
+
author,
|
|
1023
|
+
markdown,
|
|
1024
|
+
...(replyMapping ? { replyTo: replyMapping.remoteId } : {}),
|
|
1025
|
+
attachments: outboundAttachments(message.attachments),
|
|
1026
|
+
...(forward ? { forward } : {}),
|
|
1027
|
+
});
|
|
1028
|
+
await this.options.store.put({
|
|
1029
|
+
registration,
|
|
1030
|
+
channel: portal.channel,
|
|
1031
|
+
kind: "message",
|
|
1032
|
+
remoteId,
|
|
1033
|
+
rkey: key,
|
|
1034
|
+
origin: "colibri",
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
async outboundDelete(portal, subject) {
|
|
1038
|
+
const registration = this.mappingRegistration(portal);
|
|
1039
|
+
const key = colibriRecordKey({ channel: portal.channel, ...subject });
|
|
1040
|
+
const mapping = await this.options.store.byColibri({
|
|
1041
|
+
registration,
|
|
1042
|
+
kind: "message",
|
|
1043
|
+
rkey: key,
|
|
1044
|
+
});
|
|
1045
|
+
if (!mapping)
|
|
1046
|
+
return;
|
|
1047
|
+
if (mapping.origin === "colibri" && canDelete(this.options.connector)) {
|
|
1048
|
+
await this.options.connector.deleteMessage({
|
|
1049
|
+
...this.location(portal),
|
|
1050
|
+
id: mapping.remoteId,
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
await this.options.store.remove({ registration, kind: "message", rkey: key });
|
|
1054
|
+
}
|
|
1055
|
+
async outboundForward(portal, forward) {
|
|
1056
|
+
const source = forward.source;
|
|
1057
|
+
const mapping = await this.options.store.byColibri({
|
|
1058
|
+
registration: this.mappingRegistration(portal),
|
|
1059
|
+
kind: "message",
|
|
1060
|
+
rkey: colibriRecordKey({ channel: source.space, did: source.did, rkey: source.rkey }),
|
|
1061
|
+
});
|
|
1062
|
+
const sourcePortal = mapping
|
|
1063
|
+
? this.portalsFor(source.space).find((held) => held.registration === portal.registration)
|
|
1064
|
+
: undefined;
|
|
1065
|
+
return {
|
|
1066
|
+
markdown: colibriToMarkdown(forward.text, forward.facets ?? [], this.referrer(portal)),
|
|
1067
|
+
...(mapping && sourcePortal
|
|
1068
|
+
? { source: { ...this.location(sourcePortal), id: mapping.remoteId } }
|
|
1069
|
+
: {}),
|
|
1070
|
+
attachments: outboundAttachments(forward.attachments),
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
async outboundHide(portal, subject) {
|
|
1074
|
+
const registration = this.mappingRegistration(portal);
|
|
1075
|
+
const key = colibriRecordKey({ channel: portal.channel, ...subject });
|
|
1076
|
+
const mapping = await this.options.store.byColibri({
|
|
1077
|
+
registration,
|
|
1078
|
+
kind: "message",
|
|
1079
|
+
rkey: key,
|
|
1080
|
+
});
|
|
1081
|
+
if (!mapping)
|
|
1082
|
+
return;
|
|
1083
|
+
const connector = this.options.connector;
|
|
1084
|
+
const target = { ...this.location(portal), id: mapping.remoteId };
|
|
1085
|
+
if (mapping.origin === "colibri") {
|
|
1086
|
+
if (!canDelete(connector))
|
|
1087
|
+
return;
|
|
1088
|
+
await connector.deleteMessage(target);
|
|
1089
|
+
}
|
|
1090
|
+
else {
|
|
1091
|
+
if (!portal.mirrorModeration || !canModerate(connector))
|
|
1092
|
+
return;
|
|
1093
|
+
await connector.removeMessage(target);
|
|
1094
|
+
}
|
|
1095
|
+
await this.options.store.remove({ registration, kind: "message", rkey: key });
|
|
1096
|
+
}
|
|
1097
|
+
async outboundThread(parent, thread) {
|
|
1098
|
+
const connector = this.options.connector;
|
|
1099
|
+
if (!canThread(connector))
|
|
1100
|
+
return;
|
|
1101
|
+
const registration = this.mappingRegistration(parent);
|
|
1102
|
+
const mapping = await this.options.store.byColibri({
|
|
1103
|
+
registration,
|
|
1104
|
+
kind: "thread",
|
|
1105
|
+
rkey: thread.space,
|
|
1106
|
+
});
|
|
1107
|
+
if (mapping) {
|
|
1108
|
+
await connector.renameThread({
|
|
1109
|
+
...this.location(parent),
|
|
1110
|
+
remoteThread: mapping.remoteId,
|
|
1111
|
+
id: mapping.remoteId,
|
|
1112
|
+
name: thread.name,
|
|
1113
|
+
});
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
if (thread.private)
|
|
1117
|
+
return;
|
|
1118
|
+
const anchor = thread.anchor && thread.anchor.space === parent.channel
|
|
1119
|
+
? await this.options.store.byColibri({
|
|
1120
|
+
registration,
|
|
1121
|
+
kind: "message",
|
|
1122
|
+
rkey: colibriRecordKey({
|
|
1123
|
+
channel: parent.channel,
|
|
1124
|
+
did: thread.anchor.did,
|
|
1125
|
+
rkey: thread.anchor.rkey,
|
|
1126
|
+
}),
|
|
1127
|
+
})
|
|
1128
|
+
: null;
|
|
1129
|
+
const id = await connector.createThread({
|
|
1130
|
+
...this.location(parent),
|
|
1131
|
+
name: thread.name,
|
|
1132
|
+
...(anchor ? { anchor: anchor.remoteId } : {}),
|
|
1133
|
+
});
|
|
1134
|
+
await this.options.store.put({
|
|
1135
|
+
registration,
|
|
1136
|
+
channel: parent.channel,
|
|
1137
|
+
kind: "thread",
|
|
1138
|
+
remoteId: id,
|
|
1139
|
+
rkey: thread.space,
|
|
1140
|
+
origin: "colibri",
|
|
1141
|
+
});
|
|
1142
|
+
const portal = this.addThread(parent, thread.space, id);
|
|
1143
|
+
const earlier = await this.options.client.listMessages(thread.space).catch((error) => {
|
|
1144
|
+
this.log.warn({ thread: thread.space, error: String(error) }, "bridge.thread.backlogFailed");
|
|
1145
|
+
return [];
|
|
1146
|
+
});
|
|
1147
|
+
for (const message of earlier)
|
|
1148
|
+
await this.outboundMessage(portal, message);
|
|
1149
|
+
}
|
|
1150
|
+
async outboundThreadDelete(space, portal) {
|
|
1151
|
+
const registration = this.mappingRegistration(portal);
|
|
1152
|
+
const mapping = await this.options.store.byColibri({
|
|
1153
|
+
registration,
|
|
1154
|
+
kind: "thread",
|
|
1155
|
+
rkey: space,
|
|
1156
|
+
});
|
|
1157
|
+
if (!mapping)
|
|
1158
|
+
return;
|
|
1159
|
+
if (canThread(this.options.connector)) {
|
|
1160
|
+
await this.options.connector.archiveThread({
|
|
1161
|
+
...this.location(portal),
|
|
1162
|
+
id: mapping.remoteId,
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
await this.options.store.remove({ registration, kind: "thread", rkey: space });
|
|
1166
|
+
this.dropThread(space, portal);
|
|
1167
|
+
}
|
|
1168
|
+
async outboundReaction(portal, frame) {
|
|
1169
|
+
const connector = this.options.connector;
|
|
1170
|
+
if (!canReact(connector))
|
|
1171
|
+
return;
|
|
1172
|
+
const actor = frame.actor;
|
|
1173
|
+
const bridged = frame.bridged;
|
|
1174
|
+
if (this.isOwnEcho(portal, { did: actor, bridgeRegistration: bridged?.registration }))
|
|
1175
|
+
return;
|
|
1176
|
+
const target = frame.target;
|
|
1177
|
+
const emoji = frame.emoji;
|
|
1178
|
+
const registration = this.mappingRegistration(portal);
|
|
1179
|
+
const message = await this.options.store.byColibri({
|
|
1180
|
+
registration,
|
|
1181
|
+
kind: "message",
|
|
1182
|
+
rkey: colibriRecordKey({ channel: portal.channel, ...target }),
|
|
1183
|
+
});
|
|
1184
|
+
if (!message)
|
|
1185
|
+
return;
|
|
1186
|
+
const reactionKey = `${registration} ${message.remoteId} ${emoji}`;
|
|
1187
|
+
const reactor = bridged?.remoteId ? `${actor} ${bridged.remoteId}` : actor;
|
|
1188
|
+
const reaction = {
|
|
1189
|
+
...this.location(portal),
|
|
1190
|
+
messageId: message.remoteId,
|
|
1191
|
+
emoji,
|
|
1192
|
+
};
|
|
1193
|
+
if (frame.event === "delete") {
|
|
1194
|
+
if ((await this.options.store.removeReactor(reactionKey, reactor)) === 0) {
|
|
1195
|
+
await connector.removeReaction(reaction);
|
|
1196
|
+
}
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
1199
|
+
if ((await this.options.store.addReactor(reactionKey, reactor)) === 1) {
|
|
1200
|
+
await connector.addReaction(reaction);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
const outboundAttachments = (attachments) => attachments.map((attachment) => ({
|
|
1205
|
+
url: attachment.url,
|
|
1206
|
+
mimeType: attachment.mimeType,
|
|
1207
|
+
...(attachment.name ? { name: attachment.name } : {}),
|
|
1208
|
+
}));
|
|
1209
|
+
//# sourceMappingURL=bridge.js.map
|