2022-11-29 05:41:36 +01:00
|
|
|
from typing import Set
|
|
|
|
|
2022-11-18 02:52:00 +01:00
|
|
|
from django import forms
|
|
|
|
from django.views.generic import FormView
|
|
|
|
|
2022-11-29 05:41:36 +01:00
|
|
|
from activities.models import Hashtag
|
2022-11-23 05:07:22 +01:00
|
|
|
from users.models import Domain, Identity
|
2022-11-18 02:52:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Search(FormView):
|
|
|
|
|
|
|
|
template_name = "activities/search.html"
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
2022-11-29 05:41:36 +01:00
|
|
|
query = forms.CharField(
|
|
|
|
help_text="Search for a user by @username@domain or hashtag by #tagname"
|
|
|
|
)
|
2022-11-23 05:07:22 +01:00
|
|
|
|
2022-11-29 05:41:36 +01:00
|
|
|
def search_identities(self, query: str):
|
|
|
|
query = query.lstrip("@")
|
|
|
|
results: Set[Identity] = set()
|
2022-11-18 02:52:00 +01:00
|
|
|
if "@" in query:
|
|
|
|
username, domain = query.split("@", 1)
|
2022-11-23 05:07:22 +01:00
|
|
|
|
|
|
|
# Resolve the domain to the display domain
|
|
|
|
domain_instance = Domain.get_domain(domain)
|
|
|
|
try:
|
|
|
|
if domain_instance is None:
|
|
|
|
raise Identity.DoesNotExist()
|
|
|
|
identity = Identity.objects.get(
|
|
|
|
domain=domain_instance, username=username
|
|
|
|
)
|
|
|
|
except Identity.DoesNotExist:
|
|
|
|
if self.request.identity is not None:
|
|
|
|
# Allow authenticated users to fetch remote
|
|
|
|
identity = Identity.by_username_and_domain(
|
|
|
|
username, domain, fetch=True
|
|
|
|
)
|
|
|
|
identity = None
|
|
|
|
if identity:
|
2022-11-29 05:41:36 +01:00
|
|
|
results.add(identity)
|
2022-11-23 05:07:22 +01:00
|
|
|
|
2022-11-18 02:52:00 +01:00
|
|
|
else:
|
|
|
|
for identity in Identity.objects.filter(username=query)[:20]:
|
2022-11-29 05:41:36 +01:00
|
|
|
results.add(identity)
|
2022-11-18 02:52:00 +01:00
|
|
|
for identity in Identity.objects.filter(username__startswith=query)[:20]:
|
2022-11-29 05:41:36 +01:00
|
|
|
results.add(identity)
|
|
|
|
return results
|
|
|
|
|
|
|
|
def search_hashtags(self, query: str):
|
|
|
|
results: Set[Hashtag] = set()
|
|
|
|
|
|
|
|
if "@" in query:
|
|
|
|
return results
|
|
|
|
|
|
|
|
query = query.lstrip("#")
|
|
|
|
for hashtag in Hashtag.objects.public().hashtag_or_alias(query)[:10]:
|
|
|
|
results.add(hashtag)
|
|
|
|
for hashtag in Hashtag.objects.public().filter(hashtag__startswith=query)[:10]:
|
|
|
|
results.add(hashtag)
|
|
|
|
return results
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
query = form.cleaned_data["query"].lower()
|
|
|
|
results = {
|
|
|
|
"identities": self.search_identities(query),
|
|
|
|
"hashtags": self.search_hashtags(query),
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:52:00 +01:00
|
|
|
# Render results
|
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
context["results"] = results
|
|
|
|
return self.render_to_response(context)
|