Filter uncategorised category at the SQL level

Instead of loading all categories and filtering out
UNCATEGORISED in Go, add LoadConsentCategoriesByCookieBannerID
which excludes it in the query. This avoids fetching data we
immediately discard and makes the intent explicit at each call
site.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-05-04 15:13:10 +04:00
parent 4f1ecabcec
commit d35b8777da
3 changed files with 65 additions and 16 deletions

View File

@@ -428,7 +428,7 @@ func (s *Service) ensureDraftVersionForBanner(
}
var categories coredata.CookieCategories
if err := categories.LoadAllByCookieBannerID(ctx, tx, scope, bannerID); err != nil {
if err := categories.LoadConsentCategoriesByCookieBannerID(ctx, tx, scope, bannerID); err != nil {
return nil, fmt.Errorf("cannot load cookie categories: %w", err)
}
@@ -1887,7 +1887,7 @@ func (s *Service) GetActiveBannerConfig(
}
var categories coredata.CookieCategories
if err := categories.LoadAllByCookieBannerID(ctx, conn, scope, banner.ID); err != nil {
if err := categories.LoadConsentCategoriesByCookieBannerID(ctx, conn, scope, banner.ID); err != nil {
return fmt.Errorf("cannot load cookie categories: %w", err)
}

View File

@@ -31,7 +31,8 @@ func resolveTranslations(
translations coredata.CookieBannerTranslations,
categories coredata.CookieCategories,
) map[string]coredata.CookieBannerVersionSnapshotTranslation {
return buildSnapshotTranslations(translations, consentCategories(categories))
sortConsentCategories(categories)
return buildSnapshotTranslations(translations, categories)
}
// snapshotsEqual reports whether two version snapshots are visitor-identical.
@@ -56,23 +57,16 @@ func snapshotCategoryKindOrder(k coredata.CookieCategoryKind) int {
}
}
// consentCategories returns the categories that are part of the consent
// contract (everything except UNCATEGORISED), sorted in snapshot order.
// UNCATEGORISED is an admin-side inbox and never shown to visitors.
func consentCategories(categories coredata.CookieCategories) coredata.CookieCategories {
filtered := make(coredata.CookieCategories, 0, len(categories))
for _, c := range categories {
if c.Kind != coredata.CookieCategoryKindUncategorised {
filtered = append(filtered, c)
}
}
slices.SortStableFunc(filtered, func(a, b *coredata.CookieCategory) int {
// sortConsentCategories sorts categories in snapshot order: NECESSARY first,
// then NORMAL sorted by ID. The caller is expected to have already excluded
// UNCATEGORISED at load time.
func sortConsentCategories(categories coredata.CookieCategories) {
slices.SortStableFunc(categories, func(a, b *coredata.CookieCategory) int {
if d := snapshotCategoryKindOrder(a.Kind) - snapshotCategoryKindOrder(b.Kind); d != 0 {
return d
}
return bytes.Compare(a.ID[:], b.ID[:])
})
return filtered
}
func buildSnapshot(
@@ -80,7 +74,7 @@ func buildSnapshot(
categories coredata.CookieCategories,
allPatterns coredata.CookiePatterns,
) coredata.CookieBannerVersionSnapshot {
categories = consentCategories(categories)
sortConsentCategories(categories)
cookiesByCategory := make(map[gid.GID]coredata.CookieItems)
for _, p := range allPatterns {