Skip to content

Commit

Permalink
fix beatmap server, move error message variants to def
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Apr 28, 2024
1 parent 49edc2a commit 46c55e2
Show file tree
Hide file tree
Showing 27 changed files with 69 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/common/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GucchoError } from '~/server/trpc/messages'
import type { GucchoError } from '~/def/messages'

export function fromGucchoErrorCode<T extends GucchoError>(err: T): `G:${T}` {
return `G:${err}`
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/locale-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type RetrievablePath<T, Delimiter extends string, Path extends string = ''> =
T[K],
Delimiter,
Path extends ''
? `${K &(string |number)}`
? `${K &(string | number)}`
: `${Path}${Delimiter}${K & (string | number)}`
>
}
Expand Down
2 changes: 1 addition & 1 deletion src/composables/useEditorVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rank, modes, rulesets } from '../def'
import { Rank, modes, rulesets } from '~/def'

const ranks = [Rank.PPv1, Rank.PPv2, Rank.Score, Rank.RankedScore, Rank.TotalScore]

Expand Down
8 changes: 8 additions & 0 deletions src/server/trpc/messages/index.ts → src/def/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ export enum GucchoError {
UnknownError = 1,
MissingServerAvatarConfig,

// basic
ModeNotSupported = 1000,
InvalidId,

// user
UserNotFound = 2000,
Expand All @@ -21,15 +23,21 @@ export enum GucchoError {
OldPasswordMismatch,
EmailTokenNotFound,

// relation
RelationTypeNotFound = 4000,

ConflictRelation,
AtLeastOneUserNotExists,

// session
UnableToRetrieveSession = 5000,
UnableToRefreshToken,
YouNeedToLogin,
SessionNotFound,

// admin
RequireAdminPrivilege = 6000,

// beatmap
BeatmapNotFound = 7000,
}
8 changes: 4 additions & 4 deletions src/locales/@types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { VueMessageType } from '@nuxtjs/i18n/dist/runtime/composables'
import type { DeepPartial } from '@trpc/server'
import type { CountryCode } from '../def/country-code'
import type { GucchoError } from '../server/trpc/messages'
import type { Mail } from '../def/mail'
import type { RankingStatus } from '../def/beatmap'
import type { CountryCode } from '~/def/country-code'
import type { GucchoError } from '~/def/messages'
import type { Mail } from '~/def/mail'
import type { RankingStatus } from '~/def/beatmap'
import type { Scope, UserRole } from '~/def/user'
import { Lang, type Rank } from '~/def'
import type { ActiveMode, ActiveRuleset } from '~/def/common'
Expand Down
4 changes: 3 additions & 1 deletion src/locales/base/en-GB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CountryCode } from '~/def/country-code'
import { Scope, UserRole } from '~/def/user'
import { Mail } from '~/def/mail'
import { Mode, Rank, Ruleset } from '~/def'
import { GucchoError } from '~/server/trpc/messages'
import { GucchoError } from '~/def/messages'
import { RankingStatus } from '~/def/beatmap'

