Private
Public Access
reapply_rules_for_user_backlog narrowed its *backlog* to the triggering rule's feed/tag scope, but then loaded every enabled rule for the user and ran all of them against that backlog with no per-rule scope check — so re-applying one feed- or tag-scoped rule could also fire an unrelated rule scoped to a different feed/tag, if it happened to match. rule_in_scope() moves the feed/tag scope check into the engine itself (apply_filter_rules_for_feed already computed this per-subscription via query filters; it now just passes the tag ids and lets the engine decide), so backlog re-apply and ingest-time dispatch can't drift into scoping rules differently again. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
184 lines
6.4 KiB
Python
184 lines
6.4 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
|
|
import signal
|
|
|
|
from django.utils import timezone
|
|
|
|
from apps.articles.models import (
|
|
STARRED_TAG_NAME,
|
|
Article,
|
|
ArticleTag,
|
|
Tag,
|
|
UserArticleState,
|
|
get_system_tag,
|
|
)
|
|
|
|
from .models import FilterRule
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_TAG_RE = re.compile(r"<[^>]+>")
|
|
|
|
# Bounds how much CPU/time a single user-authored regex rule can burn
|
|
# against one article. re has no built-in match timeout, so this uses
|
|
# SIGALRM to interrupt a runaway match (catastrophic backtracking) —
|
|
# only effective on the main thread of a process with SIGALRM available,
|
|
# which covers gunicorn's sync workers and Celery's (default) prefork
|
|
# pool; elsewhere it's skipped and MAX_REGEX_HAYSTACK_CHARS is the only
|
|
# bound. reapply_rules_for_user (apps.rules.tasks) also carries a Celery
|
|
# task time_limit as a second, coarser backstop.
|
|
REGEX_TIMEOUT_SECONDS = 1
|
|
MAX_REGEX_HAYSTACK_CHARS = 20_000
|
|
|
|
|
|
class _RegexTimeout(Exception):
|
|
pass
|
|
|
|
|
|
def _raise_regex_timeout(signum, frame):
|
|
raise _RegexTimeout()
|
|
|
|
|
|
def _regex_search_with_timeout(pattern: str, haystack: str, flags: int):
|
|
if not hasattr(signal, "SIGALRM"):
|
|
return re.search(pattern, haystack, flags)
|
|
try:
|
|
previous_handler = signal.signal(signal.SIGALRM, _raise_regex_timeout)
|
|
except ValueError:
|
|
# Not the main thread (e.g. a threaded dev server) — signal
|
|
# timers only work there.
|
|
return re.search(pattern, haystack, flags)
|
|
signal.setitimer(signal.ITIMER_REAL, REGEX_TIMEOUT_SECONDS)
|
|
try:
|
|
return re.search(pattern, haystack, flags)
|
|
finally:
|
|
signal.setitimer(signal.ITIMER_REAL, 0)
|
|
signal.signal(signal.SIGALRM, previous_handler)
|
|
|
|
|
|
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
|
|
haystack = raw_haystack[:MAX_REGEX_HAYSTACK_CHARS]
|
|
try:
|
|
return _regex_search_with_timeout(rule.match_value, haystack, flags) is not None
|
|
except _RegexTimeout:
|
|
logger.warning("Regex timed out for rule %s", rule.pk)
|
|
return False
|
|
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:
|
|
tag = get_system_tag(user, STARRED_TAG_NAME)
|
|
ArticleTag.objects.get_or_create(user=user, article=article, tag=tag)
|
|
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 rule_in_scope(rule: FilterRule, feed_id, subscription_tag_ids) -> bool:
|
|
"""Whether `rule`'s own feed/tag scope (see FilterRule's docstring)
|
|
covers an article from `feed_id`, given the user's subscription-tag
|
|
ids for that feed.
|
|
|
|
Centralized here — rather than left for each caller to re-derive via
|
|
its own query filters — so ingest-time dispatch and backlog re-apply
|
|
can't drift into scoping rules differently (see apps.articles.tasks
|
|
and apps.rules.tasks).
|
|
"""
|
|
if rule.feed_id is not None and rule.feed_id != feed_id:
|
|
return False
|
|
if rule.tag_id is not None and rule.tag_id not in subscription_tag_ids:
|
|
return False
|
|
return True
|
|
|
|
|
|
def evaluate_rules_for_article(user, article: Article, rules, subscription_tag_ids=()) -> None:
|
|
"""Evaluates an ordered iterable of a user's enabled rules against one
|
|
article, applying actions as they match. `rules` doesn't need to be
|
|
pre-scoped to this article's feed/tags — out-of-scope rules (per
|
|
rule_in_scope) are skipped here, so callers can pass one user's whole
|
|
enabled ruleset regardless of which feed(s) the article(s) belong to.
|
|
"""
|
|
matched_any = False
|
|
state = None
|
|
|
|
for rule in rules:
|
|
if not rule_in_scope(rule, article.feed_id, subscription_tag_ids):
|
|
continue
|
|
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
|