chirkle 0.3.0 → 0.4.1
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 +99 -47
- package/bin/release/darwin-amd64/chirkle-server +4 -0
- package/bin/release/darwin-arm64/chirkle-server +0 -0
- package/bin/release/linux-amd64/chirkle-server +0 -0
- package/bin/release/linux-arm64/chirkle-server +0 -0
- package/package.json +9 -6
- package/pb_hooks/federation/inbox.js +2 -69
- package/pb_hooks/federation/messages.js +5 -3
- package/pb_hooks/federation/net.js +1 -0
- package/pb_hooks/federation/people.js +1 -0
- package/pb_hooks/federation.pb.js +1 -8
- package/pb_hooks/lib/access.js +33 -10
- package/pb_hooks/lib/account.js +542 -6
- package/pb_hooks/lib/anonymous.js +2 -129
- package/pb_hooks/lib/bulk.js +1 -2
- package/pb_hooks/lib/collections.js +4 -4
- package/pb_hooks/lib/dms.js +1 -1
- package/pb_hooks/lib/files.js +82 -120
- package/pb_hooks/lib/handles.js +2 -2
- package/pb_hooks/lib/inbox.js +40 -54
- package/pb_hooks/lib/invites.js +266 -0
- package/pb_hooks/lib/media.js +5 -43
- package/pb_hooks/lib/messages.js +72 -117
- package/pb_hooks/lib/moderation.js +2 -16
- package/pb_hooks/lib/reactions.js +28 -4
- package/pb_hooks/lib/retention.js +88 -0
- package/pb_hooks/lib/site.js +13 -2
- package/pb_hooks/lib/stats.js +6 -34
- package/pb_hooks/lib/users.js +88 -7
- package/pb_hooks/lib/worker.js +22 -1
- package/pb_hooks/main.pb.js +85 -94
- package/pb_hooks/telegram/api.js +64 -12
- package/pb_hooks/telegram/inbound.js +158 -31
- package/pb_hooks/telegram/links.js +4 -3
- package/pb_hooks/telegram/outbound.js +86 -45
- package/pb_hooks/telegram/people.js +3 -3
- package/pb_hooks/telegram/worker.js +59 -15
- package/pb_migrations/1760000045_password_reset.js +38 -0
- package/pb_migrations/1760000047_telegram_supergroup_ids.js +51 -0
- package/pb_migrations/1760000048_inbox_reactions_merged.js +24 -0
- package/pb_migrations/1760000049_inbox_room_index.js +22 -0
- package/pb_migrations/1760000050_messages_unread_index.js +22 -0
- package/pb_migrations/1760000051_users_kind.js +31 -0
- package/pb_migrations/1760000052_deleted_accounts.js +19 -0
- package/pb_migrations/1760000053_account_jobs.js +45 -0
- package/pb_migrations/1760000054_room_invites.js +39 -0
- package/pb_migrations/1760000055_invite_barred.js +16 -0
- package/pb_migrations/1760000056_telegram_fetch.js +21 -0
- package/pb_public/assets/{access-PB77EH4F.css → access-GPRLOZOO.css} +2 -2
- package/pb_public/assets/{access-PB77EH4F.css.map → access-GPRLOZOO.css.map} +2 -2
- package/pb_public/assets/access-UK3L54NR.js +2 -0
- package/pb_public/assets/access-UK3L54NR.js.map +7 -0
- package/pb_public/assets/account-CT2BVMAX.css +2 -0
- package/pb_public/assets/account-CT2BVMAX.css.map +7 -0
- package/pb_public/assets/account-api-J6A6IYVX.js +2 -0
- package/pb_public/assets/account-api-J6A6IYVX.js.map +7 -0
- package/pb_public/assets/app-HB3NZHQ2.js +3 -0
- package/pb_public/assets/app-HB3NZHQ2.js.map +7 -0
- package/pb_public/assets/app-XJ73EU5O.css +2 -0
- package/pb_public/assets/app-XJ73EU5O.css.map +7 -0
- package/pb_public/assets/chunk-56ACD3V5.js +2 -0
- package/pb_public/assets/chunk-56ACD3V5.js.map +7 -0
- package/pb_public/assets/chunk-AG73QNUN.js +2 -0
- package/pb_public/assets/chunk-AG73QNUN.js.map +7 -0
- package/pb_public/assets/chunk-DCLJRQ53.js +2 -0
- package/pb_public/assets/chunk-DCLJRQ53.js.map +7 -0
- package/pb_public/assets/chunk-DZC4SY4M.js +2 -0
- package/pb_public/assets/chunk-DZC4SY4M.js.map +7 -0
- package/pb_public/assets/chunk-N2OFGBCZ.js +2 -0
- package/pb_public/assets/chunk-N2OFGBCZ.js.map +7 -0
- package/pb_public/assets/chunk-OKIIBH7O.js +2 -0
- package/pb_public/assets/chunk-OKIIBH7O.js.map +7 -0
- package/pb_public/assets/chunk-RPCBA7VF.js +2 -0
- package/pb_public/assets/chunk-RPCBA7VF.js.map +7 -0
- package/pb_public/assets/chunk-UZDXAORM.js +2 -0
- package/pb_public/assets/chunk-UZDXAORM.js.map +7 -0
- package/pb_public/assets/chunk-VTOUI25E.js +4 -0
- package/pb_public/assets/chunk-VTOUI25E.js.map +7 -0
- package/pb_public/assets/chunk-WR25F7FU.js +2 -0
- package/pb_public/assets/chunk-WR25F7FU.js.map +7 -0
- package/pb_public/assets/{emoji-VA2WCEAC.css → emoji-WWNPWCF3.css} +2 -2
- package/pb_public/assets/{emoji-VA2WCEAC.css.map → emoji-WWNPWCF3.css.map} +2 -2
- package/pb_public/assets/{emoji-picker-65VVVGYW.js → emoji-picker-K6JTK2GV.js} +4 -4
- package/pb_public/assets/emoji-picker-K6JTK2GV.js.map +7 -0
- package/pb_public/assets/forward-267HRNPY.js +2 -0
- package/pb_public/assets/forward-267HRNPY.js.map +7 -0
- package/pb_public/assets/inbox-CLMYESNI.js +2 -0
- package/pb_public/assets/inbox-CLMYESNI.js.map +7 -0
- package/pb_public/assets/invite-OGT5E2MZ.js +2 -0
- package/pb_public/assets/invite-OGT5E2MZ.js.map +7 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js +2 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js.map +7 -0
- package/pb_public/assets/lazy-H3KIJVB5.js +2 -0
- package/pb_public/assets/lazy-H3KIJVB5.js.map +7 -0
- package/pb_public/assets/login-TK6K4WGB.js +2 -0
- package/pb_public/assets/login-TK6K4WGB.js.map +7 -0
- package/pb_public/assets/media-WZ4653L5.js +2 -0
- package/pb_public/assets/media-WZ4653L5.js.map +7 -0
- package/pb_public/assets/mention-suggest-ODJD2IC5.js +2 -0
- package/pb_public/assets/mention-suggest-ODJD2IC5.js.map +7 -0
- package/pb_public/assets/notify-EPDM772N.js +2 -0
- package/pb_public/assets/notify-EPDM772N.js.map +7 -0
- package/pb_public/assets/profile-3R6S5QYS.js +2 -0
- package/pb_public/assets/profile-3R6S5QYS.js.map +7 -0
- package/pb_public/assets/search-WCHOGAJQ.js +2 -0
- package/pb_public/assets/search-WCHOGAJQ.js.map +7 -0
- package/pb_public/assets/stats-GTO2H6PG.js +2 -0
- package/pb_public/assets/stats-GTO2H6PG.js.map +7 -0
- package/pb_public/assets/switcher-TFSO45KV.js +2 -0
- package/pb_public/assets/switcher-TFSO45KV.js.map +7 -0
- package/pb_public/assets/user-search-3HQOCQWL.js +2 -0
- package/pb_public/assets/user-search-3HQOCQWL.js.map +7 -0
- package/pb_public/assets/viewer-2Y5FJBW4.js +2 -0
- package/pb_public/assets/viewer-2Y5FJBW4.js.map +7 -0
- package/pb_public/index.html +10 -2
- package/pb_public/stats.html +1 -1
- package/scripts/service.mjs +201 -0
- package/scripts/start.mjs +21 -17
- package/pb_hooks/lib/enrich.js +0 -75
- package/pb_hooks/lib/lists.js +0 -179
- package/pb_hooks/lib/live.js +0 -137
- package/pb_hooks/lib/reads.js +0 -183
- package/pb_public/assets/access-GUDTDXIP.js +0 -2
- package/pb_public/assets/access-GUDTDXIP.js.map +0 -7
- package/pb_public/assets/app-3WX5JYL2.js +0 -3
- package/pb_public/assets/app-3WX5JYL2.js.map +0 -7
- package/pb_public/assets/app-WAFKVN2H.css +0 -2
- package/pb_public/assets/app-WAFKVN2H.css.map +0 -7
- package/pb_public/assets/emoji-picker-65VVVGYW.js.map +0 -7
- package/pb_public/assets/forward-66PELNWT.js +0 -2
- package/pb_public/assets/forward-66PELNWT.js.map +0 -7
- package/pb_public/assets/inbox-TZJWPRTG.js +0 -2
- package/pb_public/assets/inbox-TZJWPRTG.js.map +0 -7
- package/pb_public/assets/lazy-4CXRBHHM.js +0 -2
- package/pb_public/assets/lazy-4CXRBHHM.js.map +0 -7
- package/pb_public/assets/login-G7Y44CJ7.js +0 -2
- package/pb_public/assets/login-G7Y44CJ7.js.map +0 -7
- package/pb_public/assets/media-FPRU2QH5.js +0 -2
- package/pb_public/assets/media-FPRU2QH5.js.map +0 -7
- package/pb_public/assets/mention-suggest-R7MXUH6R.js +0 -2
- package/pb_public/assets/mention-suggest-R7MXUH6R.js.map +0 -7
- package/pb_public/assets/notify-RQ6ZLR46.js +0 -2
- package/pb_public/assets/notify-RQ6ZLR46.js.map +0 -7
- package/pb_public/assets/profile-2CNVT2KX.js +0 -2
- package/pb_public/assets/profile-2CNVT2KX.js.map +0 -7
- package/pb_public/assets/search-3UT4HMES.js +0 -2
- package/pb_public/assets/search-3UT4HMES.js.map +0 -7
- package/pb_public/assets/stats-JX52AQCO.js +0 -2
- package/pb_public/assets/stats-JX52AQCO.js.map +0 -7
- package/pb_public/assets/switcher-CEUKRLEY.js +0 -2
- package/pb_public/assets/switcher-CEUKRLEY.js.map +0 -7
- package/pb_public/assets/user-search-7GNEIXMN.js +0 -2
- package/pb_public/assets/user-search-7GNEIXMN.js.map +0 -7
- package/pb_public/assets/viewer-DXT7SKOU.js +0 -2
- package/pb_public/assets/viewer-DXT7SKOU.js.map +0 -7
- package/scripts/fetch-pocketbase.sh +0 -63
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// becomes a chat message of its sender's stand-in, with its files; an album (sent as one update per item) becomes one
|
|
4
4
|
// message once all of it is in; an edit edits it. Telegram doesn't tell bots about deleted messages.
|
|
5
5
|
|
|
6
|
-
const { call, config, download } = require(`${__hooks}/telegram/api.js`);
|
|
6
|
+
const { asTelegram, call, config, download } = require(`${__hooks}/telegram/api.js`);
|
|
7
7
|
const { personOf } = require(`${__hooks}/telegram/people.js`);
|
|
8
8
|
const { activeLink, ensureLink, linkedRoom, linkOfChat, moveLink, tellLinkChange } = require(`${__hooks}/telegram/links.js`);
|
|
9
9
|
const { richText } = require(`${__hooks}/telegram/rich.js`);
|
|
@@ -79,10 +79,10 @@ function quoteLines(msg, known) {
|
|
|
79
79
|
* A Telegram message's text (its caption, or a rich message's blocks as text), its files (that can be brought over),
|
|
80
80
|
* and lines for what can't be: files over the download limit, and things that aren't files (a location, a poll…).
|
|
81
81
|
* @param {any} msg
|
|
82
|
-
* @returns {{ text: string, files: { id: string, name: string }[], notes: string[] }}
|
|
82
|
+
* @returns {{ text: string, files: { id: string, name: string, size: number }[], notes: string[] }}
|
|
83
83
|
*/
|
|
84
84
|
function partsOf(msg) {
|
|
85
|
-
/** @type {{ id: string, name: string }[]} */
|
|
85
|
+
/** @type {{ id: string, name: string, size: number }[]} */
|
|
86
86
|
const files = [];
|
|
87
87
|
/** @type {string[]} */
|
|
88
88
|
const notes = [];
|
|
@@ -93,7 +93,7 @@ function partsOf(msg) {
|
|
|
93
93
|
*/
|
|
94
94
|
const add = (f, name) => {
|
|
95
95
|
if (f.file_size && f.file_size > MAX_FILE) notes.push(`[${name}, ${mb(f.file_size)}: too large to bring over from Telegram]`);
|
|
96
|
-
else files.push({ id: f.file_id, name });
|
|
96
|
+
else files.push({ id: f.file_id, name, size: f.file_size || 0 });
|
|
97
97
|
};
|
|
98
98
|
if (msg.photo?.length) add(msg.photo[msg.photo.length - 1], `photo_${n}.jpg`);
|
|
99
99
|
if (msg.video) add(msg.video, msg.video.file_name || `video_${n}.mp4`);
|
|
@@ -162,32 +162,60 @@ function orderedIds(n) {
|
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
|
-
* What a Telegram message came over as: its tg_messages rows (either way), first part first.
|
|
165
|
+
* What a Telegram message came over as: its tg_messages rows (either way), first part first. `date` is the Telegram
|
|
166
|
+
* message's, when known: rows older than it are stale and left out (`current`).
|
|
166
167
|
* @param {core.App} app
|
|
167
168
|
* @param {string} chatId
|
|
168
169
|
* @param {number} tgId
|
|
170
|
+
* @param {number} [date]
|
|
169
171
|
*/
|
|
170
|
-
function partsMapped(app, chatId, tgId) {
|
|
172
|
+
function partsMapped(app, chatId, tgId, date = 0) {
|
|
171
173
|
return app
|
|
172
174
|
.findRecordsByFilter("tg_messages", "chat_id = {:chat} && tg_message_id = {:id}", "part", 0, 0, { chat: chatId, id: tgId })
|
|
173
|
-
.filter((r) => !!r)
|
|
175
|
+
.filter((r) => !!r)
|
|
176
|
+
.filter((r) => current(r, date));
|
|
174
177
|
}
|
|
175
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Clock skew allowed between Telegram and this server when telling a mapping from a stale one.
|
|
181
|
+
* A mapping is made once its Telegram message exists, so one made well before it was sent is another message's: a
|
|
182
|
+
* group that became a supergroup, before its mappings stayed with the old group (QA BUG-257), had the new group's ids,
|
|
183
|
+
* which start over, stand for its old messages.
|
|
184
|
+
*/
|
|
185
|
+
const SKEW_MS = 5 * 60_000;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Is a tg_messages row this Telegram message's, which was sent at `date` (Unix seconds; 0 when unknown)?
|
|
189
|
+
* @param {core.Record} row
|
|
190
|
+
* @param {number} date
|
|
191
|
+
*/
|
|
192
|
+
const current = (row, date) => !date || row.getDateTime("created").time().unixMilli() >= date * 1000 - SKEW_MS;
|
|
193
|
+
|
|
176
194
|
/**
|
|
177
195
|
* The chat message a Telegram message is (either way; its first part, when it came over as several), or null.
|
|
196
|
+
* `date` is the Telegram message's, when known: a mapping older than it is stale.
|
|
178
197
|
* @param {core.App} app
|
|
179
198
|
* @param {string} chatId
|
|
180
199
|
* @param {number} tgId
|
|
200
|
+
* @param {number} [date]
|
|
181
201
|
*/
|
|
182
|
-
function mapped(app, chatId, tgId) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
202
|
+
function mapped(app, chatId, tgId, date = 0) {
|
|
203
|
+
const row = partsMapped(app, chatId, tgId, date).find((r) => r.getInt("part") === 0);
|
|
204
|
+
return row ? findMessage(app, row.getString("message")) : null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Drop the stale mappings of a Telegram message (see `current`), before it gets its own.
|
|
209
|
+
* @param {core.App} tx
|
|
210
|
+
* @param {string} chatId
|
|
211
|
+
* @param {number} tgId
|
|
212
|
+
*/
|
|
213
|
+
function forgetStale(tx, chatId, tgId) {
|
|
214
|
+
for (const row of tx.findRecordsByFilter("tg_messages", "chat_id = {:chat} && tg_message_id = {:id}", "", 0, 0, {
|
|
215
|
+
chat: chatId,
|
|
216
|
+
id: tgId,
|
|
217
|
+
})) {
|
|
218
|
+
if (row) tx.delete(row);
|
|
191
219
|
}
|
|
192
220
|
}
|
|
193
221
|
|
|
@@ -250,17 +278,26 @@ function ensureMember(app, room, userId) {
|
|
|
250
278
|
|
|
251
279
|
/**
|
|
252
280
|
* Bring Telegram messages over as one chat message: one message, or the items of an album.
|
|
281
|
+
*
|
|
282
|
+
* A file that fails to download for now (no answer from Telegram) doesn't become "couldn't be brought over": `live`
|
|
283
|
+
* (as they come) the messages are queued for the worker (a `fetch`, §26.4) and come over once their files do; `retry`
|
|
284
|
+
* (the worker's) the failure is thrown, for the worker to try again later; `last` (its last try) what still fails
|
|
285
|
+
* says so in the text, as one Telegram refuses does.
|
|
253
286
|
* @param {core.App} app
|
|
254
287
|
* @param {core.Record} link tg_links
|
|
255
288
|
* @param {core.Record} room
|
|
256
289
|
* @param {any[]} msgs
|
|
290
|
+
* @param {"live" | "retry" | "last"} [mode]
|
|
257
291
|
*/
|
|
258
|
-
function bringOver(app, link, room, msgs) {
|
|
292
|
+
function bringOver(app, link, room, msgs, mode = "live") {
|
|
259
293
|
const chatId = link.getString("chat_id");
|
|
260
|
-
// Delivered twice (a webhook retry): brought over already, or an album item queued twice,
|
|
261
|
-
// of the whole album on the unique (chat_id, tg_message_id) (QA BUG-147).
|
|
294
|
+
// Delivered twice (a webhook retry): brought over already, waiting for its files, or an album item queued twice,
|
|
295
|
+
// which would fail the save of the whole album on the unique (chat_id, tg_message_id) (QA BUG-147).
|
|
262
296
|
const seen = new Set();
|
|
263
|
-
|
|
297
|
+
const waiting = mode === "live" ? fetching(app, chatId) : new Set();
|
|
298
|
+
msgs = msgs.filter(
|
|
299
|
+
(m) => !seen.has(m.message_id) && seen.add(m.message_id) && !waiting.has(m.message_id) && !mapped(app, chatId, m.message_id, m.date),
|
|
300
|
+
);
|
|
264
301
|
if (!msgs.length) return;
|
|
265
302
|
msgs.sort((a, b) => a.message_id - b.message_id);
|
|
266
303
|
const first = msgs[0];
|
|
@@ -269,9 +306,6 @@ function bringOver(app, link, room, msgs) {
|
|
|
269
306
|
// A service message (a new group photo, a timer, a topic…) has nothing to show: it makes no stand-in, and none joins
|
|
270
307
|
// a private room for it (QA BUG-88).
|
|
271
308
|
if (!notes.length && !parts.some((p) => p.files.length) && !parts.some((p) => p.text.trim())) return;
|
|
272
|
-
const author = personOf(app, first);
|
|
273
|
-
ensureMember(app, room, author.id);
|
|
274
|
-
|
|
275
309
|
/** @type {any[]} */
|
|
276
310
|
const files = [];
|
|
277
311
|
for (const f of parts.flatMap((p) => p.files)) {
|
|
@@ -280,13 +314,18 @@ function bringOver(app, link, room, msgs) {
|
|
|
280
314
|
continue;
|
|
281
315
|
}
|
|
282
316
|
try {
|
|
283
|
-
files.push(download(f.id, f.name));
|
|
317
|
+
files.push(download(f.id, f.name, f.size));
|
|
284
318
|
} catch (err) {
|
|
285
|
-
|
|
319
|
+
const later = mode !== "last" && !!asTelegram(err)?.transient;
|
|
320
|
+
app.logger().warn("telegram: download", "chat", chatId, "message", first.message_id, "later", later, "error", String(err));
|
|
321
|
+
if (later && mode === "retry") throw err;
|
|
322
|
+
if (later && queueFetch(app, chatId, msgs, String(err))) return;
|
|
286
323
|
notes.push(`[${f.name}: couldn't be brought over from Telegram]`);
|
|
287
324
|
}
|
|
288
325
|
}
|
|
289
|
-
const
|
|
326
|
+
const author = personOf(app, first);
|
|
327
|
+
ensureMember(app, room, author.id);
|
|
328
|
+
const reply = first.reply_to_message ? mapped(app, chatId, first.reply_to_message.message_id, first.reply_to_message.date) : null;
|
|
290
329
|
const known = !!reply && reply.getString("room") === room.id && !reply.getBool("deleted");
|
|
291
330
|
const textOf = (/** @type {boolean} */ withReply) =>
|
|
292
331
|
[forwardLine(first), ...quoteLines(first, withReply && known), ...parts.map((p) => p.text), ...notes].filter(Boolean).join("\n");
|
|
@@ -308,6 +347,7 @@ function bringOver(app, link, room, msgs) {
|
|
|
308
347
|
if (withReply && known && reply) rec.set("reply_to", reply.id);
|
|
309
348
|
tx.save(rec);
|
|
310
349
|
msgs.forEach((m, i) => {
|
|
350
|
+
forgetStale(tx, chatId, m.message_id);
|
|
311
351
|
rememberIn(tx, rec.id, chatId, m.message_id, i === 0 ? "text" : "media", 0);
|
|
312
352
|
});
|
|
313
353
|
for (let i = 1; i < pieces.length; i++) {
|
|
@@ -330,9 +370,10 @@ function bringOver(app, link, room, msgs) {
|
|
|
330
370
|
*/
|
|
331
371
|
function editOver(app, msg) {
|
|
332
372
|
const chatId = String(msg.chat.id);
|
|
333
|
-
const rows = partsMapped(app, chatId, msg.message_id);
|
|
373
|
+
const rows = partsMapped(app, chatId, msg.message_id, msg.date);
|
|
334
374
|
const rec = rows[0]?.getInt("part") === 0 ? findMessage(app, rows[0].getString("message")) : null;
|
|
335
|
-
if (!rec
|
|
375
|
+
if (!rec) return editWaiting(app, chatId, msg);
|
|
376
|
+
if (rec.getBool("deleted") || rec.getString("forwarded_from")) return;
|
|
336
377
|
const quoted = quoteLines(msg, !!rec.getString("reply_to"));
|
|
337
378
|
const { text, notes } = partsOf(msg);
|
|
338
379
|
const lines = [forwardLine(msg), ...quoted, text, ...notes].filter(Boolean);
|
|
@@ -388,6 +429,85 @@ function queueAlbumItem(app, msg) {
|
|
|
388
429
|
app.save(row);
|
|
389
430
|
}
|
|
390
431
|
|
|
432
|
+
/**
|
|
433
|
+
* The queued fetches of a chat (Telegram messages waiting for their files, §26.4).
|
|
434
|
+
* @param {core.App} app
|
|
435
|
+
* @param {string} chatId
|
|
436
|
+
*/
|
|
437
|
+
const fetchesOf = (app, chatId) =>
|
|
438
|
+
app.findRecordsByFilter("tg_queue", "op = 'fetch' && chat_id = {:chat}", "created", 0, 0, { chat: chatId }).filter((r) => !!r);
|
|
439
|
+
|
|
440
|
+
/** @param {core.Record} row a fetch */
|
|
441
|
+
const fetchMessages = (row) => /** @type {any[]} */ (JSON.parse(row.getString("payload") || "null")?.msgs || []);
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* The ids of a chat's Telegram messages waiting for their files.
|
|
445
|
+
* @param {core.App} app
|
|
446
|
+
* @param {string} chatId
|
|
447
|
+
*/
|
|
448
|
+
function fetching(app, chatId) {
|
|
449
|
+
return new Set(fetchesOf(app, chatId).flatMap((r) => fetchMessages(r).map((m) => m.message_id)));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Queue Telegram messages whose files couldn't be downloaded for now: the worker brings them over later (fetchOver).
|
|
454
|
+
* @param {core.App} app
|
|
455
|
+
* @param {string} chatId
|
|
456
|
+
* @param {any[]} msgs
|
|
457
|
+
* @param {string} error
|
|
458
|
+
* @returns {boolean} queued
|
|
459
|
+
*/
|
|
460
|
+
function queueFetch(app, chatId, msgs, error) {
|
|
461
|
+
try {
|
|
462
|
+
const row = new Record(app.findCollectionByNameOrId("tg_queue"));
|
|
463
|
+
row.set("op", "fetch");
|
|
464
|
+
row.set("chat_id", chatId);
|
|
465
|
+
row.set("payload", { msgs });
|
|
466
|
+
row.set("tries", 1);
|
|
467
|
+
row.set("next_at", new DateTime(new Date(Date.now() + 2000).toISOString()));
|
|
468
|
+
row.set("error", error.slice(0, 1000));
|
|
469
|
+
app.save(row);
|
|
470
|
+
return true;
|
|
471
|
+
} catch (err) {
|
|
472
|
+
// Too big to queue (a payload over 64 KB): it comes over now, saying what couldn't.
|
|
473
|
+
app.logger().error("telegram: fetch", "chat", chatId, "error", String(err));
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* An edit of a Telegram message still waiting for its files: it comes over as edited.
|
|
480
|
+
* @param {core.App} app
|
|
481
|
+
* @param {string} chatId
|
|
482
|
+
* @param {any} msg
|
|
483
|
+
*/
|
|
484
|
+
function editWaiting(app, chatId, msg) {
|
|
485
|
+
const has = (/** @type {core.Record} */ row) => fetchMessages(row).some((m) => m.message_id === msg.message_id);
|
|
486
|
+
if (!fetchesOf(app, chatId).some(has)) return;
|
|
487
|
+
// Read again in a transaction: the worker may be saving the row (its tries) meanwhile.
|
|
488
|
+
app.runInTransaction((tx) => {
|
|
489
|
+
for (const row of fetchesOf(tx, chatId).filter(has)) {
|
|
490
|
+
row.set("payload", { msgs: fetchMessages(row).map((m) => (m.message_id === msg.message_id ? msg : m)) });
|
|
491
|
+
tx.save(row);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* The worker's try at a queued fetch: its messages come over with their files, or (`last`) with what still fails
|
|
498
|
+
* said in their text. Fails, for the worker to try again, while a file fails to download for now.
|
|
499
|
+
* @param {core.App} app
|
|
500
|
+
* @param {core.Record} row
|
|
501
|
+
* @param {boolean} last
|
|
502
|
+
*/
|
|
503
|
+
function fetchOver(app, row, last) {
|
|
504
|
+
const chatId = row.getString("chat_id");
|
|
505
|
+
const link = linkOfChat(app, chatId);
|
|
506
|
+
const room = link && activeLink(app, link);
|
|
507
|
+
if (!link || !room) return;
|
|
508
|
+
bringOver(app, link, room, fetchMessages(row), last ? "last" : "retry");
|
|
509
|
+
}
|
|
510
|
+
|
|
391
511
|
/**
|
|
392
512
|
* Take an album's queued items, so only one caller brings it over.
|
|
393
513
|
* @param {core.App} app
|
|
@@ -490,8 +610,15 @@ function handleUpdate(app, update) {
|
|
|
490
610
|
return;
|
|
491
611
|
}
|
|
492
612
|
// A group that became a supergroup: Telegram says so in both, in either order (QA BUG-89).
|
|
493
|
-
|
|
494
|
-
if (msg.
|
|
613
|
+
// The old group's albums come over first, under its id: their messages are its own.
|
|
614
|
+
if (msg.migrate_to_chat_id) {
|
|
615
|
+
flushAlbums(app, chatId);
|
|
616
|
+
return moveLink(app, chatId, String(msg.migrate_to_chat_id));
|
|
617
|
+
}
|
|
618
|
+
if (msg.migrate_from_chat_id) {
|
|
619
|
+
flushAlbums(app, String(msg.migrate_from_chat_id));
|
|
620
|
+
return moveLink(app, String(msg.migrate_from_chat_id), chatId);
|
|
621
|
+
}
|
|
495
622
|
const link = ensureLink(app, msg.chat);
|
|
496
623
|
if (posted && COMMAND.test(msg.text || "")) return sayStatus(app, chatId);
|
|
497
624
|
const room = activeLink(app, link);
|
|
@@ -550,4 +677,4 @@ function webhook(e) {
|
|
|
550
677
|
return e.json(200, { ok: true });
|
|
551
678
|
}
|
|
552
679
|
|
|
553
|
-
module.exports = { webhook, handleUpdate, flushAlbums, mapped };
|
|
680
|
+
module.exports = { webhook, handleUpdate, flushAlbums, fetchOver, mapped };
|
|
@@ -88,9 +88,10 @@ function moveLink(app, from, to) {
|
|
|
88
88
|
}
|
|
89
89
|
link.set("chat_id", to);
|
|
90
90
|
tx.save(link);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
// What waits to go to Telegram goes to the new group. tg_messages stays with the old one: the new group numbers
|
|
92
|
+
// its messages afresh, so the old ids would stand for its new messages (QA BUG-257). Queued album items are the
|
|
93
|
+
// old group's messages, brought over before this (handleUpdate).
|
|
94
|
+
tx.db().newQuery("UPDATE tg_queue SET chat_id = {:to} WHERE chat_id = {:from} AND op != 'album'").bind({ to, from }).execute();
|
|
94
95
|
});
|
|
95
96
|
}
|
|
96
97
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// refuses for now (it is down, or asks to slow down) is retried in order.
|
|
5
5
|
|
|
6
6
|
const { roomName, sourceName } = require(`${__hooks}/lib/anonymous.js`);
|
|
7
|
-
const { asTelegram, call } = require(`${__hooks}/telegram/api.js`);
|
|
7
|
+
const { asTelegram, call, transferTimeout } = require(`${__hooks}/telegram/api.js`);
|
|
8
8
|
const { linkOfChat, linksOfRoom } = require(`${__hooks}/telegram/links.js`);
|
|
9
9
|
|
|
10
10
|
/** Telegram's limits: a caption's length, and the largest photo it takes as a photo (bigger ones go as files). */
|
|
@@ -183,7 +183,10 @@ function mappings(app, messageId, chatId, filter = "") {
|
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
/**
|
|
186
|
-
* Record what Telegram messages a chat message became.
|
|
186
|
+
* Record what Telegram messages a chat message became. Telegram has just given the id to this message, so a row that
|
|
187
|
+
* has it already is stale (a group that became a supergroup, whose new ids started over: QA BUG-257) and gives way.
|
|
188
|
+
* The message is in Telegram now whatever happens here: a failure is logged, not thrown, or the worker would send it
|
|
189
|
+
* again and again.
|
|
187
190
|
* @param {core.App} app
|
|
188
191
|
* @param {string} messageId
|
|
189
192
|
* @param {string} chatId
|
|
@@ -191,13 +194,25 @@ function mappings(app, messageId, chatId, filter = "") {
|
|
|
191
194
|
* @param {"text" | "caption" | "media"} kind
|
|
192
195
|
*/
|
|
193
196
|
function remember(app, messageId, chatId, sent, kind) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
197
|
+
try {
|
|
198
|
+
app.runInTransaction((tx) => {
|
|
199
|
+
for (const old of tx.findRecordsByFilter("tg_messages", "chat_id = {:chat} && tg_message_id = {:id}", "", 0, 0, {
|
|
200
|
+
chat: chatId,
|
|
201
|
+
id: sent.message_id,
|
|
202
|
+
})) {
|
|
203
|
+
if (old) tx.delete(old);
|
|
204
|
+
}
|
|
205
|
+
const row = new Record(tx.findCollectionByNameOrId("tg_messages"));
|
|
206
|
+
row.set("message", messageId);
|
|
207
|
+
row.set("chat_id", chatId);
|
|
208
|
+
row.set("tg_message_id", sent.message_id);
|
|
209
|
+
row.set("direction", "out");
|
|
210
|
+
row.set("kind", kind);
|
|
211
|
+
tx.save(row);
|
|
212
|
+
});
|
|
213
|
+
} catch (err) {
|
|
214
|
+
app.logger().error("telegram: remember", "message", messageId, "chat", chatId, "tg", sent?.message_id, "error", String(err));
|
|
215
|
+
}
|
|
201
216
|
}
|
|
202
217
|
|
|
203
218
|
/**
|
|
@@ -222,7 +237,7 @@ function filesOf(app, fsys, source) {
|
|
|
222
237
|
const kind = sticker ? "sticker" : PHOTO_EXT.has(ext) && size <= MAX_PHOTO ? "photo" : VIDEO_EXT.has(ext) ? "video" : "document";
|
|
223
238
|
const file = fsys.getReuploadableFile(key, true);
|
|
224
239
|
file.name = names[name] || name.replace(/_[a-z0-9]{10}(\.[^.]*)?$/i, "$1");
|
|
225
|
-
return { kind, file };
|
|
240
|
+
return { kind, file, size };
|
|
226
241
|
});
|
|
227
242
|
}
|
|
228
243
|
|
|
@@ -235,23 +250,26 @@ const SEND = /** @type {Record<string, string>} */ ({
|
|
|
235
250
|
|
|
236
251
|
/**
|
|
237
252
|
* Send a message to a group: its text (with its files as their caption when it fits), its files as one or two
|
|
238
|
-
* albums, as a reply when the message it answers is there.
|
|
253
|
+
* albums, as a reply when the message it answers is there. Those are its steps, sent in turn: `done` of them were
|
|
254
|
+
* sent by an earlier try, which failed at the next one, and `sent(n)` is told after each (the worker keeps it), so a
|
|
255
|
+
* try again sends the rest, neither the steps already in the group again nor nothing more (§26.7).
|
|
239
256
|
* @param {core.App} app
|
|
240
257
|
* @param {string} messageId
|
|
241
258
|
* @param {string} chatId
|
|
259
|
+
* @param {number} [done]
|
|
260
|
+
* @param {(n: number) => void} [sent]
|
|
242
261
|
*/
|
|
243
|
-
function send(app, messageId, chatId) {
|
|
262
|
+
function send(app, messageId, chatId, done = 0, sent = () => {}) {
|
|
244
263
|
const msg = findMessage(app, messageId);
|
|
245
264
|
if (!msg || msg.getBool("deleted")) return;
|
|
246
265
|
const link = linkOfChat(app, chatId);
|
|
247
266
|
if (!link?.getBool("enabled") || link.getString("room") !== msg.getString("room")) return;
|
|
248
|
-
// Came from this group, or already sent (a retry after the worker stopped halfway).
|
|
249
|
-
if (mappings(app, messageId, chatId).length) return;
|
|
267
|
+
// Came from this group, or already sent (a retry after the worker stopped halfway, before it kept the steps).
|
|
268
|
+
if (!done && mappings(app, messageId, chatId).length) return;
|
|
250
269
|
|
|
251
270
|
const { html, plain, source } = contentOf(app, msg, link.getBool("anonymous"));
|
|
252
271
|
const replyTo = msg.getString("reply_to") ? mappings(app, msg.getString("reply_to"), chatId)[0] : null;
|
|
253
|
-
|
|
254
|
-
let reply = replyTo ? { reply_parameters: { message_id: replyTo.getInt("tg_message_id"), allow_sending_without_reply: true } } : {};
|
|
272
|
+
const reply = replyTo ? { reply_parameters: { message_id: replyTo.getInt("tg_message_id"), allow_sending_without_reply: true } } : {};
|
|
255
273
|
|
|
256
274
|
const fsys = app.newFilesystem();
|
|
257
275
|
try {
|
|
@@ -259,44 +277,67 @@ function send(app, messageId, chatId) {
|
|
|
259
277
|
const groups = [files.filter((f) => f.kind !== "document"), files.filter((f) => f.kind === "document")].filter((g) => g.length);
|
|
260
278
|
// A sticker takes no caption: its text goes before it.
|
|
261
279
|
const captioned = groups.length && files[0].kind !== "sticker";
|
|
262
|
-
|
|
280
|
+
const caption = captioned && [...plain].length <= MAX_CAPTION ? { caption: html, parse_mode: "HTML" } : null;
|
|
281
|
+
/** @type {((reply: Record<string, unknown>) => void)[]} */
|
|
282
|
+
const steps = [];
|
|
263
283
|
// No text goes alone when there is none: an anonymous link's sticker, or a forward of a deleted message.
|
|
264
284
|
if (!caption && html) {
|
|
265
|
-
|
|
266
|
-
|
|
285
|
+
steps.push((r) =>
|
|
286
|
+
remember(app, messageId, chatId, call("sendMessage", { chat_id: chatId, text: html, parse_mode: "HTML", ...r }), "text"),
|
|
287
|
+
);
|
|
267
288
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
} else {
|
|
278
|
-
/** @type {Record<string, any>} */
|
|
279
|
-
const attach = {};
|
|
280
|
-
const media = group.map((f, i) => {
|
|
281
|
-
attach[`file${i}`] = f.file;
|
|
282
|
-
return {
|
|
283
|
-
type: f.kind,
|
|
284
|
-
media: `attach://file${i}`,
|
|
285
|
-
...(f.kind === "video" ? STREAM : {}),
|
|
286
|
-
...(i === 0 && caption ? caption : {}),
|
|
287
|
-
};
|
|
288
|
-
});
|
|
289
|
-
const sent = call("sendMediaGroup", { chat_id: chatId, media, ...reply }, attach);
|
|
290
|
-
for (let i = 0; i < sent.length; i++) remember(app, messageId, chatId, sent[i], i === 0 && caption ? "caption" : "media");
|
|
291
|
-
}
|
|
292
|
-
caption = null;
|
|
293
|
-
reply = {};
|
|
289
|
+
steps.push(
|
|
290
|
+
...groups.map(
|
|
291
|
+
(group, g) => (/** @type {Record<string, unknown>} */ r) => sendGroup(app, messageId, chatId, group, g === 0 ? caption : null, r),
|
|
292
|
+
),
|
|
293
|
+
);
|
|
294
|
+
// The first step answers what it replies to, the rest follow it.
|
|
295
|
+
for (let i = done; i < steps.length; i++) {
|
|
296
|
+
steps[i](i === 0 ? reply : {});
|
|
297
|
+
if (i + 1 < steps.length) sent(i + 1);
|
|
294
298
|
}
|
|
295
299
|
} finally {
|
|
296
300
|
fsys.close();
|
|
297
301
|
}
|
|
298
302
|
}
|
|
299
303
|
|
|
304
|
+
/**
|
|
305
|
+
* One album of a message (or its one file), with the caption on its first item.
|
|
306
|
+
* @param {core.App} app
|
|
307
|
+
* @param {string} messageId
|
|
308
|
+
* @param {string} chatId
|
|
309
|
+
* @param {{ kind: string, file: any, size: number }[]} group
|
|
310
|
+
* @param {Record<string, unknown> | null} caption
|
|
311
|
+
* @param {Record<string, unknown>} reply
|
|
312
|
+
*/
|
|
313
|
+
function sendGroup(app, messageId, chatId, group, caption, reply) {
|
|
314
|
+
const timeout = transferTimeout(group.reduce((n, f) => n + f.size, 0));
|
|
315
|
+
if (group.length === 1) {
|
|
316
|
+
const f = group[0];
|
|
317
|
+
const sent = call(
|
|
318
|
+
SEND[f.kind],
|
|
319
|
+
{ chat_id: chatId, ...(f.kind === "video" ? STREAM : {}), ...(caption || {}), ...reply },
|
|
320
|
+
{ [f.kind]: f.file },
|
|
321
|
+
timeout,
|
|
322
|
+
);
|
|
323
|
+
remember(app, messageId, chatId, sent, caption ? "caption" : "media");
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
/** @type {Record<string, any>} */
|
|
327
|
+
const attach = {};
|
|
328
|
+
const media = group.map((f, i) => {
|
|
329
|
+
attach[`file${i}`] = f.file;
|
|
330
|
+
return {
|
|
331
|
+
type: f.kind,
|
|
332
|
+
media: `attach://file${i}`,
|
|
333
|
+
...(f.kind === "video" ? STREAM : {}),
|
|
334
|
+
...(i === 0 && caption ? caption : {}),
|
|
335
|
+
};
|
|
336
|
+
});
|
|
337
|
+
const sent = call("sendMediaGroup", { chat_id: chatId, media, ...reply }, attach, timeout);
|
|
338
|
+
for (let i = 0; i < sent.length; i++) remember(app, messageId, chatId, sent[i], i === 0 && caption ? "caption" : "media");
|
|
339
|
+
}
|
|
340
|
+
|
|
300
341
|
/**
|
|
301
342
|
* Pass an edit on: the Telegram messages that hold the text (sent from here) get the new one.
|
|
302
343
|
* @param {core.App} app
|
|
@@ -92,6 +92,7 @@ function personOf(app, msg) {
|
|
|
92
92
|
const user = new Record(app.findCollectionByNameOrId("users"));
|
|
93
93
|
user.set("email", `tg${s.id.replace(/\D/g, "")}.${$security.randomStringWithAlphabet(6, "abcdefghijklmnopqrstuvwxyz")}@telegram.invalid`);
|
|
94
94
|
user.setPassword($security.randomString(40));
|
|
95
|
+
user.set("kind", "telegram");
|
|
95
96
|
user.set("no_dms", true);
|
|
96
97
|
// Their Telegram username as their handle when it is one and free (§41), so "@username" means them on both sides.
|
|
97
98
|
const { HANDLE, freeHandle, normalHandle } = require(`${__hooks}/lib/handles.js`);
|
|
@@ -138,14 +139,13 @@ function refreshPhoto(app, row, user, tgUserId) {
|
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
/**
|
|
141
|
-
* Is this user a Telegram stand-in? Their messages came from Telegram and don't go back.
|
|
142
|
+
* Is this user a Telegram stand-in (`users.kind`, §5.1)? Their messages came from Telegram and don't go back.
|
|
142
143
|
* @param {core.App} app
|
|
143
144
|
* @param {string} userId
|
|
144
145
|
*/
|
|
145
146
|
function isStandIn(app, userId) {
|
|
146
147
|
try {
|
|
147
|
-
app.
|
|
148
|
-
return true;
|
|
148
|
+
return require(`${__hooks}/lib/users.js`).kindOf(app.findRecordById("users", userId)) === "telegram";
|
|
149
149
|
} catch {
|
|
150
150
|
return false;
|
|
151
151
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// POST /api/telegram/tick runs one pass at once (tests, or an admin who doesn't want to wait).
|
|
6
6
|
|
|
7
7
|
const { asTelegram, call, config, UPDATES } = require(`${__hooks}/telegram/api.js`);
|
|
8
|
-
const { flushAlbums, handleUpdate } = require(`${__hooks}/telegram/inbound.js`);
|
|
8
|
+
const { fetchOver, flushAlbums, handleUpdate } = require(`${__hooks}/telegram/inbound.js`);
|
|
9
9
|
const { edit, remove, send } = require(`${__hooks}/telegram/outbound.js`);
|
|
10
10
|
|
|
11
11
|
const LOCK = "telegram.worker";
|
|
@@ -20,6 +20,11 @@ const STALE_MS = 90_000;
|
|
|
20
20
|
const CLAIM_MS = 10 * 60_000;
|
|
21
21
|
/** How many times a send that failed for now is tried before it is dropped. */
|
|
22
22
|
const MAX_TRIES = 8;
|
|
23
|
+
/**
|
|
24
|
+
* How many times a send is tried again after a failure that may have sent it (no answer once the request may have
|
|
25
|
+
* reached Telegram, api.js maybeDone): each such try may post it twice, so it gets one, not MAX_TRIES (§26.7).
|
|
26
|
+
*/
|
|
27
|
+
const MAX_UNSURE = 1;
|
|
23
28
|
/** Queue rows looked at in one pass. */
|
|
24
29
|
const BATCH = 50;
|
|
25
30
|
|
|
@@ -65,6 +70,32 @@ function poll(app, timeout) {
|
|
|
65
70
|
}
|
|
66
71
|
}
|
|
67
72
|
|
|
73
|
+
/** @param {core.Record} row */
|
|
74
|
+
const payloadOf = (row) => /** @type {Record<string, any>} */ (JSON.parse(row.getString("payload") || "null") || {});
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Change queue row `id` as it is stored now: what this pass loaded may be older (its claim, a fetch's edited
|
|
78
|
+
* messages, a send's steps). False when it is gone.
|
|
79
|
+
* @param {core.App} app
|
|
80
|
+
* @param {string} id
|
|
81
|
+
* @param {(row: core.Record) => void} change
|
|
82
|
+
*/
|
|
83
|
+
function update(app, id, change) {
|
|
84
|
+
let found = false;
|
|
85
|
+
app.runInTransaction((tx) => {
|
|
86
|
+
let row;
|
|
87
|
+
try {
|
|
88
|
+
row = tx.findRecordById("tg_queue", id);
|
|
89
|
+
} catch {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
change(row);
|
|
93
|
+
tx.save(row);
|
|
94
|
+
found = true;
|
|
95
|
+
});
|
|
96
|
+
return found;
|
|
97
|
+
}
|
|
98
|
+
|
|
68
99
|
/**
|
|
69
100
|
* Send what is queued for Telegram, oldest first. A group whose oldest item has to wait (a failure that may pass, or
|
|
70
101
|
* another pass sending it) gets nothing more this pass, so its messages arrive in order. `renew` after each one.
|
|
@@ -78,40 +109,53 @@ function drain(app, renew) {
|
|
|
78
109
|
for (const row of rows) {
|
|
79
110
|
if (!row) continue;
|
|
80
111
|
const chat = row.getString("chat_id");
|
|
81
|
-
|
|
112
|
+
const op = row.getString("op");
|
|
113
|
+
// What comes from a group (a fetch) waits apart from what goes to it: each keeps its own order.
|
|
114
|
+
const lane = op === "fetch" ? `in ${chat}` : chat;
|
|
115
|
+
if (blocked.has(lane)) continue;
|
|
82
116
|
const due = row.getDateTime("next_at");
|
|
83
117
|
if (!due.isZero() && due.time().unixMilli() > now) {
|
|
84
|
-
blocked.add(
|
|
118
|
+
blocked.add(lane);
|
|
85
119
|
continue;
|
|
86
120
|
}
|
|
87
121
|
if (!claim(app, row.id)) {
|
|
88
|
-
blocked.add(
|
|
122
|
+
blocked.add(lane);
|
|
89
123
|
continue;
|
|
90
124
|
}
|
|
91
|
-
const op = row.getString("op");
|
|
92
125
|
const message = row.getString("message");
|
|
126
|
+
const tries = row.getInt("tries") + 1;
|
|
127
|
+
/** @type {Record<string, any>} */
|
|
128
|
+
let payload = payloadOf(row);
|
|
93
129
|
try {
|
|
94
|
-
if (op === "send")
|
|
95
|
-
|
|
130
|
+
if (op === "send") {
|
|
131
|
+
send(app, message, chat, payload.done || 0, (/** @type {number} */ done) => {
|
|
132
|
+
payload = { ...payload, done };
|
|
133
|
+
update(app, row.id, (r) => r.set("payload", payload));
|
|
134
|
+
});
|
|
135
|
+
} else if (op === "edit") edit(app, message, chat);
|
|
96
136
|
else if (op === "delete") remove(app, message, chat);
|
|
97
137
|
else if (op === "react") require(`${__hooks}/telegram/reactions.js`).react(app, message, chat);
|
|
138
|
+
else if (op === "fetch") fetchOver(app, row, tries >= MAX_TRIES);
|
|
98
139
|
app.delete(row);
|
|
99
140
|
} catch (err) {
|
|
100
|
-
const tries = row.getInt("tries") + 1;
|
|
101
141
|
const tg = asTelegram(err);
|
|
102
142
|
const transient = !tg || tg.transient;
|
|
143
|
+
const unsure = (payload.unsure || 0) + (op === "send" && tg?.maybeDone ? 1 : 0);
|
|
103
144
|
app.logger().warn(`telegram: ${op}`, "message", message, "chat", chat, "try", tries, "error", String(err));
|
|
104
|
-
if (!transient || tries >= MAX_TRIES) {
|
|
145
|
+
if (!transient || tries >= MAX_TRIES || unsure > MAX_UNSURE) {
|
|
146
|
+
if (unsure > MAX_UNSURE) app.logger().warn(`telegram: ${op} dropped, it may have been sent`, "message", message, "chat", chat);
|
|
105
147
|
app.delete(row);
|
|
106
148
|
continue;
|
|
107
149
|
}
|
|
108
150
|
const wait = Math.max(tg ? tg.retryAfter : 0, 2 ** tries) * 1000;
|
|
109
|
-
row.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
151
|
+
update(app, row.id, (r) => {
|
|
152
|
+
r.set("tries", tries);
|
|
153
|
+
r.set("next_at", new DateTime(new Date(Date.now() + wait).toISOString()));
|
|
154
|
+
r.set("locked_until", "");
|
|
155
|
+
r.set("error", String(err).slice(0, 1000));
|
|
156
|
+
if (unsure) r.set("payload", { ...payloadOf(r), unsure });
|
|
157
|
+
});
|
|
158
|
+
blocked.add(lane);
|
|
115
159
|
} finally {
|
|
116
160
|
renew();
|
|
117
161
|
}
|