Skip to content

Commit

Permalink
Protocol.receive: following protocol user enables that protocol
Browse files Browse the repository at this point in the history
for #880
  • Loading branch information
snarfed committed Apr 18, 2024
1 parent d368857 commit 8bcae4c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
23 changes: 17 additions & 6 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,12 @@ def receive(from_cls, obj, authed_as=None, internal=False):
return 'OK', 200

@ndb.transactional()
def block():
nonlocal from_user
from_user = from_user.key.get()
remove(from_user.enabled_protocols, proto.LABEL)
from_user.put()
def disable_protocol():
user = from_user.key.get()
remove(user.enabled_protocols, proto.LABEL)
user.put()

block()
disable_protocol()
return 'OK', 200

# fetch actor if necessary
Expand All @@ -832,6 +831,18 @@ def block():
}

if obj.type == 'follow':
proto = Protocol.for_bridgy_subdomain(inner_obj_id)
if proto:
# follow of one of our protocol users; enable that protocol
@ndb.transactional()
def enable_protocol():
user = from_user.key.get()
add(user.enabled_protocols, proto.LABEL)
user.put()

enable_protocol()
return 'OK', 200

from_cls.handle_follow(obj)

# deliver to targets
Expand Down
4 changes: 2 additions & 2 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,9 +1200,9 @@ def test_inbox_unsupported_type(self, mock_head, mock_get, mock_post):
got = self.post('/user.com/inbox', json={
'@context': ['https://www.w3.org/ns/activitystreams'],
'id': 'https://xoxo.zone/users/aaronpk#follows/40',
'type': 'Block',
'type': 'Arrive',
'actor': 'https://xoxo.zone/users/aaronpk',
'object': 'http://snarfed.org/',
'object': 'http://a/place',
})
self.assertEqual(501, got.status_code)

Expand Down
25 changes: 20 additions & 5 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,14 @@ def test_resolve_ids_reply(self):
}],
}, obj.key.get().our_as1)

def test_block_protocol_user_removes_from_enabled_protocols(self):
def test_follow_and_block_protocol_user_adds_and_removes_enabled_protocols(self):
follow = {
'objectType': 'activity',
'verb': 'follow',
'id': 'eefake:follow',
'actor': 'eefake:user',
'object': 'fa.brid.gy',
}
block = {
'objectType': 'activity',
'verb': 'block',
Expand All @@ -1791,16 +1798,24 @@ def test_block_protocol_user_removes_from_enabled_protocols(self):
user = self.make_user('eefake:user', cls=ExplicitEnableFake)
self.assertFalse(ExplicitEnableFake.is_enabled_to(Fake, user))

# protocol isn't enabled yet, block is a noop
# protocol isn't enabled yet, block should be a noop
self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(block))
user = user.key.get()
self.assertEqual([], user.enabled_protocols)

# enable protocol, now block should remove it
user.enabled_protocols = ['fake']
user.put()
# follow should add to enabled_protocols
self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(follow))
user = user.key.get()
self.assertEqual(['fake'], user.enabled_protocols)
self.assertTrue(ExplicitEnableFake.is_enabled_to(Fake, user))

# another follow should be a noop
follow['id'] += '2'
self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(follow))
user = user.key.get()
self.assertEqual(['fake'], user.enabled_protocols)

# block should remove from enabled_protocols
block['id'] += '2'
self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(block))
user = user.key.get()
Expand Down

0 comments on commit 8bcae4c

Please sign in to comment.