commit e18aacb05b9b2b2458fd59628a1cc99c4b414102
parent 57228303172b1788675cf1b5074bb2cd4bfb78a5
Author: notamitgamer-bot <bot@notamitgamer.dev>
Date: Fri, 11 Sep 2026 07:59:42 +0000
Fix silent failures in edit-history tracking
Two bugs in the MESSAGE_EDIT handling from #5 were causing edits to
go completely unrecorded with zero visibility:
1. Race condition: if a message is edited shortly after being sent,
the edit event can be processed before the original message's own
Firestore write has committed. The code checked snap.exists once
and silently gave up - this is the most likely reason edits
weren't showing up when testing (send + quick edit is the natural
way to try the feature). Now retries up to 3 times with backoff
(500ms, 1000ms) before giving up.
2. Errors and outcomes were completely unlogged (bare catch (err)
{}, no success path logging), so even the app's own built-in
Live Server Logs viewer showed nothing when something went wrong.
Added logging for: detection of a malformed/textless edit event,
transaction failures, successful edit recording, and giving up
after retries - all visible via Settings -> Live Server Logs.
Diffstat:
| M | src/whatsapp.js | | | 79 | ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 52 insertions(+), 27 deletions(-)
diff --git a/src/whatsapp.js b/src/whatsapp.js
@@ -124,7 +124,10 @@ async function startWhatsApp() {
const protocolMsg = msg.message.protocolMessage;
if (protocolMsg && protocolMsg.type === proto.Message.ProtocolMessage.Type.MESSAGE_EDIT) {
const targetId = protocolMsg.key?.id;
- if (!targetId) continue;
+ if (!targetId) {
+ console.error('System: Received MESSAGE_EDIT protocol message with no target key — ignoring.');
+ continue;
+ }
const editedText =
protocolMsg.editedMessage?.conversation ||
@@ -133,7 +136,10 @@ async function startWhatsApp() {
protocolMsg.editedMessage?.videoMessage?.caption ||
"";
- if (!editedText) continue;
+ if (!editedText) {
+ console.error(`System: Received edit for ${targetId} with no extractable text — ignoring.`);
+ continue;
+ }
const editTimestamp = msg.messageTimestamp
? (typeof msg.messageTimestamp === 'number' ? msg.messageTimestamp : msg.messageTimestamp.low)
@@ -141,31 +147,50 @@ async function startWhatsApp() {
const msgRef = db.collection('Chats').doc(remoteJid).collection('Messages').doc(targetId);
- try {
- await db.runTransaction(async (tx) => {
- const snap = await tx.get(msgRef);
- if (!snap.exists) return; // original was never logged; nothing to attach history to
-
- const data = snap.data();
- const priorEdits = Array.isArray(data.edits) ? data.edits : [];
-
- // On the first edit, seed history with the pre-edit text so
- // the original is preserved alongside every later revision.
- const history = priorEdits.length > 0
- ? priorEdits
- : [{ text: data.text, timestamp: data.timestamp }];
-
- history.push({ text: editedText, timestamp: editTimestamp });
-
- tx.set(msgRef, {
- text: editedText, // latest text — existing UI keeps working unchanged
- edited: true,
- editCount: history.length - 1,
- lastEditedAt: editTimestamp,
- edits: history // full chronological history, oldest (original) first
- }, { merge: true });
- });
- } catch (err) {}
+ // Guard against a race: if this edit arrives before the original
+ // message's own write has committed (e.g. someone edits within
+ // ~1s of sending), the doc won't exist yet. Retry briefly instead
+ // of silently dropping the edit.
+ let applied = false;
+ for (let attempt = 0; attempt < 3 && !applied; attempt++) {
+ if (attempt > 0) await new Promise(r => setTimeout(r, 500 * attempt));
+
+ try {
+ applied = await db.runTransaction(async (tx) => {
+ const snap = await tx.get(msgRef);
+ if (!snap.exists) return false;
+
+ const data = snap.data();
+ const priorEdits = Array.isArray(data.edits) ? data.edits : [];
+
+ // On the first edit, seed history with the pre-edit text so
+ // the original is preserved alongside every later revision.
+ const history = priorEdits.length > 0
+ ? priorEdits
+ : [{ text: data.text, timestamp: data.timestamp }];
+
+ history.push({ text: editedText, timestamp: editTimestamp });
+
+ tx.set(msgRef, {
+ text: editedText, // latest text — existing UI keeps working unchanged
+ edited: true,
+ editCount: history.length - 1,
+ lastEditedAt: editTimestamp,
+ edits: history // full chronological history, oldest (original) first
+ }, { merge: true });
+
+ return true;
+ });
+ } catch (err) {
+ console.error(`System: Edit-history transaction failed for ${targetId} (attempt ${attempt + 1}):`, err.message);
+ }
+ }
+
+ if (applied) {
+ console.log(`System: Recorded edit for message ${targetId} in ${remoteJid}.`);
+ } else {
+ console.error(`System: Could not attach edit for message ${targetId} in ${remoteJid} — original was never found (gave up after retries).`);
+ }
continue;
}