fix: prevent initialization if previously done for blacklist

This commit is contained in:
pommee
2025-12-11 10:54:20 +01:00
parent 5d4dc69e8f
commit c8d6ee513b
2 changed files with 17 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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)