Private
Public Access
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>
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from celery import shared_task
|
|
|
|
from apps.articles.models import Article
|
|
from apps.feeds.models import Subscription
|
|
|
|
from .engine import evaluate_rules_for_article
|
|
from .models import FilterRule
|
|
|
|
|
|
def reapply_rules_for_user_backlog(rule_id: int) -> int:
|
|
"""Re-evaluates a user's full enabled ruleset (not just the triggering
|
|
rule, so priority/short-circuit ordering stays correct) against their
|
|
unread backlog, scoped by the triggering rule's own feed/folder scope.
|
|
|
|
Exists as a plain function so it can be called synchronously for
|
|
immediate feedback (e.g. from a "Re-apply" view) or wrapped in a task
|
|
for background use — see reapply_rules_for_user below.
|
|
"""
|
|
try:
|
|
rule = FilterRule.objects.select_related("user").get(pk=rule_id)
|
|
except FilterRule.DoesNotExist:
|
|
return 0
|
|
|
|
user = rule.user
|
|
|
|
if rule.feed_id:
|
|
feed_ids = [rule.feed_id]
|
|
elif rule.folder_id:
|
|
feed_ids = list(
|
|
Subscription.objects.filter(user=user, folder_id=rule.folder_id).values_list(
|
|
"feed_id", flat=True
|
|
)
|
|
)
|
|
else:
|
|
feed_ids = list(Subscription.objects.filter(user=user).values_list("feed_id", flat=True))
|
|
|
|
if not feed_ids:
|
|
return 0
|
|
|
|
backlog = (
|
|
Article.objects.filter(feed_id__in=feed_ids)
|
|
.exclude(user_states__user=user, user_states__is_read=True)
|
|
.select_related("feed")
|
|
)
|
|
|
|
rules = list(FilterRule.objects.filter(user=user, is_enabled=True).order_by("priority", "id"))
|
|
|
|
count = 0
|
|
for article in backlog:
|
|
evaluate_rules_for_article(user, article, rules)
|
|
count += 1
|
|
return count
|
|
|
|
|
|
@shared_task
|
|
def reapply_rules_for_user(rule_id: int):
|
|
return reapply_rules_for_user_backlog(rule_id)
|