From ee86fb1af7f7780732502f9c3decda79c2ef8e17 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 31 Mar 2022 00:16:32 +0100 Subject: [PATCH] Make max goals and milestones configurable --- server/src/lib/setup/config.ts | 6 ++++++ server/src/lib/targets/goals.ts | 2 +- .../games/_game/_playtype/targets/goals/router.ts | 5 +++-- .../games/_game/_playtype/targets/milestones/router.ts | 10 ++++++---- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/server/src/lib/setup/config.ts b/server/src/lib/setup/config.ts index afbee29fe..d2de97f81 100644 --- a/server/src/lib/setup/config.ts +++ b/server/src/lib/setup/config.ts @@ -79,6 +79,8 @@ export interface TachiServerConfig { }; USC_QUEUE_SIZE: integer; BEATORAJA_QUEUE_SIZE: integer; + MAX_GOAL_SUBSCRIPTIONS: integer; + MAX_MILESTONE_SUBSCRIPTIONS: integer; OUR_URL: string; INVITE_CODE_CONFIG?: { BATCH_SIZE: integer; @@ -152,6 +154,8 @@ const err = p(config, { }), USC_QUEUE_SIZE: p.optional(p.gteInt(2)), BEATORAJA_QUEUE_SIZE: p.optional(p.gteInt(2)), + MAX_GOAL_SUBSCRIPTIONS: p.optional(p.isPositiveInteger), + MAX_MILESTONE_SUBSCRIPTIONS: p.optional(p.isPositiveInteger), OUR_URL: "string", INVITE_CODE_CONFIG: p.optional({ BATCH_SIZE: p.isPositiveInteger, @@ -209,6 +213,8 @@ tachiServerConfig.RATE_LIMIT ??= 500; tachiServerConfig.OAUTH_CLIENT_CAP ??= 15; tachiServerConfig.USC_QUEUE_SIZE ??= 3; tachiServerConfig.BEATORAJA_QUEUE_SIZE ??= 3; +tachiServerConfig.MAX_GOAL_SUBSCRIPTIONS ??= 1_000; +tachiServerConfig.MAX_MILESTONE_SUBSCRIPTIONS ??= 100; // Assign sane defaults to the logger config. tachiServerConfig.LOGGER_CONFIG = Object.assign( diff --git a/server/src/lib/targets/goals.ts b/server/src/lib/targets/goals.ts index fb8dd594f..a72b04778 100644 --- a/server/src/lib/targets/goals.ts +++ b/server/src/lib/targets/goals.ts @@ -320,7 +320,7 @@ export async function SubscribeToGoal( progress: result.progress, progressHuman: result.progressHuman, userID, - lastInteraction: Date.now(), + lastInteraction: null, timeAchieved: result.achieved ? Date.now() : null, timeSet: Date.now(), // if this goal subscription came from a milestone, add the milestone to diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/goals/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/goals/router.ts index 387c4348e..f5339826a 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/goals/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/goals/router.ts @@ -11,6 +11,7 @@ import { AssignToReqTachiData } from "utils/req-tachi-data"; import { GetGoalForIDGuaranteed, GetMilestoneForIDGuaranteed } from "utils/db"; import { RequireAuthedAsUser } from "../../../../../middleware"; import { SubscribeFailReasons } from "lib/constants/err-codes"; +import { ServerConfig } from "lib/setup/config"; const router: Router = Router({ mergeParams: true }); @@ -136,10 +137,10 @@ router.post( playtype, }); - if (existingGoalsCount > 1_000) { + if (existingGoalsCount > ServerConfig.MAX_GOAL_SUBSCRIPTIONS) { return res.status(400).json({ success: false, - description: `You already have 1000 goals. You cannot have anymore.`, + description: `You already have ${ServerConfig.MAX_GOAL_SUBSCRIPTIONS} goals. You cannot have anymore.`, }); } diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts index 2aae070e2..6ec77b3b3 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts @@ -3,15 +3,16 @@ import db from "external/mongo/db"; import { SubscribeFailReasons } from "lib/constants/err-codes"; import { SYMBOL_TachiData } from "lib/constants/tachi"; import CreateLogCtx from "lib/logger/logger"; +import { ServerConfig } from "lib/setup/config"; import { EvaluateMilestoneProgress, SubscribeToMilestone, UnsubscribeFromMilestone, } from "lib/targets/milestones"; import { RequirePermissions } from "server/middleware/auth"; -import { GetMilestoneForIDGuaranteed } from "utils/db"; import { AssignToReqTachiData } from "utils/req-tachi-data"; import { FormatUserDoc } from "utils/user"; +import { RequireAuthedAsUser } from "../../../../../middleware"; const logger = CreateLogCtx(__filename); @@ -72,7 +73,6 @@ const GetMilestoneSubscription: RequestHandler = async (req, res, next) => { }; const GetMilestone: RequestHandler = async (req, res, next) => { - const user = req[SYMBOL_TachiData]!.requestedUser!; const game = req[SYMBOL_TachiData]!.game!; const playtype = req[SYMBOL_TachiData]!.playtype!; @@ -130,6 +130,7 @@ router.get("/:milestoneID", GetMilestone, GetMilestoneSubscription, async (req, */ router.put( "/:milestoneID", + RequireAuthedAsUser, GetMilestone, RequirePermissions("manage_targets"), async (req, res) => { @@ -143,10 +144,10 @@ router.put( playtype, }); - if (existingMilestonesCount > 100) { + if (existingMilestonesCount > ServerConfig.MAX_MILESTONE_SUBSCRIPTIONS) { return res.status(400).json({ success: false, - description: `You already have 100 milestones. You cannot have anymore for this game.`, + description: `You already have ${ServerConfig.MAX_MILESTONE_SUBSCRIPTIONS} milestones. You cannot have anymore for this game.`, }); } @@ -195,6 +196,7 @@ router.put( */ router.delete( "/:milestoneID", + RequireAuthedAsUser, GetMilestone, RequirePermissions("manage_targets"), async (req, res) => {