levix-bot 4.9.1 → 5.0.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.
Files changed (245) hide show
  1. package/README.md +57 -3
  2. package/SETUP.md +63 -7
  3. package/app.cjs +159 -46
  4. package/deploy/install.sh +10 -5
  5. package/package.json +9 -6
  6. package/public/dashboard/assets/40af6d88b63079a0-BZ8ojJNu.woff2 +0 -0
  7. package/public/dashboard/assets/481acf6760031cee-BypgxfGb.woff2 +0 -0
  8. package/public/dashboard/assets/6d3351fc535f8ef3-D2-eand5.woff2 +0 -0
  9. package/public/dashboard/assets/8ab0af53a3bda9d3-CyCXRvzh.woff2 +0 -0
  10. package/public/dashboard/assets/8f262393e9249e61-TQp-UTiE.woff2 +0 -0
  11. package/public/dashboard/assets/94d65887a83f1282-DmpS2jIq.woff2 +0 -0
  12. package/public/dashboard/assets/99e400330f36ae77-D3oMJlXt.woff2 +0 -0
  13. package/public/dashboard/assets/9d1f41b13e519d90-BVNSOH3d.woff2 +0 -0
  14. package/public/dashboard/assets/a005256cb957ecc7-CoYeBiSI.woff2 +0 -0
  15. package/public/dashboard/assets/b19c2080652a3c2b-DIC32ArD.woff2 +0 -0
  16. package/public/dashboard/assets/c4064400db3750ce-BehTv68k.woff2 +0 -0
  17. package/public/dashboard/assets/c69d345fa1068c2d-CmI34b-g.woff2 +0 -0
  18. package/public/dashboard/assets/cc27b30471250057-6fWv1k7M.woff2 +0 -0
  19. package/public/dashboard/assets/d8c30647abfeffe6-eXO_dkmS.woff2 +0 -0
  20. package/public/dashboard/assets/eb83d8d40af4411f-Buze_B52.woff2 +0 -0
  21. package/public/dashboard/assets/effc6711249e7349-qRpaaN48.woff2 +0 -0
  22. package/public/dashboard/assets/gateway-BDYet0pI.css +1 -0
  23. package/public/dashboard/assets/gateway-BYt6-s1O.js +1 -0
  24. package/public/dashboard/assets/index-B9wFpD_z.css +2 -0
  25. package/public/dashboard/assets/index-DWcQmu0_.js +124 -0
  26. package/public/dashboard/assets/index-Da-j89O-.js +458 -0
  27. package/public/dashboard/font-licenses/jetbrainsmono-OFL.txt +93 -0
  28. package/public/dashboard/font-licenses/plusjakartasans-OFL.txt +93 -0
  29. package/public/dashboard/font-licenses/tajawal-OFL.txt +93 -0
  30. package/public/dashboard/gateway.html +17 -0
  31. package/public/dashboard/index.html +3 -5
  32. package/public/ui-boot.js +33 -14
  33. package/scheduler.cjs +76 -23
  34. package/src/auth/auth-storage.cjs +54 -7
  35. package/src/auth/use-database-auth-state.js +70 -39
  36. package/src/automation/engine.js +412 -0
  37. package/src/automation/limits.cjs +6 -0
  38. package/src/automation/manage.js +290 -0
  39. package/src/automation/reactions.js +471 -0
  40. package/src/automation/rules.cjs +114 -0
  41. package/src/automation/status.cjs +76 -0
  42. package/src/backup/account.cjs +29 -0
  43. package/src/backup/activation.cjs +247 -0
  44. package/src/backup/content.cjs +36 -0
  45. package/src/backup/create.cjs +280 -0
  46. package/src/backup/extensions.cjs +101 -0
  47. package/src/backup/format.cjs +565 -0
  48. package/src/backup/jobs.cjs +111 -0
  49. package/src/backup/journal.cjs +41 -0
  50. package/src/backup/plan.cjs +223 -0
  51. package/src/backup/recovery.cjs +110 -0
  52. package/src/backup/restore.cjs +460 -0
  53. package/src/backup/uploads.cjs +146 -0
  54. package/src/bootstrap/core.js +50 -12
  55. package/src/bootstrap/panel.js +53 -2
  56. package/src/bootstrap/session-startup-policy.js +8 -1
  57. package/src/cli.js +163 -22
  58. package/src/commands/autodelete.cjs +41 -57
  59. package/src/commands/autoschedule.cjs +7 -33
  60. package/src/commands/blacklist.cjs +17 -35
  61. package/src/commands/block.cjs +4 -18
  62. package/src/commands/calc.cjs +22 -74
  63. package/src/commands/debt.cjs +28 -59
  64. package/src/commands/deleteschedule.cjs +7 -23
  65. package/src/commands/digit.cjs +5 -39
  66. package/src/commands/gemini.cjs +135 -218
  67. package/src/commands/group/add.cjs +28 -76
  68. package/src/commands/group/all.cjs +4 -15
  69. package/src/commands/group/antilink.cjs +28 -41
  70. package/src/commands/group/antispam.cjs +25 -35
  71. package/src/commands/group/approveall.cjs +7 -21
  72. package/src/commands/group/clearwarns.cjs +4 -18
  73. package/src/commands/group/deletenote.cjs +4 -18
  74. package/src/commands/group/demote.cjs +6 -23
  75. package/src/commands/group/flagged.cjs +10 -19
  76. package/src/commands/group/kick.cjs +26 -36
  77. package/src/commands/group/media.cjs +21 -36
  78. package/src/commands/group/members.cjs +16 -28
  79. package/src/commands/group/note.cjs +3 -14
  80. package/src/commands/group/policy.cjs +16 -37
  81. package/src/commands/group/promote.cjs +5 -22
  82. package/src/commands/group/removeall.cjs +6 -38
  83. package/src/commands/group/rules.cjs +4 -19
  84. package/src/commands/group/save.cjs +4 -18
  85. package/src/commands/group/setname.cjs +5 -19
  86. package/src/commands/group/setpp.cjs +8 -26
  87. package/src/commands/group/setrules.cjs +4 -15
  88. package/src/commands/group/setwarn.cjs +6 -23
  89. package/src/commands/group/tagadmins.cjs +5 -16
  90. package/src/commands/group/warn.cjs +5 -16
  91. package/src/commands/group/warns.cjs +12 -33
  92. package/src/commands/group/welcome.cjs +17 -34
  93. package/src/commands/group.cjs +91 -68
  94. package/src/commands/help.cjs +48 -89
  95. package/src/commands/lang.cjs +38 -97
  96. package/src/commands/listschedules.cjs +19 -30
  97. package/src/commands/loop.cjs +9 -55
  98. package/src/commands/md.cjs +2 -13
  99. package/src/commands/memory.cjs +53 -87
  100. package/src/commands/mod.cjs +40 -79
  101. package/src/commands/notes.cjs +4 -15
  102. package/src/commands/pack.cjs +26 -49
  103. package/src/commands/packs.cjs +8 -18
  104. package/src/commands/perm.cjs +48 -51
  105. package/src/commands/ping.cjs +2 -10
  106. package/src/commands/poll.cjs +5 -38
  107. package/src/commands/prayer.cjs +55 -36
  108. package/src/commands/qr.cjs +4 -18
  109. package/src/commands/rand.cjs +9 -58
  110. package/src/commands/restart.cjs +15 -33
  111. package/src/commands/schedule.cjs +5 -25
  112. package/src/commands/score.cjs +6 -27
  113. package/src/commands/send.cjs +4 -18
  114. package/src/commands/setprefix.cjs +11 -23
  115. package/src/commands/shortlink.cjs +13 -39
  116. package/src/commands/status.cjs +188 -76
  117. package/src/commands/sticker.cjs +5 -16
  118. package/src/commands/stickers.cjs +16 -20
  119. package/src/commands/stopbot.cjs +9 -25
  120. package/src/commands/stt.cjs +75 -107
  121. package/src/commands/todo.cjs +11 -31
  122. package/src/commands/toimage.cjs +11 -27
  123. package/src/commands/translate.cjs +53 -36
  124. package/src/commands/tts.cjs +46 -40
  125. package/src/commands/unblock.cjs +3 -17
  126. package/src/commands/update.cjs +23 -0
  127. package/src/commands/weather.cjs +40 -47
  128. package/src/config/ai-identity.cjs +14 -1
  129. package/src/config/baileys.config.js +16 -3
  130. package/src/config/data-dir.cjs +37 -0
  131. package/src/config/defaults.cjs +1 -0
  132. package/src/config/exit-codes.cjs +11 -0
  133. package/src/config/lock.cjs +55 -81
  134. package/src/config/official-links.cjs +61 -0
  135. package/src/config/paths.cjs +4 -33
  136. package/src/config/release.cjs +87 -0
  137. package/src/config/settings.cjs +199 -10
  138. package/src/core/connection.js +77 -9
  139. package/src/core/events.js +119 -33
  140. package/src/core/session.js +536 -84
  141. package/src/core/socket.js +81 -10
  142. package/src/db/copy.cjs +136 -0
  143. package/src/db/db.cjs +241 -36
  144. package/src/db/startup-guard.js +5 -0
  145. package/src/db/store.cjs +561 -54
  146. package/src/diagnostics/baileysLog.cjs +87 -0
  147. package/src/diagnostics/debugLog.cjs +16 -4
  148. package/src/diagnostics/device.cjs +4 -2
  149. package/src/diagnostics/nodeRuntime.cjs +131 -0
  150. package/src/diagnostics/signalSessionInspect.cjs +38 -0
  151. package/src/domain/command.js +24 -22
  152. package/src/extensions/autoReply.cjs +521 -0
  153. package/src/extensions/context.cjs +73 -0
  154. package/src/extensions/contract.cjs +69 -0
  155. package/src/extensions/entitlement/client.cjs +381 -0
  156. package/src/extensions/entitlement/index.cjs +629 -0
  157. package/src/extensions/entitlement/installation.cjs +117 -0
  158. package/src/extensions/entitlement/keys.cjs +72 -0
  159. package/src/extensions/entitlement/state.cjs +201 -0
  160. package/src/extensions/entitlement/target.cjs +7 -0
  161. package/src/extensions/entitlement/token.cjs +225 -0
  162. package/src/extensions/host.cjs +519 -0
  163. package/src/extensions/templateKeys.cjs +81 -0
  164. package/src/handlers/command.handler.js +191 -64
  165. package/src/handlers/group.handler.js +78 -18
  166. package/src/handlers/message.handler.js +56 -25
  167. package/src/i18n/catalog.generated.cjs +3537 -0
  168. package/src/index.js +51 -12
  169. package/src/legal/bundled/ar.json +102 -0
  170. package/src/legal/bundled/en.json +102 -0
  171. package/src/legal/bundled/es-419.json +102 -0
  172. package/src/legal/bundled/pt-BR.json +102 -0
  173. package/src/legal/cli.cjs +200 -0
  174. package/src/legal/consent.cjs +187 -0
  175. package/src/legal/document.cjs +156 -0
  176. package/src/legal/keys.cjs +100 -0
  177. package/src/legal/release.cjs +226 -0
  178. package/src/legal/remote.cjs +419 -0
  179. package/src/legal/terms.cjs +142 -0
  180. package/src/messaging/escape.cjs +31 -0
  181. package/src/messaging/health.cjs +65 -0
  182. package/src/messaging/identity.cjs +42 -0
  183. package/src/messaging/message.cjs +136 -0
  184. package/src/messaging/parser.cjs +88 -0
  185. package/src/messaging/router.cjs +217 -0
  186. package/src/middleware/permissions.middleware.js +34 -2
  187. package/src/panel/account-profile.cjs +489 -0
  188. package/src/panel/fallback-page.cjs +177 -0
  189. package/src/panel/password-reset.cjs +13 -14
  190. package/src/panel/recovery-email.cjs +3 -1
  191. package/src/routes/backup.api.esm.js +175 -0
  192. package/src/routes/dashboard.api.esm.js +183 -7
  193. package/src/routes/plus.api.esm.js +172 -0
  194. package/src/routes/reactions.api.esm.js +60 -0
  195. package/src/services/aiAgent.cjs +56 -43
  196. package/src/services/aiDocs.cjs +187 -0
  197. package/src/services/aiImage.cjs +183 -0
  198. package/src/services/aiMedia.cjs +146 -0
  199. package/src/services/aiProviders.cjs +76 -80
  200. package/src/services/aiRouter.cjs +2 -2
  201. package/src/services/aiToolAuth.cjs +76 -4
  202. package/src/services/aiTools.cjs +431 -105
  203. package/src/services/autoDelete.cjs +9 -1
  204. package/src/services/levix-docs.generated.json +2623 -0
  205. package/src/services/memoryPolicy.cjs +56 -0
  206. package/src/services/updateCheck.cjs +362 -0
  207. package/src/stickers/errors.cjs +53 -97
  208. package/src/stickers/library.cjs +13 -6
  209. package/src/stickers/owner.cjs +1 -1
  210. package/src/utils/commandContext.cjs +13 -0
  211. package/src/utils/commandDocs.cjs +42 -20
  212. package/src/utils/geminiHttpOptions.cjs +56 -0
  213. package/src/utils/geminiMedia.cjs +28 -3
  214. package/src/utils/groupMetadataCache.cjs +386 -21
  215. package/src/utils/httpsFetch.cjs +113 -0
  216. package/src/utils/i18n.cjs +117 -7
  217. package/src/utils/linkPreview.cjs +90 -0
  218. package/src/utils/logger.cjs +6 -0
  219. package/src/utils/markdownParser.cjs +3 -3
  220. package/src/utils/memory.cjs +375 -28
  221. package/src/utils/messageContent.cjs +7 -1
  222. package/src/utils/moderationAction.cjs +116 -73
  223. package/src/utils/moderationSummary.cjs +32 -56
  224. package/src/utils/permissionLevel.cjs +7 -28
  225. package/src/utils/permissions.cjs +23 -14
  226. package/src/utils/permissions.esm.js +99 -166
  227. package/src/utils/recentMessageCache.esm.js +241 -37
  228. package/src/utils/recurrence.cjs +78 -66
  229. package/src/utils/sendBotMessage.cjs +14 -29
  230. package/src/utils/sendBotMessage.esm.js +28 -13
  231. package/src/utils/statusMessage.cjs +94 -33
  232. package/src/utils/stickerBot.cjs +18 -16
  233. package/src/utils/storage.cjs +2 -0
  234. package/src/utils/storage.esm.js +2 -0
  235. package/src/utils/thumbnail.cjs +5 -21
  236. package/public/dashboard/assets/index-B1sOW2VX.js +0 -561
  237. package/public/dashboard/assets/index-CK8osB4o.css +0 -2
  238. package/public/dashboard.css +0 -3127
  239. package/public/dashboard.js +0 -1981
  240. package/public/gateway-i18n.js +0 -321
  241. package/public/gateway.js +0 -70
  242. package/views/dashboard.ejs +0 -775
  243. package/views/login.ejs +0 -408
  244. package/views/qr.ejs +0 -155
  245. package/views/setup.ejs +0 -118
