diff --git a/protocol.py b/protocol.py index 4e6b8a8..6bcd4f4 100644 --- a/protocol.py +++ b/protocol.py @@ -800,13 +800,12 @@ class Protocol: 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 @@ -832,6 +831,18 @@ class Protocol: } 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 diff --git a/tests/test_activitypub.py b/tests/test_activitypub.py index efb2e7d..0e0fa43 100644 --- a/tests/test_activitypub.py +++ b/tests/test_activitypub.py @@ -1200,9 +1200,9 @@ class ActivityPubTest(TestCase): 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) diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 0ab7812..7a4e5f8 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -1779,7 +1779,14 @@ class ProtocolReceiveTest(TestCase): }], }, 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', @@ -1791,16 +1798,24 @@ class ProtocolReceiveTest(TestCase): 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()