Skip to content

Commit

Permalink
bluesky.to_as1: add support for app.bsky.embed.record[WithMedia]
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jun 17, 2024
1 parent 3720166 commit 92a35db
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ _Non-breaking changes:_
* `bluesky`:
* `to_as1`:
* Add support for:
* `app.bsky.embed.record`
* `app.bsky.embed.recordWithMedia`
* `app.bsky.feed.defs#notFoundPost`
* `app.bsky.feed.generator`
* `app.bsky.graph.block`
Expand Down
26 changes: 16 additions & 10 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,10 +1149,9 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
elif embed_type in ('app.bsky.embed.external', 'app.bsky.embed.record'):
ret['attachments'] = [to_as1(embed, **kwargs)]
elif embed_type == 'app.bsky.embed.recordWithMedia':
# TODO
# ret['attachments'] = [to_as1(embed.get('record', {}).get('record'),
# type='com.atproto.repo.strongRef', **kwargs)]
ret['attachments'] = []
ret['attachments'] = [to_as1(embed, **kwargs)]
# TODO: stop reaching inside and do this in the to_as1 call instead?
# need to make it return both the quote post attachment and the image.
media = embed.get('media')
media_type = media.get('$type')
if media_type == 'app.bsky.embed.external':
Expand Down Expand Up @@ -1235,14 +1234,21 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
ret['image'] = thumb

elif type == 'app.bsky.embed.record':
return None
# TODO
# return (to_as1(record, **kwargs, type='com.atproto.repo.strongRef')
# if record else None)
at_uri = to_as1(obj.get('record'), type='com.atproto.repo.strongRef', **kwargs)
if not at_uri:
return None
ret = {
'objectType': 'note',
'id': at_uri,
'url': at_uri_to_web_url(at_uri),
}

elif type == 'app.bsky.embed.recordWithMedia':
ret = to_as1(obj.get('record'), type='app.bsky.embed.record', **kwargs)
# TODO: move media handling here from app.bsky.feed.post embed block above

elif type == 'app.bsky.embed.record#view':
record = obj.get('record')
return to_as1(record, **kwargs) if record else None
return to_as1(obj.get('record'), type='app.bsky.embed.record', **kwargs)

elif type in ('app.bsky.embed.record#viewNotFound',
'app.bsky.embed.record#viewBlocked',
Expand Down
55 changes: 55 additions & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,61 @@ def test_to_as1_embed_with_blobs(self):
self.assert_equals(
post_as, to_as1(post_bsky, repo_did='did:plc:foo', repo_handle='han.dull'))

def test_to_as1_embed_record(self):
self.assert_equals({
'objectType': 'note',
'content': 'something to say',
'attachments': [{
'objectType': 'note',
'id': 'at://did:alice/app.bsky.feed.post/tid',
'url': 'https://bsky.app/profile/did:alice/post/tid',
}],
}, to_as1({
'$type': 'app.bsky.feed.post',
'text': 'something to say',
'embed': {
'$type': 'app.bsky.embed.record',
'record': {
'uri': 'at://did:alice/app.bsky.feed.post/tid',
'cid': 'my-syd',
},
},
}))

def test_to_as1_embed_record_with_media(self):
self.assert_equals({
'objectType': 'note',
'content': 'something to say',
'author': 'did:plc:foo',
'attachments': [{
'objectType': 'note',
'id': 'at://did:alice/app.bsky.feed.post/tid',
'url': 'https://bsky.app/profile/did:alice/post/tid',
}],
'image': [NEW_BLOB_URL],
}, to_as1({
'$type': 'app.bsky.feed.post',
'text': 'something to say',
'embed': {
'$type': 'app.bsky.embed.recordWithMedia',
'record': {
'$type': 'app.bsky.embed.record',
'record': {
'uri': 'at://did:alice/app.bsky.feed.post/tid',
'cid': 'my-syd',
},
},
'media': {
'$type': 'app.bsky.embed.images',
'images': [{
'$type': 'app.bsky.embed.images#image',
'alt': '',
'image': NEW_BLOB,
}]
}
},
}, repo_did='did:plc:foo'))

def test_to_as1_embed_block(self):
self.assertIsNone(to_as1({
'$type': 'app.bsky.embed.record#viewBlocked',
Expand Down

0 comments on commit 92a35db

Please sign in to comment.