forked from retoor/devplacepy
update
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from devplacepy.database import (
|
||||
get_daily_topic,
|
||||
get_polls_by_post_uids,
|
||||
get_reactions_by_targets,
|
||||
get_recent_comments_by_post_uids,
|
||||
get_site_stats,
|
||||
get_top_authors,
|
||||
get_user_bookmarks,
|
||||
get_users_by_uids,
|
||||
)
|
||||
from devplacepy.database.attachments_data import get_attachments_by_type
|
||||
from devplacepy.routers.feed import get_feed_posts
|
||||
|
||||
|
||||
def build_feed_page(
|
||||
user: dict | None = None,
|
||||
tab: str = "all",
|
||||
topic: str | None = None,
|
||||
search: str = "",
|
||||
before: str | None = None,
|
||||
) -> dict:
|
||||
posts, next_cursor = get_feed_posts(user, tab, topic, search, before)
|
||||
stats = get_site_stats()
|
||||
top_authors = get_top_authors(5)
|
||||
daily_topic = get_daily_topic()
|
||||
post_uids = [item["post"]["uid"] for item in posts]
|
||||
attachments_map = get_attachments_by_type("post", post_uids)
|
||||
recent_comments = get_recent_comments_by_post_uids(post_uids, 3, user)
|
||||
reactions_map = get_reactions_by_targets("post", post_uids, user)
|
||||
bookmark_set = (
|
||||
get_user_bookmarks(user["uid"], "post", post_uids) if user else set()
|
||||
)
|
||||
polls_map = get_polls_by_post_uids(post_uids, user)
|
||||
for item in posts:
|
||||
uid = item["post"]["uid"]
|
||||
item["attachments"] = attachments_map.get(uid, [])
|
||||
item["recent_comments"] = recent_comments.get(uid, [])
|
||||
item["reactions"] = reactions_map.get(uid, {"counts": {}, "mine": []})
|
||||
item["bookmarked"] = uid in bookmark_set
|
||||
item["poll"] = polls_map.get(uid)
|
||||
return {
|
||||
"posts": posts,
|
||||
"next_cursor": next_cursor,
|
||||
"stats": stats,
|
||||
"top_authors": top_authors,
|
||||
"daily_topic": daily_topic,
|
||||
"online_users": [],
|
||||
"current_tab": tab,
|
||||
"current_topic": topic,
|
||||
"search": search,
|
||||
}
|
||||
|
||||
|
||||
def build_post_detail(post_uid: str, user: dict | None = None) -> dict:
|
||||
from devplacepy.database import get_table, load_comments
|
||||
|
||||
post = get_table("posts").find_one(uid=post_uid, deleted_at=None)
|
||||
if not post:
|
||||
return {"post": None, "author": None, "comments": [], "attachments": []}
|
||||
author = get_users_by_uids([post["user_uid"]]).get(post["user_uid"])
|
||||
comments = load_comments("post", post_uid, user)
|
||||
attachments = get_attachments_by_type("post", [post_uid]).get(post_uid, [])
|
||||
return {
|
||||
"post": post,
|
||||
"author": author,
|
||||
"comments": comments,
|
||||
"attachments": attachments,
|
||||
}
|
||||
|
||||
|
||||
def build_profile_bundle(profile_uid: str, viewer: dict | None = None) -> dict:
|
||||
from devplacepy.database import get_table
|
||||
from devplacepy.database.follows import get_follow_counts
|
||||
|
||||
user = get_table("users").find_one(uid=profile_uid)
|
||||
if not user:
|
||||
return {"user": None, "follow_counts": {}, "awards": [], "posts_count": 0}
|
||||
follow_counts = get_follow_counts(profile_uid)
|
||||
return {
|
||||
"user": user,
|
||||
"follow_counts": follow_counts,
|
||||
"awards": [],
|
||||
"posts_count": 0,
|
||||
"viewer_relation": {},
|
||||
}
|
||||
|
||||
|
||||
def build_messages_page(user_uid: str) -> dict:
|
||||
return {"threads": [], "unread": 0, "partners": {}}
|
||||
|
||||
|
||||
def build_notifications_page(user_uid: str) -> dict:
|
||||
return {"notifications": [], "actors": {}, "unread_count": 0}
|
||||
|
||||
|
||||
def build_leaderboard_page(viewer_uid: str | None = None) -> dict:
|
||||
from devplacepy.database import get_leaderboard, get_user_rank
|
||||
|
||||
return {
|
||||
"leaderboard": get_leaderboard(),
|
||||
"viewer_rank": get_user_rank(viewer_uid) if viewer_uid else None,
|
||||
}
|
||||
|
||||
|
||||
def build_project_detail(project_uid: str) -> dict:
|
||||
from devplacepy.database import get_table
|
||||
|
||||
project = get_table("projects").find_one(uid=project_uid, deleted_at=None)
|
||||
owner = None
|
||||
if project:
|
||||
owner = get_users_by_uids([project["user_uid"]]).get(project["user_uid"])
|
||||
return {"project": project, "owner": owner, "files": [], "containers": []}
|
||||
|
||||
|
||||
def build_gist_detail(gist_uid: str, user: dict | None = None) -> dict:
|
||||
from devplacepy.database import get_table, load_comments
|
||||
|
||||
gist = get_table("gists").find_one(uid=gist_uid, deleted_at=None)
|
||||
author = None
|
||||
if gist:
|
||||
author = get_users_by_uids([gist["user_uid"]]).get(gist["user_uid"])
|
||||
comments = load_comments("gist", gist_uid, user) if gist else []
|
||||
return {"gist": gist, "author": author, "comments": comments}
|
||||
|
||||
|
||||
def build_game_state(user_uid: str | None = None) -> dict:
|
||||
return {"store": {}, "leaderboard": []}
|
||||
|
||||
|
||||
def build_admin_dashboard() -> dict:
|
||||
return {"stats": get_site_stats(), "service_states": []}
|
||||
Reference in New Issue
Block a user