Controller v0.7.2: notification preferences sync to hub
- SyncPreferences() method on Notifier: POST to hub /api/v1/preferences - IsEnabled() getter for hub connectivity check - settingsNotificationsHandler: sync to hub after local save (3 flash message variants) - Startup sync: non-blocking goroutine pushes prefs to hub on boot (DB rebuild recovery) - Updated CONTEXT.md, README.md with v0.7.2 changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -51,6 +52,60 @@ func New(hubURL, apiKey, customerID string, sett *settings.Settings, logger *log
|
||||
}
|
||||
}
|
||||
|
||||
// IsEnabled returns whether the notifier has a configured hub connection.
|
||||
func (n *Notifier) IsEnabled() bool {
|
||||
return n.enabled
|
||||
}
|
||||
|
||||
// preferencesRequest is the JSON payload sent to the hub preferences endpoint.
|
||||
type preferencesRequest struct {
|
||||
CustomerID string `json:"customer_id"`
|
||||
Email string `json:"email"`
|
||||
EnabledEvents []string `json:"enabled_events"`
|
||||
}
|
||||
|
||||
// SyncPreferences pushes the current notification preferences to the hub.
|
||||
// Called after the user saves notification settings on the settings page.
|
||||
// Synchronous — returns error for the handler to display to the user.
|
||||
func (n *Notifier) SyncPreferences(email string, enabledEvents []string) error {
|
||||
if !n.enabled {
|
||||
return fmt.Errorf("hub nem konfigurált")
|
||||
}
|
||||
|
||||
payload := preferencesRequest{
|
||||
CustomerID: n.customerID,
|
||||
Email: email,
|
||||
EnabledEvents: enabledEvents,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal: %w", err)
|
||||
}
|
||||
|
||||
url := n.hubURL + "/api/v1/preferences"
|
||||
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+n.apiKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := n.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hub elérhetetlen: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
return fmt.Errorf("hub hiba (%d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
n.logger.Printf("[INFO] Notification preferences synced to hub: email=%s, events=%v", email, enabledEvents)
|
||||
return nil
|
||||
}
|
||||
|
||||
// notifyRequest is the JSON payload sent to the hub.
|
||||
type notifyRequest struct {
|
||||
CustomerID string `json:"customer_id"`
|
||||
|
||||
@@ -371,8 +371,18 @@ func (s *Server) settingsNotificationsHandler(w http.ResponseWriter, r *http.Req
|
||||
|
||||
s.logger.Printf("[INFO] Notification preferences updated: email=%s, events=%v", email, enabledEvents)
|
||||
|
||||
// Sync preferences to hub
|
||||
data := s.settingsData()
|
||||
data["NotificationSuccess"] = "Értesítési beállítások mentve."
|
||||
if s.notifier != nil && s.notifier.IsEnabled() {
|
||||
if err := s.notifier.SyncPreferences(email, enabledEvents); err != nil {
|
||||
s.logger.Printf("[WARN] Failed to sync preferences to hub: %v", err)
|
||||
data["NotificationSuccess"] = fmt.Sprintf("Értesítési beállítások mentve (helyi). A központi szinkronizálás sikertelen: %v", err)
|
||||
} else {
|
||||
data["NotificationSuccess"] = "Értesítési beállítások mentve."
|
||||
}
|
||||
} else {
|
||||
data["NotificationSuccess"] = "Értesítési beállítások mentve."
|
||||
}
|
||||
s.render(w, "settings", data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user