export default {
Expand Down Expand Up @@ -135,6 +135,8 @@ export default {
[GucchoError.DeletingMoreThanOneAvatars]: 'Attempting to delete more than 2 files. Please contact support to clean your old avatars.',
[GucchoError.RequireAdminPrivilege]: 'Requires staff roles.',
[GucchoError.EmailTokenNotFound]: 'Email verification token has expired.',
[GucchoError.InvalidId]: 'ID Invalid',
[GucchoError.BeatmapNotFound]: 'Beatmap not found',
},
country: {
[CountryCode.Unknown]: 'Unknown',
Expand Down
4 changes: 3 additions & 1 deletion src/locales/base/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RankingStatus } from '~/def/beatmap'
import { CountryCode } from '~/def/country-code'
import { Mail } from '~/def/mail'
import { Scope, UserRole } from '~/def/user'
import { GucchoError } from '~/server/trpc/messages'
import { GucchoError } from '~/def/messages'

export default {
// reuse en-GB
Expand Down Expand Up @@ -131,6 +131,8 @@ export default {
[GucchoError.YouNeedToLogin]: '你还未登录',
[GucchoError.SessionNotFound]: '找不到会话',
[GucchoError.RequireAdminPrivilege]: '需要管理员权限',
[GucchoError.InvalidId]: 'ID不合法',
[GucchoError.BeatmapNotFound]: '找不到图',
},

country: {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/auth/account-recovery.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
import md5 from 'md5'
import { GucchoError } from '~/server/trpc/messages'
import { GucchoError } from '~/def/messages'
import type { MailTokenProvider } from '$base/server'
import { useSession } from '~/store/session'
Expand Down
43 changes: 0 additions & 43 deletions src/pages/beatmap/[id].server.vue

This file was deleted.

2 changes: 1 addition & 1 deletion src/server/api/test/locale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { object, record, string, unknown } from 'zod'
import { GucchoError } from '../../trpc/messages'
import { GucchoError } from '~/def/messages'

const vQ = object({
key: string(),
Expand Down
2 changes: 1 addition & 1 deletion src/server/api/test/sendmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assertHaveSession } from '../../middleware/0.session'
import { assertLoggedIn } from '../../middleware/1.user'
import { assertIsAdmin } from '../../middleware/2.admin'
import { mail } from '../../singleton/service'
import { GucchoError } from '../../trpc/messages'
import { GucchoError } from '~/def/messages'

const vQ = object({
to: string().email(),
Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/bancho.py/server/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Logger } from '../log'
import { type DatabaseUserCompactFields, type DatabaseUserOptionalFields, fromCountryCode, toBanchoPyPriv, toSafeName, toUserCompact, toUserOptional } from '../transforms'
import { BanchoPyPrivilege } from '../enums'
import { useDrizzle } from './source/drizzle'
import { GucchoError } from '~/server/trpc/messages'
import { GucchoError } from '~/def/messages'
import { type UserClan, type UserCompact, type UserOptional, UserRole, type UserSecrets } from '~/def/user'
import { AdminProvider as Base } from '$base/server'

Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/bancho.py/server/clan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import * as schema from '../drizzle/schema'
import { config } from '../env'
import { Logger } from '../log'
import { type DatabaseUserCompactFields, assertIsBanchoPyMode, fromBanchoPyMode, idToString, stringToId, toBanchoPyMode, toRankingSystemScore, toUserAvatarSrc, toUserCompact } from '../transforms'
import { GucchoError } from '../../../trpc/messages'
import { BanchoPyScoreStatus, ClanPrivilege as BanchopyClanPrivilege } from './../enums'
import { useDrizzle, userPriv } from './source/drizzle'
import { GucchoError } from '~/def/messages'
import { type AbnormalStatus, type NormalBeatmapWithMeta, type RankingStatus } from '~/def/beatmap'
import { ClanProvider as Base } from '$base/server'
import { type Mode, Rank } from '~/def'
Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/bancho.py/server/rank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
} from '../transforms'
import * as schema from '../drizzle/schema'
import { BanchoPyScoreStatus } from '../enums'
import { GucchoError } from '../../../trpc/messages'
import { RedisNotReadyError, client as redisClient } from './source/redis'
import { useDrizzle, userPriv } from './source/drizzle'
import { prismaClient } from './source/prisma'
import { GucchoError } from '~/def/messages'
import type { ComponentLeaderboard } from '~/def/leaderboard'
import type { ActiveMode, AvailableRuleset, LeaderboardRankingSystem, RankingSystem } from '~/def/common'
import { type Mode, Rank } from '~/def'
Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/bancho.py/server/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ import {
toUserOptional,
} from '../transforms'
import * as schema from '../drizzle/schema'
import { GucchoError } from '../../../trpc/messages'
import { ArticleProvider } from './article'
import { prismaClient } from './source/prisma'
import { client as redisClient } from './source/redis'
import { UserRelationProvider } from './user-relations'
import { useDrizzle, userPriv } from './source/drizzle'
import { GucchoError } from '~/def/messages'
import { type DynamicSettingStore, Scope, type UserCompact, type UserOptional, UserRole, type UserStatistic, UserStatus } from '~/def/user'
import type { CountryCode } from '~/def/country-code'
import type { ActiveMode, ActiveRuleset, LeaderboardRankingSystem } from '~/def/common'
Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/[email protected]/server/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { useDrizzle, userPriv } from '../../../bancho.py/server/source/drizzle'
import { FilterType } from '../../../bancho.py/server/user'
import * as schema from '../../drizzle/schema'
import { Logger } from '../../log'
import { GucchoError } from '../../../../trpc/messages'
import { controlChars } from './reg-exps'
import { GucchoError } from '~/def/messages'
import type { Mode, Ruleset } from '~/def'
import type { CountryCode } from '~/def/country-code'
import { Scope, type UserCompact, UserRole, UserStatus } from '~/def/user'
Expand Down
1 change: 1 addition & 0 deletions src/server/routes/b/[id].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '../beatmap/[id]'
25 changes: 25 additions & 0 deletions src/server/routes/beatmap/[id].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GucchoError } from '~/def/messages'
import { maps } from '~/server/singleton/service'

export default defineEventHandler(async (h3Event) => {
const p = getRouterParams(h3Event)
const { id } = p

if (!id || Array.isArray(id)) {
throw createError({
statusCode: 400,
statusMessage: fromGucchoErrorCode(GucchoError.InvalidId),
})
}

const bm = await maps.getBeatmap(id).catch(_ => undefined)

if (!bm || !beatmapIsVisible(bm)) {
return createError({
statusCode: 404,
statusMessage: fromGucchoErrorCode(GucchoError.BeatmapNotFound),
})
}

await sendRedirect(h3Event, `/beatmapset/${bm.beatmapset.id}?$md5=${bm.md5}&mode=${bm.mode}`)
})
1 change: 1 addition & 0 deletions src/server/routes/beatmaps/[id].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '../beatmap/[id]'
2 changes: 1 addition & 1 deletion src/server/trpc/middleware/admin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GucchoError } from '../messages'
import { userProcedure } from './user'
import { GucchoError } from '~/def/messages'

export const roleProcedure = userProcedure.use(async ({ ctx, next }) => {
const role = computeUserRoles(ctx.user)
Expand Down
2 changes: 1 addition & 1 deletion src/server/trpc/middleware/session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GucchoError } from '../messages'
import { publicProcedure } from '../trpc'
import { GucchoError } from '~/def/messages'
import { Constant } from '~/server/common/constants'
import { haveSession } from '~/server/middleware/0.session'
import { sessions } from '~/server/singleton/service'
Expand Down
2 changes: 1 addition & 1 deletion src/server/trpc/middleware/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GucchoError } from '../messages'
import { sessionProcedure } from './session'
import { GucchoError } from '~/def/messages'
import { UserProvider, users } from '~/server/singleton/service'

export const userProcedure = sessionProcedure.use(async ({ ctx, next }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/server/trpc/routers/me.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ZodSchema, instanceof as instanceof_, nativeEnum, object, string } from 'zod'
import { GucchoError } from '../messages'
import { zodEmailValidation, zodHandle, zodRelationType, zodTipTapJSONContent } from '../shapes'
import { router as _router } from '../trpc'
import { GucchoError } from '~/def/messages'
import { settings } from '$active/dynamic-settings'
import { extractLocationSettings, extractSettingValidators } from '$base/@define-setting'
import { type MailTokenProvider } from '$base/server'
Expand Down
6 changes: 3 additions & 3 deletions src/server/trpc/routers/session.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { TRPCError } from '@trpc/server'
import { boolean, object, string } from 'zod'
import {
GucchoError,
} from '../messages'
import { sessionProcedure as pSession } from '../middleware/session'
import { zodHandle } from '../shapes'
import { router as _router, publicProcedure } from '../trpc'
import {
GucchoError,
} from '~/def/messages'
import { UserProvider, sessions, users } from '~/server/singleton/service'
import { Constant } from '~/server/common/constants'
import { Logger } from '$base/logger'
Expand Down
4 changes: 2 additions & 2 deletions src/server/trpc/routers/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TRPCError } from '@trpc/server'
import { array, number, object, string } from 'zod'
import { hasLeaderboardRankingSystem, hasRuleset } from '../config'
import { GucchoError } from '../messages'
import { optionalUserProcedure } from '../middleware/optional-user'
import { sessionProcedure } from '../middleware/session'
import {
Expand All @@ -14,17 +13,18 @@ import {
zodRuleset,
} from '../shapes'
import { router as _router, publicProcedure as p } from '../trpc'
import { Logger } from '$base/logger'
import { type MailTokenProvider as MBase, type MailTokenProvider } from '$base/server'
import { type Mode } from '~/def'
import { RankingStatus } from '~/def/beatmap'
import { type LeaderboardRankingSystem } from '~/def/common'
import { Mail } from '~/def/mail'
import { GucchoError } from '~/def/messages'
import { type RankingSystemScore } from '~/def/score'
import { Scope, type UserCompact, UserRole } from '~/def/user'
import { Constant } from '~/server/common/constants'
import { MapProvider, ScoreProvider, UserProvider, mail, mailToken, sessions, userRelations, users } from '~/server/singleton/service'
import ui from '~~/guccho.ui.config'
import { Logger } from '$base/logger'

const logger = Logger.child({ label: 'user' })

Expand Down
4 changes: 3 additions & 1 deletion src/server/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TRPCError } from '@trpc/server'
import { GucchoError } from '../trpc/messages'
import { GucchoError } from '~/def/messages'

export function throwGucchoError(code: GucchoError): never {
throw createGucchoError(code)
Expand Down Expand Up @@ -30,6 +30,7 @@ export function createGucchoError(code: GucchoError): TRPCError {
}

case GucchoError.DeletingMoreThanOneAvatars:
case GucchoError.InvalidId:
case GucchoError.HackerTryingToDeleteAllAvatars: {
return new TRPCError(merge({ code: 'BAD_REQUEST' }))
}
Expand All @@ -46,6 +47,7 @@ export function createGucchoError(code: GucchoError): TRPCError {
case GucchoError.RelationTypeNotFound:
case GucchoError.SessionNotFound:
case GucchoError.UserNotFound:
case GucchoError.BeatmapNotFound:
case GucchoError.AtLeastOneUserNotExists: {
return new TRPCError(merge({ code: 'NOT_FOUND' }))
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/dynamic-settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia'
import { DynamicSettingStore } from '../def/user'
import { DynamicSettingStore } from '~/def/user'
import { type ExtractSettingType, extractLocationSettings, extractSettingValidators } from '$base/@define-setting'
import { IntlPicker } from '$base/intl-picker'
import { settings } from '$active/dynamic-settings'
Expand Down

0 comments on commit 46c55e2

Please sign in to comment.