Fixes accept follow request

Fixes #3
This commit is contained in:
Mouse Reeve 2020-02-14 22:44:07 -08:00
parent c4549e9b17
commit e0e419a757
3 changed files with 25 additions and 17 deletions

View file

@ -16,7 +16,7 @@ def get_recipients(user, post_privacy, direct_recipients=None):
recipients = direct_recipients or [] recipients = direct_recipients or []
if post_privacy == 'direct': if post_privacy == 'direct':
# all we care about is direct_recipients, not followers # all we care about is direct_recipients, not followers
return recipients return [u.inbox for u in recipients]
# load all the followers of the user who is sending the message # load all the followers of the user who is sending the message
followers = user.followers.all() followers = user.followers.all()

View file

@ -8,13 +8,12 @@ from django.http import HttpResponse, HttpResponseBadRequest, \
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import json import json
import requests import requests
from uuid import uuid4
from fedireads import models from fedireads import models
from fedireads.remote_user import get_or_create_remote_user from fedireads import outgoing
from fedireads.openlibrary import get_or_create_book from fedireads.openlibrary import get_or_create_book
from fedireads.remote_user import get_or_create_remote_user
from fedireads.sanitize_html import InputHtmlParser from fedireads.sanitize_html import InputHtmlParser
from fedireads.settings import DOMAIN
def webfinger(request): def webfinger(request):
@ -259,25 +258,16 @@ def handle_incoming_follow(activity):
to_follow = models.User.objects.get(actor=activity['object']) to_follow = models.User.objects.get(actor=activity['object'])
# figure out who they are # figure out who they are
user = get_or_create_remote_user(activity['actor']) user = get_or_create_remote_user(activity['actor'])
to_follow.followers.add(user)
# verify uuid and accept the request
models.FollowActivity( models.FollowActivity(
uuid=activity['id'], uuid=activity['id'],
user=user, user=user,
followed=to_follow, followed=to_follow,
content=activity, content=activity,
activity_type='Follow',
) )
uuid = uuid4() # TODO: allow users to manually approve requests
# TODO does this need to be signed? outgoing.handle_outgoing_accept(user, to_follow, activity)
# TODO: handle users who moderate followers instead of auto-accepting return HttpResponse()
return JsonResponse({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://%s/%s' % (DOMAIN, uuid),
'type': 'Accept',
'actor': user.actor,
'object': activity,
})
def handle_incoming_follow_accept(activity): def handle_incoming_follow_accept(activity):

View file

@ -108,6 +108,24 @@ def handle_outgoing_follow(user, to_follow):
raise(error['error']) raise(error['error'])
def handle_outgoing_accept(user, to_follow, activity):
''' send an acceptance message to a follow request '''
to_follow.followers.add(user)
activity = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://%s/%s#accepts/follows/' % (DOMAIN, to_follow.localname),
'type': 'Accept',
'actor': to_follow.actor,
'object': activity,
}
recipient = get_recipients(
to_follow,
'direct',
direct_recipients=[user]
)
broadcast(to_follow, activity, recipient)
def handle_shelve(user, book, shelf): def handle_shelve(user, book, shelf):
''' a local user is getting a book put on their shelf ''' ''' a local user is getting a book put on their shelf '''
# update the database # update the database