Skip to content

Commit

Permalink
bluesky.from_as1: only add facets when we have their indices
Browse files Browse the repository at this point in the history
for #675
  • Loading branch information
snarfed committed Apr 5, 2024
1 parent c4377fd commit 8dd5e47
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ Changelog
* Add hashtag facet support.
* `from_as1`:
* Add hashtag and mention support.
* Guess missing indices in facets based on content text.
* Guess missing indices in facets based on content text. Otherwise, if we still don't know a facet's indices, discard it.
* Populate `reply.root` properly in reply posts ([snarfed/bridgy#1696](https://github.com/snarfed/bridgy/issues/1696)).
* Add `value` boolean kwarg to `from_as1_to_strong_ref`.
* `microformats2`:
Expand Down
14 changes: 8 additions & 6 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,16 @@ def from_as1(obj, out_type=None, blobs=None, client=None):
'byteStart': len(content[:match.start(1)].encode()),
'byteEnd': len(content[:match.end(1)].encode()),
}
else:
continue

# skip or trim this facet if it's off the end of content that got truncated
if index := facet.get('index'):
text_len = len(text.encode())
if index.get('byteStart', 0) >= text_len:
continue
if index.get('byteEnd', 0) > text_len:
index['byteEnd'] = text_len
index = facet.get('index')
text_len = len(text.encode())
if index.get('byteStart', 0) >= text_len:
continue
if index.get('byteEnd', 0) > text_len:
index['byteEnd'] = text_len

facets.append(facet)

Expand Down
71 changes: 42 additions & 29 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,28 @@
'$type': 'app.bsky.richtext.facet#mention',
'did': 'did:plc:foo',
}],
'index': {
'byteStart': 5,
'byteEnd': 12,
},
}
TAG_MENTION_DID = {
'objectType': 'mention',
'url': 'did:plc:foo',
'displayName': 'did:plc:foo',
}
TAG_MENTION_URL = {
'objectType': 'mention',
'url': 'https://bsky.app/profile/did:plc:foo',
'displayName': 'you.com',
}
NOTE_AS_TAG_MENTION ={
'objectType': 'note',
'content': 'foo @you.com bar',
'tags': [{
'objectType': 'mention',
'url': 'did:plc:foo',
'displayName': 'you.com',
**TAG_MENTION_URL,
'startIndex': 4,
'length': 8,
}]
}
POST_BSKY_FACET_MENTION = {
Expand Down Expand Up @@ -726,34 +732,46 @@ def test_from_as1_post_without_tag_indices(self):
'url': 'http://my/link',
}]

expected = {
**POST_BSKY,
'facets': [copy.deepcopy(FACET_LINK)]
}
del expected['facets'][0]['index']

self.assert_equals(expected, from_as1(post_as))
# no facet
self.assert_equals(POST_BSKY, from_as1(post_as))

def test_from_as1_tag_without_url(self):
self.assert_equals(POST_BSKY, from_as1({
**POST_AS,
'tags': [{'objectType': 'mention'}],
}))

def test_from_as1_tag_mention_did(self):
self.assert_equals({
**POST_BSKY,
'facets': [FACET_MENTION],
}, from_as1({
# def test_from_as1_tag_mention_did(self):
# content = f'did:plc:foo {POST_AS["object"]["content"]}'

# self.assert_equals({
# **POST_BSKY,
# 'text': content,
# 'facets': [FACET_MENTION],
# }, from_as1({
# **POST_AS['object'],
# 'content': content,
# 'tags': [TAG_MENTION_DID],
# }))

def test_from_as1_tag_mention_did_not_in_content(self):
self.assert_equals(POST_BSKY, from_as1({
**POST_AS['object'],
'tags': [TAG_MENTION_DID],
}))

def test_from_as1_tag_mention_url(self):
self.assert_equals({
**POST_BSKY,
'facets': [FACET_MENTION],
}, from_as1({
# def test_from_as1_tag_mention_url(self):
# self.assert_equals({
# **POST_BSKY,
# 'text': f'did:plc:foo {POST_BSKY["text"]}',
# 'facets': [FACET_MENTION],
# }, from_as1({
# **POST_AS['object'],
# 'tags': [TAG_MENTION_URL],
# }))

def test_from_as1_tag_mention_url_not_in_content(self):
self.assert_equals(POST_BSKY, from_as1({
**POST_AS['object'],
'tags': [TAG_MENTION_URL],
}))
Expand Down Expand Up @@ -1341,15 +1359,10 @@ def test_to_as1_facet_hashtag(self):
self.assert_equals(NOTE_AS_TAG_HASHTAG, to_as1(POST_BSKY_FACET_HASHTAG))

def test_to_as1_facet_mention(self):
self.assert_equals(trim_nulls({
**POST_AS['object'],
'id': None,
'url': None,
'tags': [TAG_MENTION_URL],
}), to_as1({
**POST_BSKY,
'facets': [FACET_MENTION],
}))
expected = copy.deepcopy(NOTE_AS_TAG_MENTION)
expected['tags'][0]['displayName'] = '@you.com'
self.assert_equals(expected, to_as1(POST_BSKY_FACET_MENTION),
ignore=['published'])

def test_to_as1_facet_bad_index_inside_unicode_code_point(self):
# byteStart points into the middle of a Unicode code point
Expand Down

0 comments on commit 8dd5e47

Please sign in to comment.