Skip to content

Commit

Permalink
Protocol: when disabling a protocol, also delete that protocol's copy…
Browse files Browse the repository at this point in the history
… user

for #783
  • Loading branch information
snarfed committed May 22, 2024
1 parent eb53ac0 commit f39392e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ def receive(from_cls, obj, authed_as=None, internal=False):
return 'OK', 200

from_user.disable_protocol(proto)
proto.maybe_delete_copy(from_user)
return 'OK', 200

elif obj.type == 'post':
Expand All @@ -898,6 +899,7 @@ def receive(from_cls, obj, authed_as=None, internal=False):
proto.bot_follow(from_user)
elif content == 'no':
from_user.disable_protocol(proto)
proto.maybe_delete_copy(from_user)
return 'OK', 200

# fetch actor if necessary
Expand Down Expand Up @@ -1075,6 +1077,44 @@ def bot_follow(bot_cls, user):
url=target, protocol=user.LABEL,
user=bot.key.urlsafe())

@classmethod
def maybe_delete_copy(copy_cls, user):
"""Deletes a user's copy actor in a given protocol.
...if ``copy_cls`` 's :attr:`Protocol.HAS_COPIES` is True. Otherwise,
does nothing.
TODO: this should eventually go through receive for protocols that need
to deliver to all followers' targets, eg AP.
Args:
user (User)
"""
if not copy_cls.HAS_COPIES:
return

copy_user_id = user.get_copy(copy_cls)
if not copy_user_id:
logger.warning(f"Tried to delete {user.key} copy for {copy_cls.LABEL}, which doesn't exist!")
return

now = util.now().isoformat()
delete_id = f'{copy_user_id}#delete-copy-{copy_cls.LABEL}-{now}'
delete = Object(id=delete_id, our_as1={
'id': delete_id,
'objectType': 'activity',
'verb': 'delete',
'actor': 'fake:user',
'object': copy_user_id,
})
target = Target(protocol=copy_cls.LABEL, uri=copy_cls.target_for(delete))
delete.undelivered = [target]
delete.put()

common.create_task(queue='send', obj=delete.key.urlsafe(),
url=target.uri, protocol=target.protocol,
user=user.key.urlsafe())

@classmethod
def handle_bare_object(cls, obj):
"""If obj is a bare object, wraps it in a create or update activity.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,12 @@ def test_follow_and_block_protocol_user_sets_enabled_protocols(self):
self.assertEqual([], Fake.created_for)
self.assertFalse(user.is_enabled(Fake))

# ...and delete copy actor
self.assertEqual(
[('fake:u:eefake:user#delete-copy-fake-2022-01-02T03:04:05+00:00',
'fake:u:eefake:user#delete-copy-fake-2022-01-02T03:04:05+00:00:target')],
Fake.sent)

def test_follow_bot_user_refreshes_profile(self):
# bot user
self.make_user('fa.brid.gy', cls=Web)
Expand Down Expand Up @@ -2132,6 +2138,12 @@ def test_dm_no_yes_sets_enabled_protocols(self):
self.assertEqual([], Fake.created_for)
self.assertFalse(user.is_enabled(Fake))

# ...and delete copy actor
self.assertEqual(
[('fake:u:eefake:user#delete-copy-fake-2022-01-02T03:04:05+00:00',
'fake:u:eefake:user#delete-copy-fake-2022-01-02T03:04:05+00:00:target')],
Fake.sent)

@patch('protocol.LIMITED_DOMAINS', ['lim.it'])
@patch('requests.get')
def test_limited_domain_update_profile_without_follow(self, mock_get):
Expand Down

0 comments on commit f39392e

Please sign in to comment.