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>
80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
from django.conf import settings
|
|
from django.db import models
|
|
|
|
from apps.feeds.models import Feed, Folder
|
|
|
|
|
|
class FilterRule(models.Model):
|
|
"""A single condition -> action rule, evaluated at ingest time against
|
|
new/updated articles for every subscriber of the matching feed(s).
|
|
|
|
v1 supports one condition per rule; users combine effects by stacking
|
|
multiple rules (evaluated in ``priority`` order, ascending — lower
|
|
numbers run first). Compound AND/OR condition groups are deferred.
|
|
"""
|
|
|
|
FIELD_TITLE = "title"
|
|
FIELD_CONTENT = "content"
|
|
FIELD_AUTHOR = "author"
|
|
FIELD_FEED_TITLE = "feed_title"
|
|
FIELD_URL = "url"
|
|
MATCH_FIELD_CHOICES = [
|
|
(FIELD_TITLE, "Title"),
|
|
(FIELD_CONTENT, "Content"),
|
|
(FIELD_AUTHOR, "Author"),
|
|
(FIELD_FEED_TITLE, "Feed"),
|
|
(FIELD_URL, "URL"),
|
|
]
|
|
|
|
MATCH_CONTAINS = "contains"
|
|
MATCH_NOT_CONTAINS = "not_contains"
|
|
MATCH_REGEX = "regex"
|
|
MATCH_EXACT = "exact"
|
|
MATCH_TYPE_CHOICES = [
|
|
(MATCH_CONTAINS, "Contains"),
|
|
(MATCH_NOT_CONTAINS, "Does not contain"),
|
|
(MATCH_REGEX, "Regex"),
|
|
(MATCH_EXACT, "Exact match"),
|
|
]
|
|
|
|
ACTION_TAG = "tag"
|
|
ACTION_STAR = "star"
|
|
ACTION_HIDE = "hide"
|
|
ACTION_MARK_READ = "mark_read"
|
|
ACTION_CHOICES = [
|
|
(ACTION_TAG, "Add tag"),
|
|
(ACTION_STAR, "Star"),
|
|
(ACTION_HIDE, "Hide"),
|
|
(ACTION_MARK_READ, "Mark read"),
|
|
]
|
|
|
|
user = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="filter_rules"
|
|
)
|
|
name = models.CharField(max_length=255, blank=True)
|
|
|
|
# Scope: null means "every subscription", matching the plan's decision
|
|
# to keep folders flat and scoping simple.
|
|
feed = models.ForeignKey(Feed, null=True, blank=True, on_delete=models.CASCADE)
|
|
folder = models.ForeignKey(Folder, null=True, blank=True, on_delete=models.CASCADE)
|
|
|
|
match_field = models.CharField(max_length=20, choices=MATCH_FIELD_CHOICES)
|
|
match_type = models.CharField(max_length=20, choices=MATCH_TYPE_CHOICES)
|
|
match_value = models.CharField(max_length=500)
|
|
case_sensitive = models.BooleanField(default=False)
|
|
|
|
action = models.CharField(max_length=20, choices=ACTION_CHOICES)
|
|
action_value = models.CharField(
|
|
max_length=100, blank=True, help_text="Tag name, only used when action is 'Add tag'."
|
|
)
|
|
|
|
priority = models.PositiveIntegerField(default=0, help_text="Lower numbers run first.")
|
|
is_enabled = models.BooleanField(default=True)
|
|
|
|
class Meta:
|
|
ordering = ["priority", "id"]
|
|
|
|
def __str__(self):
|
|
label = self.name or f"{self.get_match_field_display()} {self.get_match_type_display()}"
|
|
return f"{label} -> {self.get_action_display()}"
|