|
|
@@ -67,6 +67,9 @@ func (s *server) handleDeviceOverridePut(w http.ResponseWriter, r *http.Request)
|
|
|
Name *string `json:"name"`
|
|
|
Type *string `json:"type"`
|
|
|
NotifyOffline *bool `json:"notifyOffline"`
|
|
|
+ // NameSource (#2163): fuente de nombre elegida para este cliente,
|
|
|
+ // "auto" o vacío para volver a la precedencia del servidor.
|
|
|
+ NameSource *string `json:"nameSource"`
|
|
|
}
|
|
|
if st := readJSONBody(w, r, &body); st != 0 {
|
|
|
writeBodyError(w, st, "invalid_body", "body JSON inválido")
|
|
|
@@ -80,6 +83,10 @@ func (s *server) handleDeviceOverridePut(w http.ResponseWriter, r *http.Request)
|
|
|
writeError(w, http.StatusBadRequest, "invalid_type")
|
|
|
return
|
|
|
}
|
|
|
+ if body.NameSource != nil && *body.NameSource != "" && *body.NameSource != "auto" && !adapters.ValidNameSource(*body.NameSource) {
|
|
|
+ writeError(w, http.StatusBadRequest, "invalid_name_source")
|
|
|
+ return
|
|
|
+ }
|
|
|
name := ""
|
|
|
if body.Name != nil {
|
|
|
name = strings.TrimSpace(*body.Name)
|
|
|
@@ -94,10 +101,10 @@ func (s *server) handleDeviceOverridePut(w http.ResponseWriter, r *http.Request)
|
|
|
|
|
|
// Valores actuales (los campos ausentes conservan lo persistido).
|
|
|
var curIcon sql.NullString
|
|
|
- var curName, curType string
|
|
|
+ var curName, curType, curNameSource string
|
|
|
var curNotifyOffline bool
|
|
|
- row := s.db.QueryRow("SELECT icon, name, device_type, notify_offline FROM device_overrides WHERE mac = ?", mac)
|
|
|
- switch err := row.Scan(&curIcon, &curName, &curType, &curNotifyOffline); err {
|
|
|
+ row := s.db.QueryRow("SELECT icon, name, device_type, notify_offline, name_source FROM device_overrides WHERE mac = ?", mac)
|
|
|
+ switch err := row.Scan(&curIcon, &curName, &curType, &curNotifyOffline, &curNameSource); err {
|
|
|
case nil:
|
|
|
case sql.ErrNoRows:
|
|
|
default:
|
|
|
@@ -107,6 +114,7 @@ func (s *server) handleDeviceOverridePut(w http.ResponseWriter, r *http.Request)
|
|
|
nextIcon := curIcon.String
|
|
|
nextName, nextType := curName, curType
|
|
|
nextNotifyOffline := curNotifyOffline
|
|
|
+ nextNameSource := curNameSource
|
|
|
if body.Icon != nil {
|
|
|
nextIcon = *body.Icon
|
|
|
}
|
|
|
@@ -119,18 +127,26 @@ func (s *server) handleDeviceOverridePut(w http.ResponseWriter, r *http.Request)
|
|
|
if body.NotifyOffline != nil {
|
|
|
nextNotifyOffline = *body.NotifyOffline
|
|
|
}
|
|
|
+ if body.NameSource != nil {
|
|
|
+ // "auto" es la forma explícita de limpiar la elección.
|
|
|
+ if *body.NameSource == "auto" {
|
|
|
+ nextNameSource = ""
|
|
|
+ } else {
|
|
|
+ nextNameSource = *body.NameSource
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
now := time.Now().UnixMilli()
|
|
|
- if nextIcon == "" && nextName == "" && nextType == "" && !nextNotifyOffline {
|
|
|
+ if nextIcon == "" && nextName == "" && nextType == "" && !nextNotifyOffline && nextNameSource == "" {
|
|
|
_, _ = s.db.Exec("DELETE FROM device_overrides WHERE mac = ?", mac)
|
|
|
} else {
|
|
|
_, err := s.db.Exec(
|
|
|
- `INSERT INTO device_overrides (mac, icon, name, device_type, banned_bands, notify_offline, created_at, updated_at)
|
|
|
- VALUES (?, ?, ?, ?, '', ?, ?, ?)
|
|
|
+ `INSERT INTO device_overrides (mac, icon, name, device_type, banned_bands, notify_offline, name_source, created_at, updated_at)
|
|
|
+ VALUES (?, ?, ?, ?, '', ?, ?, ?, ?)
|
|
|
ON CONFLICT(mac) DO UPDATE SET icon=excluded.icon, name=excluded.name,
|
|
|
device_type=excluded.device_type, notify_offline=excluded.notify_offline,
|
|
|
- updated_at=excluded.updated_at`,
|
|
|
- mac, nextIcon, nextName, nextType, nextNotifyOffline, now, now,
|
|
|
+ name_source=excluded.name_source, updated_at=excluded.updated_at`,
|
|
|
+ mac, nextIcon, nextName, nextType, nextNotifyOffline, nextNameSource, now, now,
|
|
|
)
|
|
|
if err != nil {
|
|
|
writeError(w, http.StatusInternalServerError, "db_error", err.Error())
|
|
|
@@ -143,7 +159,7 @@ func (s *server) handleDeviceOverridePut(w http.ResponseWriter, r *http.Request)
|
|
|
}
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
|
"ok": true, "mac": mac, "icon": nextIcon, "name": nextName, "type": nextType,
|
|
|
- "notifyOffline": nextNotifyOffline,
|
|
|
+ "notifyOffline": nextNotifyOffline, "nameSource": nextNameSource,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -156,8 +172,9 @@ func (s *server) handleDeviceOverrideGet(w http.ResponseWriter, r *http.Request)
|
|
|
}
|
|
|
var icon, name, deviceType, banned sql.NullString
|
|
|
var notifyOffline bool
|
|
|
- row := s.db.QueryRow("SELECT icon, name, device_type, banned_bands, notify_offline FROM device_overrides WHERE mac = ?", mac)
|
|
|
- if err := row.Scan(&icon, &name, &deviceType, &banned, ¬ifyOffline); err != nil && err != sql.ErrNoRows {
|
|
|
+ var nameSource string
|
|
|
+ row := s.db.QueryRow("SELECT icon, name, device_type, banned_bands, notify_offline, name_source FROM device_overrides WHERE mac = ?", mac)
|
|
|
+ if err := row.Scan(&icon, &name, &deviceType, &banned, ¬ifyOffline, &nameSource); err != nil && err != sql.ErrNoRows {
|
|
|
writeError(w, http.StatusInternalServerError, "db_error", err.Error())
|
|
|
return
|
|
|
}
|
|
|
@@ -168,6 +185,7 @@ func (s *server) handleDeviceOverrideGet(w http.ResponseWriter, r *http.Request)
|
|
|
"type": deviceType.String,
|
|
|
"bannedBands": banned.String,
|
|
|
"notifyOffline": notifyOffline,
|
|
|
+ "nameSource": nameSource,
|
|
|
})
|
|
|
}
|
|
|
|