mirror of
https://github.com/pommee/goaway.git
synced 2026-01-09 15:29:36 -06:00
fix: prevent initialization if previously done for blacklist
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user