fix for gifs auto-catigorisation

This commit is contained in:
onysd 2026-08-24 23:26:57 +03:00
parent 0321c196bd
commit 58a0aed4f3
3 changed files with 23 additions and 11 deletions

View file

@ -142,22 +142,23 @@ func (s *Service) createGifCatalogEntry(ctx context.Context, title string, docum
return entry, nil return entry, nil
} }
// AdminListGifCatalog returns every entry (enabled and disabled), for the // AdminListGifCatalog returns every entry (enabled and disabled), unbounded,
// admin panel's list view. // for the admin panel's list view.
func (s *Service) AdminListGifCatalog(ctx context.Context) ([]domain.GifCatalogEntry, error) { func (s *Service) AdminListGifCatalog(ctx context.Context) ([]domain.GifCatalogEntry, error) {
if s.gifCatalog == nil { if s.gifCatalog == nil {
return nil, domain.ErrGifCatalogUnavailable return nil, domain.ErrGifCatalogUnavailable
} }
return s.gifCatalog.ListGifCatalog(ctx, false) return s.gifCatalog.ListGifCatalog(ctx, false, 0)
} }
// ListGifCatalog is bots.gifCatalogSource's read: onlyEnabled=true is what // ListGifCatalog is bots.gifCatalogSource's read: onlyEnabled=true is what
// @gif actually serves. // @gif actually serves, capped at domain.MaxGifCatalogEntries -- the real
// per-response limit the client enforces.
func (s *Service) ListGifCatalog(ctx context.Context, onlyEnabled bool) ([]domain.GifCatalogEntry, error) { func (s *Service) ListGifCatalog(ctx context.Context, onlyEnabled bool) ([]domain.GifCatalogEntry, error) {
if s.gifCatalog == nil { if s.gifCatalog == nil {
return nil, nil return nil, nil
} }
return s.gifCatalog.ListGifCatalog(ctx, onlyEnabled) return s.gifCatalog.ListGifCatalog(ctx, onlyEnabled, domain.MaxGifCatalogEntries)
} }
// AdminSetGifCatalogEnabled toggles whether an entry is served. // AdminSetGifCatalogEnabled toggles whether an entry is served.
@ -219,7 +220,7 @@ func (s *Service) AdminAutoCategorizeGifCatalog(ctx context.Context) (int, error
if s.gifCatalog == nil { if s.gifCatalog == nil {
return 0, domain.ErrGifCatalogUnavailable return 0, domain.ErrGifCatalogUnavailable
} }
entries, err := s.gifCatalog.ListGifCatalog(ctx, false) entries, err := s.gifCatalog.ListGifCatalog(ctx, false, 0)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View file

@ -19,9 +19,14 @@ type GifCatalogStore interface {
// Always false for an empty filename (that's the panel-upload sentinel, // Always false for an empty filename (that's the panel-upload sentinel,
// never a real seed match). // never a real seed match).
HasGifCatalogSourceFilename(ctx context.Context, filename string) (bool, error) HasGifCatalogSourceFilename(ctx context.Context, filename string) (bool, error)
// ListGifCatalog returns every entry ordered by (sort_order, id). // ListGifCatalog returns entries ordered by (sort_order, id).
// onlyEnabled=true is what @gif serves; the admin panel lists everything. // onlyEnabled=true is what @gif serves. limit>0 caps the result (@gif's
ListGifCatalog(ctx context.Context, onlyEnabled bool) ([]domain.GifCatalogEntry, error) // live-serving path passes domain.MaxGifCatalogEntries -- the real TL-level
// cap one inline response can carry); limit<=0 means unbounded, which is
// what the admin panel and bulk operations like auto-categorize need --
// capping those at the same 50 the client renders per response would
// silently only ever touch the first page of a real catalog.
ListGifCatalog(ctx context.Context, onlyEnabled bool, limit int) ([]domain.GifCatalogEntry, error)
// SetGifCatalogEnabled toggles whether an entry is served. changed=false // SetGifCatalogEnabled toggles whether an entry is served. changed=false
// if the id doesn't exist. // if the id doesn't exist.
SetGifCatalogEnabled(ctx context.Context, id int64, enabled bool) (bool, error) SetGifCatalogEnabled(ctx context.Context, id int64, enabled bool) (bool, error)

View file

@ -48,13 +48,19 @@ func (s *GifCatalogStore) HasGifCatalogSourceFilename(ctx context.Context, filen
return exists, nil return exists, nil
} }
func (s *GifCatalogStore) ListGifCatalog(ctx context.Context, onlyEnabled bool) ([]domain.GifCatalogEntry, error) { func (s *GifCatalogStore) ListGifCatalog(ctx context.Context, onlyEnabled bool, limit int) ([]domain.GifCatalogEntry, error) {
// Postgres treats LIMIT NULL as "no limit" -- a nil *int parameter gets
// there without a second query string for the unbounded (admin) case.
var limitParam *int
if limit > 0 {
limitParam = &limit
}
rows, err := s.db.Query(ctx, ` rows, err := s.db.Query(ctx, `
SELECT id, title, document_id, enabled, sort_order, created_by, created_at, updated_at, source_filename, category SELECT id, title, document_id, enabled, sort_order, created_by, created_at, updated_at, source_filename, category
FROM gif_catalog FROM gif_catalog
WHERE NOT $1 OR enabled WHERE NOT $1 OR enabled
ORDER BY sort_order, id ORDER BY sort_order, id
LIMIT `+fmt.Sprint(domain.MaxGifCatalogEntries), onlyEnabled) LIMIT $2`, onlyEnabled, limitParam)
if err != nil { if err != nil {
return nil, fmt.Errorf("list gif catalog: %w", err) return nil, fmt.Errorf("list gif catalog: %w", err)
} }