broadcast accepts correctly

This commit is contained in:
Mouse Reeve 2021-02-17 11:28:54 -08:00
parent d5ca77362b
commit d022fef625
3 changed files with 27 additions and 17 deletions

View file

@ -91,9 +91,14 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship):
user_subject=self.user_object,
user_object=self.user_subject,
)
return None
return
except (UserFollows.DoesNotExist, UserBlocks.DoesNotExist):
pass
# this was calling itself which is not my idea of "super" ...
if not self.id:
super().save(*args, **kwargs)
return
if broadcast and self.user_subject.local and not self.user_object.local:
self.broadcast(self.to_activity(), self.user_subject)
@ -116,16 +121,17 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship):
def accept(self):
''' turn this request into the real deal'''
user = self.user_object
if not self.user_subject.local:
activity = activitypub.Accept(
id=self.get_remote_id(status='accepts'),
actor=self.user_object.remote_id,
object=self.to_activity()
).serialize()
self.broadcast(activity, user)
with transaction.atomic():
UserFollows.from_request(self)
self.delete()
self.broadcast(activity, user)
def reject(self):

View file

@ -77,7 +77,6 @@ class BookViews(TestCase):
self.assertEqual(rel.status, 'follow_request')
def test_handle_follow_local(self):
''' send a follow request '''
rat = models.User.objects.create_user(
@ -106,15 +105,18 @@ class BookViews(TestCase):
self.remote_user.followers.add(self.local_user)
self.assertEqual(self.remote_user.followers.count(), 1)
# need to see if this ACTUALLY broadcasts
raise ValueError()
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay') \
as mock:
views.unfollow(request)
self.assertEqual(mock.call_count, 1)
self.assertEqual(self.remote_user.followers.count(), 0)
def test_handle_accept(self):
''' accept a follow request '''
self.local_user.manually_approves_followers = True
self.local_user.save(broadcast=False)
request = self.factory.post('', {'user': self.remote_user.username})
request.user = self.local_user
rel = models.UserFollowRequest.objects.create(

View file

@ -1,5 +1,6 @@
''' views for actions you can take in the application '''
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.http import HttpResponseBadRequest
from django.shortcuts import redirect
from django.views.decorators.http import require_POST
@ -17,10 +18,13 @@ def follow(request):
except models.User.DoesNotExist:
return HttpResponseBadRequest()
rel, _ = models.UserFollowRequest.objects.get_or_create(
try:
models.UserFollowRequest.objects.create(
user_subject=request.user,
user_object=to_follow,
)
except IntegrityError:
pass
return redirect(to_follow.local_path)
@ -38,9 +42,7 @@ def unfollow(request):
models.UserFollows.objects.get(
user_subject=request.user,
user_object=to_unfollow
)
to_unfollow.followers.remove(request.user)
).delete()
return redirect(to_unfollow.local_path)