From f7867da7a0fd1956f04c9e8a77d3b0e1d5a878d3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 19 Feb 2020 11:33:00 -0800 Subject: [PATCH] Fixes following external fedireads user Fixes #42 --- fedireads/activitypub/actor.py | 4 +++- fedireads/outgoing.py | 5 ++++- fedireads/remote_user.py | 32 +++++++++++++-------------- fedireads/templates/user.html | 6 ++--- fedireads/templates/user_results.html | 3 +-- fedireads/urls.py | 4 ++-- fedireads/views.py | 5 ++--- 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/fedireads/activitypub/actor.py b/fedireads/activitypub/actor.py index 63d494011..1144fd1cb 100644 --- a/fedireads/activitypub/actor.py +++ b/fedireads/activitypub/actor.py @@ -13,6 +13,7 @@ def get_actor(user): 'preferredUsername': user.localname, 'name': user.name, 'inbox': user.inbox, + 'outbox': '%s/outbox' % user.actor, 'followers': '%s/followers' % user.actor, 'following': '%s/following' % user.actor, 'summary': user.summary, @@ -23,6 +24,7 @@ def get_actor(user): }, 'endpoints': { 'sharedInbox': user.shared_inbox, - } + }, + 'fedireadsUser': True, } diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 79b47e1a0..d22c2a92c 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -66,7 +66,10 @@ def handle_account_search(query): data = response.json() for link in data['links']: if link['rel'] == 'self': - user = get_or_create_remote_user(link['href']) + try: + user = get_or_create_remote_user(link['href']) + except KeyError: + return HttpResponseNotFound() return user diff --git a/fedireads/remote_user.py b/fedireads/remote_user.py index 013358b1c..8dbab1597 100644 --- a/fedireads/remote_user.py +++ b/fedireads/remote_user.py @@ -29,23 +29,21 @@ def get_or_create_remote_user(actor): shared_inbox = data.get('endpoints').get('sharedInbox') if \ data.get('endpoints') else None - try: - user = models.User.objects.create_user( - username, - '', '', # email and passwords are left blank - actor=actor, - name=data.get('name'), - summary=data.get('summary'), - inbox=data['inbox'], #fail if there's no inbox - outbox=data['outbox'], # fail if there's no outbox - shared_inbox=shared_inbox, - # TODO: I'm never actually using this for remote users - public_key=data.get('publicKey').get('publicKeyPem'), - local=False, - fedireads_user=False, - ) - except KeyError: - return False + # throws a key error if it can't find any of these fields + user = models.User.objects.create_user( + username, + '', '', # email and passwords are left blank + actor=actor, + name=data.get('name'), + summary=data.get('summary'), + inbox=data['inbox'], #fail if there's no inbox + outbox=data['outbox'], # fail if there's no outbox + shared_inbox=shared_inbox, + # TODO: I'm never actually using this for remote users + public_key=data.get('publicKey').get('publicKeyPem'), + local=False, + fedireads_user=data.get('fedireadsUser', False), + ) return user diff --git a/fedireads/templates/user.html b/fedireads/templates/user.html index 27b2f7e9d..ff3f06ba2 100644 --- a/fedireads/templates/user.html +++ b/fedireads/templates/user.html @@ -14,15 +14,13 @@ {% endif %} {% if not is_self %} {% if not request.user in user.followers.all %} -
+ {% csrf_token %} -
{% else %} -
+ {% csrf_token %} -
{% endif %} diff --git a/fedireads/templates/user_results.html b/fedireads/templates/user_results.html index 97d0d7ff8..6c91b705f 100644 --- a/fedireads/templates/user_results.html +++ b/fedireads/templates/user_results.html @@ -4,9 +4,8 @@ {% for result in results %}

{{ result.username }}

-
+ {% csrf_token %} -
diff --git a/fedireads/urls.py b/fedireads/urls.py index 506cb014c..18cdb27c6 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -56,8 +56,8 @@ urlpatterns = [ r'^shelve/(?P\w+)/(?P[\w-]+)/(?P\d+)/?$', views.shelve ), - re_path(r'^follow/?$', views.follow), - re_path(r'^unfollow/?$', views.unfollow), + re_path(r'^follow/(?P[\w@\.]+)/?$', views.follow), + re_path(r'^unfollow/(?P[\w@\.]+)/?$', views.unfollow), re_path(r'^search/?$', views.search), re_path(r'^edit_profile/?$', views.edit_profile), diff --git a/fedireads/views.py b/fedireads/views.py index fed94f259..18a65a0f0 100644 --- a/fedireads/views.py +++ b/fedireads/views.py @@ -274,11 +274,10 @@ def favorite(request, status_id): @login_required -def follow(request): +def follow(request, username): ''' follow another user, here or abroad ''' - to_follow = request.POST.get('user') # should this be an actor rather than an id? idk - to_follow = models.User.objects.get(id=to_follow) + to_follow = models.User.objects.get(username=username) outgoing.handle_outgoing_follow(request.user, to_follow) user_slug = to_follow.localname if to_follow.localname \