Skip to content

Commit

Permalink
bluesky.from_as1: if post text gets truncated, add an external embed …
Browse files Browse the repository at this point in the history
…to the original post

for snarfed/bridgy-fed#986
  • Loading branch information
snarfed committed May 14, 2024
1 parent 60af91f commit 0bfbbdd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Changelog
* `from_as1`:
* Add hashtag, mention, block, and flag support. Interpret `tags` with missing `objectType` as hashtags.
* Guess missing indices in facets based on content text. Otherwise, if we still don't know a facet's indices, discard it.
* If an output string value is longer than its ``maxGraphemes`` or ``maxLength`` in its lexicon, it's truncated with an ```` ellipsis character at the end in order to fit.
* If an output string value is longer than its ``maxGraphemes`` or ``maxLength`` in its lexicon, truncate it with an ```` ellipsis character at the end in order to fit. If this happens to post text, include a link embed pointing to the original post.
* Populate `reply.root` properly in reply posts ([snarfed/bridgy#1696](https://github.com/snarfed/bridgy/issues/1696)).
* Support `lexrpc.Client` as well as `Bluesky` for `client` kwarg.
* `from_as1_to_strong_ref`:
Expand Down
17 changes: 14 additions & 3 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,9 @@ def from_as1(obj, out_type=None, blobs=None, client=None):
# convert text from HTML and truncate
src = Bluesky('unused')
content = obj.get('content')
text = obj.get('summary') or content or obj.get('name') or ''
text = src.truncate(html_to_text(text), None, OMIT_LINK)
full_text = html_to_text(obj.get('summary') or content or obj.get('name') or '')
text = src.truncate(full_text, None, OMIT_LINK)
truncated = text != full_text

facets = []
# convert index-based tags to facets
Expand Down Expand Up @@ -667,7 +668,17 @@ def from_as1(obj, out_type=None, blobs=None, client=None):
# article/note attachments
record_embed = record_record_embed = external_embed = external_record_embed = None

for att in util.get_list(obj, 'attachments'):
attachments = util.get_list(obj, 'attachments')
if truncated:
if url := as1.get_url(obj):
# override attachments, use one link to original post instead
attachments = [{
'objectType': 'link',
'url': url,
'displayName': f'Original post on {util.domain_from_link(url)}',
}]

for att in attachments:
if not att.get('objectType') in ('article', 'link', 'note'):
continue

Expand Down
21 changes: 21 additions & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,27 @@ def test_from_as1_post_without_tag_indices(self):
# no facet
self.assert_equals(POST_BSKY, from_as1(post_as))

@patch.object(Bluesky, 'TRUNCATE_TEXT_LENGTH', 12)
def test_from_as1_post_truncate_adds_link_embed(self):
self.assert_equals({
'$type': 'app.bsky.feed.post',
'text': 'more than…',
'createdAt': '2022-01-02T03:04:05.000Z',
'embed': {
'$type': 'app.bsky.embed.external',
'external': {
'$type': 'app.bsky.embed.external#external',
'description': '',
'title': 'Original post on my.inst',
'uri': 'http://my.inst/post',
},
},
}, from_as1({
'objectType': 'note',
'url': 'http://my.inst/post',
'content': 'more than ten chars long',
}))

def test_from_as1_tag_without_url(self):
self.assert_equals(POST_BSKY, from_as1({
**POST_AS,
Expand Down

0 comments on commit 0bfbbdd

Please sign in to comment.