-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""Bluesky source class. | ||
https://bsky.app/ | ||
https://atproto.com/lexicons/app-bsky-actor | ||
https://github.com/bluesky-social/atproto/tree/main/lexicons/app/bsky | ||
""" | ||
import logging | ||
|
||
from oauth_dropins.webutil import util | ||
|
||
|
||
def from_as1(obj): | ||
"""Converts an AS1 object to a Bluesky object. | ||
The objectType field is required. | ||
Args: | ||
profile: dict, AS1 object | ||
Returns: dict, app.bsky.* object | ||
Raises: | ||
ValueError | ||
if the objectType field is missing or unsupported | ||
""" | ||
type = obj.get('objectType') | ||
if not type: | ||
raise ValueError('AS1 object missing objectType field') | ||
|
||
# TODO: once we're on Python 3.10, switch this to a match statement! | ||
if type == 'person': | ||
ret = { | ||
'$type': 'app.bsky.actor.profile', | ||
'displayName': obj.get('displayName'), | ||
'description': obj.get('summary'), | ||
'avatar': util.get_url(obj, 'image'), | ||
} | ||
|
||
elif type in ('article', 'mention', 'note'): | ||
entities = [] | ||
for tag in util.get_list(obj, 'tags'): | ||
url = tag.get('url') | ||
if url: | ||
try: | ||
start = int(tag.get('startIndex')) | ||
end = start + int(tag.get('length')) | ||
except ValueError: | ||
start = end = None | ||
entities.append({ | ||
'type': 'link', | ||
'value': url, | ||
'index': { | ||
'start': start, | ||
'end': end, | ||
}, | ||
}) | ||
|
||
ret = { | ||
'$type': 'app.bsky.feed.post', | ||
'text': obj.get('content'), | ||
'createdAt': obj.get('published'), | ||
'embed': { | ||
'images': util.get_urls(obj, 'image'), | ||
}, | ||
'entities': entities, | ||
} | ||
|
||
elif type == 'share': | ||
ret = { | ||
'$type': 'app.bsky.', | ||
} | ||
|
||
elif type == 'follow': | ||
ret = { | ||
'$type': 'app.bsky.', | ||
} | ||
|
||
else: | ||
raise ValueError(f'AS1 object has unknown objectType: {type}') | ||
|
||
return util.trim_nulls(ret) | ||
|
||
|
||
def to_as1(obj): | ||
"""Converts a Bluesky object to an AS1 object. | ||
The $type field is required. | ||
Args: | ||
profile: dict, app.bsky.* object | ||
Returns: dict, AS1 object | ||
Raises: | ||
ValueError | ||
if the $type field is missing or unsupported | ||
""" | ||
type = obj.get('$type') | ||
if not type: | ||
raise ValueError('Bluesky object missing $type field') | ||
|
||
# TODO: once we're on Python 3.10, switch this to a match statement! | ||
if type == 'app.bsky.actor.profile': | ||
return { | ||
} | ||
elif type == 'app.bsky.feed.post': | ||
return { | ||
} | ||
elif type == 'app.bsky.feed.repost': | ||
return { | ||
} | ||
elif type == 'app.bsky.graph.follow': | ||
return { | ||
} | ||
|
||
raise ValueError(f'Bluesky object has unknown $type: {type}') | ||
|
||
|
||
# class Bluesky(source.Source): | ||
# """Bluesky source class. See file docstring and Source class for details.""" | ||
|
||
# DOMAIN = 'bsky.app' | ||
# BASE_URL = 'https://bsky.app' | ||
# NAME = 'Bluesky' | ||
# # OPTIMIZED_COMMENTS = None # TODO | ||
|
||
# def __init__(self): | ||
# pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Unit tests for jsonfeed.py.""" | ||
from oauth_dropins.webutil import testutil | ||
|
||
from ..bluesky import from_as1, to_as1 | ||
|
||
# app.bsky.actor.profile | ||
# /Users/ryan/src/atproto/lexicons/app/bsky/actor/profile.json | ||
# https://github.com/bluesky-social/atproto/blob/main/packages/pds/tests/crud.test.ts#L211-L217 | ||
# { | ||
# displayName: 'alice', | ||
# createdAt: new Date().toISOString(), | ||
# }, | ||
|
||
# app.bsky.feed.post | ||
# /Users/ryan/src/atproto/lexicons/app/bsky/feed/post.json | ||
# https://github.com/bluesky-social/atproto/blob/main/packages/pds/tests/crud.test.ts#L74-L82 | ||
# record: { | ||
# $type: 'app.bsky.feed.post', | ||
# text: 'Hello, world!', | ||
# createdAt: new Date().toISOString(), | ||
# }, | ||
|
||
# app.bsky.feed.repost | ||
# /Users/ryan/src/atproto/lexicons/app/bsky/feed/repost.json | ||
# https://github.com/bluesky-social/atproto/blob/main/packages/pds/tests/seeds/client.ts#L294-L298 | ||
# { subject: subject.raw, createdAt: new Date().toISOString() }, | ||
|
||
|
||
# app.bsky.graph.follow | ||
# /Users/ryan/src/atproto/lexicons/app/bsky/graph/follow.json | ||
# https://github.com/bluesky-social/atproto/blob/main/packages/pds/tests/seeds/client.ts#L183-L190 | ||
# { | ||
# subject: to.raw, | ||
# createdAt: new Date().toISOString(), | ||
# }, | ||
|
||
# # link/other embed (no test) | ||
# app.bsky.embed.external | ||
# /Users/ryan/src/atproto/lexicons/app/bsky/embed/external.json | ||
|
||
# # image | ||
# app.bsky.embed.images | ||
# /Users/ryan/src/atproto/lexicons/app/bsky/embed/images.json | ||
# https://github.com/bluesky-social/atproto/blob/main/packages/pds/tests/crud.test.ts#L178-L191 | ||
# { | ||
# $type: 'app.bsky.feed.post', | ||
# text: "Here's a key!", | ||
# createdAt: new Date().toISOString(), | ||
# embed: { | ||
# $type: 'app.bsky.embed.images', | ||
# images: [ | ||
# { image: { cid: image.cid, mimeType: 'image/jpeg' }, alt: '' }, | ||
# ], | ||
# }, | ||
# }, | ||
|
||
|
||
class TestBluesky(testutil.TestCase): | ||
|
||
def test_to_as1_missing_objectType(self): | ||
with self.assertRaises(ValueError): | ||
to_as1({'foo': 'bar'}) | ||
|
||
def test_to_as1_unknown_objectType(self): | ||
with self.assertRaises(ValueError): | ||
to_as1({'objectType': 'poll'}) | ||
|
||
def test_to_as1_missing_type(self): | ||
with self.assertRaises(ValueError): | ||
to_as1({'foo': 'bar'}) | ||
|
||
def test_to_as1_unknown_type(self): | ||
with self.assertRaises(ValueError): | ||
to_as1({'$type': 'app.bsky.foo'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$type": "app.bsky.actor.profile", | ||
"displayName": "Martin Smith", | ||
"description": "this is my bio", | ||
"avatar": "http://example.com/martin/image" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$type": "app.bsky.graph.follow", | ||
"subject": "http://follower", | ||
"createdAt": "2012-02-22T20:26:41" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$type": "app.bsky.feed.post", | ||
"text": "A note. link too", | ||
"createdAt": "2012-02-22T20:26:41", | ||
"embed": { | ||
"images": ["http://example.com/blog-post-123/image"] | ||
}, | ||
"entities": [{ | ||
"type": "link", | ||
"value": "http://my/link", | ||
"index": { | ||
"start": 8, | ||
"end": 12 | ||
} | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$type": "app.bsky.feed.repost", | ||
"subject": "http://example.com/alice", | ||
"createdAt": "2012-02-22T20:26:41" | ||
} |