diff --git a/apps/rules/tasks.py b/apps/rules/tasks.py index 30b59fe..319f489 100644 --- a/apps/rules/tasks.py +++ b/apps/rules/tasks.py @@ -1,6 +1,6 @@ from celery import shared_task -from apps.articles.models import Article +from apps.articles.models import Article, SubscriptionTag from apps.feeds.models import Subscription from .engine import evaluate_rules_for_article @@ -12,9 +12,20 @@ def reapply_rules_for_user_backlog(rule_id: int) -> int: rule, so priority/short-circuit ordering stays correct) against their unread backlog, scoped by the triggering rule's own feed/tag 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. + 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) @@ -45,13 +56,20 @@ def reapply_rules_for_user_backlog(rule_id: int) -> int: 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: - evaluate_rules_for_article(user, article, rules) + 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 +@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) diff --git a/apps/rules/test_views.py b/apps/rules/test_views.py index 94e92bf..4f19168 100644 --- a/apps/rules/test_views.py +++ b/apps/rules/test_views.py @@ -1,8 +1,17 @@ +from unittest.mock import patch + import pytest from django.contrib.auth import get_user_model from django.urls import reverse -from apps.articles.models import STARRED_TAG_NAME, Article, ArticleTag, UserArticleState +from apps.articles.models import ( + STARRED_TAG_NAME, + Article, + ArticleTag, + SubscriptionTag, + Tag, + UserArticleState, +) from apps.feeds.models import Feed, Subscription from .models import FilterRule @@ -119,6 +128,28 @@ def test_reapply_rule_view_applies_to_unread_backlog(client, alice, feed): assert is_starred(alice, article) is True +@pytest.mark.django_db +def test_reapply_rule_view_always_queues_through_celery_never_runs_inline(client, alice, feed): + # A large backlog or a pathological regex must not be able to tie up + # the request/response cycle — the view should dispatch a task, not + # call reapply_rules_for_user_backlog directly. + Subscription.objects.create(user=alice, feed=feed) + rule = FilterRule.objects.create( + user=alice, + match_field=FilterRule.FIELD_TITLE, + match_type=FilterRule.MATCH_CONTAINS, + match_value="Python", + action=FilterRule.ACTION_STAR, + ) + client.login(username="alice", password="pw-alice-123") + + with patch("apps.rules.views.reapply_rules_for_user.delay") as mock_delay: + response = client.post(reverse("rules:reapply_rule", args=[rule.pk])) + + assert response.status_code == 302 + mock_delay.assert_called_once_with(rule.pk) + + @pytest.mark.django_db def test_reapply_skips_already_read_articles(alice, feed): article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released") @@ -160,3 +191,74 @@ def test_reapply_respects_feed_scope(alice): assert is_starred(alice, article_a) is True assert not UserArticleState.objects.filter(user=alice, article=article_b).exists() + + +@pytest.mark.django_db +def test_reapply_does_not_apply_a_different_feeds_scoped_rule_to_the_backlog(): + # Regression test for a bug where re-applying one feed-scoped rule + # loaded *every* enabled rule and ran it against the whole backlog + # with no scope check — so a same-matching rule scoped to a feed + # the user isn't re-applying would still fire on this backlog. + feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml") + feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml") + alice = User.objects.create_user("alice", password="pw-alice-123") + Subscription.objects.create(user=alice, feed=feed_a) + Subscription.objects.create(user=alice, feed=feed_b) + article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python in A") + + triggering_rule = FilterRule.objects.create( + user=alice, + feed=feed_a, + match_field=FilterRule.FIELD_TITLE, + match_type=FilterRule.MATCH_CONTAINS, + match_value="Python", + action=FilterRule.ACTION_STAR, + ) + FilterRule.objects.create( + user=alice, + feed=feed_b, + match_field=FilterRule.FIELD_TITLE, + match_type=FilterRule.MATCH_CONTAINS, + match_value="Python", + action=FilterRule.ACTION_TAG, + action_value="feed-b-only", + ) + + reapply_rules_for_user_backlog(triggering_rule.pk) + + assert is_starred(alice, article_a) is True + assert not Tag.objects.filter(user=alice, name="feed-b-only").exists() + + +@pytest.mark.django_db +def test_reapply_applies_a_tag_scoped_rule_only_to_that_tags_feeds(): + feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml") + feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml") + alice = User.objects.create_user("alice", password="pw-alice-123") + sub_a = Subscription.objects.create(user=alice, feed=feed_a) + Subscription.objects.create(user=alice, feed=feed_b) + tag = Tag.objects.create(user=alice, name="work") + SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag) + article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python in A") + article_b = Article.objects.create(feed=feed_b, guid="guid-2", title="Python in B") + + unscoped_trigger = FilterRule.objects.create( + user=alice, + match_field=FilterRule.FIELD_TITLE, + match_type=FilterRule.MATCH_CONTAINS, + match_value="unrelated-trigger-value", + action=FilterRule.ACTION_MARK_READ, + ) + FilterRule.objects.create( + user=alice, + tag=tag, + match_field=FilterRule.FIELD_TITLE, + match_type=FilterRule.MATCH_CONTAINS, + match_value="Python", + action=FilterRule.ACTION_STAR, + ) + + reapply_rules_for_user_backlog(unscoped_trigger.pk) + + assert is_starred(alice, article_a) is True + assert is_starred(alice, article_b) is False diff --git a/apps/rules/views.py b/apps/rules/views.py index 1fb3787..18b7b28 100644 --- a/apps/rules/views.py +++ b/apps/rules/views.py @@ -5,7 +5,7 @@ from django.views.decorators.http import require_POST from .forms import FilterRuleForm from .models import FilterRule -from .tasks import reapply_rules_for_user_backlog +from .tasks import reapply_rules_for_user @login_required @@ -48,8 +48,9 @@ def delete_rule(request, rule_id): @require_POST def reapply_rule(request, rule_id): rule = get_object_or_404(FilterRule, user=request.user, pk=rule_id) - # Run synchronously for immediate feedback, same pattern as add_feed — - # a Celery worker isn't required for this to work. - count = reapply_rules_for_user_backlog(rule.pk) - messages.success(request, f"Re-applied your rules to {count} unread article(s).") + # Always queued, never run inline from the request/response cycle — + # a large backlog or a pathological regex could otherwise tie up a + # web worker for a long time (see apps.rules.tasks). + reapply_rules_for_user.delay(rule.pk) + messages.success(request, "Re-applying your rules to your unread backlog…") return redirect("rules:rule_list")