API: initial support for /search endpoint (enough for instance view)

This commit is contained in:
freamon 2024-10-09 23:46:57 +00:00
parent 522a607647
commit b49bb4cf3f
4 changed files with 102 additions and 1 deletions

View file

@ -1,5 +1,6 @@
from app.api.alpha import bp
from app.api.alpha.utils import get_site, \
get_search, \
get_post_list, get_post, post_post_like, put_post_save, put_post_subscribe, \
get_reply_list, post_reply_like, put_reply_save, put_reply_subscribe, \
get_community_list, get_community, post_community_follow, post_community_block, \
@ -21,6 +22,19 @@ def get_alpha_site():
return jsonify({"error": str(ex)}), 400
# Misc
@bp.route('/api/alpha/search', methods=['GET'])
def get_alpha_search():
if not current_app.debug:
return jsonify({'error': 'alpha api routes only available in debug mode'})
try:
auth = request.headers.get('Authorization')
data = request.args.to_dict() or None
return jsonify(get_search(auth, data))
except Exception as ex:
return jsonify({"error": str(ex)}), 400
# Community
@bp.route('/api/alpha/community', methods=['GET'])
def get_alpha_community():
@ -228,7 +242,6 @@ def alpha_site():
# Miscellaneous - not yet implemented
@bp.route('/api/alpha/modlog', methods=['GET'])
@bp.route('/api/alpha/search', methods=['GET'])
@bp.route('/api/alpha/resolve_object', methods=['GET'])
@bp.route('/api/alpha/federated_instances', methods=['GET'])
def alpha_miscellaneous():

View file

@ -1,4 +1,5 @@
from app.api.alpha.utils.site import get_site
from app.api.alpha.utils.misc import get_search
from app.api.alpha.utils.post import get_post_list, get_post, post_post_like, put_post_save, put_post_subscribe
from app.api.alpha.utils.reply import get_reply_list, post_reply_like, put_reply_save, put_reply_subscribe
from app.api.alpha.utils.community import get_community, get_community_list, post_community_follow, post_community_block

View file

@ -0,0 +1,76 @@
from app.models import Community, Post, User, utcnow
from app.utils import authorise_api_user
from app.api.alpha.views import search_view, community_view, post_view, user_view
from datetime import timedelta
from sqlalchemy import desc
def get_communities_list(sort, page, limit, listing_type, user_id):
# only support 'api/alpha/search?q&type_=Communities&sort=TopAll&listing_type=Local&page=1&limit=15' for now
# (enough for instance view)
communities = Community.query.filter_by(ap_id=None).order_by(desc(Community.subscriptions_count))
communities = communities.paginate(page=page, per_page=limit, error_out=False)
community_list = []
for community in communities:
community_list.append(community_view(community, variant=2, stub=True))
return community_list
def get_posts_list(sort, page, limit, listing_type, user_id):
# only support 'api/alpha/search?q&type_=Posts&sort=TopAll&listing_type=Local&page=1&limit=15' for now
# (enough for instance view)
posts = Post.query.filter_by(instance_id=1, deleted=False)
if sort == "Hot":
posts = posts.order_by(desc(Post.ranking)).order_by(desc(Post.posted_at))
elif sort == "TopDay":
posts = posts.filter(Post.posted_at > utcnow() - timedelta(days=1)).order_by(desc(Post.up_votes - Post.down_votes))
elif sort == "New":
posts = posts.order_by(desc(Post.posted_at))
elif sort == "Active":
posts = posts.order_by(desc(Post.last_active))
posts = posts.paginate(page=page, per_page=limit, error_out=False)
post_list = []
for post in posts:
post_list.append(post_view(post, variant=2, stub=True))
return post_list
def get_users_list(sort, page, limit, listing_type, user_id):
# only support 'api/alpha/search?q&type_=Users&sort=TopAll&listing_type=Local&page=1&limit=15' for now
# (enough for instance view)
users = User.query.filter_by(instance_id=1, deleted=False).order_by(User.id)
users = users.paginate(page=page, per_page=limit, error_out=False)
user_list = []
for user in users:
user_list.append(user_view(user, variant=2, stub=True))
return user_list
def get_search(auth, data):
if not data or ('q' not in data and 'type_' not in data):
raise Exception('missing_parameters')
type = data['type_']
sort = data['sort'] if 'sort' in data else 'Top'
page = int(data['page']) if 'page' in data else 1
limit = int(data['limit']) if 'limit' in data else 15
listing_type = data['listing_type'] if 'listing_type' in data else 'Local'
user_id = authorise_api_user(auth) if auth else None
search_json = search_view(type)
if type == 'Communities':
search_json['communities'] = get_communities_list(sort, page, limit, listing_type, user_id)
elif type == 'Posts':
search_json['posts'] = get_posts_list(sort, page, limit, listing_type, user_id)
elif type == 'Users':
search_json['users'] = get_users_list(sort, page, limit, listing_type, user_id)
return search_json

View file

@ -332,6 +332,17 @@ def reply_view(reply: PostReply | int, variant, user_id=None, my_vote=0):
return v4
def search_view(type):
v1 = {
'type_': type,
'comments': [],
'posts': [],
'communities': [],
'users': []
}
return v1
@cache.memoize(timeout=86400)
def cached_modlist_for_community(community_id):
moderator_ids = db.session.execute(text('SELECT user_id FROM "community_member" WHERE community_id = :community_id and is_moderator = True'), {'community_id': community_id}).scalars()