@vielhuber/wahelper 1.6.6 → 1.6.8
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/package.json +1 -1
- package/wahelper-daemon.js +81 -18
package/package.json
CHANGED
package/wahelper-daemon.js
CHANGED
|
@@ -175,6 +175,29 @@ export default class wahelperDaemon {
|
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
// resolve a jid + its baileys "alt" counterpart (remoteJidAlt / participantAlt,
|
|
179
|
+
// which carries the opposite id type) into { pn, lid } — purely from the message.
|
|
180
|
+
resolveIdentity(primaryJid, altJid) {
|
|
181
|
+
let out = { pn: null, lid: null };
|
|
182
|
+
let classify = jid => {
|
|
183
|
+
if (!jid || typeof jid !== 'string') {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
let bare = jid.replace(/@.*$/, '');
|
|
187
|
+
if (bare === '') {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (jid.endsWith('@lid')) {
|
|
191
|
+
out.lid = bare;
|
|
192
|
+
} else if (jid.endsWith('@s.whatsapp.net') || jid.endsWith('@c.us')) {
|
|
193
|
+
out.pn = bare;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
classify(primaryJid);
|
|
197
|
+
classify(altJid);
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
|
|
178
201
|
formatMessage(message) {
|
|
179
202
|
if (message === null || message === undefined || message === '') {
|
|
180
203
|
return message;
|
|
@@ -289,28 +312,31 @@ export default class wahelperDaemon {
|
|
|
289
312
|
timestamp = Math.floor(Date.now() / 1000);
|
|
290
313
|
}
|
|
291
314
|
|
|
315
|
+
// resolve the human partner's jid to the phone number; the group
|
|
316
|
+
// chat (and "me") stay as-is, only the contact id gets canonicalized.
|
|
317
|
+
let isGroup = chatId?.endsWith('@g.us');
|
|
318
|
+
let me = this.args.device || 'me';
|
|
292
319
|
let from = null;
|
|
293
320
|
let to = null;
|
|
294
|
-
if (
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
from =
|
|
321
|
+
if (isGroup) {
|
|
322
|
+
to = chatId ? chatId.replace(/@.*$/, '') : null;
|
|
323
|
+
if (fromMe) {
|
|
324
|
+
from = me;
|
|
325
|
+
} else {
|
|
326
|
+
let participantJid = messages__value?.participant || messages__value?.key?.participant || null;
|
|
327
|
+
let ident = this.resolveIdentity(participantJid, messages__value?.key?.participantAlt || null);
|
|
328
|
+
from = ident.pn || ident.lid || (participantJid || '').replace(/@.*$/, '');
|
|
302
329
|
}
|
|
303
|
-
to = chatId;
|
|
304
330
|
} else {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
331
|
+
let ident = this.resolveIdentity(chatId || null, messages__value?.key?.remoteJidAlt || null);
|
|
332
|
+
let partnerId = ident.pn || ident.lid || (chatId || '').replace(/@.*$/, '');
|
|
333
|
+
if (fromMe) {
|
|
334
|
+
from = me;
|
|
335
|
+
to = partnerId;
|
|
336
|
+
} else {
|
|
337
|
+
from = partnerId;
|
|
338
|
+
to = me;
|
|
339
|
+
}
|
|
314
340
|
}
|
|
315
341
|
|
|
316
342
|
if (from === null || from === undefined || from === '') {
|
|
@@ -508,6 +534,35 @@ export default class wahelperDaemon {
|
|
|
508
534
|
}
|
|
509
535
|
}
|
|
510
536
|
|
|
537
|
+
async markChatsRead(updates) {
|
|
538
|
+
// baileys fires chats.update with `unreadCount: 0` when an EXISTING chat
|
|
539
|
+
// is opened on the phone. unlike messages.update (status=4 read receipt),
|
|
540
|
+
// this also covers GROUPS, which usually ship no read receipts — without
|
|
541
|
+
// it their incoming messages would stay `read = 0` forever even after the
|
|
542
|
+
// user has clearly seen them (e.g. reacted with an emoji).
|
|
543
|
+
if (!Array.isArray(updates) || updates.length === 0) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
let chats = updates.filter(c => c?.unreadCount === 0);
|
|
547
|
+
if (chats.length === 0) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
while (this.dbLock) {
|
|
551
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
552
|
+
}
|
|
553
|
+
this.dbLock = true;
|
|
554
|
+
try {
|
|
555
|
+
if (this.dbIsOpen === false) {
|
|
556
|
+
this.initDatabase();
|
|
557
|
+
}
|
|
558
|
+
this.applyReadFlags([], chats);
|
|
559
|
+
} catch (error) {
|
|
560
|
+
this.log('⛔ markChatsRead failed: ' + error.message);
|
|
561
|
+
} finally {
|
|
562
|
+
this.dbLock = false;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
511
566
|
async sendMessageToUser(number = null, message = null, attachments = null) {
|
|
512
567
|
if (!this.connected || !this.sock) {
|
|
513
568
|
throw new Error('not_connected');
|
|
@@ -624,6 +679,14 @@ export default class wahelperDaemon {
|
|
|
624
679
|
}
|
|
625
680
|
});
|
|
626
681
|
|
|
682
|
+
this.sock.ev.on('chats.update', async updates => {
|
|
683
|
+
try {
|
|
684
|
+
await this.markChatsRead(updates);
|
|
685
|
+
} catch (error) {
|
|
686
|
+
this.log('⛔ chats.update handler failed: ' + error.message);
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
|
|
627
690
|
this.sock.ev.on('creds.update', saveCreds);
|
|
628
691
|
|
|
629
692
|
this.sock.ev.on('connection.update', async update => {
|