chirkle 0.4.0 → 0.5.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 +38 -196
- package/bin/release/darwin-amd64/chirkle-server +2 -2
- 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 +18 -5
- package/pb_hooks/lib/account.js +541 -6
- package/pb_hooks/lib/collections.js +9 -7
- package/pb_hooks/lib/dms.js +1 -1
- package/pb_hooks/lib/handles.js +24 -2
- package/pb_hooks/lib/http.js +2 -2
- package/pb_hooks/lib/inbox.js +7 -6
- package/pb_hooks/lib/invites.js +266 -0
- package/pb_hooks/lib/media.js +2 -2
- package/pb_hooks/lib/messages.js +11 -2
- package/pb_hooks/lib/moderation.js +1 -1
- package/pb_hooks/lib/retention.js +3 -0
- package/pb_hooks/lib/site.js +1 -1
- package/pb_hooks/lib/users.js +70 -11
- package/pb_hooks/main.pb.js +27 -1
- package/pb_hooks/telegram/api.js +64 -12
- package/pb_hooks/telegram/inbound.js +111 -15
- package/pb_hooks/telegram/links.js +12 -1
- package/pb_hooks/telegram/outbound.js +119 -39
- package/pb_hooks/telegram/worker.js +61 -16
- 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_migrations/1760000057_reactions_seen.js +17 -0
- package/pb_migrations/1760000058_users_kind_public.js +20 -0
- package/pb_public/assets/access-KVSI7EFH.js +2 -0
- package/pb_public/assets/access-KVSI7EFH.js.br +0 -0
- package/pb_public/assets/access-KVSI7EFH.js.gz +0 -0
- package/pb_public/assets/access-KVSI7EFH.js.map +7 -0
- package/pb_public/assets/access-XGNXIUR5.css +2 -0
- package/pb_public/assets/access-XGNXIUR5.css.br +0 -0
- package/pb_public/assets/access-XGNXIUR5.css.gz +0 -0
- package/pb_public/assets/access-XGNXIUR5.css.map +7 -0
- package/pb_public/assets/account-EBCPOLWT.css +2 -0
- package/pb_public/assets/account-EBCPOLWT.css.br +0 -0
- package/pb_public/assets/account-EBCPOLWT.css.gz +0 -0
- package/pb_public/assets/account-EBCPOLWT.css.map +7 -0
- package/pb_public/assets/account-api-J6A6IYVX.js +2 -0
- package/pb_public/assets/account-api-J6A6IYVX.js.br +0 -0
- package/pb_public/assets/account-api-J6A6IYVX.js.gz +0 -0
- package/pb_public/assets/account-api-J6A6IYVX.js.map +7 -0
- package/pb_public/assets/app-5JOOV6JX.js +3 -0
- package/pb_public/assets/app-5JOOV6JX.js.br +0 -0
- package/pb_public/assets/app-5JOOV6JX.js.gz +0 -0
- package/pb_public/assets/app-5JOOV6JX.js.map +7 -0
- package/pb_public/assets/app-7OMYYLCI.css +2 -0
- package/pb_public/assets/app-7OMYYLCI.css.br +0 -0
- package/pb_public/assets/app-7OMYYLCI.css.gz +0 -0
- package/pb_public/assets/app-7OMYYLCI.css.map +7 -0
- package/pb_public/assets/chunk-5PKDEWJS.js +2 -0
- package/pb_public/assets/chunk-5PKDEWJS.js.br +0 -0
- package/pb_public/assets/chunk-5PKDEWJS.js.gz +0 -0
- package/pb_public/assets/chunk-5PKDEWJS.js.map +7 -0
- package/pb_public/assets/chunk-AG73QNUN.js +2 -0
- package/pb_public/assets/chunk-AG73QNUN.js.br +0 -0
- package/pb_public/assets/chunk-AG73QNUN.js.gz +0 -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.br +0 -0
- package/pb_public/assets/chunk-DCLJRQ53.js.gz +0 -0
- package/pb_public/assets/chunk-DCLJRQ53.js.map +7 -0
- package/pb_public/assets/chunk-EO3AMXJN.js +2 -0
- package/pb_public/assets/chunk-EO3AMXJN.js.br +0 -0
- package/pb_public/assets/chunk-EO3AMXJN.js.gz +0 -0
- package/pb_public/assets/chunk-EO3AMXJN.js.map +7 -0
- package/pb_public/assets/chunk-LDZPNZ2E.js +2 -0
- package/pb_public/assets/chunk-LDZPNZ2E.js.br +0 -0
- package/pb_public/assets/chunk-LDZPNZ2E.js.gz +0 -0
- package/pb_public/assets/chunk-LDZPNZ2E.js.map +7 -0
- package/pb_public/assets/chunk-RPCBA7VF.js.br +0 -0
- package/pb_public/assets/chunk-RPCBA7VF.js.gz +0 -0
- package/pb_public/assets/chunk-RYZHXNLF.js +2 -0
- package/pb_public/assets/chunk-RYZHXNLF.js.br +0 -0
- package/pb_public/assets/chunk-RYZHXNLF.js.gz +0 -0
- package/pb_public/assets/chunk-RYZHXNLF.js.map +7 -0
- package/pb_public/assets/chunk-UZDXAORM.js +2 -0
- package/pb_public/assets/chunk-UZDXAORM.js.br +0 -0
- package/pb_public/assets/chunk-UZDXAORM.js.gz +0 -0
- package/pb_public/assets/chunk-UZDXAORM.js.map +7 -0
- package/pb_public/assets/chunk-VSHXXDW4.js +2 -0
- package/pb_public/assets/chunk-VSHXXDW4.js.br +0 -0
- package/pb_public/assets/chunk-VSHXXDW4.js.gz +0 -0
- package/pb_public/assets/chunk-VSHXXDW4.js.map +7 -0
- package/pb_public/assets/chunk-VTOUI25E.js.br +0 -0
- package/pb_public/assets/chunk-VTOUI25E.js.gz +0 -0
- package/pb_public/assets/{chunk-DZC4SY4M.js → chunk-Y5MCBN66.js} +2 -2
- package/pb_public/assets/chunk-Y5MCBN66.js.br +0 -0
- package/pb_public/assets/chunk-Y5MCBN66.js.gz +0 -0
- package/pb_public/assets/{chunk-DZC4SY4M.js.map → chunk-Y5MCBN66.js.map} +3 -3
- package/pb_public/assets/collections-FZB6G4W4.css +2 -0
- package/pb_public/assets/collections-FZB6G4W4.css.br +0 -0
- package/pb_public/assets/collections-FZB6G4W4.css.gz +0 -0
- package/pb_public/assets/collections-FZB6G4W4.css.map +7 -0
- package/pb_public/assets/emoji-IHRUMGCY.css +2 -0
- package/pb_public/assets/emoji-IHRUMGCY.css.br +0 -0
- package/pb_public/assets/emoji-IHRUMGCY.css.gz +0 -0
- package/pb_public/assets/emoji-IHRUMGCY.css.map +7 -0
- package/pb_public/assets/{emoji-picker-K6JTK2GV.js → emoji-picker-W27IJER2.js} +2 -2
- package/pb_public/assets/emoji-picker-W27IJER2.js.br +0 -0
- package/pb_public/assets/emoji-picker-W27IJER2.js.gz +0 -0
- package/pb_public/assets/{forward-267HRNPY.js → forward-5TJEYGEV.js} +2 -2
- package/pb_public/assets/forward-5TJEYGEV.js.br +0 -0
- package/pb_public/assets/forward-5TJEYGEV.js.gz +0 -0
- package/pb_public/assets/inbox-2AXY3INA.css +2 -0
- package/pb_public/assets/inbox-2AXY3INA.css.br +0 -0
- package/pb_public/assets/inbox-2AXY3INA.css.gz +0 -0
- package/pb_public/assets/inbox-2AXY3INA.css.map +7 -0
- package/pb_public/assets/{inbox-CLMYESNI.js → inbox-A46J7ZU2.js} +2 -2
- package/pb_public/assets/inbox-A46J7ZU2.js.br +0 -0
- package/pb_public/assets/inbox-A46J7ZU2.js.gz +0 -0
- package/pb_public/assets/{inbox-CLMYESNI.js.map → inbox-A46J7ZU2.js.map} +2 -2
- package/pb_public/assets/invite-6BCBF4KV.js +2 -0
- package/pb_public/assets/invite-6BCBF4KV.js.br +0 -0
- package/pb_public/assets/invite-6BCBF4KV.js.gz +0 -0
- package/pb_public/assets/invite-6BCBF4KV.js.map +7 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js +2 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js.br +0 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js.gz +0 -0
- package/pb_public/assets/lazy-OVEJT5CP.js +2 -0
- package/pb_public/assets/lazy-OVEJT5CP.js.br +0 -0
- package/pb_public/assets/lazy-OVEJT5CP.js.gz +0 -0
- package/pb_public/assets/lazy-OVEJT5CP.js.map +7 -0
- package/pb_public/assets/login-FZY6GOMF.js +2 -0
- package/pb_public/assets/login-FZY6GOMF.js.br +0 -0
- package/pb_public/assets/login-FZY6GOMF.js.gz +0 -0
- package/pb_public/assets/{login-ECFBJP6Q.js.map → login-FZY6GOMF.js.map} +1 -1
- package/pb_public/assets/media-BCKZQR2R.css +2 -0
- package/pb_public/assets/media-BCKZQR2R.css.br +0 -0
- package/pb_public/assets/media-BCKZQR2R.css.gz +0 -0
- package/pb_public/assets/media-BCKZQR2R.css.map +7 -0
- package/pb_public/assets/{media-WZ4653L5.js → media-VCJWBQJZ.js} +2 -2
- package/pb_public/assets/media-VCJWBQJZ.js.br +0 -0
- package/pb_public/assets/media-VCJWBQJZ.js.gz +0 -0
- package/pb_public/assets/{mention-suggest-ODJD2IC5.js → mention-suggest-K3UUHR4D.js} +2 -2
- package/pb_public/assets/mention-suggest-K3UUHR4D.js.br +0 -0
- package/pb_public/assets/mention-suggest-K3UUHR4D.js.gz +0 -0
- package/pb_public/assets/notify-EPDM772N.js.br +0 -0
- package/pb_public/assets/notify-EPDM772N.js.gz +0 -0
- package/pb_public/assets/profile-7KMXLX5U.js +2 -0
- package/pb_public/assets/profile-7KMXLX5U.js.br +0 -0
- package/pb_public/assets/profile-7KMXLX5U.js.gz +0 -0
- package/pb_public/assets/profile-7KMXLX5U.js.map +7 -0
- package/pb_public/assets/rooms-forms-IADHTHBZ.js +2 -0
- package/pb_public/assets/rooms-forms-IADHTHBZ.js.br +0 -0
- package/pb_public/assets/rooms-forms-IADHTHBZ.js.gz +0 -0
- package/pb_public/assets/rooms-forms-IADHTHBZ.js.map +7 -0
- package/pb_public/assets/search-HOPAWIMQ.js +2 -0
- package/pb_public/assets/search-HOPAWIMQ.js.br +0 -0
- package/pb_public/assets/search-HOPAWIMQ.js.gz +0 -0
- package/pb_public/assets/search-HOPAWIMQ.js.map +7 -0
- package/pb_public/assets/sidebar-forms-ETSRVSPY.css +2 -0
- package/pb_public/assets/sidebar-forms-ETSRVSPY.css.br +0 -0
- package/pb_public/assets/sidebar-forms-ETSRVSPY.css.gz +0 -0
- package/pb_public/assets/sidebar-forms-ETSRVSPY.css.map +7 -0
- package/pb_public/assets/stats-5GSWDHZI.css +2 -0
- package/pb_public/assets/stats-5GSWDHZI.css.br +0 -0
- package/pb_public/assets/stats-5GSWDHZI.css.gz +0 -0
- package/pb_public/assets/stats-5GSWDHZI.css.map +7 -0
- package/pb_public/assets/stats-Y3LIXAZR.js +2 -0
- package/pb_public/assets/stats-Y3LIXAZR.js.br +0 -0
- package/pb_public/assets/stats-Y3LIXAZR.js.gz +0 -0
- package/pb_public/assets/stats-Y3LIXAZR.js.map +7 -0
- package/pb_public/assets/switcher-TFSO45KV.js.br +0 -0
- package/pb_public/assets/switcher-TFSO45KV.js.gz +0 -0
- package/pb_public/assets/user-search-GUNMIBSB.js +2 -0
- package/pb_public/assets/user-search-GUNMIBSB.js.br +0 -0
- package/pb_public/assets/user-search-GUNMIBSB.js.gz +0 -0
- package/pb_public/assets/user-search-GUNMIBSB.js.map +7 -0
- package/pb_public/assets/{viewer-2Y5FJBW4.js → viewer-BHKVAADP.js} +2 -2
- package/pb_public/assets/viewer-BHKVAADP.js.br +0 -0
- package/pb_public/assets/viewer-BHKVAADP.js.gz +0 -0
- package/pb_public/assets/viewer-JBZU46HP.css +2 -0
- package/pb_public/assets/viewer-JBZU46HP.css.br +0 -0
- package/pb_public/assets/viewer-JBZU46HP.css.gz +0 -0
- package/pb_public/assets/{viewer-P7GYCBYM.css.map → viewer-JBZU46HP.css.map} +2 -2
- package/pb_public/index.html +11 -8
- package/pb_public/stats.html +3 -3
- package/scripts/service.mjs +201 -0
- package/scripts/start.mjs +9 -2
- package/pb_public/assets/access-LHPD2EHG.js +0 -2
- package/pb_public/assets/access-LHPD2EHG.js.map +0 -7
- package/pb_public/assets/access-PB77EH4F.css +0 -2
- package/pb_public/assets/access-PB77EH4F.css.map +0 -7
- package/pb_public/assets/app-6ETOTHXQ.js +0 -3
- package/pb_public/assets/app-6ETOTHXQ.js.map +0 -7
- package/pb_public/assets/app-ZGHECF33.css +0 -2
- package/pb_public/assets/app-ZGHECF33.css.map +0 -7
- package/pb_public/assets/chunk-56ACD3V5.js +0 -2
- package/pb_public/assets/chunk-56ACD3V5.js.map +0 -7
- package/pb_public/assets/chunk-GXMWOXBX.js +0 -2
- package/pb_public/assets/chunk-GXMWOXBX.js.map +0 -7
- package/pb_public/assets/chunk-IFQGGTSP.js +0 -2
- package/pb_public/assets/chunk-IFQGGTSP.js.map +0 -7
- package/pb_public/assets/chunk-PWNJABVI.js +0 -2
- package/pb_public/assets/chunk-PWNJABVI.js.map +0 -7
- package/pb_public/assets/collections-DHJEOLHW.css +0 -2
- package/pb_public/assets/collections-DHJEOLHW.css.map +0 -7
- package/pb_public/assets/emoji-WWNPWCF3.css +0 -2
- package/pb_public/assets/emoji-WWNPWCF3.css.map +0 -7
- package/pb_public/assets/inbox-3FSOPY2T.css +0 -2
- package/pb_public/assets/inbox-3FSOPY2T.css.map +0 -7
- package/pb_public/assets/lazy-H3KIJVB5.js +0 -2
- package/pb_public/assets/lazy-H3KIJVB5.js.map +0 -7
- package/pb_public/assets/login-ECFBJP6Q.js +0 -2
- package/pb_public/assets/media-7TCBG7WM.css +0 -2
- package/pb_public/assets/media-7TCBG7WM.css.map +0 -7
- package/pb_public/assets/profile-NQQLAEDT.js +0 -2
- package/pb_public/assets/profile-NQQLAEDT.js.map +0 -7
- package/pb_public/assets/search-WCHOGAJQ.js +0 -2
- package/pb_public/assets/search-WCHOGAJQ.js.map +0 -7
- package/pb_public/assets/stats-A2TBRUI6.css +0 -2
- package/pb_public/assets/stats-A2TBRUI6.css.map +0 -7
- package/pb_public/assets/stats-ARDU576A.js +0 -2
- package/pb_public/assets/stats-ARDU576A.js.map +0 -7
- package/pb_public/assets/user-search-3HQOCQWL.js +0 -2
- package/pb_public/assets/viewer-P7GYCBYM.css +0 -2
- /package/pb_public/assets/{emoji-picker-K6JTK2GV.js.map → emoji-picker-W27IJER2.js.map} +0 -0
- /package/pb_public/assets/{forward-267HRNPY.js.map → forward-5TJEYGEV.js.map} +0 -0
- /package/pb_public/assets/{user-search-3HQOCQWL.js.map → invite-api-7G7XPXQW.js.map} +0 -0
- /package/pb_public/assets/{media-WZ4653L5.js.map → media-VCJWBQJZ.js.map} +0 -0
- /package/pb_public/assets/{mention-suggest-ODJD2IC5.js.map → mention-suggest-K3UUHR4D.js.map} +0 -0
- /package/pb_public/assets/{viewer-2Y5FJBW4.js.map → viewer-BHKVAADP.js.map} +0 -0
package/pb_hooks/telegram/api.js
CHANGED
|
@@ -22,6 +22,13 @@ function config() {
|
|
|
22
22
|
* message_reaction only where the bot is an admin. */
|
|
23
23
|
const UPDATES = ["message", "edited_message", "channel_post", "edited_channel_post", "my_chat_member", "message_reaction"];
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* What a failed request's error says when it never reached Telegram: no connection was made (no address, refused,
|
|
27
|
+
* unreachable, the connection or its TLS handshake failed or timed out). Anything else (no answer in time, the
|
|
28
|
+
* connection lost or reset once made) may have reached it: a send may have been made.
|
|
29
|
+
*/
|
|
30
|
+
const UNSENT = /dial tcp|no such host|connection refused|network is unreachable|no route to host|tls handshake|tls: /i;
|
|
31
|
+
|
|
25
32
|
/** A refused or failed call. `retryAfter`: seconds Telegram asks to wait (429). `status` 0: no answer. */
|
|
26
33
|
class TelegramError extends Error {
|
|
27
34
|
/**
|
|
@@ -40,6 +47,14 @@ class TelegramError extends Error {
|
|
|
40
47
|
get transient() {
|
|
41
48
|
return this.status === 0 || this.status === 429 || this.status >= 500;
|
|
42
49
|
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* It may have done what it asked though it failed: no answer came (a timeout, a connection lost or reset), after
|
|
53
|
+
* the request may have reached Telegram. Not when Telegram answered (it refused), nor when no connection was made.
|
|
54
|
+
*/
|
|
55
|
+
get maybeDone() {
|
|
56
|
+
return this.status === 0 && !UNSENT.test(this.message);
|
|
57
|
+
}
|
|
43
58
|
}
|
|
44
59
|
|
|
45
60
|
/**
|
|
@@ -49,13 +64,20 @@ class TelegramError extends Error {
|
|
|
49
64
|
*/
|
|
50
65
|
const asTelegram = (err) => (err instanceof TelegramError ? err : null);
|
|
51
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Seconds an upload or download of `bytes` may take: enough at 200 KB/s, from 30 s up to 300 s. A connection that
|
|
69
|
+
* stops answering fails within that, not after a fixed 5 minutes, which held the worker past its lock (§26.7).
|
|
70
|
+
* @param {number} bytes
|
|
71
|
+
*/
|
|
72
|
+
const transferTimeout = (bytes) => Math.min(300, 30 + Math.ceil(bytes / 200_000));
|
|
73
|
+
|
|
52
74
|
/**
|
|
53
75
|
* Call a Bot API method. Files (filesystem.File by field name) go as multipart, the other params as fields (objects
|
|
54
76
|
* as JSON); without files the params are a JSON body.
|
|
55
77
|
* @param {string} method
|
|
56
78
|
* @param {Record<string, unknown>} [params]
|
|
57
79
|
* @param {Record<string, any>} [files]
|
|
58
|
-
* @param {number} [timeout] seconds
|
|
80
|
+
* @param {number} [timeout] seconds (with files: the upload's, transferTimeout)
|
|
59
81
|
* @returns {any} the method's result
|
|
60
82
|
*/
|
|
61
83
|
function call(method, params = {}, files, timeout = 30) {
|
|
@@ -71,7 +93,7 @@ function call(method, params = {}, files, timeout = 30) {
|
|
|
71
93
|
if (v !== undefined) form.append(k, typeof v === "string" ? v : JSON.stringify(v));
|
|
72
94
|
}
|
|
73
95
|
for (const [k, f] of Object.entries(files)) form.append(k, f);
|
|
74
|
-
res = $http.send({ url, method: "POST", body: form, timeout
|
|
96
|
+
res = $http.send({ url, method: "POST", body: form, timeout });
|
|
75
97
|
} else {
|
|
76
98
|
res = $http.send({ url, method: "POST", body: JSON.stringify(params), headers: { "content-type": "application/json" }, timeout });
|
|
77
99
|
}
|
|
@@ -107,23 +129,53 @@ function storedName(name, path) {
|
|
|
107
129
|
}
|
|
108
130
|
|
|
109
131
|
/**
|
|
110
|
-
*
|
|
132
|
+
* `fn` again at once, up to `tries` times in all, while it fails for now: for what may run twice (getFile, a
|
|
133
|
+
* download). A failure that has to wait longer (429 with retry_after over 5 s) is left to the caller.
|
|
134
|
+
* @template T
|
|
135
|
+
* @param {() => T} fn
|
|
136
|
+
* @param {number} [tries]
|
|
137
|
+
* @returns {T}
|
|
138
|
+
*/
|
|
139
|
+
function retrying(fn, tries = 2) {
|
|
140
|
+
for (let i = 1; ; i++) {
|
|
141
|
+
try {
|
|
142
|
+
return fn();
|
|
143
|
+
} catch (err) {
|
|
144
|
+
const tg = asTelegram(err);
|
|
145
|
+
if (!tg?.transient || i >= tries || tg.retryAfter > 5) throw err;
|
|
146
|
+
sleep(tg.retryAfter ? tg.retryAfter * 1000 : 500 * i);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** getFile's timeout: a small call, which a connection that answers does at once. */
|
|
152
|
+
const GET_FILE_TIMEOUT = 10;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Download a file the bot can see (at most 20 MB, the Bot API's limit), named `name`. A failure that may pass is
|
|
156
|
+
* tried again at once: a connection that drops now and then fails one request, rarely two (§26.4).
|
|
111
157
|
* @param {string} fileId
|
|
112
158
|
* @param {string} name
|
|
159
|
+
* @param {number} [size] its size, when Telegram said: how long the download may take
|
|
113
160
|
*/
|
|
114
|
-
function download(fileId, name) {
|
|
161
|
+
function download(fileId, name, size = 0) {
|
|
115
162
|
const { token, base } = config();
|
|
116
|
-
const info = call("getFile", { file_id: fileId });
|
|
163
|
+
const info = retrying(() => call("getFile", { file_id: fileId }, undefined, GET_FILE_TIMEOUT));
|
|
117
164
|
if (!info?.file_path) throw new TelegramError(400, "getFile: no file_path");
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
165
|
+
const timeout = transferTimeout(size || info.file_size || 20 * 1024 * 1024);
|
|
166
|
+
const file = retrying(() => {
|
|
167
|
+
try {
|
|
168
|
+
return $filesystem.fileFromURL(`${base}/file/bot${token}/${info.file_path}`, timeout);
|
|
169
|
+
} catch (err) {
|
|
170
|
+
const text = String(err).replace(token, "<token>");
|
|
171
|
+
// "failed to download url … (404)": Telegram answered.
|
|
172
|
+
const status = Number((/\((\d{3})\)\s*$/.exec(text) || ["", "0"])[1]);
|
|
173
|
+
throw new TelegramError(status, `download: ${text}`);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
124
176
|
file.originalName = name;
|
|
125
177
|
file.name = storedName(name, info.file_path);
|
|
126
178
|
return file;
|
|
127
179
|
}
|
|
128
180
|
|
|
129
|
-
module.exports = { config, call, download, asTelegram, TelegramError, UPDATES };
|
|
181
|
+
module.exports = { config, call, download, asTelegram, TelegramError, transferTimeout, UPDATES };
|
|
@@ -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`);
|
|
@@ -278,17 +278,26 @@ function ensureMember(app, room, userId) {
|
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
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.
|
|
281
286
|
* @param {core.App} app
|
|
282
287
|
* @param {core.Record} link tg_links
|
|
283
288
|
* @param {core.Record} room
|
|
284
289
|
* @param {any[]} msgs
|
|
290
|
+
* @param {"live" | "retry" | "last"} [mode]
|
|
285
291
|
*/
|
|
286
|
-
function bringOver(app, link, room, msgs) {
|
|
292
|
+
function bringOver(app, link, room, msgs, mode = "live") {
|
|
287
293
|
const chatId = link.getString("chat_id");
|
|
288
|
-
// Delivered twice (a webhook retry): brought over already, or an album item queued twice,
|
|
289
|
-
// 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).
|
|
290
296
|
const seen = new Set();
|
|
291
|
-
|
|
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
|
+
);
|
|
292
301
|
if (!msgs.length) return;
|
|
293
302
|
msgs.sort((a, b) => a.message_id - b.message_id);
|
|
294
303
|
const first = msgs[0];
|
|
@@ -297,9 +306,6 @@ function bringOver(app, link, room, msgs) {
|
|
|
297
306
|
// A service message (a new group photo, a timer, a topic…) has nothing to show: it makes no stand-in, and none joins
|
|
298
307
|
// a private room for it (QA BUG-88).
|
|
299
308
|
if (!notes.length && !parts.some((p) => p.files.length) && !parts.some((p) => p.text.trim())) return;
|
|
300
|
-
const author = personOf(app, first);
|
|
301
|
-
ensureMember(app, room, author.id);
|
|
302
|
-
|
|
303
309
|
/** @type {any[]} */
|
|
304
310
|
const files = [];
|
|
305
311
|
for (const f of parts.flatMap((p) => p.files)) {
|
|
@@ -308,12 +314,17 @@ function bringOver(app, link, room, msgs) {
|
|
|
308
314
|
continue;
|
|
309
315
|
}
|
|
310
316
|
try {
|
|
311
|
-
files.push(download(f.id, f.name));
|
|
317
|
+
files.push(download(f.id, f.name, f.size));
|
|
312
318
|
} catch (err) {
|
|
313
|
-
|
|
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;
|
|
314
323
|
notes.push(`[${f.name}: couldn't be brought over from Telegram]`);
|
|
315
324
|
}
|
|
316
325
|
}
|
|
326
|
+
const author = personOf(app, first);
|
|
327
|
+
ensureMember(app, room, author.id);
|
|
317
328
|
const reply = first.reply_to_message ? mapped(app, chatId, first.reply_to_message.message_id, first.reply_to_message.date) : null;
|
|
318
329
|
const known = !!reply && reply.getString("room") === room.id && !reply.getBool("deleted");
|
|
319
330
|
const textOf = (/** @type {boolean} */ withReply) =>
|
|
@@ -325,6 +336,11 @@ function bringOver(app, link, room, msgs) {
|
|
|
325
336
|
// one message comes as several: the first with the files and the reply, the rest after it.
|
|
326
337
|
const save = (/** @type {boolean} */ withReply) =>
|
|
327
338
|
app.runInTransaction((tx) => {
|
|
339
|
+
// Brought over meanwhile: Telegram sent it again while this one was downloading its files (a webhook retry
|
|
340
|
+
// after a slow answer). Checked again here, where writes go one at a time: the forgetStale below would
|
|
341
|
+
// otherwise take the first one's mappings for stale, and the worker, finding it from nowhere, would send the
|
|
342
|
+
// first one back to the group (QA BUG-291).
|
|
343
|
+
if (msgs.some((m) => partsMapped(tx, chatId, m.message_id, m.date).length)) return;
|
|
328
344
|
const pieces = split(textOf(withReply));
|
|
329
345
|
const ids = orderedIds(pieces.length);
|
|
330
346
|
const rec = new Record(tx.findCollectionByNameOrId("messages"));
|
|
@@ -361,7 +377,8 @@ function editOver(app, msg) {
|
|
|
361
377
|
const chatId = String(msg.chat.id);
|
|
362
378
|
const rows = partsMapped(app, chatId, msg.message_id, msg.date);
|
|
363
379
|
const rec = rows[0]?.getInt("part") === 0 ? findMessage(app, rows[0].getString("message")) : null;
|
|
364
|
-
if (!rec
|
|
380
|
+
if (!rec) return editWaiting(app, chatId, msg);
|
|
381
|
+
if (rec.getBool("deleted") || rec.getString("forwarded_from")) return;
|
|
365
382
|
const quoted = quoteLines(msg, !!rec.getString("reply_to"));
|
|
366
383
|
const { text, notes } = partsOf(msg);
|
|
367
384
|
const lines = [forwardLine(msg), ...quoted, text, ...notes].filter(Boolean);
|
|
@@ -417,6 +434,85 @@ function queueAlbumItem(app, msg) {
|
|
|
417
434
|
app.save(row);
|
|
418
435
|
}
|
|
419
436
|
|
|
437
|
+
/**
|
|
438
|
+
* The queued fetches of a chat (Telegram messages waiting for their files, §26.4).
|
|
439
|
+
* @param {core.App} app
|
|
440
|
+
* @param {string} chatId
|
|
441
|
+
*/
|
|
442
|
+
const fetchesOf = (app, chatId) =>
|
|
443
|
+
app.findRecordsByFilter("tg_queue", "op = 'fetch' && chat_id = {:chat}", "created", 0, 0, { chat: chatId }).filter((r) => !!r);
|
|
444
|
+
|
|
445
|
+
/** @param {core.Record} row a fetch */
|
|
446
|
+
const fetchMessages = (row) => /** @type {any[]} */ (JSON.parse(row.getString("payload") || "null")?.msgs || []);
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* The ids of a chat's Telegram messages waiting for their files.
|
|
450
|
+
* @param {core.App} app
|
|
451
|
+
* @param {string} chatId
|
|
452
|
+
*/
|
|
453
|
+
function fetching(app, chatId) {
|
|
454
|
+
return new Set(fetchesOf(app, chatId).flatMap((r) => fetchMessages(r).map((m) => m.message_id)));
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Queue Telegram messages whose files couldn't be downloaded for now: the worker brings them over later (fetchOver).
|
|
459
|
+
* @param {core.App} app
|
|
460
|
+
* @param {string} chatId
|
|
461
|
+
* @param {any[]} msgs
|
|
462
|
+
* @param {string} error
|
|
463
|
+
* @returns {boolean} queued
|
|
464
|
+
*/
|
|
465
|
+
function queueFetch(app, chatId, msgs, error) {
|
|
466
|
+
try {
|
|
467
|
+
const row = new Record(app.findCollectionByNameOrId("tg_queue"));
|
|
468
|
+
row.set("op", "fetch");
|
|
469
|
+
row.set("chat_id", chatId);
|
|
470
|
+
row.set("payload", { msgs });
|
|
471
|
+
row.set("tries", 1);
|
|
472
|
+
row.set("next_at", new DateTime(new Date(Date.now() + 2000).toISOString()));
|
|
473
|
+
row.set("error", error.slice(0, 1000));
|
|
474
|
+
app.save(row);
|
|
475
|
+
return true;
|
|
476
|
+
} catch (err) {
|
|
477
|
+
// Too big to queue (a payload over 64 KB): it comes over now, saying what couldn't.
|
|
478
|
+
app.logger().error("telegram: fetch", "chat", chatId, "error", String(err));
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* An edit of a Telegram message still waiting for its files: it comes over as edited.
|
|
485
|
+
* @param {core.App} app
|
|
486
|
+
* @param {string} chatId
|
|
487
|
+
* @param {any} msg
|
|
488
|
+
*/
|
|
489
|
+
function editWaiting(app, chatId, msg) {
|
|
490
|
+
const has = (/** @type {core.Record} */ row) => fetchMessages(row).some((m) => m.message_id === msg.message_id);
|
|
491
|
+
if (!fetchesOf(app, chatId).some(has)) return;
|
|
492
|
+
// Read again in a transaction: the worker may be saving the row (its tries) meanwhile.
|
|
493
|
+
app.runInTransaction((tx) => {
|
|
494
|
+
for (const row of fetchesOf(tx, chatId).filter(has)) {
|
|
495
|
+
row.set("payload", { msgs: fetchMessages(row).map((m) => (m.message_id === msg.message_id ? msg : m)) });
|
|
496
|
+
tx.save(row);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* The worker's try at a queued fetch: its messages come over with their files, or (`last`) with what still fails
|
|
503
|
+
* said in their text. Fails, for the worker to try again, while a file fails to download for now.
|
|
504
|
+
* @param {core.App} app
|
|
505
|
+
* @param {core.Record} row
|
|
506
|
+
* @param {boolean} last
|
|
507
|
+
*/
|
|
508
|
+
function fetchOver(app, row, last) {
|
|
509
|
+
const chatId = row.getString("chat_id");
|
|
510
|
+
const link = linkOfChat(app, chatId);
|
|
511
|
+
const room = link && activeLink(app, link);
|
|
512
|
+
if (!link || !room) return;
|
|
513
|
+
bringOver(app, link, room, fetchMessages(row), last ? "last" : "retry");
|
|
514
|
+
}
|
|
515
|
+
|
|
420
516
|
/**
|
|
421
517
|
* Take an album's queued items, so only one caller brings it over.
|
|
422
518
|
* @param {core.App} app
|
|
@@ -586,4 +682,4 @@ function webhook(e) {
|
|
|
586
682
|
return e.json(200, { ok: true });
|
|
587
683
|
}
|
|
588
684
|
|
|
589
|
-
module.exports = { webhook, handleUpdate, flushAlbums, mapped };
|
|
685
|
+
module.exports = { webhook, handleUpdate, flushAlbums, fetchOver, mapped };
|
|
@@ -155,4 +155,15 @@ function tellLinkChange(app, link, was, now, why = "") {
|
|
|
155
155
|
if (now) tellRoom(app, now, `${groupName(link)} was connected to this room.`);
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
module.exports = {
|
|
158
|
+
module.exports = {
|
|
159
|
+
linkOfChat,
|
|
160
|
+
activeLink,
|
|
161
|
+
linksOfRoom,
|
|
162
|
+
ensureLink,
|
|
163
|
+
moveLink,
|
|
164
|
+
beforeLinkSave,
|
|
165
|
+
linkedRoom,
|
|
166
|
+
tellLinkChange,
|
|
167
|
+
groupName,
|
|
168
|
+
tellRoom,
|
|
169
|
+
};
|
|
@@ -4,8 +4,9 @@
|
|
|
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`);
|
|
8
|
-
const { linkOfChat, linksOfRoom } = require(`${__hooks}/telegram/links.js`);
|
|
7
|
+
const { asTelegram, call, transferTimeout } = require(`${__hooks}/telegram/api.js`);
|
|
8
|
+
const { groupName, linkOfChat, linksOfRoom, tellRoom } = require(`${__hooks}/telegram/links.js`);
|
|
9
|
+
const { tidy } = require(`${__hooks}/lib/text.js`);
|
|
9
10
|
|
|
10
11
|
/** Telegram's limits: a caption's length, and the largest photo it takes as a photo (bigger ones go as files). */
|
|
11
12
|
const MAX_CAPTION = 1024;
|
|
@@ -237,7 +238,7 @@ function filesOf(app, fsys, source) {
|
|
|
237
238
|
const kind = sticker ? "sticker" : PHOTO_EXT.has(ext) && size <= MAX_PHOTO ? "photo" : VIDEO_EXT.has(ext) ? "video" : "document";
|
|
238
239
|
const file = fsys.getReuploadableFile(key, true);
|
|
239
240
|
file.name = names[name] || name.replace(/_[a-z0-9]{10}(\.[^.]*)?$/i, "$1");
|
|
240
|
-
return { kind, file };
|
|
241
|
+
return { kind, file, size };
|
|
241
242
|
});
|
|
242
243
|
}
|
|
243
244
|
|
|
@@ -250,23 +251,26 @@ const SEND = /** @type {Record<string, string>} */ ({
|
|
|
250
251
|
|
|
251
252
|
/**
|
|
252
253
|
* Send a message to a group: its text (with its files as their caption when it fits), its files as one or two
|
|
253
|
-
* albums, as a reply when the message it answers is there.
|
|
254
|
+
* albums, as a reply when the message it answers is there. Those are its steps, sent in turn: `done` of them were
|
|
255
|
+
* sent by an earlier try, which failed at the next one, and `sent(n)` is told after each (the worker keeps it), so a
|
|
256
|
+
* try again sends the rest, neither the steps already in the group again nor nothing more (§26.7).
|
|
254
257
|
* @param {core.App} app
|
|
255
258
|
* @param {string} messageId
|
|
256
259
|
* @param {string} chatId
|
|
260
|
+
* @param {number} [done]
|
|
261
|
+
* @param {(n: number) => void} [sent]
|
|
257
262
|
*/
|
|
258
|
-
function send(app, messageId, chatId) {
|
|
263
|
+
function send(app, messageId, chatId, done = 0, sent = () => {}) {
|
|
259
264
|
const msg = findMessage(app, messageId);
|
|
260
265
|
if (!msg || msg.getBool("deleted")) return;
|
|
261
266
|
const link = linkOfChat(app, chatId);
|
|
262
267
|
if (!link?.getBool("enabled") || link.getString("room") !== msg.getString("room")) return;
|
|
263
|
-
// Came from this group, or already sent (a retry after the worker stopped halfway).
|
|
264
|
-
if (mappings(app, messageId, chatId).length) return;
|
|
268
|
+
// Came from this group, or already sent (a retry after the worker stopped halfway, before it kept the steps).
|
|
269
|
+
if (!done && mappings(app, messageId, chatId).length) return;
|
|
265
270
|
|
|
266
271
|
const { html, plain, source } = contentOf(app, msg, link.getBool("anonymous"));
|
|
267
272
|
const replyTo = msg.getString("reply_to") ? mappings(app, msg.getString("reply_to"), chatId)[0] : null;
|
|
268
|
-
|
|
269
|
-
let reply = replyTo ? { reply_parameters: { message_id: replyTo.getInt("tg_message_id"), allow_sending_without_reply: true } } : {};
|
|
273
|
+
const reply = replyTo ? { reply_parameters: { message_id: replyTo.getInt("tg_message_id"), allow_sending_without_reply: true } } : {};
|
|
270
274
|
|
|
271
275
|
const fsys = app.newFilesystem();
|
|
272
276
|
try {
|
|
@@ -274,44 +278,67 @@ function send(app, messageId, chatId) {
|
|
|
274
278
|
const groups = [files.filter((f) => f.kind !== "document"), files.filter((f) => f.kind === "document")].filter((g) => g.length);
|
|
275
279
|
// A sticker takes no caption: its text goes before it.
|
|
276
280
|
const captioned = groups.length && files[0].kind !== "sticker";
|
|
277
|
-
|
|
281
|
+
const caption = captioned && [...plain].length <= MAX_CAPTION ? { caption: html, parse_mode: "HTML" } : null;
|
|
282
|
+
/** @type {((reply: Record<string, unknown>) => void)[]} */
|
|
283
|
+
const steps = [];
|
|
278
284
|
// No text goes alone when there is none: an anonymous link's sticker, or a forward of a deleted message.
|
|
279
285
|
if (!caption && html) {
|
|
280
|
-
|
|
281
|
-
|
|
286
|
+
steps.push((r) =>
|
|
287
|
+
remember(app, messageId, chatId, call("sendMessage", { chat_id: chatId, text: html, parse_mode: "HTML", ...r }), "text"),
|
|
288
|
+
);
|
|
282
289
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
} else {
|
|
293
|
-
/** @type {Record<string, any>} */
|
|
294
|
-
const attach = {};
|
|
295
|
-
const media = group.map((f, i) => {
|
|
296
|
-
attach[`file${i}`] = f.file;
|
|
297
|
-
return {
|
|
298
|
-
type: f.kind,
|
|
299
|
-
media: `attach://file${i}`,
|
|
300
|
-
...(f.kind === "video" ? STREAM : {}),
|
|
301
|
-
...(i === 0 && caption ? caption : {}),
|
|
302
|
-
};
|
|
303
|
-
});
|
|
304
|
-
const sent = call("sendMediaGroup", { chat_id: chatId, media, ...reply }, attach);
|
|
305
|
-
for (let i = 0; i < sent.length; i++) remember(app, messageId, chatId, sent[i], i === 0 && caption ? "caption" : "media");
|
|
306
|
-
}
|
|
307
|
-
caption = null;
|
|
308
|
-
reply = {};
|
|
290
|
+
steps.push(
|
|
291
|
+
...groups.map(
|
|
292
|
+
(group, g) => (/** @type {Record<string, unknown>} */ r) => sendGroup(app, messageId, chatId, group, g === 0 ? caption : null, r),
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
// The first step answers what it replies to, the rest follow it.
|
|
296
|
+
for (let i = done; i < steps.length; i++) {
|
|
297
|
+
steps[i](i === 0 ? reply : {});
|
|
298
|
+
if (i + 1 < steps.length) sent(i + 1);
|
|
309
299
|
}
|
|
310
300
|
} finally {
|
|
311
301
|
fsys.close();
|
|
312
302
|
}
|
|
313
303
|
}
|
|
314
304
|
|
|
305
|
+
/**
|
|
306
|
+
* One album of a message (or its one file), with the caption on its first item.
|
|
307
|
+
* @param {core.App} app
|
|
308
|
+
* @param {string} messageId
|
|
309
|
+
* @param {string} chatId
|
|
310
|
+
* @param {{ kind: string, file: any, size: number }[]} group
|
|
311
|
+
* @param {Record<string, unknown> | null} caption
|
|
312
|
+
* @param {Record<string, unknown>} reply
|
|
313
|
+
*/
|
|
314
|
+
function sendGroup(app, messageId, chatId, group, caption, reply) {
|
|
315
|
+
const timeout = transferTimeout(group.reduce((n, f) => n + f.size, 0));
|
|
316
|
+
if (group.length === 1) {
|
|
317
|
+
const f = group[0];
|
|
318
|
+
const sent = call(
|
|
319
|
+
SEND[f.kind],
|
|
320
|
+
{ chat_id: chatId, ...(f.kind === "video" ? STREAM : {}), ...(caption || {}), ...reply },
|
|
321
|
+
{ [f.kind]: f.file },
|
|
322
|
+
timeout,
|
|
323
|
+
);
|
|
324
|
+
remember(app, messageId, chatId, sent, caption ? "caption" : "media");
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
/** @type {Record<string, any>} */
|
|
328
|
+
const attach = {};
|
|
329
|
+
const media = group.map((f, i) => {
|
|
330
|
+
attach[`file${i}`] = f.file;
|
|
331
|
+
return {
|
|
332
|
+
type: f.kind,
|
|
333
|
+
media: `attach://file${i}`,
|
|
334
|
+
...(f.kind === "video" ? STREAM : {}),
|
|
335
|
+
...(i === 0 && caption ? caption : {}),
|
|
336
|
+
};
|
|
337
|
+
});
|
|
338
|
+
const sent = call("sendMediaGroup", { chat_id: chatId, media, ...reply }, attach, timeout);
|
|
339
|
+
for (let i = 0; i < sent.length; i++) remember(app, messageId, chatId, sent[i], i === 0 && caption ? "caption" : "media");
|
|
340
|
+
}
|
|
341
|
+
|
|
315
342
|
/**
|
|
316
343
|
* Pass an edit on: the Telegram messages that hold the text (sent from here) get the new one.
|
|
317
344
|
* @param {core.App} app
|
|
@@ -364,4 +391,57 @@ function remove(app, messageId, chatId) {
|
|
|
364
391
|
}
|
|
365
392
|
}
|
|
366
393
|
|
|
367
|
-
|
|
394
|
+
/** A group's notices of undelivered messages: one in this long at most, so a group that refuses everything doesn't flood the room. */
|
|
395
|
+
const UNDELIVERED_GAP_MS = 5 * 60_000;
|
|
396
|
+
/** How much of the message's text the notice quotes, in characters. */
|
|
397
|
+
const UNDELIVERED_QUOTE = 40;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Why a send failed, for the room: what Telegram said ("Bad Request: chat not found", without the method), or that
|
|
401
|
+
* it didn't answer. Never the request itself (api.js keeps the bot's token out of its errors anyway).
|
|
402
|
+
* @param {unknown} err
|
|
403
|
+
*/
|
|
404
|
+
function failureOf(err) {
|
|
405
|
+
const tg = asTelegram(err);
|
|
406
|
+
if (!tg) return "an error on this server";
|
|
407
|
+
if (tg.status === 0) return "Telegram didn't answer";
|
|
408
|
+
const said = tidy(tg.message.replace(/^[A-Za-z]+: /, ""));
|
|
409
|
+
return said ? [...said].slice(0, 200).join("") : `Telegram refused it (${tg.status})`;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* The worker dropped a send (§26.7): the room is told, in a service message, that the message didn't reach the group
|
|
414
|
+
* (or only partly, or may not have), naming its author (not in an anonymous message) and quoting its text. Not when it was
|
|
415
|
+
* deleted meanwhile or the link no longer passes this room's messages, and once per group in UNDELIVERED_GAP_MS.
|
|
416
|
+
* A failure here is logged: the send is dropped whatever happens.
|
|
417
|
+
* @param {core.App} app
|
|
418
|
+
* @param {string} messageId
|
|
419
|
+
* @param {string} chatId
|
|
420
|
+
* @param {unknown} err
|
|
421
|
+
* @param {boolean} unsure it may have been sent (no answer, §26.7)
|
|
422
|
+
* @param {boolean} [partly] some of its steps were sent (its text, an album) before the one that failed
|
|
423
|
+
*/
|
|
424
|
+
function tellUndelivered(app, messageId, chatId, err, unsure, partly = false) {
|
|
425
|
+
try {
|
|
426
|
+
const msg = findMessage(app, messageId);
|
|
427
|
+
if (!msg || msg.getBool("deleted")) return;
|
|
428
|
+
const link = linkOfChat(app, chatId);
|
|
429
|
+
if (!link?.getBool("enabled") || link.getString("room") !== msg.getString("room")) return;
|
|
430
|
+
const key = `telegram.undelivered.${chatId}`;
|
|
431
|
+
const last = Number(app.store().get(key) || 0);
|
|
432
|
+
if (Date.now() - last < UNDELIVERED_GAP_MS) return;
|
|
433
|
+
app.store().set(key, Date.now());
|
|
434
|
+
|
|
435
|
+
const author = msg.getBool("anonymous") ? "" : tidy(userName(app, msg.getString("author")));
|
|
436
|
+
const text = tidy(msg.getString("text"));
|
|
437
|
+
const chars = [...text];
|
|
438
|
+
const quote = chars.length ? ` ("${chars.slice(0, UNDELIVERED_QUOTE).join("")}${chars.length > UNDELIVERED_QUOTE ? "…" : ""}")` : "";
|
|
439
|
+
const what = `A message${author ? ` from ${author}` : ""}${quote}`;
|
|
440
|
+
const verb = unsure ? "may not have reached" : partly ? "only partly reached" : "wasn't delivered to";
|
|
441
|
+
tellRoom(app, msg.getString("room"), `${what} ${verb} ${groupName(link).replace(/^A /, "a ")}: ${failureOf(err)}.`);
|
|
442
|
+
} catch (e) {
|
|
443
|
+
app.logger().error("telegram: undelivered notice", "message", messageId, "chat", chatId, "error", String(e));
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
module.exports = { queueCreate, queueUpdate, changeOf, send, edit, remove, tellUndelivered };
|