pyfedi/app/api/alpha/utils/site.py

102 lines
4 KiB
Python
Raw Normal View History

from app import db
2024-10-07 13:57:19 +00:00
from app.api.alpha.views import user_view, community_view
from app.utils import authorise_api_user
from app.models import Language
from flask import current_app, g
from sqlalchemy import text
def users_total():
return db.session.execute(text(
'SELECT COUNT(id) as c FROM "user" WHERE ap_id is null AND verified is true AND banned is false AND deleted is false')).scalar()
def get_site(auth):
if auth:
try:
user = authorise_api_user(auth, return_type='model')
except Exception as e:
raise e
else:
user = None
logo = g.site.logo if g.site.logo else '/static/images/logo2.png'
site = {
"enable_downvotes": g.site.enable_downvotes,
"icon": f"https://{current_app.config['SERVER_NAME']}{logo}",
"name": g.site.name,
"actor_id": f"https://{current_app.config['SERVER_NAME']}/",
"user_count": users_total(),
"all_languages": []
}
if g.site.sidebar:
site['sidebar'] = g.site.sidebar
if g.site.description:
site['description'] = g.site.description
for language in Language.query.all():
site["all_languages"].append({
"id": language.id,
"code": language.code,
"name": language.name
})
if user:
my_user = {
"local_user_view": {
"local_user": {
"show_nsfw": not user.hide_nsfw == 1,
"default_sort_type": user.default_sort.capitalize(),
"default_listing_type": user.default_filter.capitalize(),
"show_scores": True,
"show_bot_accounts": not user.ignore_bots == 1,
"show_read_posts": True,
},
"person": {
"id": user.id,
"user_name": user.user_name,
"banned": user.banned,
"published": user.created.isoformat() + 'Z',
"actor_id": user.public_url()[8:],
"local": True,
"deleted": user.deleted,
"bot": user.bot,
"instance_id": 1
},
"counts": {
"person_id": user.id,
"post_count": user.post_count,
"comment_count": user.post_reply_count
}
},
#"moderates": [],
#"follows": [],
2024-10-07 13:57:19 +00:00
"community_blocks": [],
"instance_blocks": [], # TODO
2024-10-05 20:55:04 +00:00
"person_blocks": [],
"discussion_languages": [] # TODO
}
"""
Note: Thunder doesn't use moderates[] and follows[] from here, but it would be more efficient if it did (rather than getting them from /user and /community)
cms = CommunityMember.query.filter_by(user_id=user_id, is_moderator=True)
for cm in cms:
my_user['moderates'].append({'community': Community.api_json(variant=1, id=cm.community_id, stub=True), 'moderator': User.api_json(variant=1, id=user_id, stub=True)})
cms = CommunityMember.query.filter_by(user_id=user_id, is_banned=False)
for cm in cms:
my_user['follows'].append({'community': Community.api_json(variant=1, id=cm.community_id, stub=True), 'follower': User.api_json(variant=1, id=user_id, stub=True)})
"""
2024-10-05 20:55:04 +00:00
blocked_ids = db.session.execute(text('SELECT blocked_id FROM "user_block" WHERE blocker_id = :blocker_id'), {"blocker_id": user.id}).scalars()
for blocked_id in blocked_ids:
my_user['person_blocks'].append({'person': user_view(user, variant=1, stub=True), 'target': user_view(blocked_id, variant=1, stub=True)})
2024-10-07 13:57:19 +00:00
blocked_ids = db.session.execute(text('SELECT community_id FROM "community_block" WHERE user_id = :user_id'), {"user_id": user.id}).scalars()
for blocked_id in blocked_ids:
my_user['community_blocks'].append({'person': user_view(user, variant=1, stub=True), 'community': community_view(blocked_id, variant=1, stub=True)})
data = {
"version": "1.0.0",
"site": site
}
if user:
data['my_user'] = my_user
return data