From 215578f33fed4d0e7151e23831e926f65079b801 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Thu, 12 Oct 2023 10:37:44 -0700 Subject: [PATCH] render notifications feeds as snippets, not whole activities --- pages.py | 16 +++++++++++++--- tests/test_pages.py | 13 ++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pages.py b/pages.py index 4c88674..b3cf309 100644 --- a/pages.py +++ b/pages.py @@ -136,7 +136,7 @@ def notifications(protocol, id): format = request.args.get('format') if format: - return serve_feed(objects=objects, format=format, + return serve_feed(objects=objects, format=format, as_snippets=True, title=f'Bridgy Fed notifications for {id}') # notifications tab UI page @@ -181,13 +181,15 @@ def feed(protocol, id): title=f'Bridgy Fed feed for {id}') -def serve_feed(*, objects, format, title): +def serve_feed(*, objects, format, title, as_snippets=False): """Generates a feed based on :class:`Object`s. Args: objects (sequence of models.Object) format (str): ``html``, ``atom``, or ``rss`` title (str) + as_snippets (bool): if True, render short snippets for objects instead of + full contents Returns: str or (str, dict) tuple: Flask response @@ -195,7 +197,15 @@ def serve_feed(*, objects, format, title): if format not in ('html', 'atom', 'rss'): error(f'format {format} not supported; expected html, atom, or rss') - activities = [obj.as1 for obj in objects if not obj.deleted] + if as_snippets: + activities = [{ + 'objectType': 'note', + 'content': f'{obj.actor_link(image=False)} {obj.phrase} {obj.content}', + 'content_is_html': True, + 'updated': obj.updated.isoformat(), + } for obj in objects if not obj.deleted] + else: + activities = [obj.as1 for obj in objects if not obj.deleted] # hydrate authors, actors, objects from stored Objects fields = 'author', 'actor', 'object' diff --git a/tests/test_pages.py b/tests/test_pages.py index 092c846..410b97b 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -34,6 +34,11 @@ def contents(activities): class PagesTest(TestCase): EXPECTED = contents([COMMENT, MENTION, NOTE]) + EXPECTED_SNIPPETS = [ + 'Dr. Eve replied a comment', + 'tag:fake.com:44... posted a mention', + 'tag:fake.com:44... posted my note', + ] def setUp(self): super().setUp() @@ -196,20 +201,22 @@ class PagesTest(TestCase): got = self.client.get('/web/user.com/notifications?format=rss') self.assert_equals(200, got.status_code) self.assert_equals(rss.CONTENT_TYPE, got.headers['Content-Type']) - self.assert_equals(self.EXPECTED, contents(rss.to_activities(got.text))) + self.assert_equals(self.EXPECTED_SNIPPETS, + contents(rss.to_activities(got.text))) def test_notifications_atom(self): self.add_objects() got = self.client.get('/web/user.com/notifications?format=atom') self.assert_equals(200, got.status_code) self.assert_equals(atom.CONTENT_TYPE, got.headers['Content-Type']) - self.assert_equals(self.EXPECTED, contents(atom.atom_to_activities(got.text))) + self.assert_equals(self.EXPECTED_SNIPPETS, + contents(atom.atom_to_activities(got.text))) def test_notifications_html(self): self.add_objects() got = self.client.get('/web/user.com/notifications?format=html') self.assert_equals(200, got.status_code) - self.assert_equals(self.EXPECTED, + self.assert_equals(self.EXPECTED_SNIPPETS, contents(microformats2.html_to_activities(got.text))) def test_followers_fake(self):