From eb4e11ae3d4594adce4ab0a60bbf2a7e5edb9538 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Mon, 4 Dec 2023 10:54:49 -0800 Subject: [PATCH] Web: add superfeedr_subscribed prop, populate it in maybe_superfeedr_subscribe for #550 --- tests/test_web.py | 12 ++++++++++-- web.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/test_web.py b/tests/test_web.py index 884d763..0d7af06 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -1771,8 +1771,9 @@ class WebTest(TestCase): self.assertEqual(400, got.status_code) @patch('oauth_dropins.webutil.appengine_info.LOCAL_SERVER', False) - def test_superfeedr_subscribe(self, mock_get, mock_post): - web.superfeedr_subscribe(self.user) + def test_maybe_superfeedr_subscribe(self, mock_get, mock_post): + self.assertFalse(self.user.superfeedr_subscribed) + web.maybe_superfeedr_subscribe(self.user) self.assert_req(mock_post, SUPERFEEDR_PUSH_API, data={ 'hub.mode': 'subscribe', 'hub.topic': 'https://user.com/feed', @@ -1780,6 +1781,13 @@ class WebTest(TestCase): 'format': 'atom', 'retrieve': 'true', }, auth=ANY) + self.assertTrue(self.user.key.get().superfeedr_subscribed) + + def test_maybe_superfeedr_subscribe_already_subscribed(self, mock_get, mock_post): + self.user.superfeedr_subscribed = True + self.user.put() + web.maybe_superfeedr_subscribe(self.user) + # should be a noop def _test_verify(self, redirects, hcard, actor, redirects_error=None): self.user.has_redirects = False diff --git a/web.py b/web.py index 3a026c3..5bebbbd 100644 --- a/web.py +++ b/web.py @@ -95,6 +95,7 @@ class Web(User, Protocol): redirects_error = ndb.TextProperty() has_hcard = ndb.BooleanProperty() last_webmention_in = ndb.DateTimeProperty(tzinfo=timezone.utc) + superfeedr_subscribed = ndb.BooleanProperty(default=False) # Originally, BF served Web users' AP actor ids on fed.brid.gy, eg # https://fed.brid.gy/snarfed.org . When we started adding new protocols, we @@ -591,12 +592,16 @@ def webmention_interactive(): return redirect('/', code=302) -def superfeedr_subscribe(user): +def maybe_superfeedr_subscribe(user): """Subscribes to a user's Atom or RSS feed in Superfeedr. Args: user (Web) """ + if user.superfeedr_subscribed: + logger.info('Already subscribed via Superfeedr') + return + logger.info(f'Subscribing to {user.key.id()} via Superfeedr') if appengine_info.LOCAL_SERVER: logger.info(f"Skipping since we're local") @@ -613,7 +618,9 @@ def superfeedr_subscribe(user): 'retrieve': 'true', }) resp.raise_for_status() - return resp + + user.superfeedr_subscribed = True + user.put() # generate/check per-user token for auth?