From c8d6ee513ba56dd8c0c3593b0f248735bd7ebf98 Mon Sep 17 00:00:00 2001 From: pommee Date: Thu, 11 Dec 2025 10:54:20 +0100 Subject: [PATCH] fix: prevent initialization if previously done for blacklist --- backend/blacklist/repository.go | 14 ++++++++++++++ backend/blacklist/service.go | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/blacklist/repository.go b/backend/blacklist/repository.go index 39a374f..fd38c92 100644 --- a/backend/blacklist/repository.go +++ b/backend/blacklist/repository.go @@ -16,6 +16,7 @@ type SourceRepository interface { GetSources(ctx context.Context, excludeCustom bool) ([]database.Source, error) GetSourceByName(ctx context.Context, name string) (*database.Source, error) GetSourceByNameAndURL(ctx context.Context, name, url string) (*database.Source, error) + GetSourceExists(ctx context.Context, name, url string) bool CreateOrUpdateSource(ctx context.Context, source *database.Source) error UpdateSourceName(ctx context.Context, oldName, newName, url string) error UpdateSourceLastUpdated(ctx context.Context, url string, timestamp time.Time) error @@ -113,6 +114,19 @@ func (r *repository) GetSourceByNameAndURL(ctx context.Context, name, url string return &source, nil } +func (r *repository) GetSourceExists(ctx context.Context, name, url string) bool { + var count int64 + result := r.db.WithContext(ctx).Model(&database.Source{}). + Where("name = ? AND url = ?", name, url). + Count(&count) + + if result.Error != nil || count == 0 { + return false + } + + return count > 0 +} + func (r *repository) CreateOrUpdateSource(ctx context.Context, source *database.Source) error { result := r.db.WithContext(ctx).Where(database.Source{Name: source.Name, URL: source.URL}).FirstOrCreate(source) if result.Error != nil { diff --git a/backend/blacklist/service.go b/backend/blacklist/service.go index 649f545..f130f89 100644 --- a/backend/blacklist/service.go +++ b/backend/blacklist/service.go @@ -94,7 +94,9 @@ func (s *Service) initialize(ctx context.Context) error { return fmt.Errorf("failed to count domains: %w", err) } - if count == 0 { + previouslyInitialized := s.repository.GetSourceExists(ctx, "Custom", "") + + if count == 0 && !previouslyInitialized { log.Info("No domains in blacklist. Running initialization...") if err := s.initializeBlockedDomains(ctx); err != nil { return fmt.Errorf("failed to initialize blocked domains: %w", err)