Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

fix: resolve false connection state and duplicate messages (issue #509)

Aldino Kemal6f981156dfb3
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.go
index 6e328714..65ab49cb 100644
--- a/src/infrastructure/chatstorage/sqlite_repository.go
+++ b/src/infrastructure/chatstorage/sqlite_repository.go
@@ -641,6 +641,11 @@ func (r *SQLiteRepository) CreateMessage(ctx context.Context, evt *events.Messag
return nil
}
+ if evt.Info.IsFromMe {
+ logrus.Debugf("Skipping storage of echo message %s (IsFromMe=true) - already stored by sender", evt.Info.ID)
+ return nil
+ }
+
// Get WhatsApp client for LID resolution (device-scoped if present in context)
client := whatsapp.ClientFromContext(ctx)
deviceID := ""
diff --git a/src/infrastructure/whatsapp/device_instance.go b/src/infrastructure/whatsapp/device_instance.go
index 29545dcc..cc5058c4 100644
--- a/src/infrastructure/whatsapp/device_instance.go
+++ b/src/infrastructure/whatsapp/device_instance.go
@@ -20,6 +20,7 @@ type DeviceInstance struct {
phoneNumber string
jid string
createdAt time.Time
+ onLoggedOut func(deviceID string) // Callback for remote logout cleanup
}
func NewDeviceInstance(deviceID string, client *whatsmeow.Client, chatStorageRepo domainChatStorage.IChatStorageRepository) *DeviceInstance {
@@ -153,3 +154,20 @@ func (d *DeviceInstance) refreshIdentityLocked() {
d.displayName = d.client.Store.PushName
}
}
+
+func (d *DeviceInstance) SetOnLoggedOut(callback func(deviceID string)) {
+ d.mu.Lock()
+ defer d.mu.Unlock()
+ d.onLoggedOut = callback
+}
+
+func (d *DeviceInstance) TriggerLoggedOut() {
+ d.mu.RLock()
+ callback := d.onLoggedOut
+ deviceID := d.id
+ d.mu.RUnlock()
+
+ if callback != nil {
+ callback(deviceID)
+ }
+}
diff --git a/src/infrastructure/whatsapp/device_manager.go b/src/infrastructure/whatsapp/device_manager.go
index dfc9c145..8ef9e772 100644
--- a/src/infrastructure/whatsapp/device_manager.go
+++ b/src/infrastructure/whatsapp/device_manager.go
@@ -471,6 +471,10 @@ func (m *DeviceManager) EnsureClient(ctx context.Context, deviceID string) (*Dev
handler(ctx, inst, rawEvt)
})
+ inst.SetOnLoggedOut(func(deviceID string) {
+ m.RemoveDevice(deviceID)
+ })
+
inst.SetClient(client)
inst.UpdateStateFromClient()
diff --git a/src/infrastructure/whatsapp/event_handler.go b/src/infrastructure/whatsapp/event_handler.go
index bc4e9cee..229bc499 100644
--- a/src/infrastructure/whatsapp/event_handler.go
+++ b/src/infrastructure/whatsapp/event_handler.go
@@ -130,10 +130,14 @@ func handleLoggedOut(ctx context.Context, instance *DeviceInstance, chatStorageR
}
}
+ deviceID := instance.ID()
+
+ instance.TriggerLoggedOut()
+
websocket.Broadcast <- websocket.BroadcastMessage{
Code: "LOGOUT_COMPLETE",
- Message: "Remote logout cleanup completed - ready for new login",
- Result: map[string]string{"device_id": instance.ID()},
+ Message: "Remote logout cleanup completed - device removed from server",
+ Result: map[string]string{"device_id": deviceID},
}
}
diff --git a/src/infrastructure/whatsapp/init.go b/src/infrastructure/whatsapp/init.go
index 82a38ded..60794e85 100644
--- a/src/infrastructure/whatsapp/init.go
+++ b/src/infrastructure/whatsapp/init.go
@@ -122,6 +122,9 @@ func InitWaCLI(ctx context.Context, storeContainer, keysStoreContainer *sqlstore
dm := InitializeDeviceManager(storeContainer, keysStoreContainer, deviceRepo)
if dm != nil && instanceID != "" {
dm.EnsureDefault(instance)
+ instance.SetOnLoggedOut(func(deviceID string) {
+ dm.RemoveDevice(deviceID)
+ })
}
globalStateMu.Lock()
diff --git a/src/usecase/app.go b/src/usecase/app.go
index 6db5551a..4e233914 100644
--- a/src/usecase/app.go
+++ b/src/usecase/app.go
@@ -187,6 +187,10 @@ func (service *serviceApp) Reconnect(_ context.Context, deviceID string) (err er
return err
}
+ if client.Store == nil || client.Store.ID == nil {
+ return fmt.Errorf("device %s is not logged in (session deleted)", deviceID)
+ }
+
client.Disconnect()
err = client.Connect()
instance.UpdateStateFromClient()
@@ -211,6 +215,11 @@ func (service *serviceApp) Status(_ context.Context, deviceID string) (bool, boo
if client == nil {
return false, false, nil
}
+
+ if client.Store == nil || client.Store.ID == nil {
+ return false, false, nil
+ }
+
return client.IsConnected(), client.IsLoggedIn(), nil
}
diff --git a/src/usecase/device.go b/src/usecase/device.go
index d409110f..cb9e14e8 100644
--- a/src/usecase/device.go
+++ b/src/usecase/device.go
@@ -111,6 +111,11 @@ func (s *serviceDevice) ReconnectDevice(_ context.Context, deviceID string) erro
if client == nil {
return fmt.Errorf("device %s client not initialized", deviceID)
}
+
+ if client.Store == nil || client.Store.ID == nil {
+ return fmt.Errorf("device %s is not logged in (session deleted)", deviceID)
+ }
+
client.Disconnect()
return client.Connect()
}
@@ -127,6 +132,11 @@ func (s *serviceDevice) GetStatus(_ context.Context, deviceID string) (bool, boo
if client == nil {
return false, false, nil
}
+
+ if client.Store == nil || client.Store.ID == nil {
+ return false, false, nil
+ }
+
// Update state snapshot based on live client flags
state := deriveState(inst)
_ = state