bridgy-fed/xrpc_feed.py

122 wiersze
3.6 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 flask import g
from granary import bluesky, microformats2
2022-12-30 18:10:49 +00:00
import mf2util
from oauth_dropins.webutil import util
2022-12-28 17:27:42 +00:00
from flask_app import xrpc_server
from models import Object, PAGE_SIZE, User
from web import Web
2022-12-25 05:08:12 +00:00
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 author or not re.match(util.DOMAIN_RE, author):
2022-12-30 18:10:49 +00:00
raise ValueError(f'{author} is not a domain')
g.user = Web.get_by_id(author)
if not g.user:
raise ValueError(f'User {author} not found')
elif not g.user.actor_as2:
return ValueError(f'User {author} not fully set up')
# TODO: unify with pages.feed?
limit = min(limit or PAGE_SIZE, PAGE_SIZE)
objects, _, _ = Object.query(Object.domains == author, Object.labels == 'user') \
.order(-Object.created) \
.fetch_page(limit)
activities = [obj.as1 for obj in objects if not obj.deleted]
2022-12-30 18:10:49 +00:00
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')
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
"""
if not uri:
raise ValueError('Missing uri')
obj = Object.get_by_id(uri)
if not obj:
raise ValueError(f'{uri} not found')
2023-01-13 00:36:12 +00:00
logger.info(f'AS1: {json.dumps(obj.as1, indent=2)}')
2023-01-13 00:36:12 +00:00
return {
'thread': {
'$type': 'app.bsky.feed.defs#threadViewPost',
'post': bluesky.from_as1(obj.as1)['post'],
2023-01-13 00:36:12 +00:00
'replies': [{
'$type': 'app.bsky.feed.defs#threadViewPost',
2023-01-13 00:36:12 +00:00
'post': bluesky.from_as1(reply)['post'],
} for reply in obj.as1.get('replies', {}).get('items', [])],
2023-01-13 00:36:12 +00:00
},
}
2022-12-25 05:08:12 +00:00
@xrpc_server.method('app.bsky.feed.getRepostedBy')
def getRepostedBy(input, uri=None, cid=None, limit=None, before=None):
"""
TODO: implement before, as query filter. what's input type? str or datetime?
lexicons/app/bsky/feed/getRepostedBy.json
"""
if not uri:
raise ValueError('Missing uri')
limit = min(limit or PAGE_SIZE, PAGE_SIZE)
objects, _, _ = Object.query(Object.object_ids == uri) \
.order(-Object.created) \
.fetch_page(limit)
activities = [obj.as1 for obj in objects if not obj.deleted]
logger.info(f'AS1 activities: {json.dumps(activities, indent=2)}')
return {
'uri': 'http://orig/post',
'repostBy': [{
**bluesky.from_as1(a['actor']),
'$type': 'app.bsky.feed.getRepostedBy#repostedBy',
} for a in activities if a.get('actor')],
}
2023-01-13 02:05:07 +00:00
# TODO: cursor
@xrpc_server.method('app.bsky.feed.getTimeline')
def getTimeline(input, algorithm=None, limit=50, before=None):
"""
lexicons/app/bsky/feed/getTimeline.json
"""
# TODO: how to get authed user?
domain = 'user.com'
# TODO: de-dupe with pages.feed()
logger.info(f'Fetching {limit} objects for {domain}')
objects, _, _ = Object.query(Object.domains == domain, Object.labels == 'feed') \
.order(-Object.created) \
.fetch_page(limit)
return {'feed': [bluesky.from_as1(obj.as1) for obj in objects if not obj.deleted]}
2022-12-25 05:08:12 +00:00
# TODO
@xrpc_server.method('app.bsky.feed.getLikes')
def getLikes(input, uri=None, direction=None, cid=None, limit=None, before=None):
2022-12-25 05:08:12 +00:00
"""
lexicons/app/bsky/feed/getLikes.json
2022-12-25 05:08:12 +00:00
"""
return {
'uri': uri,
'likes': [],
}