mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-26 17:08:09 +00:00
Search local users as well as webfinger
This commit is contained in:
parent
b36b306934
commit
aa2e4da6f0
2 changed files with 14 additions and 4 deletions
|
@ -24,7 +24,7 @@
|
||||||
</section>
|
</section>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% if not results %}
|
{% if not book_results %}
|
||||||
<p>No books found for "{{ query }}"</p>
|
<p>No books found for "{{ query }}"</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required, permission_required
|
from django.contrib.auth.decorators import login_required, permission_required
|
||||||
|
from django.contrib.postgres.search import SearchRank, SearchVector
|
||||||
from django.db.models import Avg, Count, Q
|
from django.db.models import Avg, Count, Q
|
||||||
from django.http import HttpResponseBadRequest, HttpResponseNotFound,\
|
from django.http import HttpResponseBadRequest, HttpResponseNotFound,\
|
||||||
JsonResponse
|
JsonResponse
|
||||||
|
@ -153,11 +154,20 @@ def search(request):
|
||||||
book_results = books_manager.local_search(query)
|
book_results = books_manager.local_search(query)
|
||||||
return JsonResponse([r.__dict__ for r in book_results], safe=False)
|
return JsonResponse([r.__dict__ for r in book_results], safe=False)
|
||||||
|
|
||||||
user_results = []
|
|
||||||
# use webfinger looks like a mastodon style account@domain.com username
|
# use webfinger looks like a mastodon style account@domain.com username
|
||||||
if re.match(r'\w+@\w+.\w+', query):
|
if re.match(r'\w+@\w+.\w+', query):
|
||||||
# if something looks like a username, search with webfinger
|
outgoing.handle_remote_webfinger(query)
|
||||||
user_results = outgoing.handle_remote_webfinger(query)
|
|
||||||
|
# do a local user search
|
||||||
|
vector = SearchVector('localname', weight='A') + \
|
||||||
|
SearchVector('username', wieght='A')
|
||||||
|
user_results = models.User.objects.annotate(
|
||||||
|
search=vector
|
||||||
|
).annotate(
|
||||||
|
rank=SearchRank(vector, query)
|
||||||
|
).filter(
|
||||||
|
rank__gt=0
|
||||||
|
).order_by('-rank')[:10]
|
||||||
|
|
||||||
book_results = books_manager.search(query)
|
book_results = books_manager.search(query)
|
||||||
data = {
|
data = {
|
||||||
|
|
Loading…
Reference in a new issue