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>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 16:31:33 -06:00
1 parent 579696efc0
commit 5ed0499845
18 files changed
+1072 -6

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
from celery import shared_task
from django.db.models import Q
from apps.feeds.models import Subscription
from apps.rules.engine import evaluate_rules_for_article
from apps.rules.models import FilterRule
from .models import Article
@shared_task
def apply_filter_rules_for_feed(feed_id: int, article_ids: list[int]):
"""For every subscriber of ``feed_id``, evaluate their enabled
FilterRules (scoped to this feed/folder or unscoped) against the given
articles. Dispatched by the fetch pipeline after new/updated articles
are committed for a feed.
"""
if not article_ids:
return 0
articles = list(Article.objects.filter(pk__in=article_ids).select_related("feed"))
if not articles:
return 0
subscriptions = Subscription.objects.filter(feed_id=feed_id).select_related("user")
evaluated = 0
for subscription in subscriptions:
rules = FilterRule.objects.filter(user=subscription.user, is_enabled=True).filter(
Q(feed_id=feed_id) | Q(feed__isnull=True)
)
if subscription.folder_id:
rules = rules.filter(Q(folder_id=subscription.folder_id) | Q(folder__isnull=True))
else:
rules = rules.filter(folder__isnull=True)
rules = list(rules.order_by("priority", "id"))
if not rules:
continue
for article in articles:
evaluate_rules_for_article(subscription.user, article, rules)
evaluated += 1
return evaluated