Protocol.receive: following protocol user enables that protocol

for #880
pull/965/head
Ryan Barrett 2024-04-18 16:03:35 -07:00
rodzic d36885728f
commit 8bcae4c09d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 39 dodań i 13 usunięć

Wyświetl plik

@ -800,13 +800,12 @@ class Protocol:
return 'OK', 200 return 'OK', 200
@ndb.transactional() @ndb.transactional()
def block(): def disable_protocol():
nonlocal from_user user = from_user.key.get()
from_user = from_user.key.get() remove(user.enabled_protocols, proto.LABEL)
remove(from_user.enabled_protocols, proto.LABEL) user.put()
from_user.put()
block() disable_protocol()
return 'OK', 200 return 'OK', 200
# fetch actor if necessary # fetch actor if necessary
@ -832,6 +831,18 @@ class Protocol:
} }
if obj.type == 'follow': 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) from_cls.handle_follow(obj)
# deliver to targets # deliver to targets

Wyświetl plik

@ -1200,9 +1200,9 @@ class ActivityPubTest(TestCase):
got = self.post('/user.com/inbox', json={ got = self.post('/user.com/inbox', json={
'@context': ['https://www.w3.org/ns/activitystreams'], '@context': ['https://www.w3.org/ns/activitystreams'],
'id': 'https://xoxo.zone/users/aaronpk#follows/40', 'id': 'https://xoxo.zone/users/aaronpk#follows/40',
'type': 'Block', 'type': 'Arrive',
'actor': 'https://xoxo.zone/users/aaronpk', 'actor': 'https://xoxo.zone/users/aaronpk',
'object': 'http://snarfed.org/', 'object': 'http://a/place',
}) })
self.assertEqual(501, got.status_code) self.assertEqual(501, got.status_code)

Wyświetl plik

@ -1779,7 +1779,14 @@ class ProtocolReceiveTest(TestCase):
}], }],
}, obj.key.get().our_as1) }, 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 = { block = {
'objectType': 'activity', 'objectType': 'activity',
'verb': 'block', 'verb': 'block',
@ -1791,16 +1798,24 @@ class ProtocolReceiveTest(TestCase):
user = self.make_user('eefake:user', cls=ExplicitEnableFake) user = self.make_user('eefake:user', cls=ExplicitEnableFake)
self.assertFalse(ExplicitEnableFake.is_enabled_to(Fake, user)) 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)) self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(block))
user = user.key.get() user = user.key.get()
self.assertEqual([], user.enabled_protocols) self.assertEqual([], user.enabled_protocols)
# enable protocol, now block should remove it # follow should add to enabled_protocols
user.enabled_protocols = ['fake'] self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(follow))
user.put() user = user.key.get()
self.assertEqual(['fake'], user.enabled_protocols)
self.assertTrue(ExplicitEnableFake.is_enabled_to(Fake, user)) 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' block['id'] += '2'
self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(block)) self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(block))
user = user.key.get() user = user.key.get()