]> git.giacomolli.uk - onechat/blobdiff - whatsapp_inbox.go
Require consent and review for autosquash
[onechat] / whatsapp_inbox.go
index a104a0d543a85f97df273841fba5b052c27a94cf..8bf8a97eaee2dfa431012a539d9af667d4829952 100644 (file)
@@ -129,7 +129,7 @@ func (i *WhatsAppInbox) deliver(ctx context.Context, l *TurnLoop, link WhatsAppL
        }
 
        if err != nil {
-               return err
+               return fmt.Errorf("close due WhatsApp batches: %w", err)
        }
 
        batches := i.state.Closed()
@@ -183,7 +183,7 @@ func (i *WhatsAppInbox) deliver(ctx context.Context, l *TurnLoop, link WhatsAppL
 
                err = i.state.Remove(batch.Number)
                if err != nil {
-                       return err
+                       return fmt.Errorf("remove delivered WhatsApp batch: %w", err)
                }
 
                if batch.Turn() {
@@ -230,6 +230,36 @@ func (i *WhatsAppInbox) startCatchup() {
        })
 }
 
+// caughtUpBy ends the catch-up of the connection with the number catchups, when
+// no later connection started a new one.
+func (i *WhatsAppInbox) caughtUpBy(catchups int) {
+       i.mutex.Lock()
+       defer i.mutex.Unlock()
+
+       if i.catchups == catchups {
+               i.finishCatchup()
+       }
+}
+
+// finishCatchup ends a running catch-up and wakes Run, which logs all batches.
+// The caller holds the mutex.
+func (i *WhatsAppInbox) finishCatchup() {
+       if !i.catchingUp {
+               return
+       }
+
+       i.catchingUp = false
+       i.caughtUp = true
+       if i.catchupTimer != nil {
+               i.catchupTimer.Stop()
+       }
+
+       select {
+       case i.wake <- struct{}{}:
+       default:
+       }
+}
+
 // receive stores images and transcribed voice notes before their batches and
 // wakes Run. link gives the device and the own IDs. It returns false when the
 // state file cannot hold the message, so that the library does not acknowledge
@@ -292,36 +322,6 @@ func (i *WhatsAppInbox) imageText(ctx context.Context, link WhatsAppLink, event
        return imageText([]string{name}, caption)
 }
 
-// caughtUpBy ends the catch-up of the connection with the number catchups, when
-// no later connection started a new one.
-func (i *WhatsAppInbox) caughtUpBy(catchups int) {
-       i.mutex.Lock()
-       defer i.mutex.Unlock()
-
-       if i.catchups == catchups {
-               i.finishCatchup()
-       }
-}
-
-// finishCatchup ends a running catch-up and wakes Run, which logs all batches.
-// The caller holds the mutex.
-func (i *WhatsAppInbox) finishCatchup() {
-       if !i.catchingUp {
-               return
-       }
-
-       i.catchingUp = false
-       i.caughtUp = true
-       if i.catchupTimer != nil {
-               i.catchupTimer.Stop()
-       }
-
-       select {
-       case i.wake <- struct{}{}:
-       default:
-       }
-}
-
 // endCatchup ends a running catch-up at OfflineSyncCompleted.
 func (i *WhatsAppInbox) endCatchup() {
        i.mutex.Lock()