filter

package
v0.28.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package filter provides pattern matching and filtering functionality for RSS items.

Index

Constants

View Source
const (
	SourceFreeDownload = "free_download"
	SourceFilterRule   = "filter_rule"
	SourceNone         = ""
)

Download source tags persisted on TorrentInfo.DownloadSource.

View Source
const MaxPatternLength = 512

MaxPatternLength is the maximum allowed length for a pattern.

Variables

View Source
var (
	ErrInvalidPattern = errors.New("invalid pattern")
	ErrInvalidRegex   = errors.New("invalid regular expression")
	ErrEmptyPattern   = errors.New("pattern cannot be empty")
	ErrPatternTooLong = errors.New("pattern exceeds maximum length")
	ErrUnknownType    = errors.New("unknown pattern type")
)

Pattern matching errors.

Functions

func ValidateRegexPattern

func ValidateRegexPattern(pattern string) error

ValidateRegexPattern validates a regex pattern without creating a matcher.

Types

type Decision added in v0.25.0

type Decision struct {
	ShouldDownload bool
	MatchedRule    *models.FilterRule
	Source         string
	Reason         string
}

Decision captures the outcome of a full download decision, including which channel (if any) approved the download and the reason if rejected.

func DecideWithoutRules added in v0.25.0

func DecideWithoutRules(ctx DecisionContext) Decision

DecideWithoutRules runs the same decision tree as Decide but skips the filter-rule channel entirely. Callers use it when the RSS has no associated rules; it preserves the global hard size limit and free-channel semantics without requiring a FilterService.

type DecisionContext added in v0.25.0

type DecisionContext struct {
	Input      MatchInput
	IsFree     bool
	CanFinish  bool
	GlobalSize int
	FilterMode models.FilterMode
}

DecisionContext bundles the full set of inputs required to make a download decision.

type FilterService

type FilterService interface {
	// MatchRules checks if a torrent title matches any enabled filter rule.
	// Returns the matched rule and true if a match is found, nil and false otherwise.
	MatchRules(title string, siteID, rssID *uint) (*models.FilterRule, bool)

	// MatchRulesWithInput checks if input matches any enabled filter rule.
	// Supports matching against title, tag, or both based on rule configuration.
	MatchRulesWithInput(input MatchInput, siteID, rssID *uint) (*models.FilterRule, bool)

	// MatchRulesForRSS checks if a torrent title matches any filter rule associated with the RSS.
	// This uses the many-to-many association table for rule lookup.
	// Returns the matched rule and true if a match is found, nil and false otherwise.
	MatchRulesForRSS(title string, rssID uint) (*models.FilterRule, bool)

	// MatchRulesForRSSWithInput checks if input matches any filter rule associated with the RSS.
	// Supports matching against title, tag, or both based on rule configuration.
	MatchRulesForRSSWithInput(input MatchInput, rssID uint) (*models.FilterRule, bool)

	// ShouldDownload determines if a torrent should be downloaded based on filter rules.
	// Returns true if the torrent should be downloaded, along with the matched rule (if any).
	ShouldDownload(title string, isFree bool, siteID, rssID *uint) (bool, *models.FilterRule)

	// ShouldDownloadWithInput determines if a torrent should be downloaded based on filter rules.
	// Supports matching against title, tag, or both based on rule configuration.
	ShouldDownloadWithInput(input MatchInput, isFree bool, siteID, rssID *uint) (bool, *models.FilterRule)

	// ShouldDownloadForRSS determines if a torrent should be downloaded based on RSS-associated filter rules.
	// This uses the many-to-many association table for rule lookup.
	// Returns true if the torrent should be downloaded, along with the matched rule (if any).
	ShouldDownloadForRSS(title string, isFree bool, rssID uint) (bool, *models.FilterRule)

	// ShouldDownloadForRSSWithInput determines if a torrent should be downloaded based on RSS-associated filter rules.
	// Supports matching against title, tag, or both based on rule configuration.
	ShouldDownloadForRSSWithInput(input MatchInput, isFree bool, rssID uint) (bool, *models.FilterRule)

	// Decide evaluates the full download decision tree for an RSS item, honoring
	// the configured FilterMode, global hard size limit, and per-rule size bounds.
	// It is the canonical entry point for the v0.25+ download decision logic.
	Decide(ctx DecisionContext, rssID uint) Decision

	// GetEnabledRules returns all enabled filter rules ordered by priority.
	GetEnabledRules() ([]models.FilterRule, error)

	// GetRulesForRSS returns all enabled filter rules associated with an RSS subscription.
	// Uses the many-to-many association table.
	GetRulesForRSS(rssID uint) ([]models.FilterRule, error)

	// RefreshCache refreshes the cached matchers from the database.
	RefreshCache() error
}

