Server qr fixes.
Some checks failed
CI / Test (1.23) (push) Failing after -25m23s
CI / Test (1.22) (push) Failing after -25m21s
CI / Build (push) Failing after -25m59s
CI / Lint (push) Successful in -25m51s

This commit is contained in:
2025-12-29 22:44:10 +02:00
parent 94fc899bab
commit fd2527219e
7 changed files with 202 additions and 76 deletions

View File

@@ -40,9 +40,8 @@ func NewManager(eventBus *events.EventBus, mediaConfig config.MediaConfig, cfg *
// Connect establishes a connection to a WhatsApp account using the appropriate client type
func (m *Manager) Connect(ctx context.Context, cfg config.WhatsAppConfig) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.clients[cfg.ID]; exists {
m.mu.Unlock()
return fmt.Errorf("client %s already connected", cfg.ID)
}
@@ -54,21 +53,39 @@ func (m *Manager) Connect(ctx context.Context, cfg config.WhatsAppConfig) error
case "business-api":
client, err = businessapi.NewClient(cfg, m.eventBus, m.mediaConfig)
case "whatsmeow", "":
client, err = whatsmeow.NewClient(cfg, m.eventBus, m.mediaConfig)
// Create callback for phone number updates
onPhoneUpdate := func(accountID, phoneNumber string) {
m.updatePhoneNumberInConfig(accountID, phoneNumber)
}
client, err = whatsmeow.NewClient(cfg, m.eventBus, m.mediaConfig, onPhoneUpdate)
default:
m.mu.Unlock()
return fmt.Errorf("unknown client type: %s", cfg.Type)
}
if err != nil {
m.mu.Unlock()
return fmt.Errorf("failed to create client: %w", err)
}
if err := client.Connect(ctx); err != nil {
return fmt.Errorf("failed to connect: %w", err)
}
// Register client immediately so it's available for QR code serving during pairing
m.clients[cfg.ID] = client
logging.Info("Client connected", "account_id", cfg.ID, "type", client.GetType())
m.mu.Unlock()
// Connect in background (this can block during QR pairing)
go func() {
if err := client.Connect(ctx); err != nil {
logging.Error("Failed to connect client", "account_id", cfg.ID, "error", err)
// Remove client if connection fails
m.mu.Lock()
delete(m.clients, cfg.ID)
m.mu.Unlock()
m.eventBus.Publish(events.WhatsAppPairFailedEvent(ctx, cfg.ID, err))
} else {
logging.Info("Client connected", "account_id", cfg.ID, "type", client.GetType())
}
}()
return nil
}
@@ -169,3 +186,29 @@ func (m *Manager) GetClient(id string) (Client, bool) {
client, exists := m.clients[id]
return client, exists
}
// updatePhoneNumberInConfig updates the phone number for an account in config and saves it
func (m *Manager) updatePhoneNumberInConfig(accountID, phoneNumber string) {
m.mu.Lock()
defer m.mu.Unlock()
// Find and update the account in config
for i := range m.config.WhatsApp {
if m.config.WhatsApp[i].ID == accountID {
if m.config.WhatsApp[i].PhoneNumber != phoneNumber {
m.config.WhatsApp[i].PhoneNumber = phoneNumber
logging.Info("Updated phone number in config", "account_id", accountID, "phone", phoneNumber)
// Save config if callback is available
if m.onConfigUpdate != nil {
if err := m.onConfigUpdate(m.config); err != nil {
logging.Error("Failed to save config after phone update", "account_id", accountID, "error", err)
} else {
logging.Debug("Config saved with updated phone number", "account_id", accountID)
}
}
}
break
}
}
}