start merging web.webmention_task into Protocol.receive

for #529
pull/582/head
Ryan Barrett 2023-06-27 11:39:57 -07:00
rodzic 9b1e396aa5
commit be7aa50eac
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
6 zmienionych plików z 89 dodań i 76 usunięć

Wyświetl plik

@ -441,7 +441,7 @@ class Object(StringIdModel):
# log, pruning data fields
props = self.to_dict()
for prop in 'as2', 'bsky', 'mf2':
for prop in 'as1', 'as2', 'bsky', 'mf2', 'our_as1':
if props.get(prop):
props[prop] = "..."
logger.info(f'Wrote {self.key} {props}')

Wyświetl plik

@ -348,7 +348,8 @@ class Protocol:
obj = Object.get_or_insert(id)
obj.clear()
obj.populate(source_protocol=from_cls.LABEL, **props)
if g.user and g.user not in obj.users:
obj.users.append(g.user.key)
obj.put()
logger.info(f'Got AS1: {json_dumps(obj.as1, indent=2)}')
@ -402,10 +403,6 @@ class Protocol:
if not inner_obj_id:
error("Couldn't find id of object to update")
obj.status = 'complete'
obj.put()
return 'OK'
elif obj.type == 'delete':
if not inner_obj_id:
error("Couldn't find id of object to delete")
@ -529,6 +526,9 @@ class Protocol:
Args:
obj: :class:`Object`, activity to deliver
"""
import web
return web._deliver(obj)
# extract source and targets
source = obj.as1.get('url') or obj.as1.get('id')
inner_obj = as1.get_object(obj.as1)

Wyświetl plik

@ -290,9 +290,7 @@ class ActivityPubTest(TestCase):
self.key_id_obj.put()
def assert_object(self, id, **props):
ignore = ['as2'] if 'our_as1' in props and 'as2' not in props else []
return super().assert_object(id, delivered_protocol='web',
ignore=ignore, **props)
return super().assert_object(id, delivered_protocol='web', **props)
def sign(self, path, body):
"""Constructs HTTP Signature, returns headers."""

Wyświetl plik

@ -30,20 +30,6 @@ REPLY = {
},
}
UPDATE_PROFILE = {
'objectType': 'activity',
'verb': 'update',
'id': 'fake:user#update-2022-01-02T03:04:05+00:00',
'actor': 'fake:user',
'object': {
'objectType': 'person',
'id': 'fake:user',
'displayName': 'Ms. ☕ Baz',
'urls': [{'displayName': 'Ms. ☕ Baz', 'value': 'https://user.com/'}],
'updated': '2022-01-02T03:04:05+00:00',
},
}
class ProtocolTest(TestCase):
@ -310,7 +296,13 @@ class ProtocolReceiveTest(TestCase):
self.bob = self.make_user('fake:bob', cls=Fake, obj_id='fake:bob')
def assert_object(self, id, **props):
return super().assert_object(id, delivered_protocol='fake', **props)
ignore = []
for field in 'as2', 'bsky', 'mf2':
if 'our_as1' in props and field not in props:
ignore.append(field)
return super().assert_object(id, delivered_protocol='fake',
ignore=ignore, **props)
def make_followers(self):
from_ = self.make_user(id, cls=ActivityPub, obj_as2=actor)
@ -790,30 +782,30 @@ class ProtocolReceiveTest(TestCase):
# self.assert_deliveries(mock_post, ['https://mas.to/inbox'], REPOST_AS2,
# ignore=['cc'])
# def test_inbox_like(self):
# # Fake.fetchable['fake:post'] = {
# # 'objectType': 'note',
# # }
def test_inbox_like(self):
# Fake.fetchable['fake:post'] = {
# 'objectType': 'note',
# }
# like_as1 = {
# # 'id': 'http://mas.to/like#ok',
# 'objectType': 'activity',
# 'verb': 'like',
# 'actor': 'fake:user',
# 'object': 'fake:post',
# }
# self.assertEqual('OK', Fake.receive('fake:like', our_as1=like_as1))
# self.assertEqual({'fake:post:target': like_as1}, Fake.sent)
like_as1 = {
# 'id': 'http://mas.to/like#ok',
'objectType': 'activity',
'verb': 'like',
'actor': 'fake:user',
'object': 'fake:post',
}
self.assertEqual('OK', Fake.receive('fake:like', our_as1=like_as1))
self.assertEqual({'fake:post:target': like_as1}, Fake.sent)
# self.assert_object('fake:like',
# users=[g.user.key],
# source_protocol='fake',
# status='complete',
# our_as1=like_as1,
# delivered=['fake:post:target'],
# type='like',
# labels=['notification', 'activity'],
# object_ids=['fake:post'])
self.assert_object('fake:like',
users=[g.user.key],
source_protocol='fake',
status='complete',
our_as1=like_as1,
delivered=['fake:post:target'],
type='like',
labels=['notification', 'activity'],
object_ids=['fake:post'])
# def test_like_stored_object_without_as2(self):
# Object(id='https://mas.to/toot', mf2=NOTE_MF2, source_protocol='ap').put()
@ -978,32 +970,49 @@ class ProtocolReceiveTest(TestCase):
# self.assertEqual(204, got.status_code)
# mock_post.assert_not_called()
# def test_update_profile(self):
# Follower.get_or_create(to=g.user, from_=self.alice)
# Follower.get_or_create(to=g.user, from_=self.bob)
def test_update_profile(self):
Follower.get_or_create(to=g.user, from_=self.alice)
Follower.get_or_create(to=g.user, from_=self.bob)
# id = UPDATE_PROFILE['id']
# Fake.receive(id, our_as1=UPDATE_PROFILE)
# # self.assertEqual([(UPDATE_PROFILE, 'shared:target')], Fake.sent)
id = 'fake:user#update-2022-01-02T03:04:05+00:00'
update_as1 = {
'objectType': 'activity',
'verb': 'update',
'id': id,
'actor': 'fake:user',
'object': {
'objectType': 'person',
'id': 'fake:user',
'displayName': 'Ms. ☕ Baz',
'urls': [{'displayName': 'Ms. ☕ Baz', 'value': 'https://user.com/'}],
'updated': '2022-01-02T03:04:05+00:00',
},
}
# # profile object
# self.assert_object('fake:user',
# source_protocol='fake',
# our_as1=UPDATE_PROFILE,
# type='person',
# )
Fake.receive(id, our_as1=update_as1)
# # update activity
# self.assert_object(id,
# users=[g.user.key],
# source_protocol='fake',
# status='complete',
# our_as1=UPDATE_PROFILE,
# delivered=['shared:target'],
# type='update',
# object_ids=['fake:user'],
# labels=['user', 'activity'],
# )
# profile object
self.assert_object('fake:user',
source_protocol='fake',
our_as1=update_as1['object'],
type='person',
)
# update activity
update_as1['actor'] = update_as1['object']
update_obj = self.assert_object(
id,
users=[g.user.key],
source_protocol='fake',
status='complete',
our_as1=update_as1,
delivered=['shared:target'],
type='update',
object_ids=['fake:user'],
labels=['user', 'activity'],
)
self.assertEqual([(update_obj, 'shared:target')], Fake.sent)
# def test_mention_object(self, *mocks):
# self._test_mention(
@ -1275,3 +1284,4 @@ class ProtocolReceiveTest(TestCase):
self.assertEqual('inactive', follower.key.get().status)
self.assertEqual('inactive', followee.key.get().status)
self.assertEqual('active', other.key.get().status)

Wyświetl plik

@ -66,6 +66,7 @@ class Fake(User, protocol.Protocol):
def send(cls, obj, url, log_data=True):
logger.info(f'Fake.send {url}')
cls.sent.append((obj, url))
return True
@classmethod
def fetch(cls, obj, **kwargs):
@ -311,6 +312,9 @@ class TestCase(unittest.TestCase, testutil.Asserts):
if got.mf2:
got.mf2.pop('url', None)
for target in got.delivered:
del target.key
ignore = props.pop('ignore', [])
self.assert_entities_equal(Object(id=id, **props), got,
ignore=['as1', 'created', 'expire',

15
web.py
Wyświetl plik

@ -533,7 +533,8 @@ def webmention_task():
# if source is home page, update Web user and send an actor Update to
# followers' instances
if g.user.is_web_url(obj.key.id()):
if g.user and (g.user.key.id() == obj.key.id()
or g.user.is_web_url(obj.key.id())):
obj.put()
g.user.obj = obj
g.user.put()
@ -552,12 +553,13 @@ def webmention_task():
'object': actor_as1,
})
return _deliver(obj)
def _deliver(obj):
targets = _targets(obj) # maps Target to Object or None
obj.populate(
users=[g.user.key],
source_protocol='web',
)
if not targets:
add(obj.labels, 'user')
obj.status = 'ignored'
@ -689,8 +691,7 @@ def _targets(obj):
logger.info('Finding recipients and their targets')
# if there's in-reply-to, like-of, or repost-of, they're the targets.
# otherwise, it's all followers' inboxes.
# sort so order is deterministic for tests.
# otherwise, it's all followers. sort so order is deterministic for tests.
orig_ids = sorted(as1.get_ids(obj.as1, 'inReplyTo'))
verb = obj.as1.get('verb')
if orig_ids: