diff --git a/models.py b/models.py index 0a3d11b..f854cb5 100644 --- a/models.py +++ b/models.py @@ -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"""\ - + {util.ellipsize(name, chars=40)} """ diff --git a/tests/test_models.py b/tests/test_models.py index fd880fd..e5350de 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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( + 'Alice', + obj.actor_link(image=False)) + def test_actor_link_sized(self): obj = Object(id='x', our_as1={ 'actor': {