Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 21x 21x 19x 21x 5x 5x 16x 3x 3x 2x 2x 2x | import { eq } from 'drizzle-orm';
import type { Database } from '../db/client.ts';
import { chart, profile, score, scoreResult, user } from '../db/schema.ts';
import { byRulesetOrder } from '../rulesets.ts';
import { canSeeScore } from './visibility.ts';
/** One play with its chart, its player (null when the profile is gone) and its figures under every ruleset. */
export async function loadScoreDetail(db: Database, scoreId: string) {
const [[row], results] = await Promise.all([
db
.select({
score,
chart,
handle: profile.handle,
displayName: profile.displayName,
anonymous: user.isAnonymous,
avatarKey: profile.avatarKey,
})
.from(score)
.innerJoin(chart, eq(chart.chartFingerprint, score.chartFingerprint))
.leftJoin(user, eq(user.id, score.userId))
.leftJoin(profile, eq(profile.userId, score.userId))
.where(eq(score.id, scoreId)),
db.select().from(scoreResult).where(eq(scoreResult.scoreId, scoreId)),
]);
if (row === undefined) return undefined;
const player =
row.handle !== null && row.displayName !== null
? {
handle: row.handle,
displayName: row.displayName,
anonymous: row.anonymous === true,
avatarKey: row.avatarKey,
}
: null;
return { score: row.score, chart: row.chart, player, results: results.sort(byRulesetOrder) };
}
export type ScoreDetail = NonNullable<Awaited<ReturnType<typeof loadScoreDetail>>>;
/** A play as `GET /api/v0/scores/:id` answers it (`scoreResponse` in `src/api/schemas.ts`). */
export function toScoreResponse(detail: ScoreDetail) {
const { score: found, player } = detail;
return {
id: found.id,
chartFingerprint: found.chartFingerprint,
playFingerprint: found.playFingerprint,
player: player === null ? null : { handle: player.handle, displayName: player.displayName },
playedRuleset: found.playedRuleset,
gauge: found.gauge,
assisted: found.assisted,
assistFlags: found.assistFlags,
verification: found.verification,
createdAt: found.createdAt.getTime(),
hidden: found.hidden,
results: detail.results.map(({ scoreId: _scoreId, ...result }) => result),
};
}
/**
* Where a play's log is stored, if the viewer may read it: anyone for a public replay of a shown play, only its player
* for a private replay or a hidden play. Undefined otherwise, so a private replay looks the same as a missing one.
*/
export async function loadReplayAccess(
db: Database,
scoreId: string,
viewerId: string | undefined,
): Promise<{ key: string; public: boolean } | undefined> {
const found = await db.query.score.findFirst({
where: eq(score.id, scoreId),
columns: { userId: true, replayKey: true, replayVisibility: true, hidden: true },
});
if (found === undefined || !canSeeScore(found, viewerId)) return undefined;
const isPublic = found.replayVisibility === 'public';
Iif (!isPublic && found.userId !== viewerId) return undefined;
return { key: found.replayKey, public: isPublic };
}
|