From d962f9c8ac30e95c24a23171c1d0a75f537ff59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E3=83=90=E3=83=8B=E3=83=A9=E3=83=BC?= <118162831+KirisameVanilla@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:49:35 -0500 Subject: [PATCH 1/2] refactor: migrate off deprecated APIs and load avatars by avatarId The backend deprecated several routes on 2026-10-21 and now returns avatar IDs with the listings that show users. - Call /api/maiscore/* instead of the deprecated routes: leaderboard/chart/by-id for GET maichart/{id}/score, leaderboard/period for POST playhistory/ranking, and sum/chart/by-uploader/{uploader} for GET stats/score-sums?uploader=. - Drop the deprecated CommentPage.commentCount field from the types. - Load every avatar through GET /api/avatar/{avatarId} with the new UserAvatar component and useAvatarUrl hook; remove the account/icon?username= endpoint from the frontend. - Use the avatarId that score sums (player), the season leaderboard (player), comments (authorProfile) and the chart leaderboard (player.avatarId) now carry; only chart uploaders and rows without a profile still read the public profile, once per username. - Add UserBrief and the new optional fields to the types. - Update tests, the e2e API mocks and api-directory.md. --- api-directory.md | 10 ++- e2e/live2d-mascot.spec.ts | 6 +- src/components/header/UserMenu.tsx | 8 +- src/components/score/ScoreCard.tsx | 8 +- src/components/score/ScoreCount.tsx | 12 +-- src/components/season/SeasonLeaderboard.tsx | 2 +- src/components/song/ScoreRanking.tsx | 8 +- src/components/song/SongComments.tsx | 9 ++- src/components/song/commentTree.ts | 2 +- src/components/user/AvatarUploader.tsx | 5 +- src/components/user/UserAvatar.tsx | 29 +++++++ src/components/user/UserInfo.tsx | 7 +- src/config/api.ts | 23 +++--- src/hooks/useAvatarUrl.ts | 36 +++++++++ src/hooks/useSeasonBreakdown.ts | 2 +- src/hooks/useSeasonRanking.ts | 2 +- src/pages/SongPage.tsx | 6 +- src/types/components.ts | 2 + src/types/event.ts | 3 + src/types/interact.ts | 5 +- src/types/score.ts | 4 + src/types/user.ts | 11 +++ test/components/score/ScoreCount.test.tsx | 25 +++++- .../season/SeasonLeaderboard.test.tsx | 14 +++- test/components/song/SongComments.test.tsx | 35 +++++++-- test/components/song/commentTree.test.ts | 5 +- test/components/user/UserAvatar.test.tsx | 78 +++++++++++++++++++ 27 files changed, 296 insertions(+), 61 deletions(-) create mode 100644 src/components/user/UserAvatar.tsx create mode 100644 src/hooks/useAvatarUrl.ts create mode 100644 test/components/user/UserAvatar.test.tsx diff --git a/api-directory.md b/api-directory.md index d2f08e9..613cd96 100644 --- a/api-directory.md +++ b/api-directory.md @@ -25,7 +25,7 @@ - GET `/api/account/profile`:当前用户资料;GET `/api/account/profile?username=...`:公开资料。 - 字段:`id`、`username`、`nickname`、`email`、`introduction`、`avatarId`、`joinDate`。其他用户的 `email` 为 null。 - POST `/api/account/profile`:FormData,可提交 `nickname`、`introduction`、`avatar`,返回更新后的资料。 -- GET `/api/avatar/{avatarId}`:头像。有资料对象时直接使用此路径;只有用户名时保留后端仍支持的 `/api/account/icon?username=...` 重定向。 +- GET `/api/avatar/{avatarId}`:头像,前端只用这一个接口(`UserAvatar` 组件 / `useAvatarUrl`)。已知 `avatarId` 时直接使用、不发请求:资料对象自带 `avatarId`;总分榜(`/api/maiscore/sum/...` 的 `players[].player`)、季赛榜(`/api/maiscore/leaderboard/period` 每条的 `player`)和评论(`authorProfile`)附带精简资料 `UserBrief` = `{ id, username, nickname, avatarId }`(未上传头像时 `avatarId` 为 `default`,`nickname` 可能为空串;账号已删除或评论为占位符时为 null);单曲排行榜 `scores[][].player` 的 `id`、`avatarId` 也已填充。只有谱面上传者(谱面响应不带资料)以及上述资料为 null 或缺失时,才按用户名读 GET `/api/account/profile?username=...` 取 `avatarId`,同名只请求一次并与个人空间共用缓存;用户不存在或无用户名时用 `default`。这些榜单和评论仍显示用户名,不换成昵称。`/api/account/icon?username=...` 仅留给 MajdataPlay,前端不再调用。 - 头像和简介上传后刷新资料,避免旧头像缓存;昵称用于显示,用户名继续用于链接、归属判断和 API 查询。 - GET `/api/account/recent?username=...`、GET `/api/account/scores` 的已有调用协议保持兼容。 @@ -34,8 +34,10 @@ | 功能 | 对齐结果 | | --- | --- | | 全站总分榜 | 改用 GET `/api/maiscore/sum/all?page=0&pageSize=...` | -| 上传者榜 | 继续使用已实现的 GET `/api/stats/score-sums?uploader=...&page=...&pageSize=...`;新 `/maiscore/sum/chart/by-uploader/...` 仍为空实现,不切换 | -| 总分返回值 | 从旧数组和 `dxAccSum` 改为 `{ geneTime, players: [{ userId, username, acc: { dx, classic } }] }` | +| 上传者榜 | 改用 GET `/api/maiscore/sum/chart/by-uploader/{uploader}?page=0&pageSize=...`(`uploader` 作为路径段转义);旧 `/api/stats/score-sums` 已废弃 | +| 单曲排行榜/成绩卡排名 | 改用 GET `/api/maiscore/leaderboard/chart/by-id/{chartId}`;旧 GET `/api/maichart/{chartId}/score` 已废弃 | +| 季赛榜 | 改用 POST `/api/maiscore/leaderboard/period`,请求体不变;旧 POST `/api/playhistory/ranking` 已废弃 | +| 总分返回值 | 从旧数组和 `dxAccSum` 改为 `{ geneTime, players: [{ userId, username, acc: { dx, classic }, player }] }`,`player` 为 `UserBrief \| null` | | 收藏歌单列表 | GET `/api/account/favorite/collection/list` 返回 `songHashs`,卡片的 `count` 从数组长度计算 | | 谱面投票 | PUT `/api/maichart/{chartId}/vote`,JSON `{ value: 1 \| 0 \| -1 }`,返回 `{ score, upvotes, downvotes, myVote }`;旧的 like/dislike Form 接口只保留给游戏客户端 | | 列表计数 | GET `/api/maichart/{chartId}/interactsum` 返回 `{ chartId, plays, score, upvotes, downvotes, comments, myVote }`;列表一页用 POST `/api/maichart/interactsum`(请求体为 ID 数组)一次取回 | @@ -50,7 +52,7 @@ 以下是目标提交本身的问题,前端未通过修改查询含义、吞掉失败或重复写入来绕过: -1. `src/WebApplication2/Controllers/StatisticsController.cs:34`:`page` 限制为至少 1,但默认值为 0,且服务按 `Skip(page * pageSize)` 做零起点分页。`page=0` 会被校验拒绝,改成 1 会漏掉第一页。影响上传者榜;应允许 `page >= 0`。全站榜已改用允许 0 的新接口。 +1. ~~`src/WebApplication2/Controllers/StatisticsController.cs:34`:`page` 限制为至少 1,但默认值为 0,且服务按 `Skip(page * pageSize)` 做零起点分页。~~ 已解决:上传者榜已改用 `/api/maiscore/sum/chart/by-uploader/{uploader}`,该接口允许 `page >= 0`。 2. `src/WebApplication2/Controllers/MaiChartApi/InteractController.cs:175`:评论/回复分支声明 `newInteract` 后没有接收 `AddCommentAsync` / `ReplyCommentAsync` 的返回值,写入后仍检查 null 并返回 400。应接收返回值并据此判断结果,否则用户可能因错误提示重复提交。 3. `src/WebApplication2/Controllers/AccountApi/AccountController.cs:218`:旧的待激活账号登录仍发送激活链接,但 `GET /account/verify` 已移除。前端提示旧账号联系管理员;需要后端提供旧账户迁移或新的激活流程。 4. `src/WebApplication2/Controllers/CollectionController.cs:209`:空字符串描述被 `SetIf(!string.IsNullOrEmpty(...))` 忽略,现有歌单不能清空描述。需要将“未传字段”与“传入空字符串”区分。资料简介也有相同限制,前端继续禁止空简介提交。 diff --git a/e2e/live2d-mascot.spec.ts b/e2e/live2d-mascot.spec.ts index c5c7771..78c63d6 100644 --- a/e2e/live2d-mascot.spec.ts +++ b/e2e/live2d-mascot.spec.ts @@ -21,15 +21,15 @@ const songs = Array.from({ length: 6 }, (_, index) => ({ async function mockApi(page: Page) { await page.route('**/api3/**', async (route) => { const path = new URL(route.request().url()).pathname; - if (path.endsWith('/image') || path.endsWith('/Icon')) { + if (path.endsWith('/image') || path.includes('/avatar/')) { await route.fulfill({ path: 'public/xxlb.jpg', contentType: 'image/jpeg' }); return; } - const body = path.includes('/account/info') ? {} + const body = path.endsWith('/account/profile') ? {} : path.endsWith('/maichart/list') ? songs : path.endsWith('/summary') ? songs[0] : path.endsWith('/interactsum') ? { likeCount: 0, commentCount: 0, playCount: 0 } - : path.endsWith('/score') ? { scores: [] } + : path.includes('/maiscore/leaderboard/chart/by-id/') ? { scores: [] } : []; await route.fulfill({ json: body }); }); diff --git a/src/components/header/UserMenu.tsx b/src/components/header/UserMenu.tsx index 36945da..332aa6d 100644 --- a/src/components/header/UserMenu.tsx +++ b/src/components/header/UserMenu.tsx @@ -2,8 +2,8 @@ import { useTranslation } from 'react-i18next'; import { useState, useRef } from 'react'; import { Link } from 'react-router-dom'; import { useUserContext } from '@/hooks'; +import UserAvatar from '@/components/user/UserAvatar'; import { handleLogout as logoutUtil } from '@/utils'; -import { endpoints } from '@/config/api'; import { DIVIDER } from './styles'; import Dropdown from './Dropdown'; @@ -46,10 +46,10 @@ export default function UserMenu({ username }: UserMenuProps) { aria-expanded={isOpen} aria-label={t("shared/UserMenu.UserMenu")} > - {username} {user?.nickname || username} diff --git a/src/components/score/ScoreCard.tsx b/src/components/score/ScoreCard.tsx index 1535bd6..f02fbeb 100644 --- a/src/components/score/ScoreCard.tsx +++ b/src/components/score/ScoreCard.tsx @@ -8,6 +8,7 @@ import useSWR from 'swr'; import { Link } from 'react-router-dom'; import { CoverPic, Level, LazyLoad } from '@/components'; import { ChartVoteBadge } from '@/components/song/SongInteraction'; +import UserAvatar from '@/components/user/UserAvatar'; import { apiFetcher } from '@/utils/apiClient'; import { endpoints } from '@/config/api'; import { getComboState } from '@/utils'; @@ -68,7 +69,7 @@ export function ScoreCard({ const targetUsername = rankUsername || currentUsername; const { data: chartScores } = useSWR( - showRank && targetUsername ? endpoints.maichart.score(score.chartInfo.id) : null, + showRank && targetUsername ? endpoints.maiscore.chartLeaderboard(score.chartInfo.id) : null, apiFetcher ); @@ -138,10 +139,9 @@ export function ScoreCard({
- {score.chartInfo.uploader} {score.chartInfo.designer} diff --git a/src/components/score/ScoreCount.tsx b/src/components/score/ScoreCount.tsx index 2214358..a00c615 100644 --- a/src/components/score/ScoreCount.tsx +++ b/src/components/score/ScoreCount.tsx @@ -2,6 +2,7 @@ import { useTranslation } from 'react-i18next'; import { endpoints } from '@/config/api'; import useSWR from 'swr'; import { LoadingSpinner } from '@/components'; +import UserAvatar from '@/components/user/UserAvatar'; import type { ScoreCardProps, ScoreCountProps, ScoreSumResponse } from '@/types'; import { Link } from 'react-router-dom'; @@ -16,7 +17,7 @@ export default function ScoreCount({ uploader, page = 0, pageSize = 10 }: ScoreC const { t } = useTranslation(); const { data: response, error, isLoading } = useSWR( - endpoints.stats.scoreSums(uploader, page, pageSize), + endpoints.maiscore.scoreSums(uploader, page, pageSize), fetcher, { refreshInterval: 30000 } // 每30秒刷新一次 ); @@ -54,6 +55,7 @@ export default function ScoreCount({ uploader, page = 0, pageSize = 10 }: ScoreC key={player.username} rank={page * pageSize + index + 1} username={player.username} + avatarId={player.player?.avatarId} scoresum={player.acc.dx} maxscore={maxScore} /> @@ -66,7 +68,7 @@ export default function ScoreCount({ uploader, page = 0, pageSize = 10 }: ScoreC /** * 单个分数卡片组件 */ -export function ScoreCard({ rank, username, scoresum, maxscore, footer }: ScoreCardProps) { +export function ScoreCard({ rank, username, avatarId, scoresum, maxscore, footer }: ScoreCardProps) { const percentage = maxscore > 0 ? (scoresum / maxscore) * 100 : 0; const isFirst = rank === 1; @@ -119,14 +121,14 @@ export function ScoreCard({ rank, username, scoresum, maxscore, footer }: ScoreC {/* 玩家头像 */}
- {username}
diff --git a/src/components/season/SeasonLeaderboard.tsx b/src/components/season/SeasonLeaderboard.tsx index 6eb3ef6..2e265ac 100644 --- a/src/components/season/SeasonLeaderboard.tsx +++ b/src/components/season/SeasonLeaderboard.tsx @@ -62,7 +62,7 @@ export default function SeasonLeaderboard({ event, status, chartState }: SeasonL
  • {t('season/SeasonLeaderboard.Rank')} #{player.rank}

    - } />
  • diff --git a/src/components/song/ScoreRanking.tsx b/src/components/song/ScoreRanking.tsx index 433b21e..a5ee0ed 100644 --- a/src/components/song/ScoreRanking.tsx +++ b/src/components/song/ScoreRanking.tsx @@ -10,6 +10,7 @@ import { motion, AnimatePresence } from 'framer-motion'; import { endpoints } from '@/config/api'; import { getComboState, getLevelName } from '@/utils'; import { LoadingSpinner } from '@/components'; +import UserAvatar from '@/components/user/UserAvatar'; import { Link } from 'react-router-dom'; import type { ChartScore, ScoreListProps } from '@/types'; @@ -225,14 +226,15 @@ function RankingCard({ > {/* 头像 */}
    - {score.player.nickname
    @@ -356,7 +358,7 @@ export function ScoreRanking({ songid }: ScoreListProps) { const [transitionDirection, setTransitionDirection] = useState<1 | -1>(1); const { data, error, isLoading } = useSWR( - endpoints.maichart.score(songid), + endpoints.maiscore.chartLeaderboard(songid), fetcher, { revalidateOnFocus: true } ); diff --git a/src/components/song/SongComments.tsx b/src/components/song/SongComments.tsx index a5afbd9..9134393 100644 --- a/src/components/song/SongComments.tsx +++ b/src/components/song/SongComments.tsx @@ -26,6 +26,7 @@ import { } from 'react-icons/pi'; import { endpoints } from '@/config/api'; import { LoadingSpinner } from '@/components'; +import UserAvatar from '@/components/user/UserAvatar'; import { useIsAdministrator, useUserContext } from '@/hooks'; import { apiFetcher, apiRequest, jsonRequest } from '@/utils/apiClient'; import { VoteControl } from './VoteControl'; @@ -219,8 +220,8 @@ function foldFrom(policy: FoldPolicy, level: number, maxDepth: number): FoldPoli return { ...policy, depth: Math.min(policy.depth, maxDepth - level) }; } -function Avatar({ username, className }: { username: string; className: string }) { - return {username}; +function Avatar({ username, avatarId, className }: { username: string; avatarId?: string | null; className: string }) { + return ; } function CommentNode({ comment, level }: { comment: Comment; level: number }) { @@ -301,7 +302,7 @@ function CommentNode({ comment, level }: { comment: Comment; level: number }) {