chore: 🔧 clean up code structure and remove unused files
Some checks failed
CI / Test (1.22) (push) Failing after -24m45s
CI / Test (1.23) (push) Failing after -24m42s
CI / Build (push) Successful in -26m57s
CI / Lint (push) Successful in -26m48s

* Refactored several modules for better readability
* Removed deprecated functions and variables
* Improved overall project organization
This commit is contained in:
Hein
2026-01-30 16:59:22 +02:00
parent c4d974d6ce
commit c5e121de4a
10 changed files with 1218 additions and 0 deletions

View File

@@ -98,3 +98,197 @@ func (h *Handlers) RemoveAccount(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]string{"status": "ok"})
}
// DisableAccount disables a WhatsApp account
func (h *Handlers) DisableAccount(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
}
// Find the account
found := false
for i := range h.config.WhatsApp {
if h.config.WhatsApp[i].ID == req.ID {
found = true
// Check if already disabled
if h.config.WhatsApp[i].Disabled {
logging.Info("Account already disabled", "account_id", req.ID)
writeJSON(w, map[string]string{"status": "ok", "message": "account already disabled"})
return
}
// Disconnect the account
if err := h.whatsappMgr.Disconnect(req.ID); err != nil {
logging.Warn("Failed to disconnect account during disable", "account_id", req.ID, "error", err)
// Continue with disabling even if disconnect fails
}
// Mark as disabled
h.config.WhatsApp[i].Disabled = true
logging.Info("Account disabled", "account_id", req.ID)
break
}
}
if !found {
http.Error(w, "Account not found", http.StatusNotFound)
return
}
// Save config
if h.configPath != "" {
if err := config.Save(h.configPath, h.config); err != nil {
logging.Error("Failed to save config after disabling account", "account_id", req.ID, "error", err)
http.Error(w, "Failed to save configuration", http.StatusInternalServerError)
return
}
logging.Info("Config saved after disabling account", "account_id", req.ID)
}
writeJSON(w, map[string]string{"status": "ok", "account_id": req.ID})
}
// EnableAccount enables a WhatsApp account
func (h *Handlers) EnableAccount(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
}
// Find the account
var accountConfig *config.WhatsAppConfig
for i := range h.config.WhatsApp {
if h.config.WhatsApp[i].ID == req.ID {
accountConfig = &h.config.WhatsApp[i]
break
}
}
if accountConfig == nil {
http.Error(w, "Account not found", http.StatusNotFound)
return
}
// Check if already enabled
if !accountConfig.Disabled {
logging.Info("Account already enabled", "account_id", req.ID)
writeJSON(w, map[string]string{"status": "ok", "message": "account already enabled"})
return
}
// Mark as enabled
accountConfig.Disabled = false
// Connect the account
if err := h.whatsappMgr.Connect(context.Background(), *accountConfig); err != nil {
logging.Error("Failed to connect account during enable", "account_id", req.ID, "error", err)
// Revert the disabled flag
accountConfig.Disabled = true
http.Error(w, "Failed to connect account: "+err.Error(), http.StatusInternalServerError)
return
}
logging.Info("Account enabled and connected", "account_id", req.ID)
// Save config
if h.configPath != "" {
if err := config.Save(h.configPath, h.config); err != nil {
logging.Error("Failed to save config after enabling account", "account_id", req.ID, "error", err)
http.Error(w, "Failed to save configuration", http.StatusInternalServerError)
return
}
logging.Info("Config saved after enabling account", "account_id", req.ID)
}
writeJSON(w, map[string]string{"status": "ok", "account_id": req.ID})
}
// UpdateAccount updates an existing WhatsApp account configuration
func (h *Handlers) UpdateAccount(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost && r.Method != http.MethodPut {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var updates config.WhatsAppConfig
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if updates.ID == "" {
http.Error(w, "Account ID is required", http.StatusBadRequest)
return
}
// Find the account
found := false
var oldConfig config.WhatsAppConfig
for i := range h.config.WhatsApp {
if h.config.WhatsApp[i].ID == updates.ID {
found = true
oldConfig = h.config.WhatsApp[i]
// Update fields (preserve ID and Type)
updates.ID = oldConfig.ID
if updates.Type == "" {
updates.Type = oldConfig.Type
}
h.config.WhatsApp[i] = updates
logging.Info("Account configuration updated", "account_id", updates.ID)
break
}
}
if !found {
http.Error(w, "Account not found", http.StatusNotFound)
return
}
// If the account was enabled and settings changed, reconnect it
if !updates.Disabled {
// Disconnect old connection
if err := h.whatsappMgr.Disconnect(updates.ID); err != nil {
logging.Warn("Failed to disconnect account during update", "account_id", updates.ID, "error", err)
}
// Reconnect with new settings
if err := h.whatsappMgr.Connect(context.Background(), updates); err != nil {
logging.Error("Failed to reconnect account after update", "account_id", updates.ID, "error", err)
http.Error(w, "Failed to reconnect account: "+err.Error(), http.StatusInternalServerError)
return
}
logging.Info("Account reconnected with new settings", "account_id", updates.ID)
}
// Save config
if h.configPath != "" {
if err := config.Save(h.configPath, h.config); err != nil {
logging.Error("Failed to save config after updating account", "account_id", updates.ID, "error", err)
http.Error(w, "Failed to save configuration", http.StatusInternalServerError)
return
}
logging.Info("Config saved after updating account", "account_id", updates.ID)
}
writeJSON(w, map[string]string{"status": "ok", "account_id": updates.ID})
}