2022-12-24 16:34:24 +00:00
|
|
|
"""app.bsky.feed.* XRPC methods."""
|
2022-12-30 18:10:49 +00:00
|
|
|
import json
|
2022-12-28 17:27:42 +00:00
|
|
|
import logging
|
2022-12-30 18:10:49 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from granary import microformats2, bluesky
|
|
|
|
import mf2util
|
|
|
|
from oauth_dropins.webutil import util
|
2022-12-28 17:27:42 +00:00
|
|
|
|
2022-12-25 05:08:12 +00:00
|
|
|
from app import xrpc_server
|
|
|
|
|
2022-12-28 17:27:42 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-12-25 05:08:12 +00:00
|
|
|
@xrpc_server.method('app.bsky.feed.getAuthorFeed')
|
2022-12-29 01:30:22 +00:00
|
|
|
def getAuthorFeed(input, author=None, limit=None, before=None):
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
2022-12-29 01:30:22 +00:00
|
|
|
lexicons/app/bsky/feed/getAuthorFeed.json, feedViewPost.json
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
2022-12-29 01:30:22 +00:00
|
|
|
if not re.match(util.DOMAIN_RE, author):
|
2022-12-30 18:10:49 +00:00
|
|
|
raise ValueError(f'{author} is not a domain')
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2022-12-30 18:10:49 +00:00
|
|
|
url = f'https://{author}/'
|
2022-12-29 01:30:22 +00:00
|
|
|
mf2 = util.fetch_mf2(url, gateway=True)
|
2022-12-30 18:10:49 +00:00
|
|
|
logger.info(f'Got mf2: {json.dumps(mf2, indent=2)}')
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2022-12-30 18:10:49 +00:00
|
|
|
feed_author = mf2util.find_author(mf2, source_url=url, fetch_mf2_func=util.fetch_mf2)
|
|
|
|
if feed_author:
|
|
|
|
logger.info(f'Authorship found: {feed_author}')
|
|
|
|
actor = {
|
|
|
|
'url': feed_author.get('url') or url,
|
|
|
|
'displayName': feed_author.get('name'),
|
|
|
|
'image': {'url': feed_author.get('photo')},
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
logger.info(f'No authorship result on {url} ; generated {feed_author}')
|
|
|
|
actor = {
|
|
|
|
'url': url,
|
|
|
|
'displayName': author,
|
|
|
|
}
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2023-01-13 00:36:12 +00:00
|
|
|
activities = microformats2.json_to_activities(mf2)
|
2022-12-30 18:10:49 +00:00
|
|
|
# default actor to feed author
|
|
|
|
for a in activities:
|
|
|
|
a.setdefault('actor', actor)
|
|
|
|
logger.info(f'AS1 activities: {json.dumps(activities, indent=2)}')
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2022-12-30 18:10:49 +00:00
|
|
|
return {'feed': [bluesky.from_as1(a) for a in activities]}
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2022-12-25 05:08:12 +00:00
|
|
|
|
|
|
|
@xrpc_server.method('app.bsky.feed.getPostThread')
|
2023-01-13 00:36:12 +00:00
|
|
|
def getPostThread(input, uri=None, depth=None):
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
|
|
|
lexicons/app/bsky/feed/getPostThread.json
|
|
|
|
"""
|
2023-01-13 00:36:12 +00:00
|
|
|
mf2 = util.fetch_mf2(uri, gateway=True)
|
|
|
|
logger.info(f'Got mf2: {json.dumps(mf2, indent=2)}')
|
|
|
|
|
|
|
|
entry = mf2util.find_first_entry(mf2, ['h-entry'])
|
|
|
|
logger.info(f'Entry: {json.dumps(entry, indent=2)}')
|
|
|
|
if not entry:
|
|
|
|
raise ValueError(f"No h-entry on {uri}")
|
|
|
|
|
|
|
|
obj = microformats2.json_to_object(entry)
|
|
|
|
logger.info(f'AS1: {json.dumps(obj, indent=2)}')
|
|
|
|
|
|
|
|
return {
|
|
|
|
'thread': {
|
|
|
|
'$type': 'app.bsky.feed.getPostThread#threadViewPost',
|
|
|
|
'post': bluesky.from_as1(obj)['post'],
|
|
|
|
'replies': [{
|
|
|
|
'$type': 'app.bsky.feed.getPostThread#threadViewPost',
|
|
|
|
'post': bluesky.from_as1(reply)['post'],
|
|
|
|
} for reply in obj.get('replies', {}).get('items', [])],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-25 05:08:12 +00:00
|
|
|
|
|
|
|
@xrpc_server.method('app.bsky.feed.getRepostedBy')
|
2022-12-31 06:52:56 +00:00
|
|
|
def getRepostedBy(input, uri=None, cid=None, limit=None, before=None):
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
|
|
|
lexicons/app/bsky/feed/getRepostedBy.json
|
|
|
|
"""
|
|
|
|
|
|
|
|
@xrpc_server.method('app.bsky.feed.getTimeline')
|
2022-12-28 17:27:42 +00:00
|
|
|
def getTimeline(input):
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
|
|
|
lexicons/app/bsky/feed/getTimeline.json
|
|
|
|
"""
|
|
|
|
|
2022-12-31 06:52:56 +00:00
|
|
|
# votes are likes
|
2022-12-25 05:08:12 +00:00
|
|
|
@xrpc_server.method('app.bsky.feed.getVotes')
|
2022-12-31 06:52:56 +00:00
|
|
|
def getVotes(input, uri=None, direction=None, cid=None, limit=None, before=None):
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
|
|
|
lexicons/app/bsky/feed/getVotes.json
|
|
|
|
"""
|
|
|
|
|
|
|
|
@xrpc_server.method('app.bsky.feed.setVote')
|
2022-12-28 17:27:42 +00:00
|
|
|
def setVote(input):
|
2022-12-25 05:08:12 +00:00
|
|
|
"""
|
|
|
|
lexicons/app/bsky/feed/setVote.json
|
|
|
|
"""
|