Files
Evection/apps/rules/engine.py
T
ksmithandClaude Sonnet 5 5ed0499845 Add the filter/rules engine
Implements FilterRule (one condition -> action, scoped to a feed and/or
folder or global) with an evaluation engine applied at ingest time:
after the fetch pipeline commits new/updated articles, each affected
subscriber's enabled rules run in priority order against them, with
tag/star/mark_read actions stacking and hide short-circuiting further
evaluation for that article.

Adds rule management (create/edit/delete) and a "Re-apply" action that
re-evaluates a user's full ruleset against their unread backlog, scoped
by the triggering rule's own feed/folder — so a newly added rule can
retroactively affect already-ingested articles, not just future ones.

Verified live in-browser: created a "title contains 'Show HN' -> star"
rule scoped to the subscribed Hacker News feed, re-applied it against
the existing unread backlog, and confirmed both matching articles were
starred.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 16:31:33 -06:00

114 lines
3.7 KiB
Python

"""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