feat(provider): add status and banning mechanism

Introduce fields for managing provider status (active, auto_disabled,
manual_disabled) and banning logic.

- Add Status, AutoBan, BanReason, and BanUntil to Provider model and DTO
- Update CreateProvider handler to process new fields with defaults
- Extend sync service to propagate status and ban information in snapshots
- Serialize BanUntil as Unix timestamp for sync compatibility
This commit is contained in:
zenfun
2025-12-12 23:40:18 +08:00
parent eaf99e1582
commit 2407afe0e6
4 changed files with 96 additions and 39 deletions

View File

@@ -65,13 +65,19 @@ func (s *SyncService) SyncProvider(provider *model.Provider) error {
models := strings.Split(provider.Models, ",")
snap := providerSnapshot{
ID: provider.ID,
Name: provider.Name,
Type: provider.Type,
BaseURL: provider.BaseURL,
APIKey: provider.APIKey,
Group: group,
Models: models,
ID: provider.ID,
Name: provider.Name,
Type: provider.Type,
BaseURL: provider.BaseURL,
APIKey: provider.APIKey,
Group: group,
Models: models,
Status: normalizeStatus(provider.Status),
AutoBan: provider.AutoBan,
BanReason: provider.BanReason,
}
if provider.BanUntil != nil {
snap.BanUntil = provider.BanUntil.UTC().Unix()
}
// 1. Update Provider Config
@@ -111,13 +117,17 @@ func (s *SyncService) SyncModel(m *model.Model) error {
}
type providerSnapshot struct {
ID uint `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
BaseURL string `json:"base_url"`
APIKey string `json:"api_key"`
Group string `json:"group"`
Models []string `json:"models"`
ID uint `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
BaseURL string `json:"base_url"`
APIKey string `json:"api_key"`
Group string `json:"group"`
Models []string `json:"models"`
Status string `json:"status"`
AutoBan bool `json:"auto_ban"`
BanReason string `json:"ban_reason,omitempty"`
BanUntil int64 `json:"ban_until,omitempty"` // unix seconds
}
// keySnapshot is no longer needed as we write directly to auth:token:*
@@ -181,13 +191,19 @@ func (s *SyncService) SyncAll(db *gorm.DB) error {
models := strings.Split(p.Models, ",")
snap := providerSnapshot{
ID: p.ID,
Name: p.Name,
Type: p.Type,
BaseURL: p.BaseURL,
APIKey: p.APIKey,
Group: group,
Models: models,
ID: p.ID,
Name: p.Name,
Type: p.Type,
BaseURL: p.BaseURL,
APIKey: p.APIKey,
Group: group,
Models: models,
Status: normalizeStatus(p.Status),
AutoBan: p.AutoBan,
BanReason: p.BanReason,
}
if p.BanUntil != nil {
snap.BanUntil = p.BanUntil.UTC().Unix()
}
payload, err := sonic.Marshal(snap)
if err != nil {
@@ -273,3 +289,16 @@ func normalizeGroup(group string) string {
}
return group
}
func normalizeStatus(status string) string {
st := strings.ToLower(strings.TrimSpace(status))
if st == "" {
return "active"
}
switch st {
case "active", "auto_disabled", "manual_disabled":
return st
default:
return st
}
}