Skip to content

Commit

Permalink
atom.activities_to_atom: handle image attachments without url field
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 24, 2024
1 parent bc87337 commit 23bd504
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -283,6 +283,8 @@ Changelog

### 6.3 - unreleased

* `atom`:
* `activities_to_atom`: handle image attachments without `url` field.
* `bluesky`:
* `to_as1`:
* Support `app.bsky.feed.defs#notFoundPost` records.
Expand Down
4 changes: 3 additions & 1 deletion granary/atom.py
Expand Up @@ -442,12 +442,14 @@ def _prepare_activity(a, reader=True):
if not image:
continue
url = image.get('url') or image.get('id')
if not url:
continue
parsed = urllib.parse.urlparse(url)
rest = urllib.parse.urlunparse(('', '') + parsed[2:])
img_src_re = re.compile(r"""src *= *['"] *((https?:)?//%s)?%s *['"]""" %
(re.escape(parsed.netloc),
_encode_ampersands(re.escape(rest))))
if (url and url not in image_urls_seen and
if (url not in image_urls_seen and
not img_src_re.search(obj['rendered_content'])):
children.append(microformats2.img(url))
image_urls_seen.add(url)
Expand Down
8 changes: 8 additions & 0 deletions granary/tests/test_atom.py
Expand Up @@ -413,6 +413,14 @@ def test_render_untitled_image(self):
atom.activities_to_atom([activity], test_instagram.ACTOR,
title='my title'))

def test_render_image_without_url(self):
activity = copy.deepcopy(test_instagram.ACTIVITY)
activity['object']['attachments'].append(
{'objectType': 'image', 'image': {'href': 'http://image/2'}})

# just check that we don't crash
atom.activities_to_atom([activity], test_instagram.ACTOR)

def test_render_share(self):
activity = {
'verb': 'share',
Expand Down

0 comments on commit 23bd504

Please sign in to comment.