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_subject=self.user_object,
user_object=self.user_subject, user_object=self.user_subject,
) )
return None return
except (UserFollows.DoesNotExist, UserBlocks.DoesNotExist): 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) super().save(*args, **kwargs)
return
if broadcast and self.user_subject.local and not self.user_object.local: if broadcast and self.user_subject.local and not self.user_object.local:
self.broadcast(self.to_activity(), self.user_subject) self.broadcast(self.to_activity(), self.user_subject)
@ -116,16 +121,17 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship):
def accept(self): def accept(self):
''' turn this request into the real deal''' ''' turn this request into the real deal'''
user = self.user_object user = self.user_object
activity = activitypub.Accept( if not self.user_subject.local:
id=self.get_remote_id(status='accepts'), activity = activitypub.Accept(
actor=self.user_object.remote_id, id=self.get_remote_id(status='accepts'),
object=self.to_activity() actor=self.user_object.remote_id,
).serialize() object=self.to_activity()
).serialize()
self.broadcast(activity, user)
with transaction.atomic(): with transaction.atomic():
UserFollows.from_request(self) UserFollows.from_request(self)
self.delete() self.delete()
self.broadcast(activity, user)
def reject(self): def reject(self):

View file

@ -77,7 +77,6 @@ class BookViews(TestCase):
self.assertEqual(rel.status, 'follow_request') self.assertEqual(rel.status, 'follow_request')
def test_handle_follow_local(self): def test_handle_follow_local(self):
''' send a follow request ''' ''' send a follow request '''
rat = models.User.objects.create_user( rat = models.User.objects.create_user(
@ -106,15 +105,18 @@ class BookViews(TestCase):
self.remote_user.followers.add(self.local_user) self.remote_user.followers.add(self.local_user)
self.assertEqual(self.remote_user.followers.count(), 1) self.assertEqual(self.remote_user.followers.count(), 1)
# need to see if this ACTUALLY broadcasts # 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) views.unfollow(request)
self.assertEqual(mock.call_count, 1)
self.assertEqual(self.remote_user.followers.count(), 0) self.assertEqual(self.remote_user.followers.count(), 0)
def test_handle_accept(self): def test_handle_accept(self):
''' accept a follow request ''' ''' 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 = self.factory.post('', {'user': self.remote_user.username})
request.user = self.local_user request.user = self.local_user
rel = models.UserFollowRequest.objects.create( rel = models.UserFollowRequest.objects.create(

View file

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