@vielhuber/wahelper 1.5.8 → 1.5.9
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 +50 -1
- package/wahelper.js +8 -3
package/package.json
CHANGED
package/wahelper-daemon.js
CHANGED
|
@@ -129,9 +129,14 @@ export default class wahelperDaemon {
|
|
|
129
129
|
content TEXT,
|
|
130
130
|
media_data TEXT,
|
|
131
131
|
media_filename TEXT,
|
|
132
|
-
timestamp INTEGER
|
|
132
|
+
timestamp INTEGER,
|
|
133
|
+
\`read\` INTEGER NOT NULL DEFAULT 0
|
|
133
134
|
);
|
|
134
135
|
`);
|
|
136
|
+
let cols = this.db.prepare('PRAGMA table_info(messages)').all();
|
|
137
|
+
if (!cols.some(c => c.name === 'read')) {
|
|
138
|
+
this.db.exec('ALTER TABLE messages ADD COLUMN `read` INTEGER NOT NULL DEFAULT 0');
|
|
139
|
+
}
|
|
135
140
|
} catch (error) {
|
|
136
141
|
this.log('⛔ Error initing database: ' + error.message + ' (code: ' + error.code + ')');
|
|
137
142
|
}
|
|
@@ -394,6 +399,42 @@ export default class wahelperDaemon {
|
|
|
394
399
|
this.dbLock = false;
|
|
395
400
|
}
|
|
396
401
|
|
|
402
|
+
async markMessagesRead(updates) {
|
|
403
|
+
if (!Array.isArray(updates) || updates.length === 0) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
// WAMessageStatus.READ === 4
|
|
407
|
+
let ids = updates
|
|
408
|
+
.filter(u => u?.update?.status === 4)
|
|
409
|
+
.map(u => u?.key?.id)
|
|
410
|
+
.filter(id => typeof id === 'string' && id.length > 0);
|
|
411
|
+
if (ids.length === 0) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
while (this.dbLock) {
|
|
415
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
416
|
+
}
|
|
417
|
+
this.dbLock = true;
|
|
418
|
+
try {
|
|
419
|
+
if (this.dbIsOpen === false) {
|
|
420
|
+
this.initDatabase();
|
|
421
|
+
}
|
|
422
|
+
let stmt = this.db.prepare('UPDATE messages SET `read` = 1 WHERE id = ? AND `read` = 0');
|
|
423
|
+
let touched = 0;
|
|
424
|
+
for (let id of ids) {
|
|
425
|
+
let res = stmt.run(id);
|
|
426
|
+
if (res && res.changes > 0) touched++;
|
|
427
|
+
}
|
|
428
|
+
if (touched > 0) {
|
|
429
|
+
this.log('marked ' + touched + '/' + ids.length + ' messages as read');
|
|
430
|
+
}
|
|
431
|
+
} catch (error) {
|
|
432
|
+
this.log('⛔ markMessagesRead failed: ' + error.message);
|
|
433
|
+
} finally {
|
|
434
|
+
this.dbLock = false;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
397
438
|
async sendMessageToUser(number = null, message = null, attachments = null) {
|
|
398
439
|
if (!this.connected || !this.sock) {
|
|
399
440
|
throw new Error('not_connected');
|
|
@@ -502,6 +543,14 @@ export default class wahelperDaemon {
|
|
|
502
543
|
await this.storeDataToDatabase(obj);
|
|
503
544
|
});
|
|
504
545
|
|
|
546
|
+
this.sock.ev.on('messages.update', async updates => {
|
|
547
|
+
try {
|
|
548
|
+
await this.markMessagesRead(updates);
|
|
549
|
+
} catch (error) {
|
|
550
|
+
this.log('⛔ messages.update handler failed: ' + error.message);
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
|
|
505
554
|
this.sock.ev.on('creds.update', saveCreds);
|
|
506
555
|
|
|
507
556
|
this.sock.ev.on('connection.update', async update => {
|
package/wahelper.js
CHANGED
|
@@ -133,7 +133,7 @@ export default class wahelper {
|
|
|
133
133
|
let messages = this.db
|
|
134
134
|
.prepare(
|
|
135
135
|
`
|
|
136
|
-
SELECT id, \`from\`, \`to\`, content, media_filename, timestamp
|
|
136
|
+
SELECT id, \`from\`, \`to\`, content, media_filename, timestamp, \`read\`
|
|
137
137
|
FROM messages
|
|
138
138
|
ORDER BY timestamp DESC
|
|
139
139
|
${limit !== null ? 'LIMIT ' + limit : ''}
|
|
@@ -166,7 +166,7 @@ export default class wahelper {
|
|
|
166
166
|
this.db
|
|
167
167
|
.prepare(
|
|
168
168
|
`
|
|
169
|
-
SELECT id, \`from\`, \`to\`, content, media_filename, timestamp
|
|
169
|
+
SELECT id, \`from\`, \`to\`, content, media_filename, timestamp, \`read\`
|
|
170
170
|
FROM messages
|
|
171
171
|
WHERE id = ?
|
|
172
172
|
LIMIT 1
|
|
@@ -487,9 +487,14 @@ export default class wahelper {
|
|
|
487
487
|
content TEXT,
|
|
488
488
|
media_data TEXT,
|
|
489
489
|
media_filename TEXT,
|
|
490
|
-
timestamp INTEGER
|
|
490
|
+
timestamp INTEGER,
|
|
491
|
+
\`read\` INTEGER NOT NULL DEFAULT 0
|
|
491
492
|
);
|
|
492
493
|
`);
|
|
494
|
+
let cols = this.db.prepare('PRAGMA table_info(messages)').all();
|
|
495
|
+
if (!cols.some(c => c.name === 'read')) {
|
|
496
|
+
this.db.exec('ALTER TABLE messages ADD COLUMN `read` INTEGER NOT NULL DEFAULT 0');
|
|
497
|
+
}
|
|
493
498
|
} catch (error) {
|
|
494
499
|
this.log('⛔ Error initing database: ' + error.message + ' (code: ' + error.code + ')');
|
|
495
500
|
}
|