Skip to content

Commit

Permalink
bluesky.from_as1: bug fix: handle attachments with no id or url
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Oct 18, 2024
1 parent 222ee31 commit dc999bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ Changelog
* When a `flag` has multiple objects, use the first one that's an ATProto record.
* Handle URLs more carefully, don't add link facets with invalid `uri`s.
* Bug fix: handle HTML links with `title` in `content` correctly.
* Bug fix: handle attachments with no `id` or `url`.
* `to_as1`:
* Extract links from `app.bsky.actor.profile#description` into `url`/`urls` fields
* Bug fix: first URL (singular) goes in `url`, list of URLs goes in `urls`.
Expand Down
11 changes: 6 additions & 5 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,13 @@ def from_as1(obj, out_type=None, blobs=None, client=None,
record_embed = record_record_embed = external_embed = external_record_embed = None

for att in attachments:
if not att.get('objectType') in ('article', 'link', 'note'):
att_id = att.get('id') or ''
att_url = as1.get_url(att) or ''
if not (att.get('objectType') in ('article', 'link', 'note')
and (att_url or att_id)):
continue

id = att.get('id') or ''
att_url = att.get('url') or ''
if (id.startswith('at://') or id.startswith(Bluesky.BASE_URL) or
if (att_id.startswith('at://') or att_id.startswith(Bluesky.BASE_URL) or
att_url.startswith('at://') or att_url.startswith(Bluesky.BASE_URL)):
# quoted Bluesky post
embed = from_as1(att, **kwargs).get('post') or {}
Expand Down Expand Up @@ -995,7 +996,7 @@ def from_as1(obj, out_type=None, blobs=None, client=None,
'labels': labels,
}

if as_embed or type == 'article':
if as_embed or (type == 'article' and url):
# render articles with just an external link embed, no text
ret.update({
'text': '',
Expand Down
15 changes: 15 additions & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,21 @@ def test_from_as1_article_to_embed_with_image_blobs(self):
'image': NEW_BLOB_URL,
}, blobs={NEW_BLOB_URL: BLOB}))

def test_from_as1_attachment_without_url_isnt_embed(self):
self.assertEqual({
'$type': 'app.bsky.feed.post',
'text': 'foo bar',
'createdAt': '2022-01-02T03:04:05.000Z',
}, from_as1({
'objectType': 'article',
'displayName': 'An article',
'content': 'foo bar',
'attachments': [{
'objectType': 'note',
'content': 'a citation? or footnote? ...'
}],
}))

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

0 comments on commit dc999bf

Please sign in to comment.