Skip to content

Commit

Permalink
ATProto._convert: bug fix for when fetching a link preview gets no data
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jan 16, 2025
1 parent 8796b9e commit 19ffd30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def fetch_blob(url, blob_field, name, check_size=True, check_type=True):
logger.warning(e)
continue

if link:
if link and link.as1:
if img := util.get_url(link.as1, 'image'):
props = appview.defs['app.bsky.embed.external#external']['properties']
fetch_blob(img, props, name='thumb',
Expand Down
38 changes: 30 additions & 8 deletions tests/test_atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,15 +1575,15 @@ def test_send_note_existing_repo(self, mock_create_task):
<meta property="og:title" content="Titull" />
<meta property="og:description" content="Descrypshun" />
</head>
</html>""", url='http://orig.co/inal'),
</html>""", url='http://orig.co/post'),
requests_response('blob contents', content_type='image/png'),
])
def test_send_note_first_link_preview_embed(self, _, __):
user = self.make_user_and_repo()

obj = Object(id='fake:post', source_protocol='fake', our_as1={
**NOTE_AS,
'content': 'My <a href="http://orig.co/inal">original</a> post',
'content': 'My <a href="http://orig.co/post">original</a> post',
})
self.assertTrue(ATProto.send(obj, 'https://bsky.brid.gy'))

Expand All @@ -1593,14 +1593,14 @@ def test_send_note_first_link_preview_embed(self, _, __):
last_tid = arroba.util.int_to_tid(arroba.util._tid_ts_last)
self.assert_equals({
**NOTE_BSKY,
'bridgyOriginalText': 'My <a href="http://orig.co/inal">original</a> post',
'bridgyOriginalText': 'My <a href="http://orig.co/post">original</a> post',
'embed': {
'$type': 'app.bsky.embed.external',
'external': {
'$type': 'app.bsky.embed.external#external',
'description': 'Descrypshun',
'title': 'Titull',
'uri': 'http://orig.co/inal',
'uri': 'http://orig.co/post',
'thumb': {
'$type': 'blob',
'mimeType': 'image/png',
Expand All @@ -1611,10 +1611,32 @@ def test_send_note_first_link_preview_embed(self, _, __):
},
}, repo.get_record('app.bsky.feed.post', last_tid), ignore=['facets'])

@patch.object(tasks_client, 'create_task', return_value=Task(name='my task'))
def test_send_note_link_preview_existing_object_no_data(self, _):
user = self.make_user_and_repo()

# no data!
Object(id='http://orig.co/post', source_protocol='web').put()

obj = Object(id='fake:post', source_protocol='fake', our_as1={
**NOTE_AS,
'content': 'My <a href="http://orig.co/post">original</a> post',
})
self.assertTrue(ATProto.send(obj, 'https://bsky.brid.gy'))

# check repo, record
did = user.key.get().get_copy(ATProto)
repo = self.storage.load_repo(did)
last_tid = arroba.util.int_to_tid(arroba.util._tid_ts_last)
self.assert_equals({
**NOTE_BSKY,
'bridgyOriginalText': 'My <a href="http://orig.co/post">original</a> post',
}, repo.get_record('app.bsky.feed.post', last_tid), ignore=['facets'])

@patch.object(tasks_client, 'create_task', return_value=Task(name='my task'))
@patch('requests.get', side_effect=[
requests_response(f'<html><head><title>A poast</title></head></html>',
url='http://orig.co/inal'),
url='http://orig.co/post'),
])
def test_send_note_link_preview_non_web_url(self, mock_get, mock_create_task):
user = self.make_user_and_repo()
Expand Down Expand Up @@ -1664,7 +1686,7 @@ def test_send_note_link_preview_blocklisted_domain(self, mock_get, __):
<meta property="og:title" content="Titull" />
<meta property="og:description" content="Descrypshun" />
</head>
</html>""", url='http://orig.co/inal'),
</html>""", url='http://orig.co/post'),
requests_response('blob contents', content_type='image/png'),
])
@patch.dict(
Expand All @@ -1676,7 +1698,7 @@ def test_send_note_truncated_original_post_embed_overrides_first_link_preview(

obj = Object(id='fake:post', source_protocol='fake', our_as1={
**NOTE_AS,
'content': 'My <a href="http://orig.co/inal">original</a> poaaast',
'content': 'My <a href="http://orig.co/post">original</a> poaaast',
})
self.assertTrue(ATProto.send(obj, 'https://bsky.brid.gy'))

Expand All @@ -1687,7 +1709,7 @@ def test_send_note_truncated_original_post_embed_overrides_first_link_preview(
self.assert_equals({
**NOTE_BSKY,
'text': 'My original […]',
'bridgyOriginalText': 'My <a href="http://orig.co/inal">original</a> poaaast',
'bridgyOriginalText': 'My <a href="http://orig.co/post">original</a> poaaast',
'embed': {
'$type': 'app.bsky.embed.external',
'external': {
Expand Down

0 comments on commit 19ffd30

Please sign in to comment.