Skip to content

Commit

Permalink
bluesky create: delete, preview delete
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jan 30, 2024
1 parent 0ab1b0a commit 3278fb2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 54 deletions.
35 changes: 35 additions & 0 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,41 @@ def _create(self, obj, preview=None, include_link=OMIT_LINK,
error_plain=f'Cannot publish type={type}, verb={verb} to Bluesky',
error_html=f'Cannot publish type={type}, verb={verb} to Bluesky')

def delete(self, at_uri):
"""Deletes a record.
Args:
at_uri (str): at:// URI of record delete
Returns:
CreationResult: content is dict with ``url`` and ``id`` fields
"""
match = AT_URI_PATTERN.match(at_uri)
if not match:
raise ValueError(f'Expected at:// URI, got {at_uri}')

authority, collection, rkey = match.groups()
self.client.com.atproto.repo.deleteRecord({
'repo': authority,
'collection': collection,
'rkey': rkey,
})
return creation_result({
'id': at_uri,
'url': at_uri_to_web_url(at_uri),
})

def preview_delete(self, at_uri):
"""Previews deleting a record.
Args:
at_uri (str): at:// URI of record delete
Returns:
CreationResult:
"""
url = at_uri_to_web_url(at_uri)
return creation_result(description=f'<span class="verb">delete</span> <a href="{url}">this</a>.')

def base_object(self, obj):
"""Returns the "base" Bluesky object that an object operates on.
Expand Down
98 changes: 44 additions & 54 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,18 +1337,6 @@ def test_preview_repost(self):
preview = self.bs.preview_create(REPOST_AS)
self.assertIn('<span class="verb">repost</span> <a href="https://bsky.app/profile/alice.com/post/tid">this post</a>:', preview.description)

def test_preview_create_override_truncate_text_length(self):
bs = Bluesky(INSTANCE, access_token='towkin',
user_id=ACCOUNT['id'], truncate_text_length=8)
got = m.preview_create(OBJECT)
self.assertEqual('foo ☕…', got.content)

self.expect_post(API_STATUSES, json={'status': 'foo ☕…'}, response=POST)
self.mox.ReplayAll()

result = bs.create(OBJECT)
self.assert_equals(POST, result.content, result)

def test_preview_with_media(self):
preview = self.bs.preview_create(POST_AS_IMAGES['object'])
self.assertEqual('<span class="verb">post</span>:', preview.description)
Expand Down Expand Up @@ -1393,51 +1381,53 @@ def test_create_with_media(self, mock_get, mock_post):
'record': POST_BSKY_IMAGES,
})

@patch('requests.post')
def test_create_with_too_many_media(self, mock_post):
image_urls = [f'http://my/picture/{i}' for i in range(MAX_IMAGES + 1)]
obj = {
'objectType': 'note',
'image': [{'url': url} for url in image_urls],
# duplicate images to check that they're de-duped
'attachments': [{'objectType': 'image', 'url': url} for url in image_urls],
}
# @patch('requests.post')
# def test_create_with_too_many_media(self, mock_post):
# image_urls = [f'http://my/picture/{i}' for i in range(MAX_IMAGES + 1)]
# obj = {
# 'objectType': 'note',
# 'image': [{'url': url} for url in image_urls],
# # duplicate images to check that they're de-duped
# 'attachments': [{'objectType': 'image', 'url': url} for url in image_urls],
# }

# # test preview
# preview = self.bs.preview_create(obj)
# self.assertEqual('<span class="verb">post</span>:', preview.description)
# self.assertEqual("""\
# <br /><br />\
# &nbsp; <img src="http://my/picture/0" alt="" /> \
# &nbsp; <img src="http://my/picture/1" alt="" /> \
# &nbsp; <img src="http://my/picture/2" alt="" />""",
# &nbsp; <img src="http://my/picture/3" alt="" />""",
# preview.content)

# # test create
# for i, url in enumerate(image_urls[:-1]):
# self.expect_requests_get(f'http://my/picture/{i}', 'pic')
# self.expect_post(API_MEDIA, {'id': str(i + 1)}, files={'file': b'pic'}, data={})

# self.expect_post(API_STATUSES, json={
# 'status': '',
# 'media_ids': ['0', '1', '2', '3'],
# }, response=POST)
# self.mox.ReplayAll()
# result = self.bs.create(obj)
# self.assert_equals(POST, result.content, result)

# test preview
preview = self.bs.preview_create(obj)
self.assertEqual('<span class="verb">post</span>:', preview.description)
self.assertEqual("""\
<br /><br />\
&nbsp; <img src="http://my/picture/0" alt="" /> \
&nbsp; <img src="http://my/picture/1" alt="" /> \
&nbsp; <img src="http://my/picture/2" alt="" />""",
preview.content)
@patch('requests.post')
def test_delete(self, mock_post):
mock_post.return_value = requests_response({})

# test create
for i, url in enumerate(image_urls[:-1]):
self.expect_requests_get(f'http://my/picture/{i}', 'pic')
self.expect_post(API_MEDIA, {'id': str(i + 1)}, files={'file': b'pic'}, data={})

self.expect_post(API_STATUSES, json={
'status': '',
'media_ids': ['0', '1', '2', '3'],
}, response=POST)
self.mox.ReplayAll()
result = self.bs.create(obj)
self.assert_equals(POST, result.content, result)

def test_delete(self):
self.expect_delete(API_STATUS % '456')
self.mox.ReplayAll()

result = self.bs.delete(456)
self.assert_equals({'url': 'http://foo.com/web/statuses/456'},
result.content, result)
self.assertIsNone(result.error_plain)
self.assertIsNone(result.error_html)
got = self.bs.delete('at://did:dyd/app.bsky.feed.post/abc123')
self.assert_call(mock_post, 'com.atproto.repo.deleteRecord', json={
'repo': self.bs.did,
'collection': 'app.bsky.feed.post',
'rkey': 'abc123',
})

def test_preview_delete(self):
got = self.bs.preview_delete('456')
self.assertIn('<span class="verb">delete</span> <a href="http://foo.com/web/statuses/456">this post</a>.', got.description)
got = self.bs.preview_delete('at://did:dyd/app.bsky.feed.post/abc123')
self.assertIn('<span class="verb">delete</span> <a href="https://bsky.app/profile/did:dyd/post/abc123">this</a>.', got.description)
self.assertIsNone(got.error_plain)
self.assertIsNone(got.error_html)

0 comments on commit 3278fb2

Please sign in to comment.