levix-bot 4.4.0 → 4.6.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 +3 -2
- package/package.json +1 -1
- package/public/dashboard/assets/index-Con1xdhN.js +515 -0
- package/public/dashboard/assets/index-De78KNvv.css +2 -0
- package/public/dashboard/index.html +2 -2
- package/scheduler.cjs +66 -7
- package/src/commands/autodelete.cjs +213 -0
- package/src/commands/pack.cjs +46 -25
- package/src/commands/packs.cjs +4 -1
- package/src/commands/shortlink.cjs +273 -66
- package/src/commands/stickers.cjs +26 -8
- package/src/config/defaults.cjs +1 -0
- package/src/config/settings.cjs +10 -0
- package/src/core/session.js +9 -1
- package/src/db/db.cjs +63 -0
- package/src/db/store.cjs +259 -3
- package/src/handlers/command.handler.js +1 -0
- package/src/handlers/message.handler.js +15 -0
- package/src/panel/feedback.cjs +43 -14
- package/src/routes/dashboard.api.esm.js +212 -15
- package/src/services/autoDelete.cjs +576 -0
- package/src/services/llmDiscovery.cjs +32 -10
- package/src/stickers/errors.cjs +7 -6
- package/src/stickers/library.cjs +118 -13
- package/src/utils/bidi.cjs +21 -0
- package/src/utils/multipart.esm.js +81 -0
- package/src/utils/storage.cjs +13 -0
- package/src/utils/storage.esm.js +14 -0
- package/public/dashboard/assets/index-5P8GnKLg.css +0 -2
- package/public/dashboard/assets/index-DXzno3X6.js +0 -515
package/src/db/store.cjs
CHANGED
|
@@ -861,6 +861,17 @@ function countDebts(settled = false) {
|
|
|
861
861
|
// A job is `{ id, type, targetJid, message, cronString?, date?, status,
|
|
862
862
|
// creatorJid, lastRunAt?, lastDeliveryStatus?, lastError? }`.
|
|
863
863
|
|
|
864
|
+
/** The `media` column holds a small JSON object, or NULL for a text job. */
|
|
865
|
+
function parseMediaColumn(value) {
|
|
866
|
+
if (!value) return undefined;
|
|
867
|
+
try {
|
|
868
|
+
const parsed = JSON.parse(value);
|
|
869
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : undefined;
|
|
870
|
+
} catch {
|
|
871
|
+
return undefined;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
864
875
|
function scheduleRow(row) {
|
|
865
876
|
if (!row) return null;
|
|
866
877
|
return {
|
|
@@ -876,6 +887,7 @@ function scheduleRow(row) {
|
|
|
876
887
|
lastRunAt: row.last_run_at ?? null,
|
|
877
888
|
lastDeliveryStatus: row.last_delivery_status ?? null,
|
|
878
889
|
lastError: row.last_error ?? null,
|
|
890
|
+
media: parseMediaColumn(row.media),
|
|
879
891
|
};
|
|
880
892
|
}
|
|
881
893
|
|
|
@@ -891,8 +903,8 @@ function saveSchedule(job) {
|
|
|
891
903
|
q(
|
|
892
904
|
`INSERT INTO schedules
|
|
893
905
|
(id, type, target_jid, message, cron_string, date, status, creator_jid,
|
|
894
|
-
created_at, last_run_at, last_delivery_status, last_error)
|
|
895
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
906
|
+
created_at, last_run_at, last_delivery_status, last_error, media)
|
|
907
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
896
908
|
ON CONFLICT(id) DO UPDATE SET
|
|
897
909
|
type = excluded.type,
|
|
898
910
|
target_jid = excluded.target_jid,
|
|
@@ -903,7 +915,8 @@ function saveSchedule(job) {
|
|
|
903
915
|
creator_jid = excluded.creator_jid,
|
|
904
916
|
last_run_at = excluded.last_run_at,
|
|
905
917
|
last_delivery_status = excluded.last_delivery_status,
|
|
906
|
-
last_error = excluded.last_error
|
|
918
|
+
last_error = excluded.last_error,
|
|
919
|
+
media = excluded.media`,
|
|
907
920
|
).run(
|
|
908
921
|
String(job.id),
|
|
909
922
|
job.type,
|
|
@@ -917,6 +930,7 @@ function saveSchedule(job) {
|
|
|
917
930
|
job.lastRunAt ?? null,
|
|
918
931
|
job.lastDeliveryStatus ?? null,
|
|
919
932
|
job.lastError ?? null,
|
|
933
|
+
job.media ? JSON.stringify(job.media) : null,
|
|
920
934
|
);
|
|
921
935
|
return getSchedule(job.id);
|
|
922
936
|
}
|
|
@@ -1587,6 +1601,231 @@ function stickerPackItemCount(packId) {
|
|
|
1587
1601
|
return q("SELECT COUNT(*) AS n FROM sticker_pack_items WHERE pack_id = ?").get(packId).n;
|
|
1588
1602
|
}
|
|
1589
1603
|
|
|
1604
|
+
// ===================================================================
|
|
1605
|
+
// --- Keyword auto-delete ---
|
|
1606
|
+
// ===================================================================
|
|
1607
|
+
//
|
|
1608
|
+
// Rules are operator configuration. The per-message matcher reads an in-memory
|
|
1609
|
+
// copy so it does not JSON-parse every rule on every incoming message. Any
|
|
1610
|
+
// write invalidates that copy. Kept copies (keepCopy) are account-scoped and
|
|
1611
|
+
// wiped on unlink; the rules themselves stay.
|
|
1612
|
+
|
|
1613
|
+
const AUTO_DELETE_LOG_CAP = 5000;
|
|
1614
|
+
|
|
1615
|
+
let autoDeleteRuleCache = null;
|
|
1616
|
+
|
|
1617
|
+
function invalidateAutoDeleteRuleCache() {
|
|
1618
|
+
autoDeleteRuleCache = null;
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
function autoDeleteRuleRow(row) {
|
|
1622
|
+
if (!row) return null;
|
|
1623
|
+
return {
|
|
1624
|
+
id: row.id,
|
|
1625
|
+
name: row.name || null,
|
|
1626
|
+
enabled: Boolean(row.enabled),
|
|
1627
|
+
keywords: parseJson(row.keywords, []),
|
|
1628
|
+
match: row.match_mode,
|
|
1629
|
+
chatScope: row.chat_scope,
|
|
1630
|
+
senders: {
|
|
1631
|
+
mode: row.senders_mode,
|
|
1632
|
+
list: parseJson(row.senders_list, []),
|
|
1633
|
+
},
|
|
1634
|
+
includeOwn: Boolean(row.include_own),
|
|
1635
|
+
forEveryone: Boolean(row.for_everyone),
|
|
1636
|
+
keepCopy: Boolean(row.keep_copy),
|
|
1637
|
+
deletedCount: row.deleted_count || 0,
|
|
1638
|
+
lastDeletedAt: row.last_deleted_at ?? null,
|
|
1639
|
+
createdAt: row.created_at,
|
|
1640
|
+
updatedAt: row.updated_at,
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
function listAutoDeleteRules() {
|
|
1645
|
+
if (!autoDeleteRuleCache) {
|
|
1646
|
+
autoDeleteRuleCache = q("SELECT * FROM auto_delete_rules ORDER BY id ASC")
|
|
1647
|
+
.all()
|
|
1648
|
+
.map(autoDeleteRuleRow);
|
|
1649
|
+
}
|
|
1650
|
+
return autoDeleteRuleCache;
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
function listEnabledAutoDeleteRules() {
|
|
1654
|
+
return listAutoDeleteRules().filter((rule) => rule.enabled);
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
function getAutoDeleteRule(id) {
|
|
1658
|
+
return autoDeleteRuleRow(q("SELECT * FROM auto_delete_rules WHERE id = ?").get(Number(id)));
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
function countAutoDeleteRules() {
|
|
1662
|
+
return q("SELECT COUNT(*) AS n FROM auto_delete_rules").get().n;
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
function insertAutoDeleteRule(rule) {
|
|
1666
|
+
const now = Date.now();
|
|
1667
|
+
const { lastInsertRowid } = q(
|
|
1668
|
+
`INSERT INTO auto_delete_rules
|
|
1669
|
+
(name, enabled, keywords, match_mode, chat_scope, senders_mode, senders_list,
|
|
1670
|
+
include_own, for_everyone, keep_copy, deleted_count, last_deleted_at,
|
|
1671
|
+
created_at, updated_at)
|
|
1672
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, NULL, ?, ?)`,
|
|
1673
|
+
).run(
|
|
1674
|
+
rule.name ?? null,
|
|
1675
|
+
bool(rule.enabled !== false),
|
|
1676
|
+
JSON.stringify(rule.keywords || []),
|
|
1677
|
+
rule.match || "contains",
|
|
1678
|
+
rule.chatScope || "all",
|
|
1679
|
+
rule.senders?.mode || "everyone",
|
|
1680
|
+
JSON.stringify(rule.senders?.list || []),
|
|
1681
|
+
bool(rule.includeOwn),
|
|
1682
|
+
bool(rule.forEveryone !== false),
|
|
1683
|
+
bool(rule.keepCopy),
|
|
1684
|
+
now,
|
|
1685
|
+
now,
|
|
1686
|
+
);
|
|
1687
|
+
invalidateAutoDeleteRuleCache();
|
|
1688
|
+
return getAutoDeleteRule(Number(lastInsertRowid));
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
function updateAutoDeleteRule(id, fields) {
|
|
1692
|
+
const current = getAutoDeleteRule(id);
|
|
1693
|
+
if (!current) return null;
|
|
1694
|
+
const next = {
|
|
1695
|
+
name: fields.name !== undefined ? fields.name : current.name,
|
|
1696
|
+
enabled: fields.enabled !== undefined ? fields.enabled : current.enabled,
|
|
1697
|
+
keywords: fields.keywords !== undefined ? fields.keywords : current.keywords,
|
|
1698
|
+
match: fields.match !== undefined ? fields.match : current.match,
|
|
1699
|
+
chatScope: fields.chatScope !== undefined ? fields.chatScope : current.chatScope,
|
|
1700
|
+
senders: fields.senders !== undefined ? fields.senders : current.senders,
|
|
1701
|
+
includeOwn: fields.includeOwn !== undefined ? fields.includeOwn : current.includeOwn,
|
|
1702
|
+
forEveryone: fields.forEveryone !== undefined ? fields.forEveryone : current.forEveryone,
|
|
1703
|
+
keepCopy: fields.keepCopy !== undefined ? fields.keepCopy : current.keepCopy,
|
|
1704
|
+
};
|
|
1705
|
+
q(
|
|
1706
|
+
`UPDATE auto_delete_rules
|
|
1707
|
+
SET name = ?, enabled = ?, keywords = ?, match_mode = ?, chat_scope = ?,
|
|
1708
|
+
senders_mode = ?, senders_list = ?, include_own = ?, for_everyone = ?,
|
|
1709
|
+
keep_copy = ?, updated_at = ?
|
|
1710
|
+
WHERE id = ?`,
|
|
1711
|
+
).run(
|
|
1712
|
+
next.name ?? null,
|
|
1713
|
+
bool(next.enabled),
|
|
1714
|
+
JSON.stringify(next.keywords || []),
|
|
1715
|
+
next.match,
|
|
1716
|
+
next.chatScope,
|
|
1717
|
+
next.senders?.mode || "everyone",
|
|
1718
|
+
JSON.stringify(next.senders?.list || []),
|
|
1719
|
+
bool(next.includeOwn),
|
|
1720
|
+
bool(next.forEveryone),
|
|
1721
|
+
bool(next.keepCopy),
|
|
1722
|
+
Date.now(),
|
|
1723
|
+
Number(id),
|
|
1724
|
+
);
|
|
1725
|
+
invalidateAutoDeleteRuleCache();
|
|
1726
|
+
return getAutoDeleteRule(id);
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
function deleteAutoDeleteRule(id) {
|
|
1730
|
+
const { changes } = q("DELETE FROM auto_delete_rules WHERE id = ?").run(Number(id));
|
|
1731
|
+
invalidateAutoDeleteRuleCache();
|
|
1732
|
+
return changes > 0;
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
function incrementAutoDeleteCounter(id) {
|
|
1736
|
+
const now = Date.now();
|
|
1737
|
+
const { changes } = q(
|
|
1738
|
+
`UPDATE auto_delete_rules
|
|
1739
|
+
SET deleted_count = deleted_count + 1, last_deleted_at = ?, updated_at = ?
|
|
1740
|
+
WHERE id = ?`,
|
|
1741
|
+
).run(now, now, Number(id));
|
|
1742
|
+
invalidateAutoDeleteRuleCache();
|
|
1743
|
+
return changes > 0;
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
function resetAutoDeleteCounter(id) {
|
|
1747
|
+
q(
|
|
1748
|
+
`UPDATE auto_delete_rules
|
|
1749
|
+
SET deleted_count = 0, last_deleted_at = NULL, updated_at = ?
|
|
1750
|
+
WHERE id = ?`,
|
|
1751
|
+
).run(Date.now(), Number(id));
|
|
1752
|
+
invalidateAutoDeleteRuleCache();
|
|
1753
|
+
return getAutoDeleteRule(id);
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
function autoDeleteLogRow(row) {
|
|
1757
|
+
if (!row) return null;
|
|
1758
|
+
return {
|
|
1759
|
+
id: row.id,
|
|
1760
|
+
ruleId: row.rule_id,
|
|
1761
|
+
chatJid: row.chat_jid,
|
|
1762
|
+
sender: row.sender || null,
|
|
1763
|
+
text: row.text || "",
|
|
1764
|
+
mediaType: row.media_type || "",
|
|
1765
|
+
mode: row.mode,
|
|
1766
|
+
createdAt: row.created_at,
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
function capAutoDeleteLog(max = AUTO_DELETE_LOG_CAP) {
|
|
1771
|
+
const limit = Math.max(0, Number(max) || 0);
|
|
1772
|
+
q(
|
|
1773
|
+
`DELETE FROM auto_delete_log WHERE id NOT IN (
|
|
1774
|
+
SELECT id FROM auto_delete_log ORDER BY created_at DESC, id DESC LIMIT ?
|
|
1775
|
+
)`,
|
|
1776
|
+
).run(limit);
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
function insertAutoDeleteLog(entry) {
|
|
1780
|
+
const now = entry.createdAt ?? Date.now();
|
|
1781
|
+
const { lastInsertRowid } = q(
|
|
1782
|
+
`INSERT INTO auto_delete_log
|
|
1783
|
+
(rule_id, chat_jid, sender, text, media_type, mode, created_at)
|
|
1784
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
1785
|
+
).run(
|
|
1786
|
+
Number(entry.ruleId),
|
|
1787
|
+
entry.chatJid,
|
|
1788
|
+
entry.sender ?? null,
|
|
1789
|
+
entry.text ?? "",
|
|
1790
|
+
entry.mediaType ?? "",
|
|
1791
|
+
entry.mode,
|
|
1792
|
+
now,
|
|
1793
|
+
);
|
|
1794
|
+
capAutoDeleteLog();
|
|
1795
|
+
return autoDeleteLogRow(
|
|
1796
|
+
q("SELECT * FROM auto_delete_log WHERE id = ?").get(Number(lastInsertRowid)),
|
|
1797
|
+
);
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
function listAutoDeleteLog({ ruleId, limit = 50, offset = 0 } = {}) {
|
|
1801
|
+
if (ruleId != null) {
|
|
1802
|
+
return q(
|
|
1803
|
+
`SELECT * FROM auto_delete_log WHERE rule_id = ?
|
|
1804
|
+
ORDER BY created_at DESC, id DESC LIMIT ? OFFSET ?`,
|
|
1805
|
+
)
|
|
1806
|
+
.all(Number(ruleId), Number(limit), Number(offset))
|
|
1807
|
+
.map(autoDeleteLogRow);
|
|
1808
|
+
}
|
|
1809
|
+
return q("SELECT * FROM auto_delete_log ORDER BY created_at DESC, id DESC LIMIT ? OFFSET ?")
|
|
1810
|
+
.all(Number(limit), Number(offset))
|
|
1811
|
+
.map(autoDeleteLogRow);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
function clearAutoDeleteLog(ruleId) {
|
|
1815
|
+
if (ruleId != null) {
|
|
1816
|
+
const { changes } = q("DELETE FROM auto_delete_log WHERE rule_id = ?").run(Number(ruleId));
|
|
1817
|
+
return changes;
|
|
1818
|
+
}
|
|
1819
|
+
const { changes } = q("DELETE FROM auto_delete_log").run();
|
|
1820
|
+
return changes;
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
function clearAutoDeleteOnUnlink() {
|
|
1824
|
+
q("DELETE FROM auto_delete_log").run();
|
|
1825
|
+
q("UPDATE auto_delete_rules SET deleted_count = 0, last_deleted_at = NULL").run();
|
|
1826
|
+
invalidateAutoDeleteRuleCache();
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1590
1829
|
module.exports = {
|
|
1591
1830
|
// Lifecycle
|
|
1592
1831
|
initStore,
|
|
@@ -1720,4 +1959,21 @@ module.exports = {
|
|
|
1720
1959
|
stickerPackReorder,
|
|
1721
1960
|
stickerPackExclusive,
|
|
1722
1961
|
stickerPackItemCount,
|
|
1962
|
+
// Keyword auto-delete
|
|
1963
|
+
AUTO_DELETE_LOG_CAP,
|
|
1964
|
+
listAutoDeleteRules,
|
|
1965
|
+
listEnabledAutoDeleteRules,
|
|
1966
|
+
getAutoDeleteRule,
|
|
1967
|
+
countAutoDeleteRules,
|
|
1968
|
+
insertAutoDeleteRule,
|
|
1969
|
+
updateAutoDeleteRule,
|
|
1970
|
+
deleteAutoDeleteRule,
|
|
1971
|
+
incrementAutoDeleteCounter,
|
|
1972
|
+
resetAutoDeleteCounter,
|
|
1973
|
+
insertAutoDeleteLog,
|
|
1974
|
+
listAutoDeleteLog,
|
|
1975
|
+
clearAutoDeleteLog,
|
|
1976
|
+
capAutoDeleteLog,
|
|
1977
|
+
clearAutoDeleteOnUnlink,
|
|
1978
|
+
invalidateAutoDeleteRuleCache,
|
|
1723
1979
|
};
|
|
@@ -3,6 +3,7 @@ import { handleAntiSpam } from "../middleware/antispam.middleware.js";
|
|
|
3
3
|
import { checkBlacklist } from "../middleware/blacklist.middleware.js";
|
|
4
4
|
import { trackForwardedMessage } from "../middleware/forward-tracking.middleware.js";
|
|
5
5
|
import normalizeJid from "../utils/normalizeJid.esm.js";
|
|
6
|
+
import { getSenderCandidates, isBotAdminInGroup, sameUser } from "../utils/permissions.esm.js";
|
|
6
7
|
import { rememberMessage } from "../utils/recentMessageCache.esm.js";
|
|
7
8
|
import { saveUserMetadata, updateUserLastSeen } from "../utils/storage.esm.js";
|
|
8
9
|
import { handleCommand } from "./command.handler.js";
|
|
@@ -11,6 +12,7 @@ const require = createRequire(import.meta.url);
|
|
|
11
12
|
const logger = require("../utils/logger.cjs");
|
|
12
13
|
const { visibleText } = require("../utils/messageContent.cjs");
|
|
13
14
|
const { detectLang, withLang } = require("../utils/i18n.cjs");
|
|
15
|
+
const autoDelete = require("../services/autoDelete.cjs");
|
|
14
16
|
|
|
15
17
|
// Envelopes that carry no user-visible content: message revokes and edits
|
|
16
18
|
// (protocolMessage), reactions, poll votes, the undecrypted edit envelope, and
|
|
@@ -78,6 +80,19 @@ export async function handleIncomingMessage(sock, m) {
|
|
|
78
80
|
// to the command handler and the user sees nothing happen.
|
|
79
81
|
const body = visibleText(msg.message);
|
|
80
82
|
|
|
83
|
+
// Keyword auto-delete: after content/status skip, before blacklist/commands
|
|
84
|
+
// /moderation. A successful delete stops the rest of the pipeline.
|
|
85
|
+
try {
|
|
86
|
+
const deleted = await autoDelete.handleIncoming(sock, msg, body, {
|
|
87
|
+
sameUser,
|
|
88
|
+
getSenderCandidates,
|
|
89
|
+
isBotAdminInGroup,
|
|
90
|
+
});
|
|
91
|
+
if (deleted) return;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
logger.error({ err: error }, "auto-delete hook failed");
|
|
94
|
+
}
|
|
95
|
+
|
|
81
96
|
// Everything the bot says about this message — a moderation warning, a
|
|
82
97
|
// command's reply — goes out in one language (see utils/i18n.cjs). A
|
|
83
98
|
// command narrows this down to its own arguments.
|
package/src/panel/feedback.cjs
CHANGED
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
//
|
|
15
15
|
// Nothing is sent unless the operator presses Send, and nothing about WhatsApp
|
|
16
16
|
// — no message, chat, contact, credential or key — is ever attached. What goes
|
|
17
|
-
// out is exactly the four fields below
|
|
18
|
-
// describeRuntime().
|
|
17
|
+
// out is exactly the four fields below, an optional file the operator picked
|
|
18
|
+
// themselves, and the runtime facts in describeRuntime().
|
|
19
19
|
|
|
20
20
|
const fs = require("node:fs");
|
|
21
21
|
const logger = require("../utils/logger.cjs");
|
|
@@ -29,6 +29,9 @@ const TOPICS = Object.freeze(["bug", "idea", "question", "praise", "other"]);
|
|
|
29
29
|
const MESSAGE_MIN = 10;
|
|
30
30
|
const MESSAGE_MAX = 2000;
|
|
31
31
|
const CONTACT_MAX = 200;
|
|
32
|
+
// One screenshot or log file rides along as multipart; the receiving end
|
|
33
|
+
// forwards it to Telegram as a document, whose own bot-API cap is 50 MB.
|
|
34
|
+
const ATTACHMENT_MAX = 20 * 1024 * 1024;
|
|
32
35
|
|
|
33
36
|
// One operator, pressing a button. This only exists so a stuck retry loop in a
|
|
34
37
|
// browser tab cannot turn into a flood the receiving end has to absorb.
|
|
@@ -150,32 +153,57 @@ function safeRemoteError(value) {
|
|
|
150
153
|
}
|
|
151
154
|
|
|
152
155
|
/**
|
|
153
|
-
* Forward one validated submission.
|
|
156
|
+
* Forward one validated submission. An attachment (already size-checked by
|
|
157
|
+
* the route) turns the forward into multipart: the fields travel as a JSON
|
|
158
|
+
* "payload" part and the file as "file", and the endpoint delivers it to
|
|
159
|
+
* Telegram as a document instead of a message.
|
|
154
160
|
*
|
|
155
161
|
* @param {{ message: string, topic: string, rating: number|null, contact: string|null }} feedback
|
|
162
|
+
* @param {{ buffer: Buffer, name: string, type: string, size: number } | null} attachment
|
|
156
163
|
* @returns {Promise<{ ok: true } | { ok: false, status: number, error: string }>}
|
|
157
164
|
*/
|
|
158
|
-
async function submitFeedback(feedback) {
|
|
165
|
+
async function submitFeedback(feedback, attachment = null) {
|
|
159
166
|
const endpoint = process.env.LEVIX_FEEDBACK_URL || DEFAULT_ENDPOINT;
|
|
160
167
|
const runtime = describeRuntime();
|
|
161
168
|
|
|
162
169
|
recordAttempt();
|
|
163
170
|
|
|
171
|
+
const envelope = {
|
|
172
|
+
...feedback,
|
|
173
|
+
source: "panel",
|
|
174
|
+
app: { version: runtime.version, platform: runtime.platform, locale: runtime.locale },
|
|
175
|
+
};
|
|
176
|
+
|
|
164
177
|
let response;
|
|
165
178
|
let data = null;
|
|
166
179
|
try {
|
|
180
|
+
let body;
|
|
181
|
+
const headers = {
|
|
182
|
+
"User-Agent": `Levix/${runtime.version} (${runtime.platform})`,
|
|
183
|
+
};
|
|
184
|
+
const hasFile =
|
|
185
|
+
attachment && Buffer.isBuffer(attachment.buffer) && attachment.buffer.length > 0;
|
|
186
|
+
if (hasFile) {
|
|
187
|
+
const form = new FormData();
|
|
188
|
+
form.set("payload", JSON.stringify(envelope));
|
|
189
|
+
form.set(
|
|
190
|
+
"file",
|
|
191
|
+
new Blob([attachment.buffer], {
|
|
192
|
+
type: attachment.type || "application/octet-stream",
|
|
193
|
+
}),
|
|
194
|
+
attachment.name || "attachment",
|
|
195
|
+
);
|
|
196
|
+
body = form;
|
|
197
|
+
} else {
|
|
198
|
+
headers["Content-Type"] = "application/json";
|
|
199
|
+
body = JSON.stringify(envelope);
|
|
200
|
+
}
|
|
167
201
|
response = await fetch(endpoint, {
|
|
168
202
|
method: "POST",
|
|
169
|
-
headers
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
body: JSON.stringify({
|
|
174
|
-
...feedback,
|
|
175
|
-
source: "panel",
|
|
176
|
-
app: { version: runtime.version, platform: runtime.platform, locale: runtime.locale },
|
|
177
|
-
}),
|
|
178
|
-
signal: AbortSignal.timeout(15000),
|
|
203
|
+
headers,
|
|
204
|
+
body,
|
|
205
|
+
// Room for a 20 MB attachment on a slow uplink, not for a hang.
|
|
206
|
+
signal: AbortSignal.timeout(120000),
|
|
179
207
|
});
|
|
180
208
|
data = await response.json().catch(() => null);
|
|
181
209
|
} catch (error) {
|
|
@@ -223,6 +251,7 @@ module.exports = {
|
|
|
223
251
|
MESSAGE_MIN,
|
|
224
252
|
MESSAGE_MAX,
|
|
225
253
|
CONTACT_MAX,
|
|
254
|
+
ATTACHMENT_MAX,
|
|
226
255
|
validateFeedback,
|
|
227
256
|
describeRuntime,
|
|
228
257
|
feedbackRetryAfter,
|