Simplifies outgoing follow logic

This commit is contained in:
Mouse Reeve 2020-10-16 14:14:07 -07:00
parent 694de44f3f
commit a567bd4e61
4 changed files with 23 additions and 4 deletions

View file

@ -143,7 +143,7 @@ def handle_follow(activity):
'FOLLOW',
related_user=actor
)
outgoing.handle_accept(actor, to_follow, relationship)
outgoing.handle_accept(relationship)
else:
# Accept will be triggered manually
status_builder.create_notification(

View file

@ -80,8 +80,10 @@ def handle_unfollow(user, to_unfollow):
to_unfollow.followers.remove(user)
def handle_accept(user, to_follow, follow_request):
def handle_accept(follow_request):
''' send an acceptance message to a follow request '''
user = follow_request.user_subject
to_follow = follow_request.user_object
with transaction.atomic():
relationship = models.UserFollows.from_request(follow_request)
follow_request.delete()

View file

@ -3,7 +3,7 @@ from django.test import TestCase
from bookwyrm import models, outgoing
class OutgoingFollow(TestCase):
class Following(TestCase):
def setUp(self):
self.remote_user = models.User.objects.create_user(
'rat', 'rat@rat.com', 'ratword',
@ -29,9 +29,26 @@ class OutgoingFollow(TestCase):
self.assertEqual(rel.user_object, self.remote_user)
self.assertEqual(rel.status, 'follow_request')
def test_handle_unfollow(self):
self.remote_user.followers.add(self.local_user)
self.assertEqual(self.remote_user.followers.count(), 1)
outgoing.handle_unfollow(self.local_user, self.remote_user)
self.assertEqual(self.remote_user.followers.count(), 0)
def test_handle_accept(self):
rel = models.UserFollowRequest.objects.create(
user_subject=self.local_user,
user_object=self.remote_user
)
rel_id = rel.id
outgoing.handle_accept(rel)
# request should be deleted
self.assertEqual(
models.UserFollowRequest.objects.filter(id=rel_id).count(), 0
)
# follow relationship should exist
self.assertEqual(self.remote_user.followers.first(), self.local_user)

View file

@ -473,7 +473,7 @@ def accept_follow_request(request):
# Request already dealt with.
pass
else:
outgoing.handle_accept(requester, request.user, follow_request)
outgoing.handle_accept(follow_request)
return redirect('/user/%s' % request.user.localname)