add 'activity' to Object.labels automatically in new _pre_put_hook

pull/468/head
Ryan Barrett 2023-03-27 21:51:18 -07:00
rodzic 70317e6739
commit 5493e2f297
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
5 zmienionych plików z 37 dodań i 10 usunięć

Wyświetl plik

@ -176,7 +176,7 @@ class FollowCallback(indieauth.Callback):
'actor': g.user.actor_id(),
'to': [as2.PUBLIC_AUDIENCE],
}
obj = Object(id=follow_id, domains=[domain], labels=['user', 'activity'],
obj = Object(id=follow_id, domains=[domain], labels=['user'],
source_protocol='ui', status='complete', as2=follow_as2)
activitypub.ActivityPub.send(obj, inbox)
@ -254,7 +254,7 @@ class UnfollowCallback(indieauth.Callback):
'object': follower.last_follow,
}
obj = Object(id=unfollow_id, domains=[domain], labels=['user', 'activity'],
obj = Object(id=unfollow_id, domains=[domain], labels=['user'],
source_protocol='ui', status='complete', as2=unfollow_as2)
activitypub.ActivityPub.send(obj, inbox)

Wyświetl plik

@ -333,11 +333,19 @@ class Object(StringIdModel):
created = ndb.DateTimeProperty(auto_now_add=True)
updated = ndb.DateTimeProperty(auto_now=True)
def _pre_put_hook(self):
if self.as1 and self.as1.get('objectType') == 'activity':
if 'activity' not in self.labels:
self.labels.append('activity')
else:
if 'activity' in self.labels:
self.labels.remove('activity')
def _post_put_hook(self, future):
"""Update :meth:`Protocol.get_object` cache."""
# TODO: assert that as1 id is same as key id? in pre put hook?
logger.info(f'Wrote Object {self.key.id()} {self.type} {self.status or ""} {self.labels} for {len(self.domains)} users')
if self.type != 'activity' and '#' not in self.key.id():
if '#' not in self.key.id():
get_object = protocol.Protocol.get_object
key = get_object.cache_key(protocol.Protocol, self.key.id())
get_object.cache[key] = self

Wyświetl plik

@ -228,9 +228,6 @@ class Protocol:
if obj.domains and 'feed' not in obj.labels:
obj.labels.append('feed')
if obj.as1.get('objectType') == 'activity' and 'activity' not in obj.labels:
obj.labels.append('activity')
obj.put()
return 'OK'

Wyświetl plik

@ -929,9 +929,9 @@ class ActivityPubTest(testutil.TestCase):
resp = self.post('/inbox', json=delete)
self.assertEqual(200, resp.status_code)
self.assertTrue(obj.key.get().deleted)
self.assert_object(delete['id'], as2=delete,
type='delete', source_protocol='activitypub',
status='complete')
self.assert_object(delete['id'], as2=delete, type='delete',
source_protocol='activitypub', status='complete',
labels=['activity'])
obj.deleted = True
self.assert_entities_equal(obj, Protocol.get_object.cache['http://an/obj'])
@ -955,7 +955,8 @@ class ActivityPubTest(testutil.TestCase):
self.assert_object('https://a/note', type='note', as2=obj,
source_protocol='activitypub')
self.assert_object(UPDATE_NOTE['id'], source_protocol='activitypub',
type='update', status='complete', as2=UPDATE_NOTE)
type='update', status='complete', as2=UPDATE_NOTE,
labels=['activity'])
self.assert_entities_equal(Object.get_by_id('https://a/note'),
Protocol.get_object.cache['https://a/note'])

Wyświetl plik

@ -313,6 +313,27 @@ class ObjectTest(testutil.TestCase):
def test_computed_properties_without_as1(self):
Object(id='a').put()
def test_put_adds_removes_activity_label(self):
obj = Object(id='x#y', our_as1={})
obj.put()
self.assertEqual([], obj.labels)
obj.our_as1 = {'objectType': 'activity'}
obj.put()
self.assertEqual(['activity'], obj.labels)
obj.labels = ['user']
obj.put()
self.assertEqual(['user', 'activity'], obj.labels)
obj.labels = ['activity', 'user']
obj.put()
self.assertEqual(['activity', 'user'], obj.labels)
obj.our_as1 = {'foo': 'bar'}
obj.put()
self.assertEqual(['user'], obj.labels)
class FollowerTest(testutil.TestCase):