From 47b90c890e1ef7a06ec3deb0bfe97e69a2365c4b Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 19 Jul 2023 19:39:22 -1000 Subject: [PATCH] /feed: load author/actor if it's in a separate Object fixes #419. thanks again @gRegorLove! --- pages.py | 13 +++++++++++++ tests/test_pages.py | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pages.py b/pages.py index 282030d..b7ef5fa 100644 --- a/pages.py +++ b/pages.py @@ -4,6 +4,7 @@ import logging import os from flask import g, render_template, request +from google.cloud import ndb from google.cloud.ndb.query import AND, OR from google.cloud.ndb.stats import KindStat from granary import as1, as2, atom, microformats2, rss @@ -145,6 +146,18 @@ 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) + 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 + actor = { 'displayName': id, 'url': g.user.web_url(), diff --git a/tests/test_pages.py b/tests/test_pages.py index 255d9fe..3aee572 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -1,4 +1,5 @@ """Unit tests for pages.py.""" +from google.cloud import ndb from granary import atom, microformats2, rss from granary.tests.test_as1 import ( ACTOR, @@ -12,6 +13,7 @@ from .testutil import Fake, TestCase from activitypub import ActivityPub from models import Object, Follower +from web import Web from .test_web import ACTOR_AS2, REPOST_AS2 @@ -251,10 +253,24 @@ class PagesTest(TestCase): def test_feed_html(self): self.add_objects() + + # note with author in separate Object + note_2 = { + 'objectType': 'note', + 'content': 'foo', + 'author': 'fake:alice', + } + alice = { + 'displayName': 'Ms Alice Macbeth', + } + self.store_object(id='z', feed=[ndb.Key(Web, 'user.com')], our_as1=note_2) + self.store_object(id='fake:alice', our_as1=alice) + got = self.client.get('/web/user.com/feed') self.assert_equals(200, got.status_code) - self.assert_equals(self.EXPECTED, + self.assert_equals(self.EXPECTED + ['foo'], contents(microformats2.html_to_activities(got.text))) + self.assertIn('Ms Alice Macbeth', got.text) def test_feed_atom_empty(self): got = self.client.get('/web/user.com/feed?format=atom')