@vielhuber/wahelper 1.6.1 → 1.6.2

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 +44 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
@@ -214,7 +214,9 @@ export default class wahelperDaemon {
214
214
  async storeDataToDatabase(data) {
215
215
  this.log('storeDataToDatabase');
216
216
 
217
- if (!data.messages || data.messages.length === 0) {
217
+ let messages = Array.isArray(data?.messages) ? data.messages : [];
218
+ let chats = Array.isArray(data?.chats) ? data.chats : [];
219
+ if (messages.length === 0 && chats.length === 0) {
218
220
  return;
219
221
  }
220
222
 
@@ -224,9 +226,8 @@ export default class wahelperDaemon {
224
226
  }
225
227
  this.dbLock = true;
226
228
 
227
- //this.log(data.messages);
228
229
  let count = 0,
229
- length = data.messages.length;
230
+ length = messages.length;
230
231
 
231
232
  // if db is closed in the meantime
232
233
  if (this.dbIsOpen === false) {
@@ -242,7 +243,7 @@ export default class wahelperDaemon {
242
243
  VALUES (?, ?, ?, ?, ?, ?, ?)
243
244
  `);
244
245
 
245
- for (let messages__value of data.messages) {
246
+ for (let messages__value of messages) {
246
247
  let id = messages__value.key?.id,
247
248
  chatId = messages__value.key?.remoteJid,
248
249
  fromMe = messages__value.key?.fromMe ? 1 : 0,
@@ -373,6 +374,8 @@ export default class wahelperDaemon {
373
374
  }
374
375
  }
375
376
 
377
+ this.applyReadFlags(messages, chats);
378
+
376
379
  this.db.exec('COMMIT');
377
380
  this.log('END TRANSACTION');
378
381
  if (count > 0) {
@@ -401,6 +404,43 @@ export default class wahelperDaemon {
401
404
  this.dbLock = false;
402
405
  }
403
406
 
407
+ // mark messages as read based on two signals that baileys ships with
408
+ // history-sync and chats.upsert payloads:
409
+ // 1. message.status === READ (=4, sometimes serialised as "READ") —
410
+ // the WA server already knows this message was seen on some device
411
+ // 2. chat.unreadCount === 0 — the user has opened that chat on the
412
+ // phone, so every incoming message there is implicitly seen
413
+ applyReadFlags(messages, chats) {
414
+ let device = this.args.device || 'me';
415
+ let byStatus = this.db.prepare('UPDATE messages SET `read` = 1 WHERE id = ? AND `read` = 0');
416
+ let byChat = this.db.prepare(
417
+ 'UPDATE messages SET `read` = 1 WHERE `read` = 0 AND ((`from` = ? AND `to` = ?) OR (`to` = ? AND `from` != ?))'
418
+ );
419
+
420
+ let touchedStatus = 0;
421
+ for (let m of messages) {
422
+ let s = m?.status;
423
+ if (s !== 4 && s !== '4' && s !== 'READ') continue;
424
+ let id = m?.key?.id;
425
+ if (!id) continue;
426
+ let r = byStatus.run(id);
427
+ if (r?.changes) touchedStatus += r.changes;
428
+ }
429
+
430
+ let touchedChat = 0;
431
+ for (let c of chats) {
432
+ if (c?.unreadCount !== 0) continue;
433
+ let cid = (c?.id ?? '').replace(/@.*$/, '');
434
+ if (!cid) continue;
435
+ let r = byChat.run(cid, device, cid, device);
436
+ if (r?.changes) touchedChat += r.changes;
437
+ }
438
+
439
+ if (touchedStatus > 0 || touchedChat > 0) {
440
+ this.log('marked read: ' + touchedStatus + ' via status, ' + touchedChat + ' via chat.unreadCount=0');
441
+ }
442
+ }
443
+
404
444
  async markMessagesRead(updates) {
405
445
  if (!Array.isArray(updates) || updates.length === 0) {
406
446
  return;