"""Evaluates FilterRules against Articles and applies their actions. Kept as plain functions over model instances (no Celery here) so rule logic can be unit-tested directly and reused both from the ingest-time dispatch (apps.articles.tasks) and the retroactive "re-apply" task. """ from __future__ import annotations import logging import re from django.utils import timezone from apps.articles.models import Article, ArticleTag, Tag, UserArticleState from .models import FilterRule logger = logging.getLogger(__name__) _TAG_RE = re.compile(r"<[^>]+>") def _haystack(article: Article, match_field: str) -> str: if match_field == FilterRule.FIELD_TITLE: return article.title or "" if match_field == FilterRule.FIELD_CONTENT: return _TAG_RE.sub(" ", article.content_html or "") if match_field == FilterRule.FIELD_AUTHOR: return article.author or "" if match_field == FilterRule.FIELD_FEED_TITLE: return article.feed.title or "" if match_field == FilterRule.FIELD_URL: return article.link or "" return "" def rule_matches(rule: FilterRule, article: Article) -> bool: raw_haystack = _haystack(article, rule.match_field) if rule.match_type == FilterRule.MATCH_REGEX: flags = 0 if rule.case_sensitive else re.IGNORECASE try: return re.search(rule.match_value, raw_haystack, flags) is not None except re.error as exc: logger.warning("Invalid regex in rule %s: %s", rule.pk, exc) return False haystack = raw_haystack needle = rule.match_value if not rule.case_sensitive: haystack = haystack.casefold() needle = needle.casefold() if rule.match_type == FilterRule.MATCH_CONTAINS: return needle in haystack if rule.match_type == FilterRule.MATCH_NOT_CONTAINS: return needle not in haystack if rule.match_type == FilterRule.MATCH_EXACT: return haystack == needle return False return False def apply_rule_action(rule: FilterRule, user, article: Article, state: UserArticleState) -> bool: """Applies a matched rule's action to the given state. Returns True if evaluation should stop for this article (the rule hid it).""" now = timezone.now() if rule.action == FilterRule.ACTION_HIDE: state.is_hidden = True state.is_read = True state.read_at = state.read_at or now state.save(update_fields=["is_hidden", "is_read", "read_at"]) return True if rule.action == FilterRule.ACTION_STAR: state.is_starred = True state.starred_at = state.starred_at or now state.save(update_fields=["is_starred", "starred_at"]) return False if rule.action == FilterRule.ACTION_MARK_READ: state.is_read = True state.read_at = state.read_at or now state.save(update_fields=["is_read", "read_at"]) return False if rule.action == FilterRule.ACTION_TAG and rule.action_value.strip(): tag, _ = Tag.objects.get_or_create(user=user, name=rule.action_value.strip()) ArticleTag.objects.get_or_create(user=user, article=article, tag=tag) return False return False def evaluate_rules_for_article(user, article: Article, rules) -> None: """Evaluates an ordered iterable of rules (already scoped/enabled) against one article for one user, applying actions as they match.""" matched_any = False state = None for rule in rules: if not rule_matches(rule, article): continue if state is None: state, _ = UserArticleState.objects.get_or_create(user=user, article=article) matched_any = True should_stop = apply_rule_action(rule, user, article, state) if should_stop: break return matched_any