Web.maybe_superfeedr_subscribe: add feed discovery

for #550
pull/746/head
Ryan Barrett 2023-12-04 11:13:50 -08:00
rodzic eb4e11ae3d
commit ba010e74b0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 30 dodań i 5 usunięć

Wyświetl plik

@ -1773,16 +1773,29 @@ class WebTest(TestCase):
@patch('oauth_dropins.webutil.appengine_info.LOCAL_SERVER', False)
def test_maybe_superfeedr_subscribe(self, mock_get, mock_post):
self.assertFalse(self.user.superfeedr_subscribed)
self.user.mf2 = {
**ACTOR_MF2,
'rel-urls': {
'https://foo': {'rels': ['me'], 'text': 'Ms. ☕ Baz'},
'https://foo/atom': {'rels': ['alternate'], 'type': atom.CONTENT_TYPE},
},
}
web.maybe_superfeedr_subscribe(self.user)
self.assert_req(mock_post, SUPERFEEDR_PUSH_API, data={
'hub.mode': 'subscribe',
'hub.topic': 'https://user.com/feed',
'hub.topic': 'https://foo/atom',
'hub.callback': 'http://localhost/superfeedr/notify/user.com',
'format': 'atom',
'retrieve': 'true',
}, auth=ANY)
self.assertTrue(self.user.key.get().superfeedr_subscribed)
def test_maybe_superfeedr_subscribe_no_feed(self, mock_get, mock_post):
self.user.mf2 = ACTOR_MF2 # no rel-urls
web.maybe_superfeedr_subscribe(self.user)
self.assertFalse(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()

20
web.py
Wyświetl plik

@ -9,7 +9,7 @@ from urllib.parse import quote, urlencode, urljoin, urlparse
from flask import g, redirect, render_template, request
from google.cloud import ndb
from google.cloud.ndb import ComputedProperty
from granary import as1, as2, atom, microformats2
from granary import as1, as2, atom, microformats2, rss
import mf2util
from oauth_dropins.webutil import flask_util, util
from oauth_dropins.webutil.appengine_config import tasks_client
@ -58,6 +58,7 @@ NON_TLDS = frozenset((
SUPERFEEDR_PUSH_API = 'https://push.superfeedr.com'
SUPERFEEDR_USERNAME = util.read('superfeedr_username')
SUPERFEEDR_TOKEN = util.read('superfeedr_token')
FEED_TYPES = [type.split(';')[0] for type in (atom.CONTENT_TYPE, rss.CONTENT_TYPE)]
def is_valid_domain(domain):
@ -599,10 +600,21 @@ def maybe_superfeedr_subscribe(user):
user (Web)
"""
if user.superfeedr_subscribed:
logger.info('Already subscribed via Superfeedr')
logger.info(f'User {user.key.id()} already subscribed via Superfeedr')
return
elif not user.mf2:
logger.info(f"User {user.key.id()} has no mf2, can't subscribe via Superfeedr")
return
logger.info(f'Subscribing to {user.key.id()} via Superfeedr')
for url, info in user.mf2.get('rel-urls', {}).items():
if ('alternate' in info.get('rels', [])
and info.get('type', '').split(';')[0] in FEED_TYPES):
break
else:
logger.info(f"User {user.key.id()} has no feed URL, can't subscribe")
return
logger.info(f'Subscribing to {url} via Superfeedr')
if appengine_info.LOCAL_SERVER:
logger.info(f"Skipping since we're local")
return
@ -610,7 +622,7 @@ def maybe_superfeedr_subscribe(user):
auth = HTTPBasicAuth(SUPERFEEDR_USERNAME, SUPERFEEDR_TOKEN)
resp = util.requests_post(SUPERFEEDR_PUSH_API, auth=auth, data={
'hub.mode': 'subscribe',
'hub.topic': f'{user.web_url()}feed',
'hub.topic': url,
'hub.callback': common.host_url(f'/superfeedr/notify/{user.key.id()}'),
# TODO
# 'hub.secret': 'xxx',