@@ -0,0 +1,290 @@
1
+ import { createRequire } from "node:module";
2
+ import { REACTION_ACTION_LIMITS, reactionActionReadiness } from "./engine.js";
3
+ import { REACTION_LIMITS } from "./reactions.js";
4
+
5
+ const require = createRequire(import.meta.url);
6
+ const store = require("../db/store.cjs");
7
+ const { MAX_IDENTITY_ALIASES } = require("./limits.cjs");
8
+ const { definitionIssues, exactJid } = require("./rules.cjs");
9
+ const { linkedIdentity } = require("../messaging/identity.cjs");
10
+ const { getCached } = require("../utils/groupMetadataCache.cjs");
11
+ const status = require("./status.cjs");
12
+ let session = null;
13
+
14
+ export function setSession(manager) {
15
+ session = manager;
16
+ }
17
+
18
+ export class ReactionManageError extends Error {
19
+ constructor(status, code, issues) {
20
+ super(code);
21
+ this.name = "ReactionManageError";
22
+ this.status = status;
23
+ this.code = code;
24
+ if (issues) this.issues = issues;
25
+ }
26
+ }
27
+
28
+ function bodyIssues(body, keys) {
29
+ if (!body || typeof body !== "object" || Array.isArray(body))
30
+ return [{ field: "body", code: "invalid" }];
31
+ return Object.keys(body).some((key) => !keys.includes(key))
32
+ ? [{ field: "body", code: "unknown_field" }]
33
+ : [];
34
+ }
35
+
36
+ function draftIssues(body, update = false) {
37
+ const issues = bodyIssues(body, [
38
+ "chatJid",
39
+ "priority",
40
+ "definition",
41
+ ...(update ? ["revision"] : []),
42
+ ]);
43
+ if (!body || typeof body !== "object" || Array.isArray(body)) return issues;
44
+ try {
45
+ exactJid(body.chatJid);
46
+ } catch {
47
+ issues.push({ field: "chatJid", code: "invalid" });
48
+ }
49
+ if (!Number.isInteger(body.priority) || body.priority < 0 || body.priority > 1000)
50
+ issues.push({ field: "priority", code: "invalid" });
51
+ issues.push(...definitionIssues(body.definition));
52
+ if (update) issues.push(...revisionIssues(body.revision));
53
+ return issues;
54
+ }
55
+
56
+ function revisionIssues(revision) {
57
+ return typeof revision === "string" && revision.length > 0 && revision.length <= 128
58
+ ? []
59
+ : [{ field: "revision", code: "invalid" }];
60
+ }
61
+
62
+ function assertValid(issues) {
63
+ if (issues.length) throw new ReactionManageError(400, "REACTION_RULE_INVALID", issues);
64
+ }
65
+
66
+ function currentRule(id, revision) {
67
+ const row = store.getReactionRule(id);
68
+ if (!row) throw new ReactionManageError(404, "REACTION_RULE_MISSING");
69
+ if (row.revision !== revision) throw new ReactionManageError(409, "REACTION_RULE_CHANGED");
70
+ return row;
71
+ }
72
+
73
+ function write(fn) {
74
+ try {
75
+ return fn();
76
+ } catch (error) {
77
+ if (error instanceof ReactionManageError) throw error;
78
+ if (error.message === "REACTION_RULE_MISSING")
79
+ throw new ReactionManageError(404, "REACTION_RULE_MISSING");
80
+ if (error.message === "REACTION_AUTOMATION_CHANGED")
81
+ throw new ReactionManageError(409, "REACTION_AUTOMATION_CHANGED");
82
+ if (["REACTION_RULE_INVALID", "REACTION_SWITCH_INVALID"].includes(error.message))
83
+ throw new ReactionManageError(400, "REACTION_RULE_INVALID", [
84
+ { field: "definition", code: "invalid" },
85
+ ]);
86
+ throw error;
87
+ }
88
+ }
89
+
90
+ function parsedRule(row) {
91
+ let definition = null;
92
+ try {
93
+ definition = JSON.parse(row.definition);
94
+ } catch {
95
+ // Corrupt rows remain visible so the operator can replace or delete them.
96
+ }
97
+ return {
98
+ row,
99
+ definition,
100
+ valid: draftIssues({ chatJid: row.chat_jid, priority: row.priority, definition }).length === 0,
101
+ };
102
+ }
103
+
104
+ function compareRules(a, b) {
105
+ return a.priority - b.priority || Buffer.compare(Buffer.from(a.id), Buffer.from(b.id));
106
+ }
107
+
108
+ function triggerKeys(definition) {
109
+ if (typeof definition?.emoji !== "string") return [];
110
+ // SQL prefiltering runs before definition validation and uses json_each.
111
+ const values = (value) =>
112
+ new Set(
113
+ (value && typeof value === "object" ? Object.values(value) : [value]).filter(
114
+ (item) => typeof item === "string",
115
+ ),
116
+ );
117
+ const keys = [];
118
+ const types = values(definition.originalTypes);
119
+ for (const transition of values(definition.transitions))
120
+ for (const type of types) keys.push(JSON.stringify([definition.emoji, transition, type]));
121
+ return keys;
122
+ }
123
+
124
+ function triggerIndex(items) {
125
+ const byChat = new Map();
126
+ for (const item of items) {
127
+ const chat = item.row.chat_jid;
128
+ if (!byChat.has(chat)) byChat.set(chat, []);
129
+ byChat.get(chat).push(item);
130
+ }
131
+ const byAliases = new Map();
132
+ const index = new Map();
133
+ for (const chat of byChat.keys()) {
134
+ const aliases = store.reactionIdentityAliases(chat);
135
+ const aliasKey = JSON.stringify([...aliases].sort());
136
+ let buckets = byAliases.get(aliasKey);
137
+ if (!buckets) {
138
+ buckets = new Map();
139
+ // reactionRulesForChats refuses an alias expansion above four chats.
140
+ for (const alias of aliases.length <= MAX_IDENTITY_ALIASES ? aliases : []) {
141
+ for (const item of byChat.get(alias) || []) {
142
+ for (const key of triggerKeys(item.definition)) {
143
+ if (!buckets.has(key)) buckets.set(key, { enabled: 0, first: [] });
144
+ const bucket = buckets.get(key);
145
+ if (item.row.enabled === 1) bucket.enabled++;
146
+ // Later rows cannot enter the capped list of earlier overlaps.
147
+ bucket.first.push(item.row);
148
+ bucket.first.sort(compareRules);
149
+ if (bucket.first.length > 20) bucket.first.pop();
150
+ }
151
+ }
152
+ }
153
+ byAliases.set(aliasKey, buckets);
154
+ }
155
+ index.set(chat, buckets);
156
+ }
157
+ return index;
158
+ }
159
+
160
+ function ruleView(item, index, context) {
161
+ const { row, definition, valid } = item;
162
+ const statuses = [];
163
+ if (!context.automation.enabled) statuses.push("AUTOMATION_OFF");
164
+ if (!row.enabled) statuses.push("RULE_OFF");
165
+ if (!valid) statuses.push("RULE_INVALID");
166
+ if (!context.connection.connected) statuses.push("NOT_CONNECTED");
167
+ if (valid) {
168
+ statuses.push(...reactionActionReadiness(definition.action));
169
+ if (definition.destination.mode === "owner_private" && !context.owner.linked)
170
+ statuses.push("OWNER_NOT_LINKED");
171
+ }
172
+ const buckets = triggerKeys(definition)
173
+ .map((key) => index.get(row.chat_jid).get(key))
174
+ .filter(Boolean);
175
+ if (buckets.some((bucket) => bucket.enabled > REACTION_ACTION_LIMITS.candidateLimit))
176
+ statuses.push("CANDIDATE_LIMIT");
177
+ if (!statuses.length) statuses.push("READY");
178
+ const earlier = new Map();
179
+ for (const bucket of buckets)
180
+ for (const other of bucket.first)
181
+ if (compareRules(other, row) < 0) earlier.set(other.id, other);
182
+ const overlaps = [...earlier.values()]
183
+ .sort(compareRules)
184
+ .slice(0, 20)
185
+ .map((other) => other.id);
186
+ return {
187
+ id: row.id,
188
+ revision: row.revision,
189
+ chatJid: row.chat_jid,
190
+ chatName: getCached(row.chat_jid)?.subject || store.describePeer(row.chat_jid).label,
191
+ priority: row.priority,
192
+ enabled: row.enabled === 1,
193
+ enabledAt: row.enabled_at,
194
+ definition,
195
+ valid,
196
+ statuses,
197
+ overlaps,
198
+ runtime: status.get(row.id) || { lastCode: null, lastAt: null, counts: {} },
199
+ };
200
+ }
201
+
202
+ function context() {
203
+ return {
204
+ automation: store.getReactionAutomation(),
205
+ connection: { connected: session?.getState().connected === true },
206
+ owner: { linked: linkedIdentity().selfJids.length > 0 },
207
+ };
208
+ }
209
+
210
+ function view(id) {
211
+ const items = store.listReactionRules().map(parsedRule);
212
+ return ruleView(
213
+ items.find((item) => item.row.id === id),
214
+ triggerIndex(items),
215
+ context(),
216
+ );
217
+ }
218
+
219
+ export function overview() {
220
+ const state = context();
221
+ const items = store.listReactionRules().map(parsedRule);
222
+ const index = triggerIndex(items);
223
+ return {
224
+ ...state,
225
+ limits: { ...REACTION_LIMITS, ...REACTION_ACTION_LIMITS },
226
+ rules: items.map((item) => ruleView(item, index, state)),
227
+ };
228
+ }
229
+
230
+ export function checkDraft(body) {
231
+ return { issues: draftIssues(body) };
232
+ }
233
+
234
+ export function createRule(body) {
235
+ assertValid(draftIssues(body));
236
+ return write(() => {
237
+ const row = store.saveReactionRule({
238
+ chatJid: body.chatJid,
239
+ priority: body.priority,
240
+ definition: body.definition,
241
+ });
242
+ return view(row.id);
243
+ });
244
+ }
245
+
246
+ export function updateRule(id, body) {
247
+ assertValid(draftIssues(body, true));
248
+ currentRule(id, body.revision);
249
+ return write(() => {
250
+ const row = store.saveReactionRule({
251
+ id,
252
+ chatJid: body.chatJid,
253
+ priority: body.priority,
254
+ definition: body.definition,
255
+ });
256
+ return view(row.id);
257
+ });
258
+ }
259
+
260
+ export function setRuleEnabled(id, body) {
261
+ const issues = bodyIssues(body, ["revision", "enabled"]);
262
+ issues.push(...revisionIssues(body?.revision));
263
+ if (typeof body?.enabled !== "boolean") issues.push({ field: "enabled", code: "invalid" });
264
+ assertValid(issues);
265
+ const row = currentRule(id, body.revision);
266
+ if (body.enabled === true) {
267
+ assertValid(
268
+ draftIssues({
269
+ chatJid: row.chat_jid,
270
+ priority: row.priority,
271
+ definition: parsedRule(row).definition,
272
+ }),
273
+ );
274
+ }
275
+ return write(() => view(store.setReactionRuleEnabled(id, body.enabled).id));
276
+ }
277
+
278
+ export function deleteRule(id, revision) {
279
+ assertValid(revisionIssues(revision));
280
+ currentRule(id, revision);
281
+ return write(() => store.deleteReactionRule(id));
282
+ }
283
+
284
+ export function setAutomation(body) {
285
+ const issues = bodyIssues(body, ["enabled", "revision"]);
286
+ if (typeof body?.enabled !== "boolean") issues.push({ field: "enabled", code: "invalid" });
287
+ issues.push(...revisionIssues(body?.revision));
288
+ assertValid(issues);
289
+ return write(() => store.setReactionAutomationEnabled(body.enabled, body.revision));
290
+ }