levix-bot 4.3.1 → 4.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 +4 -2
- package/package.json +1 -1
- package/public/dashboard/assets/index-COI8zgqa.css +2 -0
- package/public/dashboard/assets/index-IZMBuNAZ.js +505 -0
- package/public/dashboard/index.html +2 -2
- package/public/dashboard.css +32 -0
- package/scheduler.cjs +66 -7
- package/src/bootstrap/panel.js +16 -4
- package/src/commands/pack.cjs +159 -0
- package/src/commands/packs.cjs +47 -0
- package/src/commands/send.cjs +1 -18
- package/src/commands/sticker.cjs +77 -232
- package/src/commands/stickers.cjs +100 -0
- package/src/commands/toimage.cjs +88 -0
- package/src/config/defaults.cjs +5 -0
- package/src/config/settings.cjs +10 -0
- package/src/core/events.js +15 -1
- package/src/core/session.js +7 -0
- package/src/db/db.cjs +387 -0
- package/src/db/store.cjs +746 -24
- package/src/handlers/command.handler.js +4 -0
- package/src/panel/feedback.cjs +43 -14
- package/src/routes/dashboard.api.esm.js +172 -20
- package/src/routes/stickers.api.esm.js +570 -0
- package/src/services/llmDiscovery.cjs +32 -10
- package/src/stickers/errors.cjs +239 -0
- package/src/stickers/ffmpeg.cjs +133 -0
- package/src/stickers/jobs.cjs +183 -0
- package/src/stickers/library.cjs +712 -0
- package/src/stickers/limits.cjs +60 -0
- package/src/stickers/media.cjs +748 -0
- package/src/stickers/options.cjs +128 -0
- package/src/stickers/owner.cjs +103 -0
- package/src/stickers/studio.cjs +473 -0
- package/src/stickers/uploads.cjs +263 -0
- package/src/stickers/webp.cjs +231 -0
- package/src/stickers/zip.cjs +107 -0
- package/src/utils/messageContent.cjs +30 -4
- package/src/utils/multipart.esm.js +81 -0
- package/src/utils/recurrence.cjs +189 -30
- package/src/utils/stickerBot.cjs +84 -0
- package/src/utils/storage.cjs +3 -0
- package/src/utils/storage.esm.js +3 -0
- package/views/login.ejs +39 -3
- package/views/setup.ejs +21 -0
- package/public/dashboard/assets/index-B6RhYFIy.js +0 -446
- package/public/dashboard/assets/index-vEwOcxiH.css +0 -2
package/src/db/store.cjs
CHANGED
|
@@ -14,8 +14,11 @@
|
|
|
14
14
|
// handlers need the *same* singleton, and only a CJS module can be required
|
|
15
15
|
// synchronously from both sides.
|
|
16
16
|
|
|
17
|
+
const crypto = require("node:crypto");
|
|
18
|
+
|
|
17
19
|
const logger = require("../utils/logger.cjs");
|
|
18
|
-
const
|
|
20
|
+
const normalizeJid = require("../utils/normalizeJid.cjs");
|
|
21
|
+
const { db, q, parseJson, sweepExpired, checkpoint, DB_PATH } = require("./db.cjs");
|
|
19
22
|
|
|
20
23
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
21
24
|
const DEFAULT_FORWARD_TTL_DAYS = 30;
|
|
@@ -55,6 +58,13 @@ function migrateSecretsAtRest() {
|
|
|
55
58
|
|
|
56
59
|
async function initStore() {
|
|
57
60
|
sweepExpired();
|
|
61
|
+
try {
|
|
62
|
+
// Stale sticker uploads and half-finished conversions from a crash.
|
|
63
|
+
// The timer lives in uploads.cjs; this is the boot pass.
|
|
64
|
+
require("../stickers/uploads.cjs").sweepStale();
|
|
65
|
+
} catch (err) {
|
|
66
|
+
logger.error({ err }, "[Store] sticker temp sweep failed");
|
|
67
|
+
}
|
|
58
68
|
try {
|
|
59
69
|
migrateSecretsAtRest();
|
|
60
70
|
} catch (err) {
|
|
@@ -164,9 +174,9 @@ function countGroups() {
|
|
|
164
174
|
|
|
165
175
|
function upsertGroupDirectory(groupId, { subject = null, participantCount = null } = {}) {
|
|
166
176
|
if (!groupId) return;
|
|
167
|
-
const existing = q(
|
|
168
|
-
|
|
169
|
-
);
|
|
177
|
+
const existing = q(
|
|
178
|
+
"SELECT subject, participant_count FROM group_directory WHERE group_id = ?",
|
|
179
|
+
).get(groupId);
|
|
170
180
|
q(
|
|
171
181
|
`INSERT INTO group_directory (group_id, subject, participant_count, updated_at)
|
|
172
182
|
VALUES (?, ?, ?, ?)
|
|
@@ -183,9 +193,9 @@ function upsertGroupDirectory(groupId, { subject = null, participantCount = null
|
|
|
183
193
|
}
|
|
184
194
|
|
|
185
195
|
function getGroupDirectory(groupId) {
|
|
186
|
-
return q(
|
|
187
|
-
|
|
188
|
-
);
|
|
196
|
+
return q(
|
|
197
|
+
"SELECT group_id, subject, participant_count, updated_at FROM group_directory WHERE group_id = ?",
|
|
198
|
+
).get(groupId);
|
|
189
199
|
}
|
|
190
200
|
|
|
191
201
|
function getAllGroupDirectory() {
|
|
@@ -373,6 +383,33 @@ function getLidForPn(pn) {
|
|
|
373
383
|
return row ? row.lid : null;
|
|
374
384
|
}
|
|
375
385
|
|
|
386
|
+
// Every LID stored for this phone. Writers persist the pn they are given
|
|
387
|
+
// (storeLidPnMapping); a device suffix is the only form normalizeJid folds
|
|
388
|
+
// onto the same number, so the lookup is an index read of that pn plus the
|
|
389
|
+
// `user:` prefix, not a scan of the table.
|
|
390
|
+
function getLidsForPn(pn) {
|
|
391
|
+
if (!pn) return [];
|
|
392
|
+
const found = new Map();
|
|
393
|
+
const consider = (rows) => {
|
|
394
|
+
for (const row of rows) {
|
|
395
|
+
if (!row?.lid || !row?.pn || found.has(row.lid)) continue;
|
|
396
|
+
if (normalizeJid(row.pn) !== pn && row.pn !== pn) continue;
|
|
397
|
+
found.set(row.lid, row.lid);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
consider(q("SELECT lid, pn FROM lid_mapping WHERE pn = ?").all(pn));
|
|
401
|
+
if (!String(pn).includes(":")) {
|
|
402
|
+
const at = String(pn).lastIndexOf("@");
|
|
403
|
+
if (at > 0) {
|
|
404
|
+
const user = String(pn).slice(0, at);
|
|
405
|
+
consider(
|
|
406
|
+
q("SELECT lid, pn FROM lid_mapping WHERE pn >= ? AND pn < ?").all(`${user}:`, `${user};`),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return [...found.keys()];
|
|
411
|
+
}
|
|
412
|
+
|
|
376
413
|
function getLidsForPns(pns) {
|
|
377
414
|
const map = new Map();
|
|
378
415
|
if (!pns?.length) return map;
|
|
@@ -411,7 +448,79 @@ function rowToUser(row) {
|
|
|
411
448
|
firstSeen: row.first_seen,
|
|
412
449
|
lastSeen: row.last_seen,
|
|
413
450
|
displayName: row.display_name ?? null,
|
|
451
|
+
savedName: row.saved_name ?? null,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function isLidJid(value) {
|
|
456
|
+
return String(value || "").includes("@lid");
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function isGroupJid(value) {
|
|
460
|
+
return String(value || "").endsWith("@g.us");
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function asLidJid(value) {
|
|
464
|
+
if (!value) return null;
|
|
465
|
+
const raw = String(value).trim();
|
|
466
|
+
if (!raw) return null;
|
|
467
|
+
const local = raw.split(":")[0].split("@")[0];
|
|
468
|
+
if (!local) return null;
|
|
469
|
+
if (raw.includes("@lid") || (!raw.includes("@") && !/^\d{8,15}$/.test(local))) {
|
|
470
|
+
return `${local}@lid`;
|
|
471
|
+
}
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function asPnJid(value) {
|
|
476
|
+
const phone = digitsOf(value);
|
|
477
|
+
if (!phone || phone.length < 8 || phone.length > 15) return null;
|
|
478
|
+
return `${phone}@s.whatsapp.net`;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function personName(value) {
|
|
482
|
+
const text = String(value || "").trim();
|
|
483
|
+
if (!text || text.includes("@")) return null;
|
|
484
|
+
return text.slice(0, 128);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function lidLocalPart(value) {
|
|
488
|
+
if (!isLidJid(value)) return null;
|
|
489
|
+
return String(value).split("@")[0].split(":")[0];
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Find the existing user_metadata row for a person named by any mix of JID,
|
|
494
|
+
* LID and phone, including the lid_mapping table, so a LID and a phone JID
|
|
495
|
+
* of the same person land on one record.
|
|
496
|
+
*/
|
|
497
|
+
function resolveExistingUser(userData) {
|
|
498
|
+
const candidates = [];
|
|
499
|
+
const add = (value) => {
|
|
500
|
+
if (value && !candidates.includes(value)) candidates.push(value);
|
|
414
501
|
};
|
|
502
|
+
add(userData?.jid);
|
|
503
|
+
add(userData?.lid);
|
|
504
|
+
add(userData?.phone);
|
|
505
|
+
if (userData?.jid) {
|
|
506
|
+
add(getPnForLid(userData.jid));
|
|
507
|
+
add(getLidForPn(userData.jid));
|
|
508
|
+
if (!isLidJid(userData.jid)) add(asPnJid(userData.jid));
|
|
509
|
+
}
|
|
510
|
+
if (userData?.lid) {
|
|
511
|
+
add(getPnForLid(userData.lid));
|
|
512
|
+
add(asLidJid(userData.lid));
|
|
513
|
+
}
|
|
514
|
+
if (userData?.phone) {
|
|
515
|
+
const pnJid = asPnJid(userData.phone);
|
|
516
|
+
add(pnJid);
|
|
517
|
+
if (pnJid) add(getLidForPn(pnJid));
|
|
518
|
+
}
|
|
519
|
+
for (const identifier of candidates) {
|
|
520
|
+
const found = getUserMetadata(identifier);
|
|
521
|
+
if (found) return found;
|
|
522
|
+
}
|
|
523
|
+
return null;
|
|
415
524
|
}
|
|
416
525
|
|
|
417
526
|
function userRow(jid) {
|
|
@@ -427,37 +536,53 @@ function userRow(jid) {
|
|
|
427
536
|
* next message. Same story for `lid` / `phone` / `displayName`: a missing
|
|
428
537
|
* field keeps whatever we already knew instead of nulling it out.
|
|
429
538
|
*
|
|
430
|
-
* @param {object} userData - { jid, lid?, phone?, isOwner?, isAdmin?, displayName? }
|
|
539
|
+
* @param {object} userData - { jid, lid?, phone?, isOwner?, isAdmin?, displayName?, savedName? }
|
|
431
540
|
*/
|
|
432
541
|
function saveUserMetadata(userData) {
|
|
433
542
|
if (!userData?.jid) return;
|
|
434
543
|
const now = Date.now();
|
|
435
|
-
const
|
|
544
|
+
const resolved = resolveExistingUser(userData);
|
|
545
|
+
const jid =
|
|
546
|
+
resolved?.jid ||
|
|
547
|
+
(isLidJid(userData.jid) ? userData.jid : asPnJid(userData.jid) || userData.jid);
|
|
548
|
+
const existing = userRow(jid) || (resolved ? userRow(resolved.jid) : null);
|
|
549
|
+
|
|
550
|
+
let phone = userData.phone || existing?.phone_number || null;
|
|
551
|
+
const lidDigits = lidLocalPart(userData.jid);
|
|
552
|
+
if (phone && lidDigits && String(phone).replace(/\D/g, "") === lidDigits) {
|
|
553
|
+
phone = existing?.phone_number || null;
|
|
554
|
+
}
|
|
555
|
+
if (phone && isLidJid(phone)) phone = existing?.phone_number || null;
|
|
556
|
+
|
|
557
|
+
const incomingSaved = personName(userData.savedName);
|
|
558
|
+
const incomingPush = personName(userData.displayName);
|
|
436
559
|
|
|
437
560
|
const row = {
|
|
438
|
-
user_jid:
|
|
439
|
-
user_lid: userData.lid || existing?.user_lid || null,
|
|
440
|
-
phone_number:
|
|
561
|
+
user_jid: jid,
|
|
562
|
+
user_lid: userData.lid || existing?.user_lid || (isLidJid(userData.jid) ? userData.jid : null),
|
|
563
|
+
phone_number: phone,
|
|
441
564
|
is_owner:
|
|
442
565
|
userData.isOwner === undefined ? (existing?.is_owner === 1 ? 1 : 0) : bool(userData.isOwner),
|
|
443
566
|
is_admin:
|
|
444
567
|
userData.isAdmin === undefined ? (existing?.is_admin === 1 ? 1 : 0) : bool(userData.isAdmin),
|
|
445
568
|
first_seen: existing?.first_seen ?? now,
|
|
446
569
|
last_seen: now,
|
|
447
|
-
display_name:
|
|
570
|
+
display_name: incomingPush || existing?.display_name || null,
|
|
571
|
+
saved_name: incomingSaved || existing?.saved_name || null,
|
|
448
572
|
};
|
|
449
573
|
|
|
450
574
|
q(
|
|
451
575
|
`INSERT INTO user_metadata
|
|
452
|
-
(user_jid, user_lid, phone_number, is_owner, is_admin, first_seen, last_seen, display_name)
|
|
453
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
576
|
+
(user_jid, user_lid, phone_number, is_owner, is_admin, first_seen, last_seen, display_name, saved_name)
|
|
577
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
454
578
|
ON CONFLICT(user_jid) DO UPDATE SET
|
|
455
579
|
user_lid = excluded.user_lid,
|
|
456
580
|
phone_number = excluded.phone_number,
|
|
457
581
|
is_owner = excluded.is_owner,
|
|
458
582
|
is_admin = excluded.is_admin,
|
|
459
583
|
last_seen = excluded.last_seen,
|
|
460
|
-
display_name = excluded.display_name
|
|
584
|
+
display_name = excluded.display_name,
|
|
585
|
+
saved_name = excluded.saved_name`,
|
|
461
586
|
).run(
|
|
462
587
|
row.user_jid,
|
|
463
588
|
row.user_lid,
|
|
@@ -467,9 +592,72 @@ function saveUserMetadata(userData) {
|
|
|
467
592
|
row.first_seen,
|
|
468
593
|
row.last_seen,
|
|
469
594
|
row.display_name,
|
|
595
|
+
row.saved_name,
|
|
470
596
|
);
|
|
471
597
|
}
|
|
472
598
|
|
|
599
|
+
/**
|
|
600
|
+
* Store an address-book name for a number only when none is saved yet.
|
|
601
|
+
* Used when the panel creates a schedule from a picked phone-book contact.
|
|
602
|
+
*/
|
|
603
|
+
function rememberSavedName(identifier, name) {
|
|
604
|
+
const savedName = personName(name);
|
|
605
|
+
if (!identifier || !savedName) return null;
|
|
606
|
+
const existing = getUserMetadata(identifier);
|
|
607
|
+
if (existing?.savedName) return existing;
|
|
608
|
+
let jid = existing?.jid;
|
|
609
|
+
if (!jid) {
|
|
610
|
+
if (String(identifier).includes("@")) jid = identifier;
|
|
611
|
+
else {
|
|
612
|
+
const phone = digitsOf(identifier);
|
|
613
|
+
jid = phone ? `${phone}@s.whatsapp.net` : identifier;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
saveUserMetadata({
|
|
617
|
+
jid,
|
|
618
|
+
lid: existing?.lid || (isLidJid(identifier) ? identifier : null),
|
|
619
|
+
phone: existing?.phone || digitsOf(identifier),
|
|
620
|
+
savedName,
|
|
621
|
+
});
|
|
622
|
+
return getUserMetadata(jid);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
function isIgnoredContactId(id) {
|
|
626
|
+
const value = String(id || "");
|
|
627
|
+
if (!value) return true;
|
|
628
|
+
if (value.endsWith("@g.us")) return true;
|
|
629
|
+
if (value.includes("@broadcast") || value.includes("@newsletter")) return true;
|
|
630
|
+
if (value === "status@broadcast") return true;
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Persist a Baileys v7 contact record from `contacts.upsert` / `contacts.update`.
|
|
636
|
+
* `name` is the address-book name; `notify` is the push name. Never writes a
|
|
637
|
+
* push name into saved_name.
|
|
638
|
+
*/
|
|
639
|
+
function applyContactRecord(contact) {
|
|
640
|
+
if (!contact?.id || isIgnoredContactId(contact.id) || isGroupJid(contact.id)) return;
|
|
641
|
+
|
|
642
|
+
const id = String(contact.id);
|
|
643
|
+
const lid = asLidJid(contact.lid) || (isLidJid(id) ? asLidJid(id) : null);
|
|
644
|
+
const pnJid = asPnJid(contact.phoneNumber) || (!isLidJid(id) ? asPnJid(id) : null);
|
|
645
|
+
if (lid && pnJid) storeLidPnMapping(lid, pnJid);
|
|
646
|
+
|
|
647
|
+
const jid = pnJid || lid || id;
|
|
648
|
+
if (isIgnoredContactId(jid)) return;
|
|
649
|
+
|
|
650
|
+
const savedName = personName(contact.name);
|
|
651
|
+
const pushName = personName(contact.notify) || personName(contact.verifiedName);
|
|
652
|
+
saveUserMetadata({
|
|
653
|
+
jid,
|
|
654
|
+
lid,
|
|
655
|
+
phone: pnJid ? digitsOf(pnJid) : null,
|
|
656
|
+
...(savedName ? { savedName } : {}),
|
|
657
|
+
...(pushName ? { displayName: pushName } : {}),
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
|
|
473
661
|
/**
|
|
474
662
|
* Get user metadata by JID, LID, or bare phone number. Four steps, in order:
|
|
475
663
|
* exact JID, then LID, then phone, then a digits-only match against either
|
|
@@ -673,6 +861,17 @@ function countDebts(settled = false) {
|
|
|
673
861
|
// A job is `{ id, type, targetJid, message, cronString?, date?, status,
|
|
674
862
|
// creatorJid, lastRunAt?, lastDeliveryStatus?, lastError? }`.
|
|
675
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
|
+
|
|
676
875
|
function scheduleRow(row) {
|
|
677
876
|
if (!row) return null;
|
|
678
877
|
return {
|
|
@@ -688,6 +887,7 @@ function scheduleRow(row) {
|
|
|
688
887
|
lastRunAt: row.last_run_at ?? null,
|
|
689
888
|
lastDeliveryStatus: row.last_delivery_status ?? null,
|
|
690
889
|
lastError: row.last_error ?? null,
|
|
890
|
+
media: parseMediaColumn(row.media),
|
|
691
891
|
};
|
|
692
892
|
}
|
|
693
893
|
|
|
@@ -703,8 +903,8 @@ function saveSchedule(job) {
|
|
|
703
903
|
q(
|
|
704
904
|
`INSERT INTO schedules
|
|
705
905
|
(id, type, target_jid, message, cron_string, date, status, creator_jid,
|
|
706
|
-
created_at, last_run_at, last_delivery_status, last_error)
|
|
707
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
906
|
+
created_at, last_run_at, last_delivery_status, last_error, media)
|
|
907
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
708
908
|
ON CONFLICT(id) DO UPDATE SET
|
|
709
909
|
type = excluded.type,
|
|
710
910
|
target_jid = excluded.target_jid,
|
|
@@ -715,7 +915,8 @@ function saveSchedule(job) {
|
|
|
715
915
|
creator_jid = excluded.creator_jid,
|
|
716
916
|
last_run_at = excluded.last_run_at,
|
|
717
917
|
last_delivery_status = excluded.last_delivery_status,
|
|
718
|
-
last_error = excluded.last_error
|
|
918
|
+
last_error = excluded.last_error,
|
|
919
|
+
media = excluded.media`,
|
|
719
920
|
).run(
|
|
720
921
|
String(job.id),
|
|
721
922
|
job.type,
|
|
@@ -729,6 +930,7 @@ function saveSchedule(job) {
|
|
|
729
930
|
job.lastRunAt ?? null,
|
|
730
931
|
job.lastDeliveryStatus ?? null,
|
|
731
932
|
job.lastError ?? null,
|
|
933
|
+
job.media ? JSON.stringify(job.media) : null,
|
|
732
934
|
);
|
|
733
935
|
return getSchedule(job.id);
|
|
734
936
|
}
|
|
@@ -881,6 +1083,7 @@ function resolveUserPhone(jid) {
|
|
|
881
1083
|
const digits = String(pn).split("@")[0].replace(/\D/g, "");
|
|
882
1084
|
if (digits) return digits;
|
|
883
1085
|
}
|
|
1086
|
+
if (raw.includes("@lid")) return null;
|
|
884
1087
|
}
|
|
885
1088
|
if (/^\d{8,15}$/.test(local)) return local;
|
|
886
1089
|
return null;
|
|
@@ -888,30 +1091,516 @@ function resolveUserPhone(jid) {
|
|
|
888
1091
|
|
|
889
1092
|
function describePeer(jid) {
|
|
890
1093
|
const id = String(jid || "");
|
|
891
|
-
if (!id)
|
|
1094
|
+
if (!id) {
|
|
1095
|
+
return {
|
|
1096
|
+
jid: "",
|
|
1097
|
+
kind: "unknown",
|
|
1098
|
+
label: "",
|
|
1099
|
+
phone: null,
|
|
1100
|
+
savedName: null,
|
|
1101
|
+
pushName: null,
|
|
1102
|
+
memberCount: null,
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
892
1105
|
if (id.endsWith("@g.us")) {
|
|
893
1106
|
const dir = getGroupDirectory(id);
|
|
894
|
-
const label = dir?.subject ||
|
|
1107
|
+
const label = personName(dir?.subject) || "Group";
|
|
895
1108
|
return {
|
|
896
1109
|
jid: id,
|
|
897
1110
|
kind: "group",
|
|
898
|
-
label
|
|
1111
|
+
label,
|
|
899
1112
|
phone: null,
|
|
1113
|
+
savedName: null,
|
|
1114
|
+
pushName: null,
|
|
900
1115
|
memberCount: dir?.participant_count ?? null,
|
|
901
1116
|
};
|
|
902
1117
|
}
|
|
903
1118
|
const phone = resolveUserPhone(id);
|
|
904
1119
|
const meta = getUserMetadata(id);
|
|
905
|
-
const
|
|
1120
|
+
const savedName = personName(meta?.savedName);
|
|
1121
|
+
const pushName = personName(meta?.displayName);
|
|
1122
|
+
const phoneLabel = phone ? `+${phone}` : null;
|
|
1123
|
+
const label = savedName || pushName || phoneLabel || "Contact";
|
|
906
1124
|
return {
|
|
907
1125
|
jid: id,
|
|
908
1126
|
kind: "contact",
|
|
909
1127
|
label,
|
|
910
|
-
phone:
|
|
1128
|
+
phone: phoneLabel,
|
|
1129
|
+
savedName,
|
|
1130
|
+
pushName,
|
|
911
1131
|
memberCount: null,
|
|
912
1132
|
};
|
|
913
1133
|
}
|
|
914
1134
|
|
|
1135
|
+
// ===================================================================
|
|
1136
|
+
// --- Sticker Studio ---
|
|
1137
|
+
// ===================================================================
|
|
1138
|
+
//
|
|
1139
|
+
// Every statement the library uses lives here. Multi-row changes run inside
|
|
1140
|
+
// withImmediateTransaction so a crash cannot leave a pack half-merged or a
|
|
1141
|
+
// sticker deleted while its pack positions still have a hole. The WebP files
|
|
1142
|
+
// themselves are not in SQLite — the library writes them beside the row, on
|
|
1143
|
+
// the same thread, with no await in between.
|
|
1144
|
+
|
|
1145
|
+
function withImmediateTransaction(fn) {
|
|
1146
|
+
db.exec("BEGIN IMMEDIATE");
|
|
1147
|
+
try {
|
|
1148
|
+
const result = fn();
|
|
1149
|
+
db.exec("COMMIT");
|
|
1150
|
+
return result;
|
|
1151
|
+
} catch (error) {
|
|
1152
|
+
try {
|
|
1153
|
+
db.exec("ROLLBACK");
|
|
1154
|
+
} catch {
|
|
1155
|
+
// The statement that failed may already have rolled the transaction back.
|
|
1156
|
+
}
|
|
1157
|
+
throw error;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
function sqlIn(values) {
|
|
1162
|
+
return values.map(() => "?").join(", ");
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
function newStickerOwnerId() {
|
|
1166
|
+
return crypto.randomBytes(8).toString("hex");
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
function stickerOwnerInsert(id, kind, account) {
|
|
1170
|
+
q(
|
|
1171
|
+
`INSERT INTO sticker_owners (id, kind, account, legacy_key, created_at)
|
|
1172
|
+
VALUES (?, ?, ?, NULL, ?)`,
|
|
1173
|
+
).run(id, kind, account, Date.now());
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
/**
|
|
1177
|
+
* The panel library. `pairedAccount` is the linked account's normalized LID,
|
|
1178
|
+
* or its normalized phone JID when the credentials carry no lid, or null when
|
|
1179
|
+
* nothing is paired. One IMMEDIATE transaction when a row has to change; a
|
|
1180
|
+
* plain read when it does not.
|
|
1181
|
+
*
|
|
1182
|
+
* The partial unique index allows one `self` row, so an old self is demoted
|
|
1183
|
+
* before another row is promoted. A missing self whose account is already
|
|
1184
|
+
* taken promotes that row: a second insert would violate UNIQUE(account).
|
|
1185
|
+
*
|
|
1186
|
+
* `pairedPhone` is set only when the account is a LID: the same pairing's
|
|
1187
|
+
* phone JID. A self row still bound to it was bound before the credentials
|
|
1188
|
+
* carried a lid, so it moves onto the LID. A stray row already holding that
|
|
1189
|
+
* LID loses its account; it is kept, unreachable, and the panel keeps the
|
|
1190
|
+
* library it had.
|
|
1191
|
+
* @param {string|null} pairedAccount
|
|
1192
|
+
* @param {string|null} [pairedPhone]
|
|
1193
|
+
* @returns {{ key: string }}
|
|
1194
|
+
*/
|
|
1195
|
+
function stickerOwnerReconcileSelf(pairedAccount, pairedPhone = null) {
|
|
1196
|
+
const account = pairedAccount || null;
|
|
1197
|
+
const self = q("SELECT id, account FROM sticker_owners WHERE kind = 'self'").get();
|
|
1198
|
+
if (self && (account == null || self.account === account)) return { key: self.id };
|
|
1199
|
+
|
|
1200
|
+
return withImmediateTransaction(() => {
|
|
1201
|
+
const current = q("SELECT id, account FROM sticker_owners WHERE kind = 'self'").get() || null;
|
|
1202
|
+
if (current && (account == null || current.account === account)) return { key: current.id };
|
|
1203
|
+
|
|
1204
|
+
if (current && account && pairedPhone && current.account === pairedPhone) {
|
|
1205
|
+
q("UPDATE sticker_owners SET account = NULL WHERE account = ? AND id <> ?").run(
|
|
1206
|
+
account,
|
|
1207
|
+
current.id,
|
|
1208
|
+
);
|
|
1209
|
+
q("UPDATE sticker_owners SET account = ? WHERE id = ?").run(account, current.id);
|
|
1210
|
+
return { key: current.id };
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
if (!current) {
|
|
1214
|
+
if (account) {
|
|
1215
|
+
const existing = q("SELECT id FROM sticker_owners WHERE account = ?").get(account);
|
|
1216
|
+
if (existing) {
|
|
1217
|
+
q("UPDATE sticker_owners SET kind = 'self' WHERE id = ?").run(existing.id);
|
|
1218
|
+
return { key: existing.id };
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
const id = newStickerOwnerId();
|
|
1222
|
+
stickerOwnerInsert(id, "self", account);
|
|
1223
|
+
return { key: id };
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
if (current.account == null) {
|
|
1227
|
+
const existing = account
|
|
1228
|
+
? q("SELECT id FROM sticker_owners WHERE account = ?").get(account)
|
|
1229
|
+
: null;
|
|
1230
|
+
if (!existing) {
|
|
1231
|
+
q("UPDATE sticker_owners SET account = ? WHERE id = ?").run(account, current.id);
|
|
1232
|
+
return { key: current.id };
|
|
1233
|
+
}
|
|
1234
|
+
q("UPDATE sticker_owners SET kind = 'user', account = NULL WHERE id = ?").run(current.id);
|
|
1235
|
+
q("UPDATE sticker_owners SET kind = 'self' WHERE id = ?").run(existing.id);
|
|
1236
|
+
return { key: existing.id };
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// Bound to A, and the paired account is a different B. A keeps the old
|
|
1240
|
+
// library. B must not see it.
|
|
1241
|
+
q("UPDATE sticker_owners SET kind = 'user' WHERE id = ?").run(current.id);
|
|
1242
|
+
const existing = q("SELECT id FROM sticker_owners WHERE account = ?").get(account);
|
|
1243
|
+
if (existing) {
|
|
1244
|
+
q("UPDATE sticker_owners SET kind = 'self' WHERE id = ?").run(existing.id);
|
|
1245
|
+
return { key: existing.id };
|
|
1246
|
+
}
|
|
1247
|
+
const id = newStickerOwnerId();
|
|
1248
|
+
stickerOwnerInsert(id, "self", account);
|
|
1249
|
+
return { key: id };
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
/** The row whose account is this LID, created on first use. May be `self`. */
|
|
1254
|
+
function stickerOwnerEnsure(account) {
|
|
1255
|
+
if (!account) return { key: null };
|
|
1256
|
+
const existing = q("SELECT id FROM sticker_owners WHERE account = ?").get(account);
|
|
1257
|
+
if (existing) return { key: existing.id };
|
|
1258
|
+
const id = newStickerOwnerId();
|
|
1259
|
+
q(
|
|
1260
|
+
`INSERT INTO sticker_owners (id, kind, account, legacy_key, created_at)
|
|
1261
|
+
VALUES (?, 'user', ?, NULL, ?)
|
|
1262
|
+
ON CONFLICT(account) DO NOTHING`,
|
|
1263
|
+
).run(id, account, Date.now());
|
|
1264
|
+
const row = q("SELECT id FROM sticker_owners WHERE account = ?").get(account);
|
|
1265
|
+
if (!row) throw new Error("sticker owner was not created");
|
|
1266
|
+
return { key: row.id };
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
function stickerCount(owner) {
|
|
1270
|
+
if (!owner) return 0;
|
|
1271
|
+
return q("SELECT COUNT(*) AS n FROM stickers WHERE owner = ?").get(owner).n;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
function stickerFindBySha(owner, sha) {
|
|
1275
|
+
if (!owner) return null;
|
|
1276
|
+
return q("SELECT * FROM stickers WHERE sha256 = ? AND owner = ?").get(sha, owner) || null;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
function stickerGet(owner, id) {
|
|
1280
|
+
if (!owner) return null;
|
|
1281
|
+
return q("SELECT * FROM stickers WHERE id = ? AND owner = ?").get(id, owner) || null;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
function stickerInsert(row) {
|
|
1285
|
+
q(
|
|
1286
|
+
`INSERT INTO stickers (
|
|
1287
|
+
id, owner, sha256, name, animated, width, height, duration_ms, file_size,
|
|
1288
|
+
source, source_mime, is_favorite, created_at, updated_at, last_used_at
|
|
1289
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1290
|
+
).run(
|
|
1291
|
+
row.id,
|
|
1292
|
+
row.owner,
|
|
1293
|
+
row.sha256,
|
|
1294
|
+
row.name,
|
|
1295
|
+
row.animated ? 1 : 0,
|
|
1296
|
+
row.width,
|
|
1297
|
+
row.height,
|
|
1298
|
+
row.durationMs,
|
|
1299
|
+
row.fileSize,
|
|
1300
|
+
row.source,
|
|
1301
|
+
row.sourceMime,
|
|
1302
|
+
row.favorite ? 1 : 0,
|
|
1303
|
+
row.createdAt,
|
|
1304
|
+
row.updatedAt,
|
|
1305
|
+
row.lastUsedAt,
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
function stickerUpdate(id, fields) {
|
|
1310
|
+
const sets = [];
|
|
1311
|
+
const params = [];
|
|
1312
|
+
if (fields.name !== undefined) {
|
|
1313
|
+
sets.push("name = ?");
|
|
1314
|
+
params.push(fields.name);
|
|
1315
|
+
}
|
|
1316
|
+
if (fields.isFavorite !== undefined) {
|
|
1317
|
+
sets.push("is_favorite = ?");
|
|
1318
|
+
params.push(fields.isFavorite ? 1 : 0);
|
|
1319
|
+
}
|
|
1320
|
+
if (fields.updatedAt !== undefined) {
|
|
1321
|
+
sets.push("updated_at = ?");
|
|
1322
|
+
params.push(fields.updatedAt);
|
|
1323
|
+
}
|
|
1324
|
+
if (fields.lastUsedAt !== undefined) {
|
|
1325
|
+
sets.push("last_used_at = ?");
|
|
1326
|
+
params.push(fields.lastUsedAt);
|
|
1327
|
+
}
|
|
1328
|
+
if (!sets.length) return;
|
|
1329
|
+
params.push(id);
|
|
1330
|
+
q(`UPDATE stickers SET ${sets.join(", ")} WHERE id = ?`).run(...params);
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
function stickerDelete(id) {
|
|
1334
|
+
q("DELETE FROM stickers WHERE id = ?").run(id);
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
function stickerShaCount(sha) {
|
|
1338
|
+
return q("SELECT COUNT(*) AS n FROM stickers WHERE sha256 = ?").get(sha).n;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
const STICKER_SORTS = {
|
|
1342
|
+
newest: "s.created_at DESC, s.id DESC",
|
|
1343
|
+
oldest: "s.created_at ASC, s.id ASC",
|
|
1344
|
+
name: "s.name COLLATE NOCASE ASC, s.id ASC",
|
|
1345
|
+
recent: "COALESCE(s.last_used_at, s.created_at) DESC, s.id DESC",
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
function stickerQuery(owner, filter) {
|
|
1349
|
+
if (!owner) return { rows: [], total: 0 };
|
|
1350
|
+
const clauses = ["s.owner = ?"];
|
|
1351
|
+
const params = [owner];
|
|
1352
|
+
|
|
1353
|
+
const text = String(filter.q || "")
|
|
1354
|
+
.trim()
|
|
1355
|
+
.toLowerCase();
|
|
1356
|
+
if (text) {
|
|
1357
|
+
const escaped = text.replace(/[\\%_]/g, (ch) => `\\${ch}`);
|
|
1358
|
+
clauses.push("LOWER(s.name) LIKE ? ESCAPE '\\'");
|
|
1359
|
+
params.push(`%${escaped}%`);
|
|
1360
|
+
}
|
|
1361
|
+
if (filter.filter === "favorites") clauses.push("s.is_favorite = 1");
|
|
1362
|
+
if (filter.filter === "animated") clauses.push("s.animated = 1");
|
|
1363
|
+
if (filter.filter === "static") clauses.push("s.animated = 0");
|
|
1364
|
+
if (filter.filter === "recent") {
|
|
1365
|
+
clauses.push("COALESCE(s.last_used_at, s.created_at) >= ?");
|
|
1366
|
+
params.push(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
|
1367
|
+
}
|
|
1368
|
+
if (filter.pack === "none") {
|
|
1369
|
+
clauses.push(
|
|
1370
|
+
`NOT EXISTS (
|
|
1371
|
+
SELECT 1 FROM sticker_pack_items i
|
|
1372
|
+
JOIN sticker_packs p ON p.id = i.pack_id
|
|
1373
|
+
WHERE i.sticker_id = s.id AND p.owner = ?
|
|
1374
|
+
)`,
|
|
1375
|
+
);
|
|
1376
|
+
params.push(owner);
|
|
1377
|
+
} else if (filter.pack) {
|
|
1378
|
+
clauses.push(
|
|
1379
|
+
`EXISTS (
|
|
1380
|
+
SELECT 1 FROM sticker_pack_items i
|
|
1381
|
+
JOIN sticker_packs p ON p.id = i.pack_id
|
|
1382
|
+
WHERE i.sticker_id = s.id AND i.pack_id = ? AND p.owner = ?
|
|
1383
|
+
)`,
|
|
1384
|
+
);
|
|
1385
|
+
params.push(filter.pack, owner);
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
const where = clauses.join(" AND ");
|
|
1389
|
+
const total = q(`SELECT COUNT(*) AS n FROM stickers s WHERE ${where}`).get(...params).n;
|
|
1390
|
+
const sort =
|
|
1391
|
+
filter.filter === "recent"
|
|
1392
|
+
? STICKER_SORTS.recent
|
|
1393
|
+
: STICKER_SORTS[filter.sort] || STICKER_SORTS.newest;
|
|
1394
|
+
const limit = Math.min(200, Math.max(1, Number(filter.limit) || 60));
|
|
1395
|
+
const offset = Math.max(0, Number(filter.offset) || 0);
|
|
1396
|
+
const rows = q(`SELECT s.* FROM stickers s WHERE ${where} ORDER BY ${sort} LIMIT ? OFFSET ?`).all(
|
|
1397
|
+
...params,
|
|
1398
|
+
limit,
|
|
1399
|
+
offset,
|
|
1400
|
+
);
|
|
1401
|
+
return { rows, total };
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
function stickerPackMembership(owner, stickerIds) {
|
|
1405
|
+
const map = new Map();
|
|
1406
|
+
if (!owner || !stickerIds.length) return map;
|
|
1407
|
+
const rows = q(
|
|
1408
|
+
`SELECT i.sticker_id, i.pack_id
|
|
1409
|
+
FROM sticker_pack_items i
|
|
1410
|
+
JOIN sticker_packs p ON p.id = i.pack_id
|
|
1411
|
+
WHERE i.sticker_id IN (${sqlIn(stickerIds)}) AND p.owner = ?
|
|
1412
|
+
ORDER BY i.added_at ASC, i.pack_id ASC`,
|
|
1413
|
+
).all(...stickerIds, owner);
|
|
1414
|
+
for (const row of rows) {
|
|
1415
|
+
const list = map.get(row.sticker_id);
|
|
1416
|
+
if (list) list.push(row.pack_id);
|
|
1417
|
+
else map.set(row.sticker_id, [row.pack_id]);
|
|
1418
|
+
}
|
|
1419
|
+
return map;
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
function stickerPacksUsing(owner, stickerId) {
|
|
1423
|
+
if (!owner) return [];
|
|
1424
|
+
return q(
|
|
1425
|
+
`SELECT p.id, p.name
|
|
1426
|
+
FROM sticker_pack_items i
|
|
1427
|
+
JOIN sticker_packs p ON p.id = i.pack_id
|
|
1428
|
+
WHERE i.sticker_id = ? AND p.owner = ?
|
|
1429
|
+
ORDER BY LOWER(p.name) ASC, p.id ASC`,
|
|
1430
|
+
).all(stickerId, owner);
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
function stickerPackIdsOf(stickerId) {
|
|
1434
|
+
return q("SELECT pack_id FROM sticker_pack_items WHERE sticker_id = ?").all(stickerId);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
function stickerClearAll() {
|
|
1438
|
+
return withImmediateTransaction(() => {
|
|
1439
|
+
const shas = q("SELECT DISTINCT sha256 FROM stickers")
|
|
1440
|
+
.all()
|
|
1441
|
+
.map((row) => row.sha256);
|
|
1442
|
+
q("DELETE FROM stickers").run();
|
|
1443
|
+
q("DELETE FROM sticker_packs").run();
|
|
1444
|
+
q("DELETE FROM sticker_owners").run();
|
|
1445
|
+
return shas;
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
function stickerPackCount(owner) {
|
|
1450
|
+
if (!owner) return 0;
|
|
1451
|
+
return q("SELECT COUNT(*) AS n FROM sticker_packs WHERE owner = ?").get(owner).n;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
function stickerPackList(owner) {
|
|
1455
|
+
if (!owner) return [];
|
|
1456
|
+
return q(
|
|
1457
|
+
`SELECT p.*,
|
|
1458
|
+
(SELECT COUNT(*) FROM sticker_pack_items i WHERE i.pack_id = p.id) AS item_count,
|
|
1459
|
+
(SELECT i.sticker_id FROM sticker_pack_items i
|
|
1460
|
+
WHERE i.pack_id = p.id ORDER BY i.position ASC, i.added_at ASC LIMIT 1) AS cover_id,
|
|
1461
|
+
(SELECT s.sha256 FROM sticker_pack_items i
|
|
1462
|
+
JOIN stickers s ON s.id = i.sticker_id
|
|
1463
|
+
WHERE i.pack_id = p.id ORDER BY i.position ASC, i.added_at ASC LIMIT 1) AS cover_sha
|
|
1464
|
+
FROM sticker_packs p
|
|
1465
|
+
WHERE p.owner = ?
|
|
1466
|
+
ORDER BY p.name COLLATE NOCASE ASC, p.id ASC`,
|
|
1467
|
+
).all(owner);
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
function stickerPackGet(owner, id) {
|
|
1471
|
+
if (!owner) return null;
|
|
1472
|
+
return (
|
|
1473
|
+
q(
|
|
1474
|
+
`SELECT p.*,
|
|
1475
|
+
(SELECT COUNT(*) FROM sticker_pack_items i WHERE i.pack_id = p.id) AS item_count,
|
|
1476
|
+
(SELECT i.sticker_id FROM sticker_pack_items i
|
|
1477
|
+
WHERE i.pack_id = p.id ORDER BY i.position ASC, i.added_at ASC LIMIT 1) AS cover_id,
|
|
1478
|
+
(SELECT s.sha256 FROM sticker_pack_items i
|
|
1479
|
+
JOIN stickers s ON s.id = i.sticker_id
|
|
1480
|
+
WHERE i.pack_id = p.id ORDER BY i.position ASC, i.added_at ASC LIMIT 1) AS cover_sha
|
|
1481
|
+
FROM sticker_packs p
|
|
1482
|
+
WHERE p.id = ? AND p.owner = ?`,
|
|
1483
|
+
).get(id, owner) || null
|
|
1484
|
+
);
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
function stickerPackByKey(owner, nameKey) {
|
|
1488
|
+
if (!owner) return null;
|
|
1489
|
+
return (
|
|
1490
|
+
q(
|
|
1491
|
+
`SELECT p.*,
|
|
1492
|
+
(SELECT COUNT(*) FROM sticker_pack_items i WHERE i.pack_id = p.id) AS item_count,
|
|
1493
|
+
(SELECT i.sticker_id FROM sticker_pack_items i
|
|
1494
|
+
WHERE i.pack_id = p.id ORDER BY i.position ASC, i.added_at ASC LIMIT 1) AS cover_id,
|
|
1495
|
+
(SELECT s.sha256 FROM sticker_pack_items i
|
|
1496
|
+
JOIN stickers s ON s.id = i.sticker_id
|
|
1497
|
+
WHERE i.pack_id = p.id ORDER BY i.position ASC, i.added_at ASC LIMIT 1) AS cover_sha
|
|
1498
|
+
FROM sticker_packs p
|
|
1499
|
+
WHERE p.name_key = ? AND p.owner = ?`,
|
|
1500
|
+
).get(nameKey, owner) || null
|
|
1501
|
+
);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
function stickerPackInsert(row) {
|
|
1505
|
+
q(
|
|
1506
|
+
`INSERT INTO sticker_packs (id, owner, name, name_key, created_at, updated_at)
|
|
1507
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
1508
|
+
).run(row.id, row.owner, row.name, row.nameKey, row.createdAt, row.updatedAt);
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
function stickerPackRename(id, name, nameKey, updatedAt) {
|
|
1512
|
+
q("UPDATE sticker_packs SET name = ?, name_key = ?, updated_at = ? WHERE id = ?").run(
|
|
1513
|
+
name,
|
|
1514
|
+
nameKey,
|
|
1515
|
+
updatedAt,
|
|
1516
|
+
id,
|
|
1517
|
+
);
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
function stickerPackDelete(id) {
|
|
1521
|
+
q("DELETE FROM sticker_packs WHERE id = ?").run(id);
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
function stickerPackItems(packId) {
|
|
1525
|
+
return q(
|
|
1526
|
+
`SELECT s.* FROM sticker_pack_items i
|
|
1527
|
+
JOIN stickers s ON s.id = i.sticker_id
|
|
1528
|
+
WHERE i.pack_id = ?
|
|
1529
|
+
ORDER BY i.position ASC, i.added_at ASC, s.id ASC`,
|
|
1530
|
+
).all(packId);
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
function stickerPackAdd(packId, stickerId, now) {
|
|
1534
|
+
const exists = q(
|
|
1535
|
+
"SELECT 1 AS x FROM sticker_pack_items WHERE pack_id = ? AND sticker_id = ?",
|
|
1536
|
+
).get(packId, stickerId);
|
|
1537
|
+
if (exists) return false;
|
|
1538
|
+
const count = q("SELECT COUNT(*) AS n FROM sticker_pack_items WHERE pack_id = ?").get(packId).n;
|
|
1539
|
+
q(
|
|
1540
|
+
`INSERT INTO sticker_pack_items (pack_id, sticker_id, position, added_at)
|
|
1541
|
+
VALUES (?, ?, ?, ?)`,
|
|
1542
|
+
).run(packId, stickerId, count, now);
|
|
1543
|
+
return true;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
function stickerPackRemoveItem(packId, stickerId) {
|
|
1547
|
+
const result = q("DELETE FROM sticker_pack_items WHERE pack_id = ? AND sticker_id = ?").run(
|
|
1548
|
+
packId,
|
|
1549
|
+
stickerId,
|
|
1550
|
+
);
|
|
1551
|
+
if (result.changes) stickerPackRenumber(packId);
|
|
1552
|
+
return result.changes > 0;
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
function stickerPackRenumber(packId) {
|
|
1556
|
+
const rows = q(
|
|
1557
|
+
`SELECT sticker_id FROM sticker_pack_items
|
|
1558
|
+
WHERE pack_id = ?
|
|
1559
|
+
ORDER BY position ASC, added_at ASC, sticker_id ASC`,
|
|
1560
|
+
).all(packId);
|
|
1561
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
1562
|
+
q("UPDATE sticker_pack_items SET position = ? WHERE pack_id = ? AND sticker_id = ?").run(
|
|
1563
|
+
index,
|
|
1564
|
+
packId,
|
|
1565
|
+
rows[index].sticker_id,
|
|
1566
|
+
);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
function stickerPackReorder(packId, ids) {
|
|
1571
|
+
const current = q(
|
|
1572
|
+
"SELECT sticker_id FROM sticker_pack_items WHERE pack_id = ? ORDER BY position ASC, added_at ASC",
|
|
1573
|
+
)
|
|
1574
|
+
.all(packId)
|
|
1575
|
+
.map((row) => row.sticker_id);
|
|
1576
|
+
if (current.length !== ids.length || new Set(ids).size !== ids.length) return false;
|
|
1577
|
+
const have = new Set(current);
|
|
1578
|
+
if (ids.some((id) => !have.has(id))) return false;
|
|
1579
|
+
for (let index = 0; index < ids.length; index += 1) {
|
|
1580
|
+
q("UPDATE sticker_pack_items SET position = ? WHERE pack_id = ? AND sticker_id = ?").run(
|
|
1581
|
+
index,
|
|
1582
|
+
packId,
|
|
1583
|
+
ids[index],
|
|
1584
|
+
);
|
|
1585
|
+
}
|
|
1586
|
+
return true;
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
function stickerPackExclusive(packId) {
|
|
1590
|
+
return q(
|
|
1591
|
+
`SELECT i.sticker_id AS id, s.sha256 AS sha256
|
|
1592
|
+
FROM sticker_pack_items i
|
|
1593
|
+
JOIN stickers s ON s.id = i.sticker_id
|
|
1594
|
+
WHERE i.pack_id = ?
|
|
1595
|
+
AND (SELECT COUNT(*) FROM sticker_pack_items j WHERE j.sticker_id = i.sticker_id) = 1
|
|
1596
|
+
ORDER BY i.position ASC`,
|
|
1597
|
+
).all(packId);
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
function stickerPackItemCount(packId) {
|
|
1601
|
+
return q("SELECT COUNT(*) AS n FROM sticker_pack_items WHERE pack_id = ?").get(packId).n;
|
|
1602
|
+
}
|
|
1603
|
+
|
|
915
1604
|
module.exports = {
|
|
916
1605
|
// Lifecycle
|
|
917
1606
|
initStore,
|
|
@@ -961,11 +1650,14 @@ module.exports = {
|
|
|
961
1650
|
storeLidPnMapping,
|
|
962
1651
|
storeLidPnMappings,
|
|
963
1652
|
getLidForPn,
|
|
1653
|
+
getLidsForPn,
|
|
964
1654
|
getLidsForPns,
|
|
965
1655
|
getPnForLid,
|
|
966
1656
|
getAllLidMappings,
|
|
967
1657
|
// User metadata & roles
|
|
968
1658
|
saveUserMetadata,
|
|
1659
|
+
rememberSavedName,
|
|
1660
|
+
applyContactRecord,
|
|
969
1661
|
getUserMetadata,
|
|
970
1662
|
isUserOwner,
|
|
971
1663
|
isUserBotAdmin,
|
|
@@ -1012,4 +1704,34 @@ module.exports = {
|
|
|
1012
1704
|
resolveUserPhone,
|
|
1013
1705
|
describePeer,
|
|
1014
1706
|
migrateSecretsAtRest,
|
|
1707
|
+
// Sticker Studio
|
|
1708
|
+
withImmediateTransaction,
|
|
1709
|
+
stickerOwnerReconcileSelf,
|
|
1710
|
+
stickerOwnerEnsure,
|
|
1711
|
+
stickerCount,
|
|
1712
|
+
stickerFindBySha,
|
|
1713
|
+
stickerGet,
|
|
1714
|
+
stickerInsert,
|
|
1715
|
+
stickerUpdate,
|
|
1716
|
+
stickerDelete,
|
|
1717
|
+
stickerShaCount,
|
|
1718
|
+
stickerQuery,
|
|
1719
|
+
stickerPackMembership,
|
|
1720
|
+
stickerPacksUsing,
|
|
1721
|
+
stickerPackIdsOf,
|
|
1722
|
+
stickerClearAll,
|
|
1723
|
+
stickerPackCount,
|
|
1724
|
+
stickerPackList,
|
|
1725
|
+
stickerPackGet,
|
|
1726
|
+
stickerPackByKey,
|
|
1727
|
+
stickerPackInsert,
|
|
1728
|
+
stickerPackRename,
|
|
1729
|
+
stickerPackDelete,
|
|
1730
|
+
stickerPackItems,
|
|
1731
|
+
stickerPackAdd,
|
|
1732
|
+
stickerPackRemoveItem,
|
|
1733
|
+
stickerPackRenumber,
|
|
1734
|
+
stickerPackReorder,
|
|
1735
|
+
stickerPackExclusive,
|
|
1736
|
+
stickerPackItemCount,
|
|
1015
1737
|
};
|