Add actor search with infinite scroll
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 49s

Replace single WebFinger lookup with paginated search across cached
remote actors and local profiles. New _search_results.html partial
with htmx infinite scroll sentinel. Form submits via hx-get for
seamless pagination.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
giles
2026-02-23 08:18:59 +00:00
parent 8dc354ae0b
commit 87ce2d4970
4 changed files with 114 additions and 11 deletions

View File

@@ -138,13 +138,50 @@ def register(url_prefix="/social"):
async def search(): async def search():
actor = getattr(g, "_social_actor", None) actor = getattr(g, "_social_actor", None)
query = request.args.get("q", "").strip() query = request.args.get("q", "").strip()
result = None actors = []
total = 0
followed_urls: set[str] = set()
if query: if query:
result = await services.federation.search_remote_actor(g.s, query) actors, total = await services.federation.search_actors(g.s, query)
if actor:
following, _ = await services.federation.get_following(
g.s, actor.preferred_username, page=1, per_page=1000,
)
followed_urls = {a.actor_url for a in following}
return await render_template( return await render_template(
"federation/search.html", "federation/search.html",
query=query, query=query,
result=result, actors=actors,
total=total,
page=1,
followed_urls=followed_urls,
actor=actor,
)
@bp.get("/search/page")
async def search_page():
actor = getattr(g, "_social_actor", None)
query = request.args.get("q", "").strip()
page = request.args.get("page", 1, type=int)
actors = []
total = 0
followed_urls: set[str] = set()
if query:
actors, total = await services.federation.search_actors(
g.s, query, page=page,
)
if actor:
following, _ = await services.federation.get_following(
g.s, actor.preferred_username, page=1, per_page=1000,
)
followed_urls = {a.actor_url for a in following}
return await render_template(
"federation/_search_results.html",
actors=actors,
total=total,
page=page,
query=query,
followed_urls=followed_urls,
actor=actor, actor=actor,
) )

2
shared

Submodule shared updated: b16ba34b40...f085d4a8d0

View File

@@ -0,0 +1,61 @@
{% for a in actors %}
<article class="bg-white rounded-lg shadow-sm border border-stone-200 p-4 mb-3 flex items-center gap-4"
id="actor-{{ a.actor_url | replace('/', '_') | replace(':', '_') }}">
{% if a.icon_url %}
<img src="{{ a.icon_url }}" alt="" class="w-12 h-12 rounded-full">
{% else %}
<div class="w-12 h-12 rounded-full bg-stone-300 flex items-center justify-center text-stone-600 font-bold">
{{ (a.display_name or a.preferred_username)[0] | upper }}
</div>
{% endif %}
<div class="flex-1 min-w-0">
{% if a.id %}
<a href="{{ url_for('social.actor_timeline', id=a.id) }}" class="font-semibold text-stone-900 hover:underline">
{{ a.display_name or a.preferred_username }}
</a>
{% else %}
<span class="font-semibold text-stone-900">{{ a.display_name or a.preferred_username }}</span>
{% endif %}
<div class="text-sm text-stone-500">@{{ a.preferred_username }}@{{ a.domain }}</div>
{% if a.summary %}
<div class="text-sm text-stone-600 mt-1 truncate">{{ a.summary | striptags }}</div>
{% endif %}
</div>
{% if actor %}
<div class="flex-shrink-0">
{% if a.actor_url in (followed_urls or []) %}
<form method="post" action="{{ url_for('social.unfollow') }}"
hx-post="{{ url_for('social.unfollow') }}"
hx-target="closest article"
hx-swap="outerHTML">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="hidden" name="actor_url" value="{{ a.actor_url }}">
<button type="submit" class="text-sm border border-stone-300 rounded px-3 py-1 hover:bg-stone-100">
Unfollow
</button>
</form>
{% else %}
<form method="post" action="{{ url_for('social.follow') }}"
hx-post="{{ url_for('social.follow') }}"
hx-target="closest article"
hx-swap="outerHTML">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="hidden" name="actor_url" value="{{ a.actor_url }}">
<button type="submit" class="text-sm bg-stone-800 text-white rounded px-3 py-1 hover:bg-stone-700">
Follow
</button>
</form>
{% endif %}
</div>
{% endif %}
</article>
{% endfor %}
{% if actors | length >= 20 %}
<div hx-get="{{ url_for('social.search_page', q=query, page=page + 1) }}"
hx-trigger="revealed"
hx-swap="outerHTML">
</div>
{% endif %}

View File

@@ -5,11 +5,14 @@
{% block social_content %} {% block social_content %}
<h1 class="text-2xl font-bold mb-6">Search</h1> <h1 class="text-2xl font-bold mb-6">Search</h1>
<form method="get" action="{{ url_for('social.search') }}" class="mb-6"> <form method="get" action="{{ url_for('social.search') }}" class="mb-6"
hx-get="{{ url_for('social.search_page') }}"
hx-target="#search-results"
hx-push-url="{{ url_for('social.search') }}">
<div class="flex gap-2"> <div class="flex gap-2">
<input type="text" name="q" value="{{ query }}" <input type="text" name="q" value="{{ query }}"
class="flex-1 border border-stone-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-stone-500" class="flex-1 border border-stone-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-stone-500"
placeholder="@user@instance.tld"> placeholder="Search users or @user@instance.tld">
<button type="submit" <button type="submit"
class="bg-stone-800 text-white px-6 py-2 rounded hover:bg-stone-700"> class="bg-stone-800 text-white px-6 py-2 rounded hover:bg-stone-700">
Search Search
@@ -17,11 +20,13 @@
</div> </div>
</form> </form>
{% if query and not result %} {% if query and total %}
<p class="text-stone-500">No results found for <strong>{{ query }}</strong></p> <p class="text-sm text-stone-500 mb-4">{{ total }} result{{ 's' if total != 1 }} for <strong>{{ query }}</strong></p>
{% elif query %}
<p class="text-stone-500 mb-4">No results found for <strong>{{ query }}</strong></p>
{% endif %} {% endif %}
{% if result %} <div id="search-results">
{% include "federation/actor_card.html" %} {% include "federation/_search_results.html" %}
{% endif %} </div>
{% endblock %} {% endblock %}