From ca1d0dcc016e812afc7fff543388ffbde6fd853f Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Thu, 20 Jul 2023 07:24:58 -1000 Subject: [PATCH] feeds: also load repost objects from separate Objects for #419, follows 47b90c8 --- pages.py | 23 +++++++++++++---------- tests/test_pages.py | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/pages.py b/pages.py index b7ef5fa..46c82c8 100644 --- a/pages.py +++ b/pages.py @@ -1,5 +1,6 @@ """UI pages.""" import datetime +import itertools import logging import os @@ -146,17 +147,19 @@ def feed(protocol, id): .fetch(PAGE_SIZE) activities = [obj.as1 for obj in objects if not obj.deleted] - # fill in authors/actors stored in their own Objects - owners = list(filter(lambda o: isinstance(o, str), - [as1.get_owner(a) for a in activities])) - if owners: - keys = [ndb.Key(Object, id) for id in owners] - owner_objs = ndb.get_multi(keys) + # fill in authors, actors, objects stored in their own Objects + fields = 'author', 'actor', 'object' + hydrate_ids = [id for id in itertools.chain( + *[[a[f] for f in fields if isinstance(a.get(f), str)] + for a in activities])] + if hydrate_ids: + keys = [ndb.Key(Object, id) for id in hydrate_ids] + hydrated = {o.key.id(): o.as1 for o in ndb.get_multi(keys) if o} for a in activities: - if as1.get_owner(a) == owner_objs[0].key.id(): - a['author'] = a['actor'] = owner_objs.pop(0).as1 - if not owner_objs: - break + for field in fields: + id = a.get(field) + if isinstance(id, str) and hydrated.get(id): + a[field] = hydrated[id] actor = { 'displayName': id, diff --git a/tests/test_pages.py b/tests/test_pages.py index 3aee572..5e7fe6b 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -263,14 +263,29 @@ class PagesTest(TestCase): alice = { 'displayName': 'Ms Alice Macbeth', } - self.store_object(id='z', feed=[ndb.Key(Web, 'user.com')], our_as1=note_2) + user = ndb.Key(Web, 'user.com') + self.store_object(id='fake:note_2', feed=[user], our_as1=note_2) self.store_object(id='fake:alice', our_as1=alice) + # repost with object (original post) in separate Object + repost = { + 'objectType': 'activity', + 'verb': 'share', + 'object': 'fake:orig', + } + orig = { + 'objectType': 'note', + 'content': 'biff', + } + self.store_object(id='fake:repost', feed=[user], our_as1=repost) + self.store_object(id='fake:orig', our_as1=orig) + got = self.client.get('/web/user.com/feed') self.assert_equals(200, got.status_code) - self.assert_equals(self.EXPECTED + ['foo'], + self.assert_equals(self.EXPECTED + ['foo', 'biff'], contents(microformats2.html_to_activities(got.text))) self.assertIn('Ms Alice Macbeth', got.text) + self.assertIn('biff', got.text) def test_feed_atom_empty(self): got = self.client.get('/web/user.com/feed?format=atom')