levix-bot 4.6.0 → 4.7.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/src/index.js CHANGED
@@ -265,12 +265,20 @@ async function startWithPanel({ open }) {
265
265
  // living and dying
266
266
  // ---------------------------------------------------------------------------
267
267
 
268
- function installShutdownHandlers() {
268
+ export function installShutdownHandlers() {
269
269
  // After an uncaughtException the process state is unknown (a half-held lock,
270
270
  // a half-written auth row). Log and exit rather than keep working on top of
271
271
  // it. Run the bot under a supervisor (systemd / pm2 / docker restart) so it
272
272
  // comes back on its own — without one, this exit means the bot is down.
273
273
  const fatal = (error, label) => {
274
+ // One last local crash report, written synchronously before we die. It is
275
+ // never sent from here — the operator sends it from the panel later. The
276
+ // write is wrapped again here so a store failure cannot turn one fatal
277
+ // error into two.
278
+ try {
279
+ const category = label.startsWith("Unhandled Rejection") ? "node-rejection" : "node-uncaught";
280
+ require("./diagnostics/crashReports.cjs").captureNodeFatal(category, error);
281
+ } catch {}
274
282
  logger.error(error, `${label} exiting — restart is up to the supervisor`);
275
283
  console.error(label, error);
276
284
  // Half a second so pino can drain its worker before we die.
@@ -20,6 +20,7 @@
20
20
  const fs = require("node:fs");
21
21
  const logger = require("../utils/logger.cjs");
22
22
  const settings = require("../config/settings.cjs");
23
+ const { buildDiagnostics } = require("../diagnostics/device.cjs");
23
24
 
24
25
  const DEFAULT_ENDPOINT = "https://levix.leviro.net/api/feedback";
25
26
 
@@ -158,11 +159,18 @@ function safeRemoteError(value) {
158
159
  * "payload" part and the file as "file", and the endpoint delivers it to
159
160
  * Telegram as a document instead of a message.
160
161
  *
162
+ * `diagnosticsClient` is the raw section 1 client object, present only when the
163
+ * operator ticked the diagnostics box. When present the server rebuilds it with
164
+ * the same builder the preview route uses (so preview and send cannot diverge)
165
+ * and adds it to the envelope as `diagnostics`. When it is undefined the
166
+ * envelope has no `diagnostics` key at all.
167
+ *
161
168
  * @param {{ message: string, topic: string, rating: number|null, contact: string|null }} feedback
162
169
  * @param {{ buffer: Buffer, name: string, type: string, size: number } | null} attachment
170
+ * @param {object} [diagnosticsClient]
163
171
  * @returns {Promise<{ ok: true } | { ok: false, status: number, error: string }>}
164
172
  */
165
- async function submitFeedback(feedback, attachment = null) {
173
+ async function submitFeedback(feedback, attachment = null, diagnosticsClient = undefined) {
166
174
  const endpoint = process.env.LEVIX_FEEDBACK_URL || DEFAULT_ENDPOINT;
167
175
  const runtime = describeRuntime();
168
176
 
@@ -174,6 +182,10 @@ async function submitFeedback(feedback, attachment = null) {
174
182
  app: { version: runtime.version, platform: runtime.platform, locale: runtime.locale },
175
183
  };
176
184
 
185
+ if (diagnosticsClient !== undefined && diagnosticsClient !== null) {
186
+ envelope.diagnostics = buildDiagnostics(diagnosticsClient);
187
+ }
188
+
177
189
  let response;
178
190
  let data = null;
179
191
  try {
@@ -18,6 +18,8 @@ import { Router } from "express";
18
18
  import { createRequire } from "module";
19
19
  import { groupMetadataCache } from "../core/socket.js";
20
20
  import { getCommandCatalog, rebuildCommandIndex } from "../handlers/command.handler.js";
21
+ import { fileToBuffer, MultipartError, readMultipartForm } from "../utils/multipart.esm.js";
22
+ import normalizeJid, { isLidJid } from "../utils/normalizeJid.esm.js";
21
23
  import { grantRole, listRoles, revokeRole } from "../utils/permissions.esm.js";
22
24
  import {
23
25
  countDebts,
@@ -41,8 +43,6 @@ import {
41
43
  resolveUserPhone,
42
44
  saveGroupSettings,
43
45
  } from "../utils/storage.esm.js";
44
- import normalizeJid, { isLidJid } from "../utils/normalizeJid.esm.js";
45
- import { fileToBuffer, MultipartError, readMultipartForm } from "../utils/multipart.esm.js";
46
46
 
47
47
  const require = createRequire(import.meta.url);
48
48
  const logger = require("../utils/logger.cjs");
@@ -64,6 +64,8 @@ const {
64
64
  } = require("../panel/feedback.cjs");
65
65
  const { stampPanelSession } = require("../panel/session-auth.cjs");
66
66
  const { DATA_DIR } = require("../config/paths.cjs");
67
+ const { buildDiagnostics } = require("../diagnostics/device.cjs");
68
+ const crashReports = require("../diagnostics/crashReports.cjs");
67
69
  const { PERSONA_FILE, activeProviderKeySetting } = require("../services/aiAgent.cjs");
68
70
  const {
69
71
  SCHEDULE_MEDIA_MAX_BYTES,
@@ -152,6 +154,28 @@ function asyncRoute(handler) {
152
154
  };
153
155
  }
154
156
 
157
+ /**
158
+ * The feedback `diagnostics` field arrives as a JSON object (JSON body) or as a
159
+ * JSON string (a multipart field). Both are normalized here; a present but
160
+ * broken value is a caller mistake, not something to silently drop.
161
+ *
162
+ * @returns {{ present: boolean, value: object|null, error?: string }}
163
+ */
164
+ function parseDiagnosticsInput(raw) {
165
+ if (raw === undefined || raw === null) return { present: false, value: null };
166
+ if (typeof raw === "string") {
167
+ const text = raw.trim();
168
+ if (!text) return { present: false, value: null };
169
+ try {
170
+ return { present: true, value: JSON.parse(text) };
171
+ } catch {
172
+ return { present: false, value: null, error: "diagnostics is not valid JSON" };
173
+ }
174
+ }
175
+ if (typeof raw === "object" && !Array.isArray(raw)) return { present: true, value: raw };
176
+ return { present: false, value: null, error: "diagnostics must be an object" };
177
+ }
178
+
155
179
  // ===========================================================================
156
180
  // Overview
157
181
  // ===========================================================================
@@ -1309,10 +1333,22 @@ router.get("/feedback/meta", (req, res) => {
1309
1333
  });
1310
1334
  });
1311
1335
 
1336
+ // The exact object the server would forward as `diagnostics`, so the operator
1337
+ // can see what they are about to share before they send it. This is the same
1338
+ // builder submitFeedback() uses, so the preview cannot lie.
1339
+ router.post("/feedback/diagnostics-preview", (req, res) => {
1340
+ const parsed = parseDiagnosticsInput(req.body?.client);
1341
+ if (parsed.error) return badRequest(res, parsed.error);
1342
+ res.json({ success: true, diagnostics: buildDiagnostics(parsed.present ? parsed.value : {}) });
1343
+ });
1344
+
1312
1345
  // Leaves this machine only when the operator presses Send, and carries nothing
1313
1346
  // but the form's own fields plus the version and platform — see feedback.cjs.
1314
1347
  // An optional attachment (a screenshot, a log) rides along as multipart and is
1315
- // forwarded to the same endpoint, which delivers it to Telegram as a file.
1348
+ // forwarded to the same endpoint, which delivers it to Telegram as a file. An
1349
+ // optional `diagnostics` object (the preview's client object) is rebuilt by the
1350
+ // same sanitizer and attached to the envelope; without it there is no
1351
+ // `diagnostics` key on the wire.
1316
1352
  router.post(
1317
1353
  "/feedback",
1318
1354
  asyncRoute(async (req, res) => {
@@ -1343,6 +1379,9 @@ router.post(
1343
1379
  const parsed = validateFeedback(body);
1344
1380
  if (!parsed.ok) return badRequest(res, parsed.error);
1345
1381
 
1382
+ const diagnostics = parseDiagnosticsInput(body.diagnostics);
1383
+ if (diagnostics.error) return badRequest(res, diagnostics.error);
1384
+
1346
1385
  const retryAfter = feedbackRetryAfter();
1347
1386
  if (retryAfter) {
1348
1387
  res.set("Retry-After", String(retryAfter));
@@ -1352,7 +1391,11 @@ router.post(
1352
1391
  });
1353
1392
  }
1354
1393
 
1355
- const result = await submitFeedback(parsed.value, attachment);
1394
+ const result = await submitFeedback(
1395
+ parsed.value,
1396
+ attachment,
1397
+ diagnostics.present ? diagnostics.value : undefined,
1398
+ );
1356
1399
  if (!result.ok) {
1357
1400
  return res.status(result.status).json({ success: false, error: result.error });
1358
1401
  }
@@ -1600,12 +1643,12 @@ router.post("/auto-delete/rules/:id/reset", (req, res) => {
1600
1643
 
1601
1644
  router.get("/auto-delete/log", (req, res) => {
1602
1645
  try {
1603
- const log = autoDelete.listLog({
1646
+ const { log, total } = autoDelete.listLog({
1604
1647
  ruleId: req.query.ruleId,
1605
1648
  limit: req.query.limit,
1606
1649
  offset: req.query.offset,
1607
1650
  });
1608
- res.json({ success: true, log });
1651
+ res.json({ success: true, log, total });
1609
1652
  } catch (error) {
1610
1653
  autoDeleteFail(res, error, "Error listing auto-delete copies");
1611
1654
  }
@@ -1620,4 +1663,67 @@ router.delete("/auto-delete/log", (req, res) => {
1620
1663
  }
1621
1664
  });
1622
1665
 
1666
+ // ===========================================================================
1667
+ // Crash reports — local, and sent only when the operator presses Send
1668
+ // ===========================================================================
1669
+
1670
+ // Every pending report, as the exact upload object (plus the local status), and
1671
+ // the last 20 ids that went out. Listing sweeps expired reports first.
1672
+ router.get("/crash-reports", (_req, res) => {
1673
+ try {
1674
+ const { reports, sent } = crashReports.list();
1675
+ res.json({ success: true, reports, sent });
1676
+ } catch (error) {
1677
+ fail(res, error, "Error listing crash reports");
1678
+ }
1679
+ });
1680
+
1681
+ // The panel's own error hooks post here. Rate-limited because a render loop can
1682
+ // fire many times a second; the report itself is stored, nothing is sent.
1683
+ router.post("/crash-reports", (req, res) => {
1684
+ const retryAfter = crashReports.crashReportRetryAfter();
1685
+ if (retryAfter) {
1686
+ res.set("Retry-After", String(retryAfter));
1687
+ return res.status(429).json({
1688
+ success: false,
1689
+ error: "Too many crash reports for now. Try again in a little while.",
1690
+ });
1691
+ }
1692
+ crashReports.recordCrashReportAttempt();
1693
+ try {
1694
+ const report = crashReports.createReport(req.body || {});
1695
+ res.json({ success: true, id: report.id });
1696
+ } catch (error) {
1697
+ if (error?.status === 400) return badRequest(res, error.message);
1698
+ fail(res, error, "Error storing a crash report");
1699
+ }
1700
+ });
1701
+
1702
+ // One report out, by an explicit operator action. It never retries by itself.
1703
+ router.post(
1704
+ "/crash-reports/:id/send",
1705
+ asyncRoute(async (req, res) => {
1706
+ const result = await crashReports.sendReport(req.params.id);
1707
+ if (!result.ok) {
1708
+ return res.status(result.status || 502).json({ success: false, error: result.error });
1709
+ }
1710
+ res.json({ success: true });
1711
+ }),
1712
+ );
1713
+
1714
+ router.delete("/crash-reports/:id", (req, res) => {
1715
+ if (!crashReports.deleteReport(req.params.id)) {
1716
+ return res.status(404).json({ success: false, error: "Crash report not found" });
1717
+ }
1718
+ res.json({ success: true });
1719
+ });
1720
+
1721
+ router.delete("/crash-reports", (_req, res) => {
1722
+ try {
1723
+ res.json({ success: true, removed: crashReports.deleteAllReports() });
1724
+ } catch (error) {
1725
+ fail(res, error, "Error deleting crash reports");
1726
+ }
1727
+ });
1728
+
1623
1729
  export default router;
@@ -408,11 +408,13 @@ function listLog({ ruleId, limit, offset } = {}) {
408
408
  throw new AutoDeleteError("offset must be a non-negative integer");
409
409
  }
410
410
  }
411
- return store.listAutoDeleteLog({
411
+ const log = store.listAutoDeleteLog({
412
412
  ruleId: parsedRuleId,
413
413
  limit: parsedLimit,
414
414
  offset: parsedOffset,
415
415
  });
416
+ const total = store.countAutoDeleteLog({ ruleId: parsedRuleId });
417
+ return { log, total };
416
418
  }
417
419
 
418
420
  function clearLog(ruleId) {
@@ -102,6 +102,7 @@ module.exports = {
102
102
  resetAutoDeleteCounter: store.resetAutoDeleteCounter,
103
103
  insertAutoDeleteLog: store.insertAutoDeleteLog,
104
104
  listAutoDeleteLog: store.listAutoDeleteLog,
105
+ countAutoDeleteLog: store.countAutoDeleteLog,
105
106
  clearAutoDeleteLog: store.clearAutoDeleteLog,
106
107
  clearAutoDeleteOnUnlink: store.clearAutoDeleteOnUnlink,
107
108
  };
@@ -118,5 +118,6 @@ export const incrementAutoDeleteCounter = store.incrementAutoDeleteCounter;
118
118
  export const resetAutoDeleteCounter = store.resetAutoDeleteCounter;
119
119
  export const insertAutoDeleteLog = store.insertAutoDeleteLog;
120
120
  export const listAutoDeleteLog = store.listAutoDeleteLog;
121
+ export const countAutoDeleteLog = store.countAutoDeleteLog;
121
122
  export const clearAutoDeleteLog = store.clearAutoDeleteLog;
122
123
  export const clearAutoDeleteOnUnlink = store.clearAutoDeleteOnUnlink;
@@ -1,6 +1,7 @@
1
1
  <!DOCTYPE html>
2
- <html lang="en" data-theme="dark">
2
+ <html>
3
3
  <head>
4
+ <script src="/ui-boot.js"></script>
4
5
  <meta charset="UTF-8" />
5
6
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
7
  <title><%= brand.name %> · Control panel</title>
package/views/login.ejs CHANGED
@@ -1,25 +1,26 @@
1
1
  <!DOCTYPE html>
2
- <html lang="en" data-theme="dark">
2
+ <html data-brand="<%= brand.name %>">
3
3
  <head>
4
+ <script src="/ui-boot.js"></script>
4
5
  <meta charset="UTF-8" />
5
6
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
- <title><%= brand.name %> · Sign in</title>
7
+ <title data-i18n="titleLogin"><%= brand.name %> · Sign in</title>
7
8
  <link rel="icon" type="image/png" sizes="32x32" href="/brand/icon-32.png" />
8
9
  <link rel="apple-touch-icon" href="/brand/apple-touch-icon.png" />
9
10
  <link rel="stylesheet" href="/dashboard.css" />
10
11
  </head>
11
12
  <body class="gateway-page">
12
13
  <main class="gateway-shell">
13
- <section class="gateway-story" aria-label="About <%= brand.name %>">
14
+ <section class="gateway-story" aria-label="About <%= brand.name %>" data-i18n-aria-label="storyAbout">
14
15
  <div class="brand-lockup" aria-label="<%= brand.name %>">
15
16
  <img src="/brand/mark.svg" alt="" />
16
- <span><strong><%= brand.name %></strong><small><%= brand.tagline %></small></span>
17
+ <span><strong><%= brand.name %></strong><small data-i18n="tagline"><%= brand.tagline %></small></span>
17
18
  </div>
18
19
 
19
20
  <div class="gateway-copy">
20
- <span class="eyebrow">Self-hosted by design</span>
21
- <h2>The quiet control room behind every chat.</h2>
22
- <p>Connection, commands, memory, and moderation stay together—on your server and within reach.</p>
21
+ <span class="eyebrow" data-i18n="selfHosted">Self-hosted by design</span>
22
+ <h2 data-i18n="loginHeadline">The quiet control room behind every chat.</h2>
23
+ <p data-i18n="loginLede">Connection, commands, memory, and moderation stay together—on your server and within reach.</p>
23
24
  </div>
24
25
 
25
26
  <div class="gateway-mark" aria-hidden="true">
@@ -27,7 +28,7 @@
27
28
  <img src="/brand/mark.svg" alt="" />
28
29
  </div>
29
30
 
30
- <p class="gateway-trust"><span></span> Private workspace · direct control</p>
31
+ <p class="gateway-trust" data-i18n-html="loginTrust"><span></span> Private workspace · direct control</p>
31
32
  </section>
32
33
 
33
34
  <section class="gateway-panel">
@@ -35,26 +36,27 @@
35
36
  <form class="card auth-card" id="login-card" action="/login" method="post">
36
37
  <button type="button" class="gateway-back-btn" id="login-back-app" hidden>
37
38
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
38
- <span>Back to app</span>
39
+ <span data-i18n="backToApp">Back to app</span>
39
40
  </button>
40
41
  <div class="auth-head">
41
- <span class="eyebrow">Secure access</span>
42
- <h1>Welcome back</h1>
43
- <p class="sub">Sign in to manage your Levix workspace.</p>
42
+ <span class="eyebrow" data-i18n="secureAccess">Secure access</span>
43
+ <h1 data-i18n="welcomeBack">Welcome back</h1>
44
+ <p class="sub" data-i18n="loginSub">Sign in to manage your Levix workspace.</p>
44
45
  </div>
45
46
 
46
47
  <label class="field">
47
- <span>Control panel password</span>
48
+ <span data-i18n="passwordLabel">Control panel password</span>
48
49
  <div class="input-wrap">
49
50
  <input
50
- type="password"
51
+ type="password" dir="ltr" spellcheck="false" autocapitalize="off" autocorrect="off"
51
52
  name="password"
52
53
  autocomplete="current-password"
53
54
  placeholder="Enter your password"
55
+ data-i18n-placeholder="passwordPlaceholder"
54
56
  autofocus
55
57
  required
56
58
  />
57
- <button type="button" class="eye-btn" aria-label="Show password">
59
+ <button type="button" class="eye-btn" aria-label="Show password" data-i18n-aria-label="showPassword" aria-pressed="false">
58
60
  <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
59
61
  <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
60
62
  </button>
@@ -62,41 +64,41 @@
62
64
  </label>
63
65
 
64
66
  <button type="submit" class="btn btn-primary auth-submit">
65
- Open workspace
67
+ <span data-i18n="openWorkspace">Open workspace</span>
66
68
  <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
67
69
  </button>
68
70
 
69
71
  <% if (error) { %>
70
- <div class="error" role="alert"><%= error %></div>
72
+ <div class="error" role="alert" data-i18n-error="<%= errorCode %>" data-i18n-n="<%= minLength %>"><%= error %></div>
71
73
  <% } %>
72
74
 
73
75
  <div class="auth-alt">
74
- <button type="button" class="link-btn" id="forgot-link">Forgot password?</button>
76
+ <button type="button" class="link-btn" id="forgot-link" data-i18n="forgotPassword">Forgot password?</button>
75
77
  </div>
76
78
  </form>
77
79
 
78
80
  <div class="card auth-card" id="reset-card" hidden>
79
81
  <button type="button" class="gateway-back-btn" id="reset-back">
80
82
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
81
- <span>Back to sign in</span>
83
+ <span data-i18n="backToSignIn">Back to sign in</span>
82
84
  </button>
83
85
  <div class="auth-head">
84
- <span class="eyebrow">Password reset</span>
85
- <h1>Reset your password</h1>
86
- <p class="sub">
86
+ <span class="eyebrow" data-i18n="resetEyebrow">Password reset</span>
87
+ <h1 data-i18n="resetTitle">Reset your password</h1>
88
+ <p class="sub" data-i18n="resetSubtitle">
87
89
  Levix sends a one-time code to your WhatsApp — the linked account's own chat.
88
90
  The code works for 10 minutes.
89
91
  </p>
90
92
  </div>
91
93
 
92
94
  <button type="button" class="btn btn-primary auth-submit" id="reset-send">
93
- Send code to WhatsApp
95
+ <span data-i18n="resetSend">Send code to WhatsApp</span>
94
96
  <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
95
97
  </button>
96
98
 
97
99
  <form id="reset-form" hidden>
98
100
  <label class="field">
99
- <span>Reset code</span>
101
+ <span data-i18n="resetCodeLabel">Reset code</span>
100
102
  <input
101
103
  type="text"
102
104
  id="reset-code"
@@ -104,22 +106,25 @@
104
106
  autocomplete="one-time-code"
105
107
  maxlength="6"
106
108
  placeholder="6 digits"
109
+ data-i18n-placeholder="resetCodePlaceholder"
107
110
  required
108
111
  />
109
112
  </label>
110
113
 
111
114
  <label class="field">
112
- <span>New password</span>
115
+ <span data-i18n="newPassword">New password</span>
113
116
  <div class="input-wrap">
114
117
  <input
115
- type="password"
118
+ type="password" dir="ltr" spellcheck="false" autocapitalize="off" autocorrect="off"
116
119
  id="reset-pass"
117
120
  autocomplete="new-password"
118
121
  placeholder="At least <%= minLength %> characters"
122
+ data-i18n-placeholder="minLengthPlaceholder"
123
+ data-i18n-n="<%= minLength %>"
119
124
  minlength="<%= minLength %>"
120
125
  required
121
126
  />
122
- <button type="button" class="eye-btn" aria-label="Show password">
127
+ <button type="button" class="eye-btn" aria-label="Show password" data-i18n-aria-label="showPassword" aria-pressed="false">
123
128
  <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
124
129
  <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
125
130
  </button>
@@ -127,17 +132,18 @@
127
132
  </label>
128
133
 
129
134
  <label class="field">
130
- <span>Repeat password</span>
135
+ <span data-i18n="repeatPassword">Repeat password</span>
131
136
  <div class="input-wrap">
132
137
  <input
133
- type="password"
138
+ type="password" dir="ltr" spellcheck="false" autocapitalize="off" autocorrect="off"
134
139
  id="reset-confirm"
135
140
  autocomplete="new-password"
136
141
  placeholder="Type it again"
142
+ data-i18n-placeholder="repeatPlaceholder"
137
143
  minlength="<%= minLength %>"
138
144
  required
139
145
  />
140
- <button type="button" class="eye-btn" aria-label="Show password">
146
+ <button type="button" class="eye-btn" aria-label="Show password" data-i18n-aria-label="showPassword" aria-pressed="false">
141
147
  <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
142
148
  <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
143
149
  </button>
@@ -145,7 +151,7 @@
145
151
  </label>
146
152
 
147
153
  <button type="submit" class="btn btn-primary auth-submit">
148
- Set new password
154
+ <span data-i18n="resetSubmit">Set new password</span>
149
155
  <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
150
156
  </button>
151
157
  </form>
@@ -154,35 +160,25 @@
154
160
 
155
161
  </div>
156
162
 
157
- <div class="foot"><%= brand.name %> by <%= brand.developer %> · <%= brand.studio %></div>
163
+ <div class="foot"><%= brand.name %> <span data-i18n="by">by</span> <%= brand.developer %> · <%= brand.studio %></div>
158
164
  </div>
159
165
  </section>
160
166
  </main>
161
167
 
168
+ <script src="/gateway-i18n.js"></script>
162
169
  <script src="/gateway.js"></script>
163
170
  <script>
164
171
  (function () {
165
- // Android app: show back-to-app buttons, localize labels
172
+ var t = window.LevixGatewayI18n.t;
173
+
174
+ // Android app: show the back-to-app button. Its label is already in
175
+ // whichever language the page loaded in.
166
176
  var inApp = typeof window.LevixHost !== "undefined" && typeof window.LevixHost.closePanel === "function";
167
177
  if (inApp) {
168
- var isAr = false;
169
- try { isAr = localStorage.getItem("levix_lang") === "ar"; } catch(e) {}
170
- var backLabel = isAr ? "العودة للتطبيق" : "Back to app";
171
- var resetBackLabel = isAr ? "العودة لتسجيل الدخول" : "Back to sign in";
172
- ["login-back-app"].forEach(function(id) {
173
- var btn = document.getElementById(id);
174
- if (btn) {
175
- btn.hidden = false;
176
- btn.querySelector("span").textContent = backLabel;
177
- if (isAr) { btn.dir = "rtl"; btn.style.direction = "rtl"; }
178
- btn.addEventListener("click", function() { window.LevixHost.closePanel(); });
179
- }
180
- });
181
- var resetBackBtn = document.getElementById("reset-back");
182
- if (resetBackBtn && isAr) {
183
- resetBackBtn.querySelector("span").textContent = resetBackLabel;
184
- resetBackBtn.dir = "rtl";
185
- resetBackBtn.style.direction = "rtl";
178
+ var backBtn = document.getElementById("login-back-app");
179
+ if (backBtn) {
180
+ backBtn.hidden = false;
181
+ backBtn.addEventListener("click", function() { window.LevixHost.closePanel(); });
186
182
  }
187
183
  }
188
184
 
@@ -201,13 +197,52 @@
201
197
  loginCard.hidden = false;
202
198
  document.getElementById("forgot-link").focus();
203
199
  }
204
- function showError(text) {
205
- errorBox.textContent = text;
200
+ var getT = function () {
201
+ return window.LevixGatewayI18n ? window.LevixGatewayI18n.t : function (k) { return k; };
202
+ };
203
+ var currentErrorState = null;
204
+
205
+ function renderCurrentError() {
206
+ if (!currentErrorState) return;
207
+ var t = getT();
208
+ if (currentErrorState.type === "simple") {
209
+ errorBox.textContent = t(currentErrorState.key, currentErrorState.vars);
210
+ } else if (currentErrorState.type === "wrong_code") {
211
+ var msg = t("resetErrWrongCode");
212
+ if (currentErrorState.attemptsLeft) {
213
+ msg += " " + t("resetErrAttemptsLeft", { n: currentErrorState.attemptsLeft });
214
+ }
215
+ errorBox.textContent = msg;
216
+ } else if (currentErrorState.type === "invalid_password") {
217
+ errorBox.textContent =
218
+ window.LevixGatewayI18n && window.LevixGatewayI18n.lang === "ar"
219
+ ? t("resetErrInvalidPassword")
220
+ : currentErrorState.message || t("resetErrInvalidPassword");
221
+ } else if (currentErrorState.type === "raw") {
222
+ errorBox.textContent = currentErrorState.text;
223
+ }
206
224
  errorBox.hidden = false;
207
225
  }
226
+
227
+ function showErrorKey(key, vars) {
228
+ currentErrorState = { type: "simple", key: key, vars: vars };
229
+ renderCurrentError();
230
+ }
231
+ function showError(text) {
232
+ currentErrorState = { type: "raw", text: text };
233
+ renderCurrentError();
234
+ }
208
235
  function hideError() {
236
+ currentErrorState = null;
209
237
  errorBox.hidden = true;
210
238
  }
239
+
240
+ window.addEventListener("levix-gateway-lang", renderCurrentError);
241
+ window.addEventListener("levix-ui-prefs", renderCurrentError);
242
+ window.addEventListener("storage", function (e) {
243
+ if (e.key === "levix_lang") renderCurrentError();
244
+ });
245
+
211
246
  async function postJson(url, body) {
212
247
  const response = await fetch(url, {
213
248
  method: "POST",
@@ -240,19 +275,16 @@
240
275
  form.hidden = false;
241
276
  document.getElementById("reset-code").focus();
242
277
  } else if (result.reason === "throttled") {
243
- showError("Too many requests. Try again in " + (result.retryAfter || 60) + " seconds.");
278
+ showErrorKey("resetErrThrottled", { n: String(result.retryAfter || 60) });
244
279
  } else if (result.reason === "no_session") {
245
- showError(
246
- "WhatsApp isn't connected, so the code can't be sent. " +
247
- "Sign in on the server and run `levix reset-password`, or start the WhatsApp session first."
248
- );
280
+ showErrorKey("resetErrNoSession");
249
281
  } else if (result.reason === "send_failed") {
250
- showError("Couldn't send the WhatsApp message. Try again in a minute.");
282
+ showErrorKey("resetErrSendFailed");
251
283
  } else {
252
- showError("Couldn't start the reset. Try again.");
284
+ showErrorKey("resetErrStart");
253
285
  }
254
286
  } catch {
255
- showError("Couldn't reach the server. Try again.");
287
+ showErrorKey("resetErrServer");
256
288
  }
257
289
  sendBtn.disabled = false;
258
290
  });
@@ -269,34 +301,33 @@
269
301
  if (result.ok) {
270
302
  form.hidden = true;
271
303
  sendBtn.hidden = true;
272
- showError("Password updated. Taking you to sign in…");
304
+ showErrorKey("resetDone");
273
305
  setTimeout(function () {
274
306
  window.location.href = "/";
275
307
  }, 900);
276
308
  return;
277
309
  }
278
310
  if (result.reason === "wrong_code") {
279
- showError(
280
- "Wrong code." +
281
- (result.attemptsLeft ? " " + result.attemptsLeft + " attempts left." : "")
282
- );
311
+ currentErrorState = { type: "wrong_code", attemptsLeft: result.attemptsLeft };
312
+ renderCurrentError();
283
313
  } else if (
284
314
  result.reason === "expired" ||
285
315
  result.reason === "no_pending" ||
286
316
  result.reason === "too_many_attempts"
287
317
  ) {
288
- showError("That code is no longer valid. Send a new one.");
318
+ showErrorKey("resetErrExpired");
289
319
  form.hidden = true;
290
320
  sendBtn.hidden = false;
291
321
  } else if (result.reason === "mismatch") {
292
- showError("The two passwords don't match.");
322
+ showErrorKey("resetErrMismatch");
293
323
  } else if (result.reason === "invalid_password") {
294
- showError(result.message || "That password isn't allowed.");
324
+ currentErrorState = { type: "invalid_password", message: result.message };
325
+ renderCurrentError();
295
326
  } else {
296
- showError("Couldn't reset the password. Try again.");
327
+ showErrorKey("resetErrFailed");
297
328
  }
298
329
  } catch {
299
- showError("Couldn't reach the server. Try again.");
330
+ showErrorKey("resetErrServer");
300
331
  }
301
332
  });
302
333
  })();