drop Object.proxy_url, switch callers to ids.translate_object_id

pull/714/head
Ryan Barrett 2023-11-06 12:18:11 -08:00
rodzic 06275324fd
commit 98bb29b333
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
5 zmienionych plików z 10 dodań i 37 usunięć

6
ids.py
Wyświetl plik

@ -26,7 +26,7 @@ def translate_user_id(*, id, from_proto, to_proto):
str: the corresponding id in ``to_proto``
"""
assert id and from_proto and to_proto
assert from_proto.owns_id(id) is not False
assert from_proto.owns_id(id) is not False or from_proto.LABEL == 'ui'
parsed = urlparse(id)
if from_proto.LABEL == 'web' and parsed.path.strip('/') == '':
@ -91,7 +91,7 @@ def translate_handle(*, handle, from_proto, to_proto):
str: the corresponding handle in ``to_proto``
"""
assert handle and from_proto and to_proto
assert from_proto.owns_handle(handle) is not False
assert from_proto.owns_handle(handle) is not False or from_proto.LABEL == 'ui'
if from_proto == to_proto:
return handle
@ -138,7 +138,7 @@ def translate_object_id(*, id, from_proto, to_proto):
str: the corresponding id in ``to_proto``
"""
assert id and from_proto and to_proto
assert from_proto.owns_id(id) is not False
assert from_proto.owns_id(id) is not False or from_proto.LABEL == 'ui'
if from_proto == to_proto:
return id

Wyświetl plik

@ -705,7 +705,6 @@ class Object(StringIdModel):
Only needed for compatibility with historical URL paths, we're now back
to URL-encoding ``#``\s instead.
https://github.com/snarfed/bridgy-fed/issues/469
See :meth:`proxy_url` for the inverse.
"""
return super().get_by_id(id.replace('^^', '#'))
@ -806,27 +805,6 @@ class Object(StringIdModel):
if self.as1 and other_as1
else bool(self.as1) != bool(other_as1))
def proxy_url(self):
# TODO: replace with ids.translate_object_id?
"""Returns the Bridgy Fed proxy URL to render this post as HTML.
Note that some webmention receivers are struggling with the ``%23``\s
(URL-encoded ``#``\s) in these paths:
* https://github.com/snarfed/bridgy-fed/issues/469
* https://github.com/pfefferle/wordpress-webmention/issues/359
See :meth:`get_by_id()` for the inverse.
"""
# TODO: fix this circular import
from protocol import Protocol
id = quote(self.key.id(), safe=':/')
if not self.source_protocol:
logger.warning(f'!!! No source_protocol for {id} !!!')
protocol = PROTOCOLS.get(self.source_protocol) or Protocol
return common.subdomain_wrap(protocol, f'convert/web/{id}')
def actor_link(self, image=True, sized=False):
"""Returns a pretty HTML link with the actor's name and picture.

Wyświetl plik

@ -361,15 +361,6 @@ class ObjectTest(TestCase):
self.assertTrue(obj.activity_changed({}))
self.assertTrue(obj.activity_changed({'content': 'x'}))
def test_proxy_url(self):
obj = Object(id='abc', source_protocol='activitypub')
self.assertEqual('https://ap.brid.gy/convert/web/abc',
obj.proxy_url())
obj = Object(id='ab#c', source_protocol='ui')
self.assertEqual('https://fed.brid.gy/convert/web/ab%23c',
obj.proxy_url())
def test_put(self):
with self.assertRaises(AssertionError):
Object(id='x^^y').put()

Wyświetl plik

@ -2225,7 +2225,8 @@ class WebUtilTest(TestCase):
def test_send_no_endpoint(self, mock_get, mock_post):
mock_get.return_value = WEBMENTION_NO_REL_LINK
obj = Object(id='http://mas.to/like#ok', as2=test_activitypub.LIKE)
obj = Object(id='http://mas.to/like#ok', as2=test_activitypub.LIKE,
source_protocol='activitypub')
self.assertFalse(Web.send(obj, 'https://user.com/post'))

7
web.py
Wyświetl plik

@ -4,7 +4,7 @@ import difflib
import logging
import re
import urllib.parse
from urllib.parse import urlencode, urljoin, urlparse
from urllib.parse import quote, urlencode, urljoin, urlparse
from flask import g, redirect, render_template, request
from google.cloud import ndb
@ -23,6 +23,7 @@ from werkzeug.exceptions import BadGateway, BadRequest, HTTPException, NotFound
import common
from common import add, DOMAIN_RE
from flask_app import app, cache
from ids import translate_object_id
from models import Follower, Object, PROTOCOLS, Target, User
from protocol import Protocol
@ -331,7 +332,9 @@ class Web(User, Protocol):
logger.info(f'Skipping sending to blocklisted {url}')
return False
source_url = obj.proxy_url()
source_id = translate_object_id(
id=obj.key.id(), from_proto=PROTOCOLS[obj.source_protocol], to_proto=Web)
source_url = quote(source_id, safe=':/%+')
logger.info(f'Sending webmention from {source_url} to {url}')
endpoint = common.webmention_discover(url).endpoint