feat: media duplicate purge, part 1 of 3: persisted purge settings and the purge API
This commit is contained in:
1 parent
161ddc0f7d
commit
e82d0ce254
25 files changed
+2459
-41
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package purge
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrInvalidPurgeInterval = errors.New("invalid purge interval")
|
||||
var ErrPurgeConflict = errors.New("the library changed since the fingerprint")
|
||||
var ErrPurgeBusy = errors.New("a purge is already running")
|
||||
|
||||
const (
|
||||
DefaultIntervalDays = 30
|
||||
MinIntervalDays = 1
|
||||
MaxIntervalDays = 365
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
AutoPurgeEnabled bool `json:"auto_purge_enabled"`
|
||||
AutoPurgeIntervalDays int `json:"auto_purge_interval_days"`
|
||||
LastPurgeAt *time.Time `json:"last_purge_at"`
|
||||
LastPurgeMerged int `json:"last_purge_merged"`
|
||||
LastPurgeArticlesUpdated int `json:"last_purge_articles_updated"`
|
||||
EnabledAt *time.Time `json:"enabled_at"`
|
||||
}
|
||||
|
||||
func DefaultSettings() Settings {
|
||||
return Settings{AutoPurgeIntervalDays: DefaultIntervalDays}
|
||||
}
|
||||
|
||||
func ValidateIntervalDays(days int) error {
|
||||
if days < MinIntervalDays || days > MaxIntervalDays {
|
||||
return ErrInvalidPurgeInterval
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Settings) NextPurgeAt() *time.Time {
|
||||
if !s.AutoPurgeEnabled || s.EnabledAt == nil {
|
||||
return nil
|
||||
}
|
||||
base := *s.EnabledAt
|
||||
if s.LastPurgeAt != nil && s.LastPurgeAt.After(base) {
|
||||
base = *s.LastPurgeAt
|
||||
}
|
||||
next := base.AddDate(0, 0, s.AutoPurgeIntervalDays)
|
||||
return &next
|
||||
}
|
||||
Reference in new issue
Block a user