@vielhuber/wahelper 1.8.4 → 1.8.6

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 (2) hide show
  1. package/package.json +1 -1
  2. package/wahelper-daemon.js +36 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.8.4",
3
+ "version": "1.8.6",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
@@ -7,6 +7,8 @@ import makeWASocket, {
7
7
  extractMessageContent,
8
8
  fetchLatestBaileysVersion,
9
9
  normalizeMessageContent,
10
+ jidDecode,
11
+ jidNormalizedUser,
10
12
  Browsers
11
13
  } from 'baileys';
12
14
  import P from 'pino';
@@ -519,7 +521,7 @@ export default class wahelperDaemon {
519
521
  }
520
522
  }
521
523
 
522
- this.applyReadFlags(messages, chats);
524
+ await this.applyReadFlags(messages, chats);
523
525
 
524
526
  this.db.exec('COMMIT');
525
527
  this.log('END TRANSACTION');
@@ -555,12 +557,21 @@ export default class wahelperDaemon {
555
557
  // the WA server already knows this message was seen on some device
556
558
  // 2. chat.unreadCount === 0 — the user has opened that chat on the
557
559
  // phone, so every incoming message there is implicitly seen
558
- applyReadFlags(messages, chats) {
560
+ // 3. chat.unreadCount === -1 — represent a manual unread marker on
561
+ // the latest incoming message, not on the entire conversation
562
+ async applyReadFlags(messages, chats) {
559
563
  let device = this.args.device || 'me';
560
564
  let byStatus = this.db.prepare('UPDATE messages SET `read` = 1 WHERE id = ? AND `read` = 0');
561
565
  let byChat = this.db.prepare(
562
566
  'UPDATE messages SET `read` = 1 WHERE `read` = 0 AND ((`from` = ? AND `to` = ?) OR (`to` = ? AND `from` != ?))'
563
567
  );
568
+ let byChatUnread = this.db.prepare(`
569
+ UPDATE messages SET \`read\` = 0 WHERE \`read\` = 1 AND id = (
570
+ SELECT id FROM messages WHERE \`from\` != $device
571
+ AND ((\`from\` IN ($primary, $alternate) AND \`to\` = $device) OR \`to\` IN ($primary, $alternate))
572
+ ORDER BY timestamp DESC, rowid DESC LIMIT 1
573
+ )
574
+ `);
564
575
 
565
576
  let touchedStatus = 0;
566
577
  for (let m of messages) {
@@ -573,16 +584,30 @@ export default class wahelperDaemon {
573
584
  }
574
585
 
575
586
  let touchedChat = 0;
587
+ let touchedUnread = 0;
576
588
  for (let c of chats) {
577
- if (c?.unreadCount !== 0) continue;
578
- let cid = (c?.id ?? '').replace(/@.*$/, '');
579
- if (!cid) continue;
580
- let r = byChat.run(cid, device, cid, device);
581
- if (r?.changes) touchedChat += r.changes;
589
+ if (c?.unreadCount !== 0 && c?.unreadCount !== -1) continue;
590
+ let jid = jidNormalizedUser(c?.id ?? '');
591
+ let identifiers = new Set([jidDecode(jid)?.user]);
592
+ if (jid.endsWith('@lid')) {
593
+ let phone = await this.sock?.signalRepository?.lidMapping?.getPNForLID(jid);
594
+ identifiers.add(jidDecode(phone)?.user);
595
+ }
596
+ if (c.unreadCount === -1) {
597
+ let [primary, alternate = primary] = [...identifiers].filter(Boolean);
598
+ if (!primary) continue;
599
+ touchedUnread += byChatUnread.run({ device, primary, alternate }).changes;
600
+ continue;
601
+ }
602
+ for (let identifier of identifiers) {
603
+ if (!identifier) continue;
604
+ let result = byChat.run(identifier, device, identifier, device);
605
+ if (result?.changes) touchedChat += result.changes;
606
+ }
582
607
  }
583
608
 
584
- if (touchedStatus > 0 || touchedChat > 0) {
585
- this.log('marked read: ' + touchedStatus + ' via status, ' + touchedChat + ' via chat.unreadCount=0');
609
+ if (touchedStatus > 0 || touchedChat > 0 || touchedUnread > 0) {
610
+ this.log(`read flags: ${touchedStatus} via status, ${touchedChat} via chat, ${touchedUnread} manually unread`);
586
611
  }
587
612
  }
588
613
 
@@ -631,7 +656,7 @@ export default class wahelperDaemon {
631
656
  if (!Array.isArray(updates) || updates.length === 0) {
632
657
  return;
633
658
  }
634
- let chats = updates.filter(c => c?.unreadCount === 0);
659
+ let chats = updates.filter(c => c?.unreadCount === 0 || c?.unreadCount === -1);
635
660
  if (chats.length === 0) {
636
661
  return;
637
662
  }
@@ -643,7 +668,7 @@ export default class wahelperDaemon {
643
668
  if (this.dbIsOpen === false) {
644
669
  this.initDatabase();
645
670
  }
646
- this.applyReadFlags([], chats);
671
+ await this.applyReadFlags([], chats);
647
672
  } catch (error) {
648
673
  this.log('⛔ markChatsRead failed: ' + error.message);
649
674
  } finally {