Fixes webfinger

This commit is contained in:
Mouse Reeve 2020-11-01 10:42:48 -08:00
parent 29094f3c3f
commit 203a0a25eb
4 changed files with 23 additions and 14 deletions

View file

@ -15,6 +15,7 @@ from bookwyrm.status import create_generated_note
from bookwyrm.status import delete_status from bookwyrm.status import delete_status
from bookwyrm.remote_user import get_or_create_remote_user from bookwyrm.remote_user import get_or_create_remote_user
from bookwyrm.settings import DOMAIN from bookwyrm.settings import DOMAIN
from bookwyrm.utils import regex
@csrf_exempt @csrf_exempt
@ -36,13 +37,17 @@ def outbox(request, username):
def handle_remote_webfinger(query): def handle_remote_webfinger(query):
''' webfingerin' other servers ''' ''' webfingerin' other servers, username query should be user@domain '''
user = None user = None
domain = query.split('@')[1] try:
domain = query.split('@')[2]
except IndexError:
return None
try: try:
user = models.User.objects.get(username=query) user = models.User.objects.get(username=query)
except models.User.DoesNotExist: except models.User.DoesNotExist:
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \ url = 'https://%s/.well-known/webfinger?resource=acct:@%s' % \
(domain, query) (domain, query)
try: try:
response = requests.get(url) response = requests.get(url)
@ -57,7 +62,7 @@ def handle_remote_webfinger(query):
user = get_or_create_remote_user(link['href']) user = get_or_create_remote_user(link['href'])
except KeyError: except KeyError:
return None return None
return [user] return user
def handle_follow(user, to_follow): def handle_follow(user, to_follow):
@ -216,7 +221,7 @@ def handle_status(user, form):
# inspect the text for user tags # inspect the text for user tags
text = status.content text = status.content
matches = re.finditer( matches = re.finditer(
r'\W@[a-zA-Z_\-\.0-9]+(@[a-z-A-Z0-9_\-]+.[a-z]+)?', regex.username,
text text
) )
for match in matches: for match in matches:
@ -226,9 +231,8 @@ def handle_status(user, form):
username.append(DOMAIN) username.append(DOMAIN)
username = '@'.join(username) username = '@'.join(username)
try: mention_user = handle_remote_webfinger(username)
mention_user = models.User.objects.get(username=username) if not mention_user:
except models.User.DoesNotExist:
# we can ignore users we don't know about # we can ignore users we don't know about
continue continue
# add them to status mentions fk # add them to status mentions fk

View file

@ -0,0 +1 @@
from .regex import username

View file

@ -16,6 +16,7 @@ from bookwyrm.activitypub import ActivityEncoder
from bookwyrm import forms, models, books_manager from bookwyrm import forms, models, books_manager
from bookwyrm import goodreads_import from bookwyrm import goodreads_import
from bookwyrm.tasks import app from bookwyrm.tasks import app
from bookwyrm.utils import regex
def get_user_from_username(username): def get_user_from_username(username):
@ -168,7 +169,7 @@ def search(request):
return JsonResponse([r.__dict__ for r in book_results], safe=False) return JsonResponse([r.__dict__ for r in book_results], safe=False)
# 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(regex.full_username, query):
outgoing.handle_remote_webfinger(query) outgoing.handle_remote_webfinger(query)
# do a local user search # do a local user search

View file

@ -1,5 +1,5 @@
''' responds to various requests to /.well-know ''' ''' responds to various requests to /.well-know '''
from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponseNotFound
from django.http import JsonResponse from django.http import JsonResponse
from bookwyrm import models from bookwyrm import models
@ -13,11 +13,14 @@ def webfinger(request):
resource = request.GET.get('resource') resource = request.GET.get('resource')
if not resource and not resource.startswith('acct:'): if not resource and not resource.startswith('acct:'):
return HttpResponseBadRequest() return HttpResponseNotFound()
ap_id = resource.replace('acct:', '')
user = models.User.objects.filter(username=ap_id).first() username = resource.replace('acct:@', '')
if not user: try:
user = models.User.objects.get(username=username)
except models.User.DoesNotExist:
return HttpResponseNotFound('No account found') return HttpResponseNotFound('No account found')
return JsonResponse({ return JsonResponse({
'subject': 'acct:%s' % (user.username), 'subject': 'acct:%s' % (user.username),
'links': [ 'links': [