Skip to content

Commit

Permalink
activitypub.postprocess_as2: fix de-duping image attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed May 19, 2024
1 parent 7cf8a77 commit ec350e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 4 additions & 2 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,10 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
imgs = util.get_list(obj_or_activity, 'image')
if imgs:
atts = obj_or_activity['attachment']
atts.extend((img if isinstance(img, dict) else {'url': img})
for img in imgs if img not in atts)
for img in imgs:
if isinstance(img, str):
img = {'url': img}
add(atts, img)

# cc target's author(s), recipients, mentions
# https://www.w3.org/TR/activitystreams-vocabulary/#audienceTargeting
Expand Down
24 changes: 21 additions & 3 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1950,15 +1950,33 @@ def test_postprocess_as2_multiple_image(self):
'image': [{'url': 'http://r/foo'}, {'url': 'http://r/bar'}],
}))

def test_postprocess_as2_image_bare_string_url(self):
def test_postprocess_as2_object_image_bare_string_url(self):
self.assert_equals({
'id': 'http://localhost/r/xyz',
'attachment': [{'url': 'http://r/foo'}],
'image': ['http://r/foo'],
'image': 'http://r/foo',
'to': [as2.PUBLIC_AUDIENCE],
}, postprocess_as2({
'id': 'xyz',
'image': ['http://r/foo'],
'image': 'http://r/foo',
}))

def test_postprocess_as2_create_activity_image_bare_string_url(self):
self.assert_equals({
'type': 'Create',
'object': {
'id': 'http://localhost/r/xyz',
'attachment': [{'url': 'http://r/foo'}],
'image': ['http://r/foo'],
'to': [as2.PUBLIC_AUDIENCE],
},
'to': [as2.PUBLIC_AUDIENCE],
}, postprocess_as2({
'type': 'Create',
'object': {
'id': 'xyz',
'image': ['http://r/foo'],
},
}))

def test_postprocess_as2_note(self):
Expand Down

0 comments on commit ec350e7

Please sign in to comment.