Skip to content

Commit

Permalink
bluesky.from_as1: mention facet bug fix for mentions at beginning of …
Browse files Browse the repository at this point in the history
…content

fixes snarfed/bridgy-fed#957 (again)
  • Loading branch information
snarfed committed May 30, 2024
1 parent 5dd16b6 commit d262fb1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
11 changes: 7 additions & 4 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,16 +644,18 @@ def from_as1(obj, out_type=None, blobs=None, client=None):
# can't use \b for word boundaries here because that only includes
# alphanumerics, and Bluesky hashtags can include emoji
prefix = '#' if type == 'hashtag' else '@' if type == 'mention' else ''
match = re.search(fr'[\s^]({prefix}{name})[\s$]', text)
# can't use \b at beginning/end because # and @ and emoji aren't
# word-constituent chars
match = re.search(fr'(^|\s)({prefix}{name})($|\s)', text)
if not match and type == 'mention' and '@' in name:
# try without @[server] suffix
username = name.split('@')[0]
match = re.search(fr'[\s^]({prefix}{username})[\s$]', text)
match = re.search(fr'(^|\s)({prefix}{username})($|\s)', text)

if match:
facet['index'] = {
'byteStart': len(full_text[:match.start(1)].encode()),
'byteEnd': len(full_text[:match.end(1)].encode()),
'byteStart': len(full_text[:match.start(2)].encode()),
'byteEnd': len(full_text[:match.end(2)].encode()),
}

# skip or trim this facet if it's off the end of content that got truncated
Expand Down Expand Up @@ -926,6 +928,7 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,
ret.update({
'url': util.dedupe_urls(urls),
'displayName': obj.get('displayName'),
# TODO: for app.bsky.feed.generator, use descriptionFacets
'summary': util.linkify(obj.get('description') or '', pretty=True),
'image': images,
'published': obj.get('createdAt'),
Expand Down
28 changes: 28 additions & 0 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,36 @@ def test_from_as1_tag_mention_at_char_html_content_guess_index(self):
note = copy.deepcopy(NOTE_AS_TAG_MENTION_URL)
note['content'] = '<p>foo <a href="https://bsky.app/...">@you.com</a> bar</p>'
note['tags'][0]['displayName'] = '@you.com'
del note['tags'][0]['startIndex']
del note['tags'][0]['length']
self.assert_equals(POST_BSKY_FACET_MENTION, from_as1(note))

def test_from_as1_tag_mention_at_beginning(self):
self.assert_equals({
'$type': 'app.bsky.feed.post',
'createdAt': '2022-01-02T03:04:05.000Z',
'text': '@shreyanjain.net hello there',
'facets': [{
'$type': 'app.bsky.richtext.facet',
'features': [{
'$type': 'app.bsky.richtext.facet#mention',
'did': 'did:plc:foo',
}],
'index': {
'byteStart': 0,
'byteEnd': 16,
},
}],
}, from_as1({
'objectType' : 'note',
'content' : '<p><span class=\'h-card\' translate=\'no\'><a href=\'https://bsky.brid.gy/r/https://bsky.app/profile/shreyanjain.net\' class=\'u-url mention\'>@<span>shreyanjain.net</span></a></span> hello there</p>',
'tags' : [{
'objectType': 'mention',
'displayName': '@[email protected]',
'url': 'did:plc:foo',
}],
}))

def test_from_as1_tag_mention_not_bluesky(self):
self.assert_equals({
'$type': 'app.bsky.feed.post',
Expand Down

0 comments on commit d262fb1

Please sign in to comment.