@vielhuber/wahelper 1.6.6 → 1.6.7

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 +37 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.6.6",
3
+ "version": "1.6.7",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
@@ -508,6 +508,35 @@ export default class wahelperDaemon {
508
508
  }
509
509
  }
510
510
 
511
+ async markChatsRead(updates) {
512
+ // baileys fires chats.update with `unreadCount: 0` when an EXISTING chat
513
+ // is opened on the phone. unlike messages.update (status=4 read receipt),
514
+ // this also covers GROUPS, which usually ship no read receipts — without
515
+ // it their incoming messages would stay `read = 0` forever even after the
516
+ // user has clearly seen them (e.g. reacted with an emoji).
517
+ if (!Array.isArray(updates) || updates.length === 0) {
518
+ return;
519
+ }
520
+ let chats = updates.filter(c => c?.unreadCount === 0);
521
+ if (chats.length === 0) {
522
+ return;
523
+ }
524
+ while (this.dbLock) {
525
+ await new Promise(resolve => setTimeout(resolve, 100));
526
+ }
527
+ this.dbLock = true;
528
+ try {
529
+ if (this.dbIsOpen === false) {
530
+ this.initDatabase();
531
+ }
532
+ this.applyReadFlags([], chats);
533
+ } catch (error) {
534
+ this.log('⛔ markChatsRead failed: ' + error.message);
535
+ } finally {
536
+ this.dbLock = false;
537
+ }
538
+ }
539
+
511
540
  async sendMessageToUser(number = null, message = null, attachments = null) {
512
541
  if (!this.connected || !this.sock) {
513
542
  throw new Error('not_connected');
@@ -624,6 +653,14 @@ export default class wahelperDaemon {
624
653
  }
625
654
  });
626
655
 
656
+ this.sock.ev.on('chats.update', async updates => {
657
+ try {
658
+ await this.markChatsRead(updates);
659
+ } catch (error) {
660
+ this.log('⛔ chats.update handler failed: ' + error.message);
661
+ }
662
+ });
663
+
627
664
  this.sock.ev.on('creds.update', saveCreds);
628
665
 
629
666
  this.sock.ev.on('connection.update', async update => {