101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.warky.dev/wdevs/whatshooked/pkg/config"
|
|
"git.warky.dev/wdevs/whatshooked/pkg/logging"
|
|
)
|
|
|
|
// Accounts returns the list of all configured WhatsApp accounts
|
|
func (h *Handlers) Accounts(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
writeJSON(w, h.config.WhatsApp)
|
|
}
|
|
|
|
// AddAccount adds a new WhatsApp account to the system
|
|
func (h *Handlers) AddAccount(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var account config.WhatsAppConfig
|
|
if err := json.NewDecoder(r.Body).Decode(&account); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Connect to the account
|
|
if err := h.whatsappMgr.Connect(context.Background(), account); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Update config
|
|
h.config.WhatsApp = append(h.config.WhatsApp, account)
|
|
if h.configPath != "" {
|
|
if err := config.Save(h.configPath, h.config); err != nil {
|
|
logging.Error("Failed to save config after adding account", "account_id", account.ID, "error", err)
|
|
} else {
|
|
logging.Info("Config saved after adding account", "account_id", account.ID)
|
|
}
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
writeJSON(w, map[string]string{"status": "ok", "account_id": account.ID})
|
|
}
|
|
|
|
// RemoveAccount removes a WhatsApp account from the system
|
|
func (h *Handlers) RemoveAccount(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
ID string `json:"id"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Disconnect the account
|
|
if err := h.whatsappMgr.Disconnect(req.ID); err != nil {
|
|
logging.Warn("Failed to disconnect account during removal", "account_id", req.ID, "error", err)
|
|
// Continue with removal even if disconnect fails
|
|
}
|
|
|
|
// Remove from config
|
|
found := false
|
|
newAccounts := make([]config.WhatsAppConfig, 0)
|
|
for _, acc := range h.config.WhatsApp {
|
|
if acc.ID != req.ID {
|
|
newAccounts = append(newAccounts, acc)
|
|
} else {
|
|
found = true
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
http.Error(w, "Account not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
h.config.WhatsApp = newAccounts
|
|
|
|
// Save config
|
|
if h.configPath != "" {
|
|
if err := config.Save(h.configPath, h.config); err != nil {
|
|
logging.Error("Failed to save config after removing account", "account_id", req.ID, "error", err)
|
|
} else {
|
|
logging.Info("Config saved after removing account", "account_id", req.ID)
|
|
}
|
|
}
|
|
|
|
writeJSON(w, map[string]string{"status": "ok"})
|
|
}
|