bridgy-fed/xrpc_feed.py

82 wiersze
2.2 KiB
Python
Czysty Zwykły widok Historia

"""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')
def getAuthorFeed(input, author=None, limit=None, before=None):
2022-12-25 05:08:12 +00:00
"""
lexicons/app/bsky/feed/getAuthorFeed.json, feedViewPost.json
2022-12-25 05:08:12 +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-30 18:10:49 +00:00
url = f'https://{author}/'
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-30 18:10:49 +00:00
# hcard = mf2util.representative_hcard(mf2, mf2['url'])
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-30 18:10:49 +00:00
# actor = microformats2.json_to_object(author)
activities = microformats2.json_to_activities(mf2) #, actor)
# 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-30 18:10:49 +00:00
return {'feed': [bluesky.from_as1(a) for a in activities]}
2022-12-25 05:08:12 +00:00
@xrpc_server.method('app.bsky.feed.getPostThread')
2022-12-28 17:27:42 +00:00
def getPostThread(input):
2022-12-25 05:08:12 +00:00
"""
lexicons/app/bsky/feed/getPostThread.json
"""
@xrpc_server.method('app.bsky.feed.getRepostedBy')
2022-12-28 17:27:42 +00:00
def getRepostedBy(input):
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
"""
@xrpc_server.method('app.bsky.feed.getVotes')
2022-12-28 17:27:42 +00:00
def getVotes(input):
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
"""