Skip to content

Commit

Permalink
fix; show user nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 20, 2024
1 parent fcbc2ef commit 7c83b86
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
48 changes: 34 additions & 14 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def static_file_handler(fp: str) -> Response[bytes]:
raise NotFoundException() # noqa: B904


async def __fetch_users(rows: list[asyncpg.Record]) -> dict[int, asyncpg.Record]:
user_id = {x["from_user_id"] for x in rows} | {x["wiki_user_id"] for x in rows}
user_id.discard(None)
user_id.discard(0)

users = {
x["user_id"]: x
for x in await pg.fetch("select * from patch_users where user_id = any($1)", user_id)
}

return users


@litestar.get("/")
async def index(request: Request) -> Template:
if not request.auth:
Expand All @@ -79,22 +92,21 @@ async def index(request: Request) -> Template:
"select * from patch where from_user_id = $1 and deleted_at is NULL order by created_at desc",
request.auth.user_id,
)
return Template("list.html.jinja2", context={"rows": rows, "auth": request.auth})

rows = await pg.fetch(
"""
select * from patch where deleted_at is NULL and state = $1
union
(select * from patch where deleted_at is NULL and state != $1 order by updated_at desc limit 10)
""",
PatchState.Pending,
)
else:
rows = await pg.fetch(
"""
select * from patch where deleted_at is NULL and state = $1
union
(select * from patch where deleted_at is NULL and state != $1 order by updated_at desc limit 10)
""",
PatchState.Pending,
)

rows.sort(key=__index_row_sorter, reverse=True)
rows.sort(key=__index_row_sorter, reverse=True)

return Template(
"list.html.jinja2",
context={"rows": rows, "auth": request.auth},
context={"rows": rows, "auth": request.auth, "users": await __fetch_users(rows)},
)


Expand All @@ -104,13 +116,17 @@ async def show_user_contrib(user_id: int, request: Request) -> Template:
"select * from patch where from_user_id = $1 and deleted_at is NULL order by created_at desc",
user_id,
)

users = await __fetch_users(rows)

return Template(
"list.html.jinja2",
context={
"rows": rows,
"users": users,
"auth": request.auth,
"user_id": user_id,
"title": f"{user_id} 的历史贡献",
"title": f"{users[user_id]['nickname']} 的历史贡献",
},
)

Expand All @@ -121,13 +137,17 @@ async def show_user_review(user_id: int, request: Request) -> Template:
"select * from patch where wiki_user_id = $1 and deleted_at is NULL order by created_at desc",
user_id,
)

users = await __fetch_users(rows)

return Template(
"list.html.jinja2",
context={
"rows": rows,
"users": users,
"auth": request.auth,
"user_id": user_id,
"title": f"{user_id} 的历史审核",
"title": f"{users[user_id]['nickname']} 的历史审核",
},
)

Expand Down
8 changes: 4 additions & 4 deletions server/templates/list.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
{% if user_id is defined %}
{% include 'component/header.html' %}
<div class="row m-1">
<a href="https://bgm.tv/user/{{ user_id }}">
<h3>用户 {{ user_id }} 的个人主页</h3>
<a href="https://bgm.tv/user/{{ user_id }}" style="text-underline: none">
<h3>用户 {{ users.get(user_id).nickname }} 的个人主页</h3>
</a>
</div>
{% else %}
Expand Down Expand Up @@ -89,11 +89,11 @@
</div>

<small>from:
<span class="badge bg-light text-dark">{{ patch.from_user_id }}</span>
<span class="badge bg-light text-dark">{{ users.get(patch.from_user_id).nickname | default(patch.from_user_id) }}</span>
</small>
{% if patch.state != 0 %}
<small>reviewed by:
<span class="badge bg-light text-dark">{{ patch.wiki_user_id }}</span>
<span class="badge bg-light text-dark">{{ users.get(patch.wiki_user_id).nickname | default(patch.wiki_user_id) }}</span>
</small>
{% endif %}
</a>
Expand Down

0 comments on commit 7c83b86

Please sign in to comment.