FilterService provides filter rule matching functionality.

func NewFilterService

func NewFilterService(db *gorm.DB) FilterService

NewFilterService creates a new FilterService.

type KeywordMatcher

type KeywordMatcher struct {
	// contains filtered or unexported fields
}

KeywordMatcher implements case-insensitive keyword matching.

func NewKeywordMatcher

func NewKeywordMatcher(pattern string) (*KeywordMatcher, error)

NewKeywordMatcher creates a new KeywordMatcher.

func (*KeywordMatcher) Match

func (m *KeywordMatcher) Match(title string) bool

Match returns true if the title contains the keyword (case-insensitive).

func (*KeywordMatcher) Pattern

func (m *KeywordMatcher) Pattern() string

Pattern returns the original pattern string.

func (*KeywordMatcher) Type

func (m *KeywordMatcher) Type() PatternType

Type returns the pattern type.

func (*KeywordMatcher) Validate

func (m *KeywordMatcher) Validate() error

Validate checks if the pattern is valid.

type MatchInput

type MatchInput struct {
	Title string
	Tag   string
	// SizeGB is the torrent size in GB. Zero means unknown (skip size checks).
	SizeGB float64
}

MatchInput represents the input fields for matching against filter rules.

type MatchResult

type MatchResult struct {
	Matched        bool
	Rule           *models.FilterRule
	ShouldDownload bool
}

MatchResult represents the result of a filter match.

type PatternMatcher

type PatternMatcher interface {
	// Match returns true if the title matches the pattern.
	Match(title string) bool
	// Validate checks if the pattern is valid.
	Validate() error
	// Pattern returns the original pattern string.
	Pattern() string
	// Type returns the pattern type.
	Type() PatternType
}

PatternMatcher defines the interface for pattern matching.

func NewMatcher

func NewMatcher(patternType PatternType, pattern string) (PatternMatcher, error)

NewMatcher creates a new PatternMatcher based on the pattern type.

type PatternType

type PatternType string

PatternType represents the type of pattern matching to use.

const (
	// PatternKeyword matches if the title contains the keyword (case-insensitive).
	PatternKeyword PatternType = "keyword"
	// PatternWildcard uses * and ? wildcards for matching.
	PatternWildcard PatternType = "wildcard"
	// PatternRegex uses regular expressions for matching.
	PatternRegex PatternType = "regex"
)

type RegexMatcher

type RegexMatcher struct {
	// contains filtered or unexported fields
}

RegexMatcher implements regular expression pattern matching.

func NewRegexMatcher

func NewRegexMatcher(pattern string) (*RegexMatcher, error)

NewRegexMatcher creates a new RegexMatcher.

func (*RegexMatcher) Match

func (m *RegexMatcher) Match(title string) bool

Match returns true if the title matches the regex pattern.

func (*RegexMatcher) Pattern

func (m *RegexMatcher) Pattern() string

Pattern returns the original pattern string.

func (*RegexMatcher) Type

func (m *RegexMatcher) Type() PatternType

Type returns the pattern type.

func (*RegexMatcher) Validate

func (m *RegexMatcher) Validate() error

Validate checks if the pattern is valid.

type WildcardMatcher

type WildcardMatcher struct {
	// contains filtered or unexported fields
}

WildcardMatcher implements wildcard pattern matching using * and ? characters. * matches any sequence of characters (including empty). ? matches exactly one character.

func NewWildcardMatcher

func NewWildcardMatcher(pattern string) (*WildcardMatcher, error)

NewWildcardMatcher creates a new WildcardMatcher. It converts the wildcard pattern to a regular expression.

func (*WildcardMatcher) Match

func (m *WildcardMatcher) Match(title string) bool

Match returns true if the title matches the wildcard pattern.

func (*WildcardMatcher) Pattern

func (m *WildcardMatcher) Pattern() string

Pattern returns the original pattern string.

func (*WildcardMatcher) Type

func (m *WildcardMatcher) Type() PatternType

Type returns the pattern type.

func (*WildcardMatcher) Validate

func (m *WildcardMatcher) Validate() error

Validate checks if the pattern is valid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL