Skip to content

Commit

Permalink
bluesky: rename _to_external_embed to to_external_embed, don't set th…
Browse files Browse the repository at this point in the history
…umb to null

for snarfed/bridgy-fed#1591
  • Loading branch information
snarfed committed Dec 3, 2024
1 parent f4a0ac8 commit c29dd72
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
25 changes: 12 additions & 13 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def from_as1(obj, out_type=None, blobs=None, client=None,
full_text = QUOTE_RE_SUFFIX.sub('', full_text)
else:
# external link
external_record_embed = _to_external_embed(att, blobs=blobs)
external_record_embed = to_external_embed(att, blobs=blobs)
external_embed = {
'$type': f'app.bsky.embed.external#view',
'external': {
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def from_as1(obj, out_type=None, blobs=None, client=None,
ret.update({
'text': '',
'facets': None,
'embed': _to_external_embed(obj, description=full_text, blobs=blobs),
'embed': to_external_embed(obj, description=full_text, blobs=blobs),
})
if images_record_embed:
ret['embed']['external']['thumb'] = images_record_embed['images'][0]['image']
Expand Down Expand Up @@ -1076,7 +1076,7 @@ def from_as1(obj, out_type=None, blobs=None, client=None,
return ret


def _to_external_embed(obj, description=None, blobs=None):
def to_external_embed(obj, description=None, blobs=None):
"""Converts an AS1 object to a Bluesky ``app.bsky.embed.external#external``.
Args:
Expand All @@ -1089,25 +1089,24 @@ def _to_external_embed(obj, description=None, blobs=None):
url = obj.get('url') or obj.get('id')
assert url

thumb = None
if blobs:
for img in as1.get_objects(obj, 'image'):
if img_url := img.get('url') or img.get('id'):
if blob := blobs.get(img_url):
thumb = blob
break

return {
ret = {
'$type': f'app.bsky.embed.external',
'external': {
'$type': f'app.bsky.embed.external#external',
'uri': url,
'title': obj.get('displayName') or '', # required
'description': description or obj.get('summary') or obj.get('content') or '',
'thumb': thumb,
}
}

if blobs:
for img in as1.get_objects(obj, 'image'):
if img_url := img.get('url') or img.get('id'):
if blob := blobs.get(img_url):
ret['external']['thumb'] = blob
break

return ret

def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
pds=DEFAULT_PDS):
Expand Down
34 changes: 34 additions & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
MAX_IMAGES,
NO_AUTHENTICATED_LABEL,
to_as1,
to_external_embed,
url_to_did_web,
web_url_to_at_uri,
)
Expand Down Expand Up @@ -1761,6 +1762,39 @@ def test_from_as1_attachment_to_external_embed_thumb_blob(self):
}],
}, blobs={'http://pic': BLOB}))

def test_to_external_embed(self):
self.assertEqual({
'$type': 'app.bsky.embed.external',
'external': {
'$type': 'app.bsky.embed.external#external',
'uri': 'http://my/link',
'title': 'A link',
'description': 'foo bar',
'thumb': BLOB,
},
}, to_external_embed({
'objectType': 'link',
'url': 'http://my/link',
'displayName': 'A link',
'summary': 'foo bar',
'image': 'http://pic',
}, blobs={'http://pic': BLOB}))

def test_to_external_embed_no_title_description_blob(self):
self.assertEqual({
'$type': 'app.bsky.embed.external',
'external': {
'$type': 'app.bsky.embed.external#external',
'uri': 'http://my/link',
'title': '',
'description': '',
},
}, to_external_embed({
'objectType': 'link',
'url': 'http://my/link',
'image': 'http://pic',
}))

def test_from_as1_note_display_name_as_embed(self):
self.assert_equals({
'$type': 'app.bsky.feed.post',
Expand Down

0 comments on commit c29dd72

Please sign in to comment.