Private
Public Access
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:
1 parent
579696efc0
commit
5ed0499845
18 files changed
+1072
-6
No files matched your search
@@ -0,0 +1,122 @@
|
||||
import pytest
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from apps.feeds.models import Feed, Folder, Subscription
|
||||
from apps.rules.models import FilterRule
|
||||
|
||||
from .models import Article, UserArticleState
|
||||
from .tasks import apply_filter_rules_for_feed
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def feed(db):
|
||||
return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example Feed")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def alice(db):
|
||||
return User.objects.create_user("alice", password="pw-alice-123")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bob(db):
|
||||
return User.objects.create_user("bob", password="pw-bob-123")
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dispatch_applies_rules_only_for_subscribers_with_matching_rules(feed, alice, bob):
|
||||
Subscription.objects.create(user=alice, feed=feed)
|
||||
Subscription.objects.create(user=bob, feed=feed)
|
||||
FilterRule.objects.create(
|
||||
user=alice,
|
||||
match_field=FilterRule.FIELD_TITLE,
|
||||
match_type=FilterRule.MATCH_CONTAINS,
|
||||
match_value="Python",
|
||||
action=FilterRule.ACTION_STAR,
|
||||
)
|
||||
article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released")
|
||||
|
||||
apply_filter_rules_for_feed(feed.id, [article.pk])
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=article).is_starred is True
|
||||
# Bob has no rules, so he should get no state row at all.
|
||||
assert not UserArticleState.objects.filter(user=bob, article=article).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_feed_scoped_rule_does_not_apply_to_other_feeds(alice):
|
||||
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")
|
||||
Subscription.objects.create(user=alice, feed=feed_a)
|
||||
Subscription.objects.create(user=alice, feed=feed_b)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python news")
|
||||
article_b = Article.objects.create(feed=feed_b, guid="guid-2", title="Python news too")
|
||||
|
||||
apply_filter_rules_for_feed(feed_a.id, [article_a.pk])
|
||||
apply_filter_rules_for_feed(feed_b.id, [article_b.pk])
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=article_a).is_starred is True
|
||||
assert not UserArticleState.objects.filter(user=alice, article=article_b).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_folder_scoped_rule_only_applies_within_that_folder(alice, feed):
|
||||
other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml")
|
||||
folder = Folder.objects.create(user=alice, name="Tech")
|
||||
Subscription.objects.create(user=alice, feed=feed, folder=folder)
|
||||
Subscription.objects.create(user=alice, feed=other_feed, folder=None)
|
||||
|
||||
FilterRule.objects.create(
|
||||
user=alice,
|
||||
folder=folder,
|
||||
match_field=FilterRule.FIELD_TITLE,
|
||||
match_type=FilterRule.MATCH_CONTAINS,
|
||||
match_value="Python",
|
||||
action=FilterRule.ACTION_STAR,
|
||||
)
|
||||
|
||||
in_folder_article = Article.objects.create(feed=feed, guid="guid-1", title="Python in folder")
|
||||
outside_folder_article = Article.objects.create(
|
||||
feed=other_feed, guid="guid-2", title="Python outside folder"
|
||||
)
|
||||
|
||||
apply_filter_rules_for_feed(feed.id, [in_folder_article.pk])
|
||||
apply_filter_rules_for_feed(other_feed.id, [outside_folder_article.pk])
|
||||
|
||||
assert UserArticleState.objects.get(user=alice, article=in_folder_article).is_starred is True
|
||||
assert not UserArticleState.objects.filter(user=alice, article=outside_folder_article).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_disabled_rule_does_not_apply(feed, alice):
|
||||
Subscription.objects.create(user=alice, feed=feed)
|
||||
FilterRule.objects.create(
|
||||
user=alice,
|
||||
match_field=FilterRule.FIELD_TITLE,
|
||||
match_type=FilterRule.MATCH_CONTAINS,
|
||||
match_value="Python",
|
||||
action=FilterRule.ACTION_STAR,
|
||||
is_enabled=False,
|
||||
)
|
||||
article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released")
|
||||
|
||||
apply_filter_rules_for_feed(feed.id, [article.pk])
|
||||
|
||||
assert not UserArticleState.objects.filter(user=alice, article=article).exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dispatch_with_empty_article_ids_is_a_noop(feed):
|
||||
assert apply_filter_rules_for_feed(feed.id, []) == 0
|
||||
Reference in new issue
Block a user