Files
ksmithandClaude Sonnet 5 fd8eeb605e Scope backlog re-apply per-feed and always queue it through Celery (T5, S5)
Completes the T5 fix: reapply_rules_for_user_backlog now computes each
feed's subscription-tag ids up front and passes them to
evaluate_rules_for_article per article, so a feed/tag-scoped rule from
a different feed can't fire on this backlog (see the previous commit's
rule_in_scope() in apps.rules.engine).

Also completes S5's second half: the "Re-apply" view now always
dispatches reapply_rules_for_user.delay() instead of calling the
backlog function inline from the request — a large backlog or a
regex that outruns the new timeout guard no longer risks tying up a
web worker.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:20:25 -06:00

76 lines
2.9 KiB
Python

from celery import shared_task
from apps.articles.models import Article, SubscriptionTag
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/tag scope.
The *backlog* is narrowed to the triggering rule's own feed/tag scope
(re-applying a feed-scoped rule has no reason to touch other feeds'
articles), but which of the user's *other* rules apply to each of
those articles is still per-article/per-feed — evaluate_rules_for_article
checks that via rule_in_scope, so a feed- or tag-scoped rule for a
different feed can't fire on this backlog just because it happened to
match (see apps.rules.engine.rule_in_scope).
Exists as a plain function, separate from the reapply_rules_for_user
task below, so it's unit-testable without Celery — but every caller
should go through that task (see apps.rules.views.reapply_rule), not
call this directly from a request: a large backlog or a pathological
regex (bounded but not eliminated by apps.rules.engine's timeout
guard) could otherwise tie up a web worker for a long time.
"""
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.tag_id:
feed_ids = list(
Subscription.objects.filter(
user=user, subscription_tags__user=user, subscription_tags__tag_id=rule.tag_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"))
subscription_tag_ids_by_feed: dict[int, set[int]] = {}
for feed_id, tag_id in SubscriptionTag.objects.filter(
user=user, subscription__feed_id__in=feed_ids
).values_list("subscription__feed_id", "tag_id"):
subscription_tag_ids_by_feed.setdefault(feed_id, set()).add(tag_id)
count = 0
for article in backlog:
subscription_tag_ids = subscription_tag_ids_by_feed.get(article.feed_id, set())
evaluate_rules_for_article(user, article, rules, subscription_tag_ids)
count += 1
return count
@shared_task(time_limit=120, soft_time_limit=90)
def reapply_rules_for_user(rule_id: int):
return reapply_rules_for_user_backlog(rule_id)