Don't let logged out viwers search for users

This commit is contained in:
Mouse Reeve 2021-04-29 12:18:55 -07:00
parent 9e2b4f61bb
commit 9d89aaf9fc
2 changed files with 22 additions and 21 deletions

View file

@ -70,10 +70,11 @@
{% endif %} {% endif %}
</div> </div>
<div class="column"> <div class="column">
{% if request.user.is_authenticated %}
<section class="block"> <section class="block">
<h2 class="title is-4">{% trans "Matching Users" %}</h2> <h2 class="title is-4">{% trans "Matching Users" %}</h2>
{% if not user_results %} {% if not user_results %}
<p>{% blocktrans %}No users found for "{{ query }}"{% endblocktrans %}</p> <p><em>{% blocktrans %}No users found for "{{ query }}"{% endblocktrans %}</em></p>
{% endif %} {% endif %}
<ul> <ul>
{% for result in user_results %} {% for result in user_results %}
@ -87,10 +88,11 @@
{% endfor %} {% endfor %}
</ul> </ul>
</section> </section>
{% endif %}
<section class="block"> <section class="block">
<h2 class="title is-4">{% trans "Lists" %}</h2> <h2 class="title is-4">{% trans "Lists" %}</h2>
{% if not list_results %} {% if not list_results %}
<p>{% blocktrans %}No lists found for "{{ query }}"{% endblocktrans %}</p> <p><em>{% blocktrans %}No lists found for "{{ query }}"{% endblocktrans %}</em></p>
{% endif %} {% endif %}
{% for result in list_results %} {% for result in list_results %}
<div class="block"> <div class="block">

View file

@ -30,27 +30,30 @@ class Search(View):
) )
return JsonResponse([r.json() for r in book_results], safe=False) return JsonResponse([r.json() for r in book_results], safe=False)
data = {"query": query or ""}
# use webfinger for mastodon style account@domain.com username # use webfinger for mastodon style account@domain.com username
if query and re.match(regex.full_username, query): if query and re.match(regex.full_username, query):
handle_remote_webfinger(query) handle_remote_webfinger(query)
# do a user search # do a user search
user_results = ( if request.user.is_authenticated:
models.User.viewer_aware_objects(request.user) data["user_results"] = (
.annotate( models.User.viewer_aware_objects(request.user)
similarity=Greatest( .annotate(
TrigramSimilarity("username", query), similarity=Greatest(
TrigramSimilarity("localname", query), TrigramSimilarity("username", query),
TrigramSimilarity("localname", query),
)
) )
.filter(
similarity__gt=0.5,
)
.order_by("-similarity")[:10]
) )
.filter(
similarity__gt=0.5,
)
.order_by("-similarity")[:10]
)
# any relevent lists? # any relevent lists?
list_results = ( data["list_results"] = (
privacy_filter( privacy_filter(
request.user, request.user,
models.List.objects, models.List.objects,
@ -68,11 +71,7 @@ class Search(View):
.order_by("-similarity")[:10] .order_by("-similarity")[:10]
) )
book_results = connector_manager.search(query, min_confidence=min_confidence) data["book_results"] = connector_manager.search(
data = { query, min_confidence=min_confidence
"book_results": book_results, )
"user_results": user_results,
"list_results": list_results,
"query": query or "",
}
return TemplateResponse(request, "search_results.html", data) return TemplateResponse(request, "search_results.html", data)