Fixes unfollow

This commit is contained in:
Mouse Reeve 2021-02-16 16:35:28 -08:00
parent b57a86d4e2
commit d81bfb6573
5 changed files with 47 additions and 21 deletions

View file

@ -85,11 +85,14 @@ class ActivityObject:
# figure out the right model -- wish I had a better way for this
model = model or get_model_from_type(self.type)
if hasattr(model, 'ignore_activity') and model.ignore_activity(self):
return instance
# only reject statuses if we're potentially creating them
if allow_create and \
hasattr(model, 'ignore_activity') and model.ignore_activity(self):
return None
# check for an existing instance
instance = instance or model.find_existing(self.serialize())
if not instance and not allow_create:
# so that we don't create when we want to delete or update
return None

View file

@ -1,6 +1,7 @@
''' undo wrapper activity '''
from dataclasses import dataclass
from typing import List
from django.apps import apps
from .base_activity import ActivityObject, Signature, resolve_remote_id
from .book import Edition
@ -59,7 +60,12 @@ class Undo(Verb):
def action(self):
''' find and remove the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
# this is so hacky but it does make it work....
# (because you Reject a request and Undo a follow
model = None
if self.object.type == 'Follow':
model = apps.get_model('bookwyrm.UserFollows')
obj = self.object.to_model(model=model, save=False, allow_create=False)
obj.delete()

View file

@ -56,7 +56,7 @@ class UserRelationship(BookWyrmModel):
return '%s#%s/%d' % (base_path, status, self.id)
class UserFollows(UserRelationship):
class UserFollows(ActivitypubMixin, UserRelationship):
''' Following a user '''
status = 'follows'

View file

@ -42,7 +42,7 @@ class Inbox(TestCase):
'type': 'Create',
'actor': 'hi',
"to": [
"https://www.w3.org/ns/activitystreams#Public"
"https://www.w3.org/ns/activitystreams#public"
],
"cc": [
"https://example.com/user/mouse/followers"
@ -296,19 +296,23 @@ class Inbox(TestCase):
def test_handle_unfollow(self):
''' remove a relationship '''
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
rel = models.UserFollows.objects.create(
user_subject=self.remote_user, user_object=self.local_user)
activity = {
"type": "Undo",
"id": "bleh",
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"cc": ["https://example.com/user/mouse/followers"],
'actor': self.remote_user.remote_id,
"@context": "https://www.w3.org/ns/activitystreams",
"object": {
"id": "https://example.com/users/rat/follows/123",
"id": rel.remote_id,
"type": "Follow",
"actor": "https://example.com/users/rat",
"object": "https://example.com/user/mouse"
}
}
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.UserFollows.objects.create(
user_subject=self.remote_user, user_object=self.local_user)
self.assertEqual(self.remote_user, self.local_user.followers.first())
views.inbox.activity_task(activity)
@ -493,13 +497,16 @@ class Inbox(TestCase):
activity = {
'id': 'https://example.com/fav/1#undo',
'type': 'Undo',
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"cc": ["https://example.com/user/mouse/followers"],
'actor': self.remote_user.remote_id,
'object': {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://example.com/fav/1',
'actor': 'https://example.com/users/rat',
'type': 'Like',
'published': 'Mon, 25 May 2020 19:31:20 GMT',
'object': 'https://example.com/fav/1',
'object': self.status.remote_id,
}
}
models.Favorite.objects.create(
@ -557,17 +564,21 @@ class Inbox(TestCase):
def test_handle_unboost(self):
''' undo a boost '''
boost = models.Boost.objects.create(
boosted_status=self.status, user=self.remote_user)
activity = {
'type': 'Undo',
'actor': 'hi',
'id': 'bleh',
"to": ["https://www.w3.org/ns/activitystreams#public"],
"cc": ["https://example.com/user/mouse/followers"],
'object': {
'type': 'Announce',
'id': '%s/boost' % self.status.remote_id,
'id': boost.remote_id,
'actor': self.local_user.remote_id,
'object': self.status.to_activity(),
'object': self.status.remote_id,
}
}
models.Boost.objects.create(
boosted_status=self.status, user=self.remote_user)
views.inbox.activity_task(activity)
@ -695,12 +706,18 @@ class Inbox(TestCase):
self.assertEqual(block.user_subject, self.remote_user)
self.assertEqual(block.user_object, self.local_user)
activity = {'type': 'Undo', 'object': {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://example.com/9e1f41ac-9ddd-4159",
"type": "Block",
"actor": "https://example.com/users/rat",
"object": "https://example.com/user/mouse"
activity = {
'type': 'Undo',
'actor': 'hi',
'id': 'bleh',
"to": ["https://www.w3.org/ns/activitystreams#public"],
"cc": ["https://example.com/user/mouse/followers"],
'object': {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://example.com/9e1f41ac-9ddd-4159",
"type": "Block",
"actor": "https://example.com/users/rat",
"object": "https://example.com/user/mouse"
}}
views.inbox.activity_task(activity)
self.assertFalse(models.UserBlocks.objects.exists())

View file

@ -56,7 +56,7 @@ def activity_task(activity_json):
try:
activity = activitypub.parse(activity_json)
except activitypub.ActivitySerializerError:
return
raise#return
# cool that worked, now we should do the action described by the type
# (create, update, delete, etc)