Skip to content

Commit

Permalink
bluesky.to_as1: prioritize bsky.app profile URL before handle in `url…
Browse files Browse the repository at this point in the history
…` field

for snarfed/bridgy#1640
  • Loading branch information
snarfed committed Jan 16, 2024
1 parent e68bf9d commit 0e6e590
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Changelog
* Strip trailing slash from home page URLs in order to remove visible `/` from rel-me verified links on Mastodon etc.
* Convert `attributedTo` to singular if it has only one element.
* If `name` isn't set, fall back to `preferredUsername` or infer Webfinger handle from `id` or `url`.
* Prioritize bsky.app profile URL before handle URL in `url` field ([bridgy#1640](https://github.com/snarfed/bridgy/issues/1640)).
* `facebook`:
* Remove `Facebook.fql_stream_to_post`. [Facebook turned down FQL in 2016.](https://en.wikipedia.org/wiki/Facebook_Query_Language#History)
* `github`:
Expand Down
2 changes: 1 addition & 1 deletion granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ def to_as1(obj, type=None, uri=None, repo_did=None, repo_handle=None,

urls = []
if handle:
urls.append(Bluesky.user_url(handle))
if not util.domain_or_parent_in(handle, [DEFAULT_PDS_DOMAIN]):
urls.append(f'https://{handle}/')
urls.append(Bluesky.user_url(handle))
elif did and did.startswith('did:web:'):
urls.extend([did_web_to_url(did), Bluesky.user_url(did)])

Expand Down
25 changes: 13 additions & 12 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@
POST_AUTHOR_PROFILE_AS = copy.deepcopy(POST_AUTHOR_AS)
POST_AUTHOR_PROFILE_AS['object']['author'].update({
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
})
POST_AUTHOR_PROFILE_AS['actor'].update({
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
})
POST_AUTHOR_BSKY = copy.deepcopy(POST_VIEW_BSKY)
POST_AUTHOR_BSKY['author'] = {
Expand Down Expand Up @@ -246,14 +246,14 @@
'objectType': 'person',
'id': 'did:web:bob.com',
'displayName': 'Bob',
'url': ['https://bob.com/', 'https://bsky.app/profile/bob.com'],
'url': ['https://bsky.app/profile/bob.com', 'https://bob.com/'],
},
'object': POST_AUTHOR_PROFILE_AS['object'],
}
REPOST_PROFILE_AS = copy.deepcopy(REPOST_AS)
REPOST_PROFILE_AS['actor'].update({
'username': 'bob.com',
'url': ['https://bob.com/', 'https://bsky.app/profile/bob.com'],
'url': ['https://bsky.app/profile/bob.com', 'https://bob.com/'],
})

REPOST_BSKY = {
Expand Down Expand Up @@ -294,12 +294,12 @@
THREAD_AS['object']['author'] = ACTOR_AS
THREAD_AS['object']['author'].update({
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
})
THREAD_AS['object']['url'] = 'https://bsky.app/profile/alice.com/post/tid'
THREAD_AS['actor'].update({
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
})

THREAD_BSKY = {
Expand Down Expand Up @@ -368,7 +368,7 @@
POST_AUTHOR_PROFILE_WITH_LIKES_AS['object']['tags'][0]['author'].update({
'id': 'tag:bsky.app:did:web:alice.com',
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
})

POST_AUTHOR_PROFILE_WITH_REPOSTS_AS = copy.deepcopy(POST_AUTHOR_PROFILE_AS)
Expand All @@ -383,7 +383,7 @@
POST_AUTHOR_PROFILE_WITH_REPOSTS_AS['object']['tags'][0]['author'].update({
'id': 'tag:bsky.app:did:web:alice.com',
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
})

class BlueskyTest(testutil.TestCase):
Expand All @@ -394,7 +394,8 @@ def setUp(self):

def assert_equals(self, expected, actual, ignore=(), **kwargs):
ignore = list(ignore) + ['uri']
return super().assert_equals(expected, actual, ignore=ignore, **kwargs)
return super().assert_equals(expected, actual, ignore=ignore, in_order=True,
**kwargs)

def assert_call(self, mock, url, json=None):
mock.assert_any_call(url, data=None, json=json, headers={
Expand Down Expand Up @@ -783,7 +784,7 @@ def test_to_as1_profile(self):
'objectType': 'featured',
'url': OLD_BLOB_URL,
}],
'url': ['https://han.dull/', 'https://bsky.app/profile/han.dull'],
'url': ['https://bsky.app/profile/han.dull', 'https://han.dull/'],
}, to_as1(ACTOR_PROFILE_BSKY, repo_did='did:plc:foo', repo_handle='han.dull'))

def test_to_as1_profile_bsky_social_handle_is_not_url(self):
Expand All @@ -797,7 +798,7 @@ def test_to_as1_profile_view(self):
self.assert_equals({
**ACTOR_AS,
'username': 'alice.com',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
}, to_as1(ACTOR_PROFILE_VIEW_BSKY))

def test_to_as1_profile_no_repo_did_handle_or_pds(self):
Expand Down Expand Up @@ -911,7 +912,7 @@ def test_to_as1_listView(self):
'author': {
'objectType': 'person',
'id': 'did:alice',
'url': ['https://alice.com/', 'https://bsky.app/profile/alice.com'],
'url': ['https://bsky.app/profile/alice.com', 'https://alice.com/'],
'username': 'alice.com',
'image': [{'url': 'https://cdn.bsky.app/alice@jpeg'}]
}
Expand Down
2 changes: 1 addition & 1 deletion granary/tests/testdata/actor.as-from-bsky.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"displayName": "Martin Smith",
"username": "example.com",
"summary": "this is my bio",
"url": ["https://example.com/", "https://bsky.app/profile/example.com"]
"url": ["https://bsky.app/profile/example.com", "https://example.com/"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"id": "did:plc:foo",
"displayName": "Martin Smith",
"username": "example.com",
"url": ["https://example.com/", "https://bsky.app/profile/example.com"]
"url": ["https://bsky.app/profile/example.com", "https://example.com/"]
}
2 changes: 1 addition & 1 deletion granary/tests/testdata/note.as-from-bsky.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": {
"objectType": "person",
"id": "did:plc:asdf",
"url": ["https://example.com/", "https://bsky.app/profile/example.com"],
"url": ["https://bsky.app/profile/example.com", "https://example.com/"],
"displayName": "Ryan Barrett",
"username": "example.com"
},
Expand Down
2 changes: 1 addition & 1 deletion granary/tests/testdata/quote.as-from-bsky.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"author": {
"objectType": "person",
"id": "did:plc:pv7fudn",
"url": ["https://alice.example.com/", "https://bsky.app/profile/alice.example.com"],
"url": ["https://bsky.app/profile/alice.example.com", "https://alice.example.com/"],
"displayName": "Alice",
"username": "alice.example.com",
"image": [{"url": "https://cdn.bsky.social/imgproxy/alice/pv7fudn@jpeg"}]
Expand Down
4 changes: 2 additions & 2 deletions granary/tests/testdata/repost.as-from-bsky.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": {
"objectType": "person",
"id": "did:web:example.com",
"url": ["https://example.com/", "https://bsky.app/profile/example.com"],
"url": ["https://bsky.app/profile/example.com", "https://example.com/"],
"displayName": "Bob",
"username": "example.com"
},
Expand All @@ -17,7 +17,7 @@
"actor": {
"objectType": "person",
"id": "did:web:alice.example.com",
"url": ["https://alice.example.com/", "https://bsky.app/profile/alice.example.com"],
"url": ["https://bsky.app/profile/alice.example.com", "https://alice.example.com/"],
"displayName": "Alice",
"username": "alice.example.com"
}
Expand Down

0 comments on commit 0e6e590

Please sign in to comment.