Fixes unfollow
This commit is contained in:
parent
b57a86d4e2
commit
d81bfb6573
|
@ -85,11 +85,14 @@ class ActivityObject:
|
||||||
# figure out the right model -- wish I had a better way for this
|
# figure out the right model -- wish I had a better way for this
|
||||||
model = model or get_model_from_type(self.type)
|
model = model or get_model_from_type(self.type)
|
||||||
|
|
||||||
if hasattr(model, 'ignore_activity') and model.ignore_activity(self):
|
# only reject statuses if we're potentially creating them
|
||||||
return instance
|
if allow_create and \
|
||||||
|
hasattr(model, 'ignore_activity') and model.ignore_activity(self):
|
||||||
|
return None
|
||||||
|
|
||||||
# check for an existing instance
|
# check for an existing instance
|
||||||
instance = instance or model.find_existing(self.serialize())
|
instance = instance or model.find_existing(self.serialize())
|
||||||
|
|
||||||
if not instance and not allow_create:
|
if not instance and not allow_create:
|
||||||
# so that we don't create when we want to delete or update
|
# so that we don't create when we want to delete or update
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
''' undo wrapper activity '''
|
''' undo wrapper activity '''
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from django.apps import apps
|
||||||
|
|
||||||
from .base_activity import ActivityObject, Signature, resolve_remote_id
|
from .base_activity import ActivityObject, Signature, resolve_remote_id
|
||||||
from .book import Edition
|
from .book import Edition
|
||||||
|
@ -59,7 +60,12 @@ class Undo(Verb):
|
||||||
|
|
||||||
def action(self):
|
def action(self):
|
||||||
''' find and remove the activity object '''
|
''' 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()
|
obj.delete()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ class UserRelationship(BookWyrmModel):
|
||||||
return '%s#%s/%d' % (base_path, status, self.id)
|
return '%s#%s/%d' % (base_path, status, self.id)
|
||||||
|
|
||||||
|
|
||||||
class UserFollows(UserRelationship):
|
class UserFollows(ActivitypubMixin, UserRelationship):
|
||||||
''' Following a user '''
|
''' Following a user '''
|
||||||
status = 'follows'
|
status = 'follows'
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ class Inbox(TestCase):
|
||||||
'type': 'Create',
|
'type': 'Create',
|
||||||
'actor': 'hi',
|
'actor': 'hi',
|
||||||
"to": [
|
"to": [
|
||||||
"https://www.w3.org/ns/activitystreams#Public"
|
"https://www.w3.org/ns/activitystreams#public"
|
||||||
],
|
],
|
||||||
"cc": [
|
"cc": [
|
||||||
"https://example.com/user/mouse/followers"
|
"https://example.com/user/mouse/followers"
|
||||||
|
@ -296,19 +296,23 @@ class Inbox(TestCase):
|
||||||
|
|
||||||
def test_handle_unfollow(self):
|
def test_handle_unfollow(self):
|
||||||
''' remove a relationship '''
|
''' 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 = {
|
activity = {
|
||||||
"type": "Undo",
|
"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",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
"object": {
|
"object": {
|
||||||
"id": "https://example.com/users/rat/follows/123",
|
"id": rel.remote_id,
|
||||||
"type": "Follow",
|
"type": "Follow",
|
||||||
"actor": "https://example.com/users/rat",
|
"actor": "https://example.com/users/rat",
|
||||||
"object": "https://example.com/user/mouse"
|
"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())
|
self.assertEqual(self.remote_user, self.local_user.followers.first())
|
||||||
|
|
||||||
views.inbox.activity_task(activity)
|
views.inbox.activity_task(activity)
|
||||||
|
@ -493,13 +497,16 @@ class Inbox(TestCase):
|
||||||
activity = {
|
activity = {
|
||||||
'id': 'https://example.com/fav/1#undo',
|
'id': 'https://example.com/fav/1#undo',
|
||||||
'type': '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': {
|
'object': {
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
'id': 'https://example.com/fav/1',
|
'id': 'https://example.com/fav/1',
|
||||||
'actor': 'https://example.com/users/rat',
|
'actor': 'https://example.com/users/rat',
|
||||||
'type': 'Like',
|
'type': 'Like',
|
||||||
'published': 'Mon, 25 May 2020 19:31:20 GMT',
|
'published': 'Mon, 25 May 2020 19:31:20 GMT',
|
||||||
'object': 'https://example.com/fav/1',
|
'object': self.status.remote_id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
models.Favorite.objects.create(
|
models.Favorite.objects.create(
|
||||||
|
@ -557,17 +564,21 @@ class Inbox(TestCase):
|
||||||
|
|
||||||
def test_handle_unboost(self):
|
def test_handle_unboost(self):
|
||||||
''' undo a boost '''
|
''' undo a boost '''
|
||||||
|
boost = models.Boost.objects.create(
|
||||||
|
boosted_status=self.status, user=self.remote_user)
|
||||||
activity = {
|
activity = {
|
||||||
'type': 'Undo',
|
'type': 'Undo',
|
||||||
|
'actor': 'hi',
|
||||||
|
'id': 'bleh',
|
||||||
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
||||||
|
"cc": ["https://example.com/user/mouse/followers"],
|
||||||
'object': {
|
'object': {
|
||||||
'type': 'Announce',
|
'type': 'Announce',
|
||||||
'id': '%s/boost' % self.status.remote_id,
|
'id': boost.remote_id,
|
||||||
'actor': self.local_user.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)
|
views.inbox.activity_task(activity)
|
||||||
|
|
||||||
|
|
||||||
|
@ -695,7 +706,13 @@ class Inbox(TestCase):
|
||||||
|
|
||||||
self.assertEqual(block.user_subject, self.remote_user)
|
self.assertEqual(block.user_subject, self.remote_user)
|
||||||
self.assertEqual(block.user_object, self.local_user)
|
self.assertEqual(block.user_object, self.local_user)
|
||||||
activity = {'type': 'Undo', 'object': {
|
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",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
"id": "https://example.com/9e1f41ac-9ddd-4159",
|
"id": "https://example.com/9e1f41ac-9ddd-4159",
|
||||||
"type": "Block",
|
"type": "Block",
|
||||||
|
|
|
@ -56,7 +56,7 @@ def activity_task(activity_json):
|
||||||
try:
|
try:
|
||||||
activity = activitypub.parse(activity_json)
|
activity = activitypub.parse(activity_json)
|
||||||
except activitypub.ActivitySerializerError:
|
except activitypub.ActivitySerializerError:
|
||||||
return
|
raise#return
|
||||||
|
|
||||||
# cool that worked, now we should do the action described by the type
|
# cool that worked, now we should do the action described by the type
|
||||||
# (create, update, delete, etc)
|
# (create, update, delete, etc)
|
||||||
|
|
Loading…
Reference in New Issue