Skip to content

Commit

Permalink
bluesky.from_as1: interpret tags with missing objectType as hashtags
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed May 1, 2024
1 parent 0add249 commit 84e3989
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Changelog
* Add hashtag facet support.
* Convert blobs in embeds to `getBlob` image URLs.
* `from_as1`:
* Add hashtag, mention, block, and flag support.
* 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.
* Populate `reply.root` properly in reply posts ([snarfed/bridgy#1696](https://github.com/snarfed/bridgy/issues/1696)).
Expand Down
5 changes: 4 additions & 1 deletion granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,11 @@ def from_as1(obj, out_type=None, blobs=None, client=None):
facets = []
# convert index-based tags to facets
for tag in util.get_list(obj, 'tags'):
name = tag.get('displayName', '').strip().lstrip('@#')
type = tag.get('objectType')
if name and not type:
type = 'hashtag'

url = tag.get('url')
if not url and type != 'hashtag':
continue
Expand All @@ -578,7 +582,6 @@ def from_as1(obj, out_type=None, blobs=None, client=None):
except (KeyError, ValueError, IndexError, TypeError):
pass

name = tag.get('displayName', '').strip().lstrip('@#')
if type == 'hashtag':
facet['features'] = [{
'$type': 'app.bsky.richtext.facet#tag',
Expand Down
1 change: 1 addition & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ def test_from_as1_tag_hashtag_guess_index(self):
note = copy.deepcopy(NOTE_AS_TAG_HASHTAG)
del note['tags'][0]['startIndex']
del note['tags'][0]['length']
del note['tags'][0]['objectType']

expected = copy.deepcopy(POST_BSKY_FACET_HASHTAG)
expected['facets'][0]['index']['byteStart'] = 4
Expand Down
9 changes: 9 additions & 0 deletions granary/tests/testdata/note_with_hashtag.as2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Note",
"id": "tag:example.com,2011:note-123",
"tag": [{
"type": "Tag",
"name": "foo"
}]
}

0 comments on commit 84e3989

Please sign in to comment.