Private
Public Access
Implements the backend-agnostic search interface from the plan, selected at runtime by connection.vendor: - SQLite: an FTS5 virtual table (external-content, mirroring Article.title/content_html) created via a vendor-conditional migration, kept in sync by insert/update/delete triggers. Queries wrap the whole user input as a single FTS5 phrase so punctuation in a search box can't break MATCH syntax. `reindex_search` management command rebuilds it after bulk imports. - Postgres: computes the search vector on the fly per the plan's decision (title weighted above content, no stored/indexed column) rather than diverging the schema from SQLite. Both backends scope results to the requesting user's own subscriptions and share the stream/detail article-row template and read/star/save actions via a new apps.articles.queries.with_user_state() helper, extracted from apps.reader.views to avoid duplicating that annotation logic for search results. Verified live: the SQLite path end-to-end in-browser against the real Hacker News subscription (search UI, phrase matching, no-match case), and the Postgres path against a throwaway Postgres container (search, feed-scoping, and title-vs-content ranking), torn down afterward — not part of this project's own infrastructure. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
29 lines
991 B
Python
29 lines
991 B
Python
from .models import UserArticleState
|
|
|
|
|
|
def with_user_state(articles, user):
|
|
"""Annotates an iterable of Article instances with is_read/is_starred/
|
|
is_saved_for_later booleans for the given user, in two queries total
|
|
regardless of how many articles there are (no N+1).
|
|
|
|
Accepts either a queryset or a plain list (search backends return
|
|
plain lists already ordered by relevance), materializing it either
|
|
way.
|
|
"""
|
|
articles = list(articles)
|
|
if not articles:
|
|
return articles
|
|
|
|
states = {
|
|
state.article_id: state
|
|
for state in UserArticleState.objects.filter(
|
|
user=user, article_id__in=[a.pk for a in articles]
|
|
)
|
|
}
|
|
for article in articles:
|
|
state = states.get(article.pk)
|
|
article.is_read = state.is_read if state else False
|
|
article.is_starred = state.is_starred if state else False
|
|
article.is_saved_for_later = state.is_saved_for_later if state else False
|
|
return articles
|