Object.actor_link: add image kwarg

pull/676/head
Ryan Barrett 2023-10-12 10:37:22 -07:00
rodzic 8f4c353936
commit ff168231d6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 17 dodań i 5 usunięć

Wyświetl plik

@ -843,11 +843,12 @@ class Object(StringIdModel):
protocol = PROTOCOLS.get(self.source_protocol) or Protocol
return protocol.subdomain_url(f'convert/web/{id}')
def actor_link(self, sized=False):
def actor_link(self, image=True, sized=False):
"""Returns a pretty HTML link with the actor's name and picture.
Args:
sized (bool): if True, sets an explicit size (``width=32``) on the
image (bool): whether to include an ``img`` tag with the actor's picture
sized (bool): whether to set an explicit (``width=32``) size on the
profile picture ``img` tag
Returns:
@ -879,13 +880,13 @@ class Object(StringIdModel):
url = util.get_first(actor, 'url') or ''
name = actor.get('displayName') or actor.get('username') or ''
image = util.get_url(actor, 'image')
if not image:
img_url = util.get_url(actor, 'image')
if not image or not img_url:
return common.pretty_link(url, text=name, attrs=attrs)
return f"""\
<a class="h-card u-author" href="{url}" title="{name}">
<img class="profile" src="{image}" {'width="32"' if sized else ''}/>
<img class="profile" src="{img_url}" {'width="32"' if sized else ''}/>
{util.ellipsize(name, chars=40)}
</a>"""

Wyświetl plik

@ -422,6 +422,17 @@ class ObjectTest(TestCase):
obj = Object(id='x', source_protocol='fake', our_as1={'actor': 'fake:alice'})
self.assertIn('Alice', obj.actor_link())
def test_actor_link_no_image(self):
obj = Object(id='x', our_as1={
'actor': {
'displayName': 'Alice',
'image': 'foo.jpg',
},
})
self.assert_multiline_equals(
'<a class="h-card u-author" href="">Alice</a>',
obj.actor_link(image=False))
def test_actor_link_sized(self):
obj = Object(id='x', our_as1={
'actor': {