mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 11:26:56 -08:00
API: re-use existing list functions for search
This commit is contained in:
parent
d79050c5ed
commit
c84c1f9c4b
3 changed files with 38 additions and 68 deletions
|
@ -4,18 +4,13 @@ from app.api.alpha.utils.validators import required, integer_expected, boolean_e
|
|||
from app.utils import authorise_api_user
|
||||
from app.models import Community, CommunityMember
|
||||
from app.shared.community import join_community, leave_community, block_community, unblock_community
|
||||
from app.utils import communities_banned_from, blocked_instances
|
||||
from app.utils import communities_banned_from, blocked_instances, blocked_communities
|
||||
|
||||
from sqlalchemy import desc
|
||||
|
||||
|
||||
@cache.memoize(timeout=3)
|
||||
def cached_community_list(type, sort, limit, user_id):
|
||||
if user_id:
|
||||
banned_from = communities_banned_from(user_id)
|
||||
else:
|
||||
banned_from = None
|
||||
|
||||
if type == 'Subscribed':
|
||||
communities = Community.query.filter_by(banned=False).join(CommunityMember).filter(CommunityMember.user_id == user_id)
|
||||
elif type == 'Local':
|
||||
|
@ -23,13 +18,16 @@ def cached_community_list(type, sort, limit, user_id):
|
|||
else:
|
||||
communities = Community.query.filter_by(banned=False)
|
||||
|
||||
if banned_from:
|
||||
communities = communities.filter(Community.id.not_in(banned_from))
|
||||
|
||||
if user_id:
|
||||
banned_from = communities_banned_from(user_id)
|
||||
if banned_from:
|
||||
communities = communities.filter(Community.id.not_in(banned_from))
|
||||
blocked_instance_ids = blocked_instances(user_id)
|
||||
if blocked_instance_ids:
|
||||
communities = communities.filter(Community.instance_id.not_in(blocked_instance_ids))
|
||||
blocked_community_ids = blocked_communities(user_id)
|
||||
if blocked_community_ids:
|
||||
communities = communities.filter(Community.id.not_in(blocked_community_ids))
|
||||
|
||||
if sort == 'Active': # 'Trending Communities' screen
|
||||
communities = communities.order_by(desc(Community.last_active)).limit(limit)
|
||||
|
|
|
@ -1,55 +1,7 @@
|
|||
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
|
||||
from app.api.alpha.utils.community import get_community_list
|
||||
from app.api.alpha.utils.post import get_post_list
|
||||
from app.api.alpha.utils.user import get_user_list
|
||||
from app.api.alpha.views import search_view
|
||||
|
||||
|
||||
def get_search(auth, data):
|
||||
|
@ -57,20 +9,17 @@ def get_search(auth, 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
|
||||
data['type_'] = listing_type
|
||||
|
||||
search_json = search_view(type)
|
||||
if type == 'Communities':
|
||||
search_json['communities'] = get_communities_list(sort, page, limit, listing_type, user_id)
|
||||
search_json['communities'] = get_community_list(auth, data)['communities']
|
||||
elif type == 'Posts':
|
||||
search_json['posts'] = get_posts_list(sort, page, limit, listing_type, user_id)
|
||||
search_json['posts'] = get_post_list(auth, data)['posts']
|
||||
elif type == 'Users':
|
||||
search_json['users'] = get_users_list(sort, page, limit, listing_type, user_id)
|
||||
search_json['users'] = get_user_list(auth, data)['users']
|
||||
|
||||
return search_json
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ from app.utils import authorise_api_user
|
|||
from app.api.alpha.utils.post import get_post_list
|
||||
from app.api.alpha.utils.reply import get_reply_list
|
||||
from app.api.alpha.utils.validators import required, integer_expected, boolean_expected
|
||||
from app.models import User
|
||||
from app.shared.user import block_another_user, unblock_another_user
|
||||
|
||||
|
||||
|
@ -35,6 +36,28 @@ def get_user(auth, data):
|
|||
return user_json
|
||||
|
||||
|
||||
def get_user_list(auth, data):
|
||||
# only support 'api/alpha/search?q&type_=Users&sort=TopAll&listing_type=Local&page=1&limit=15' for now
|
||||
# (enough for instance view)
|
||||
|
||||
type = data['type_'] if data and 'type_' in data else "All"
|
||||
sort = data['sort'] if data and 'sort' in data else "Hot"
|
||||
page = int(data['page']) if data and 'page' in data else 1
|
||||
limit = int(data['limit']) if data and 'limit' in data else 10
|
||||
|
||||
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))
|
||||
list_json = {
|
||||
"users": user_list
|
||||
}
|
||||
|
||||
return list_json
|
||||
|
||||
|
||||
# would be in app/constants.py
|
||||
SRC_API = 3
|
||||
|
||||
|
|
Loading…
Reference in a new issue