Web.convert: call translate_ids

pull/714/head
Ryan Barrett 2023-11-03 15:52:37 -07:00
rodzic aa5c6a396e
commit 06275324fd
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
4 zmienionych plików z 39 dodań i 6 usunięć

Wyświetl plik

@ -328,10 +328,10 @@ class ActivityPub(User, Protocol):
Args:
obj (models.Object)
kwargs: passed through to :func:`postprocess_as2`
Returns:
dict: AS2 JSON
kwargs: passed through to :func:`postprocess_as2`
"""
if not obj:
return {}

Wyświetl plik

@ -413,13 +413,15 @@ class Protocol:
For example, an HTML string for :class:`Web`, or a dict with AS2 JSON
and ``application/activity+json`` for :class:`ActivityPub`.
To be implemented by subclasses.
To be implemented by subclasses. Implementations should generally call
:meth:`Protocol.translate_ids` (as their own class) before converting to
their format.
Args:
obj (models.Object):
Returns:
converted object data
converted object in the protocol's native format, often a dict
"""
raise NotImplementedError()
@ -489,11 +491,12 @@ class Protocol:
Args:
to_proto (Protocol subclass)
obj (dict): AS1 object or activity
obj (dict): AS1 object or activity (not :class:`models.Object`!)
Returns:
dict: wrapped version of ``obj``
"""
assert to_cls != Protocol
if not obj:
return obj

Wyświetl plik

@ -2285,6 +2285,34 @@ class WebUtilTest(TestCase):
</html>
""", Web.convert(obj), ignore_blanks=True)
def test_convert_translates_ids(self, *_):
self.store_object(id='http://fed/post', source_protocol='activitypub')
self.assert_multiline_in("""\
<article class="h-entry">
<span class="p-uid">https://fa.brid.gy/convert/web/fake:reply</span>
<span class="p-author h-card">
<data class="p-uid" value="fake:alice"></data>
<span class="p-name">Ms. Alice</span>
</span>
<span class="p-name"></span>
<div class="">
</div>
<a class="u-in-reply-to" href="https://ap.brid.gy/convert/web/http:/fed/post"></a>
</article>""", Web.convert(Object(our_as1={
'objectType': 'activity',
'verb': 'post',
'object': {
'id': 'fake:reply',
'objectType': 'note',
'inReplyTo': 'http://fed/post',
'author': {
'id': 'fake:alice',
'displayName': 'Ms. Alice',
},
},
})), ignore_blanks=True)
def test_target_for(self, _, __):
self.assertIsNone(Web.target_for(Object(id='x', source_protocol='web')))

6
web.py
Wyświetl plik

@ -447,8 +447,10 @@ class Web(User, Protocol):
Returns:
str:
"""
obj_as1 = obj.as1
if not obj or not obj.as1:
return ''
obj_as1 = obj.as1
from_proto = PROTOCOLS.get(obj.source_protocol)
if from_proto:
# fill in author/actor if available
@ -461,7 +463,7 @@ class Web(User, Protocol):
else:
logger.debug(f'Not hydrating actor or author due to source_protocol {obj.source_protocol}')
html = microformats2.activities_to_html([obj_as1])
html = microformats2.activities_to_html([cls.translate_ids(obj_as1)])
# add HTML meta redirect to source page. should trigger for end users in
# browsers but not for webmention receivers (hopefully).