add Protocol.subdomain_url()

for #512, #548
deploy
Ryan Barrett 2023-06-14 14:57:59 -07:00
rodzic 435389d0b9
commit 002c6a9740
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 21 dodań i 0 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
"""Base protocol class and common code."""
import logging
import threading
from urllib.parse import urljoin
from cachetools import cached, LRUCache
from flask import g, request
@ -106,6 +107,21 @@ class Protocol:
label = domain.removesuffix(common.SUPERDOMAIN)
return PROTOCOLS.get(label)
@classmethod
def subdomain_url(cls, path=None):
"""Returns the URL for a given path on this protocol's subdomain.
Eg for the path 'foo/bar' on ActivityPub, returns
'https://ap.brid.gy/foo/bar'.
Args:
path: str
Returns:
str, URL
"""
return urljoin(f'https://{cls.ABBREV or "fed"}{common.SUPERDOMAIN}/', path)
@classmethod
def owns_id(cls, id):
"""Returns whether this protocol owns the id, or None if it's unclear.

Wyświetl plik

@ -78,6 +78,11 @@ class ProtocolTest(TestCase):
with app.test_request_context('/foo', base_url=url):
self.assertEqual(expected, Protocol.for_request(fed=Fake))
def test_subdomain_url(self):
self.assertEqual('https://fa.brid.gy/', Fake.subdomain_url())
self.assertEqual('https://fa.brid.gy/foo?bar', Fake.subdomain_url('foo?bar'))
self.assertEqual('https://fed.brid.gy/', UIProtocol.subdomain_url())
@patch('requests.get')
def test_receive_reply_not_feed_not_notification(self, mock_get):
Follower.get_or_create(to=Fake.get_or_create(id=ACTOR['id']),