From d9ffcfb4eea04ab7fe6a4b848fe75385e4fb6d52 Mon Sep 17 00:00:00 2001 From: Evan <64989416+Ferotiq@users.noreply.github.com> Date: Fri, 6 May 2022 16:48:15 -0500 Subject: [PATCH] Preparing for George Update (#2895) * bot-commands rename * removed bot dal * reomoved unnecessary configuration entry * quotes:code_javascript: Add regex-heavy quote. (#2929) markstos * quotes:code_javascript: Add regex-heavy quote. This quote contains a long regex that will be a workout for typing symbols and numbers. Source code can be confirmed to available under an open source license here: https://github.com/markstos/parse-coordinates/blob/master/index.js * fixed length Co-authored-by: Miodec * fixed script names * fixed incorrect path * definitely didnt forget about those Co-authored-by: Miodec Co-authored-by: Mark Stosberg Co-authored-by: Jack --- backend/src/api/controllers/result.ts | 13 +----- backend/src/api/controllers/user.ts | 16 +------ backend/src/constants/base-configuration.ts | 3 -- backend/src/dal/bot.ts | 46 ++++++++++---------- backend/src/jobs/update-leaderboards.ts | 10 +---- backend/src/tasks/george.ts | 48 +++++++++++---------- backend/src/types/types.d.ts | 3 -- 7 files changed, 52 insertions(+), 87 deletions(-) diff --git a/backend/src/api/controllers/result.ts b/backend/src/api/controllers/result.ts index 02b573064..ecc8bd644 100644 --- a/backend/src/api/controllers/result.ts +++ b/backend/src/api/controllers/result.ts @@ -7,7 +7,6 @@ import { updateTypingStats, } from "../../dal/user"; import * as PublicStatsDAL from "../../dal/public-stats"; -import * as BotDAL from "../../dal/bot"; import { roundTo2, stdDev } from "../../utils/misc"; import objectHash from "object-hash"; import Logger from "../../utils/logger"; @@ -81,8 +80,6 @@ export async function addResult( ): Promise { const { uid } = req.ctx.decodedToken; - const useRedisForBotTasks = req.ctx.configuration.useRedisForBotTasks.enabled; - const result = Object.assign({}, req.body.result); result.uid = uid; if (isTestTooShort(result)) { @@ -261,18 +258,12 @@ export async function addResult( if (result.mode === "time" && String(result.mode2) === "60") { incrementBananas(uid, result.wpm); if (isPb && user.discordId) { - if (useRedisForBotTasks) { - George.updateDiscordRole(user.discordId, result.wpm); - } - BotDAL.updateDiscordRole(user.discordId, result.wpm); + George.updateDiscordRole(user.discordId, result.wpm); } } if (result.challenge && user.discordId) { - if (useRedisForBotTasks) { - George.awardChallenge(user.discordId, result.challenge); - } - BotDAL.awardChallenge(user.discordId, result.challenge); + George.awardChallenge(user.discordId, result.challenge); } else { delete result.challenge; } diff --git a/backend/src/api/controllers/user.ts b/backend/src/api/controllers/user.ts index 313110a3e..c21f7a28b 100644 --- a/backend/src/api/controllers/user.ts +++ b/backend/src/api/controllers/user.ts @@ -1,5 +1,4 @@ import * as UserDAL from "../../dal/user"; -import * as BotDAL from "../../dal/bot"; import MonkeyError from "../../utils/error"; import Logger from "../../utils/logger"; import { MonkeyResponse } from "../../utils/monkey-response"; @@ -127,8 +126,6 @@ export async function linkDiscord( data: { tokenType, accessToken }, } = req.body; - const useRedisForBotTasks = req.ctx.configuration.useRedisForBotTasks.enabled; - const userInfo = await UserDAL.getUser(uid, "link discord"); if (userInfo.discordId) { throw new MonkeyError( @@ -160,10 +157,7 @@ export async function linkDiscord( await UserDAL.linkDiscord(uid, discordId); - if (useRedisForBotTasks) { - George.linkDiscord(discordId, uid); - } - await BotDAL.linkDiscord(uid, discordId); + George.linkDiscord(discordId, uid); Logger.logToDb("user_discord_link", `linked to ${discordId}`, uid); return new MonkeyResponse("Discord account linked", discordId); @@ -174,18 +168,12 @@ export async function unlinkDiscord( ): Promise { const { uid } = req.ctx.decodedToken; - const useRedisForBotTasks = req.ctx.configuration.useRedisForBotTasks.enabled; - const userInfo = await UserDAL.getUser(uid, "unlink discord"); if (!userInfo.discordId) { throw new MonkeyError(404, "User does not have a linked Discord account"); } - if (useRedisForBotTasks) { - George.unlinkDiscord(userInfo.discordId, uid); - } - await BotDAL.unlinkDiscord(uid, userInfo.discordId); - + George.unlinkDiscord(userInfo.discordId, uid); await UserDAL.unlinkDiscord(uid); Logger.logToDb("user_discord_unlinked", userInfo.discordId, uid); diff --git a/backend/src/constants/base-configuration.ts b/backend/src/constants/base-configuration.ts index 1acab24f8..977fc3a99 100644 --- a/backend/src/constants/base-configuration.ts +++ b/backend/src/constants/base-configuration.ts @@ -26,9 +26,6 @@ const BASE_CONFIGURATION: MonkeyTypes.Configuration = { enableSavingResults: { enabled: false, }, - useRedisForBotTasks: { - enabled: false, - }, favoriteQuotes: { maxFavorites: 100, }, diff --git a/backend/src/dal/bot.ts b/backend/src/dal/bot.ts index 86536a8b3..83783e103 100644 --- a/backend/src/dal/bot.ts +++ b/backend/src/dal/bot.ts @@ -1,61 +1,59 @@ import { InsertManyResult, InsertOneResult } from "mongodb"; import * as db from "../init/db"; -async function addCommand( - command, - commandArguments +async function addTask( + task, + taskArgs ): Promise> { - return await db.collection("bot-commands").insertOne({ - command, - arguments: commandArguments, - executed: false, + return await db.collection("bot-tasks").insertOne({ + name: task, + args: taskArgs, requestTimestamp: Date.now(), }); } -async function addCommands( - commands, - commandArguments +async function addTasks( + tasks, + taskArgs ): Promise { - if (commands.length === 0 || commands.length !== commandArguments.length) { + if (tasks.length === 0 || tasks.length !== taskArgs.length) { return; } - const normalizedCommands = commands.map((command, index) => { + const normalizedTasks = tasks.map((task, index) => { return { - command, - arguments: commandArguments[index], - executed: false, + name: task, + args: taskArgs[index], requestTimestamp: Date.now(), }; }); - return await db.collection("bot-commands").insertMany(normalizedCommands); + return await db.collection("bot-tasks").insertMany(normalizedTasks); } export async function updateDiscordRole( discordId, wpm ): Promise { - return await addCommand("updateRole", [discordId, wpm]); + return await addTask("updateRole", [discordId, wpm]); } export async function linkDiscord(uid, discordId): Promise { - return await addCommand("linkDiscord", [discordId, uid]); + return await addTask("linkDiscord", [discordId, uid]); } export async function unlinkDiscord(uid, discordId): Promise { - return await addCommand("unlinkDiscord", [discordId, uid]); + return await addTask("unlinkDiscord", [discordId, uid]); } export async function awardChallenge( discordId, challengeName ): Promise { - return await addCommand("awardChallenge", [discordId, challengeName]); + return await addTask("awardChallenge", [discordId, challengeName]); } -export async function announceLbUpdate( +export async function announceLeaderboardUpdate( newRecords, leaderboardId ): Promise { @@ -63,8 +61,8 @@ export async function announceLbUpdate( return; } - const leaderboardCommands = Array(newRecords.length).fill("sayLbUpdate"); - const leaderboardCommandsArguments = newRecords.map((newRecord) => { + const leaderboardTasks = Array(newRecords.length).fill("sayLbUpdate"); + const leaderboardTaskArgs = newRecords.map((newRecord) => { return [ newRecord.discordId ?? newRecord.name, newRecord.rank, @@ -76,5 +74,5 @@ export async function announceLbUpdate( ]; }); - return await addCommands(leaderboardCommands, leaderboardCommandsArguments); + return await addTasks(leaderboardTasks, leaderboardTaskArgs); } diff --git a/backend/src/jobs/update-leaderboards.ts b/backend/src/jobs/update-leaderboards.ts index ed775e965..d77d0463c 100644 --- a/backend/src/jobs/update-leaderboards.ts +++ b/backend/src/jobs/update-leaderboards.ts @@ -1,8 +1,6 @@ import { CronJob } from "cron"; -import { announceLbUpdate } from "../dal/bot"; import * as George from "../tasks/george"; import * as LeaderboardsDAL from "../dal/leaderboards"; -import { getCachedConfiguration } from "../init/configuration"; const CRON_SCHEDULE = "30 14/15 * * * *"; const RECENT_AGE_MINUTES = 10; @@ -51,15 +49,9 @@ async function updateLeaderboardAndNotifyChanges( }); if (newRecords.length > 0) { - const cachedConfig = await getCachedConfiguration(); - const leaderboardId = `time ${leaderboardTime} english`; - if (cachedConfig.useRedisForBotTasks.enabled) { - await George.announceLbUpdate(newRecords, leaderboardId); - } - - await announceLbUpdate(newRecords, leaderboardId); + await George.announceLeaderboardUpdate(newRecords, leaderboardId); } } diff --git a/backend/src/tasks/george.ts b/backend/src/tasks/george.ts index 4e7bbc7d0..698e37ab1 100644 --- a/backend/src/tasks/george.ts +++ b/backend/src/tasks/george.ts @@ -3,15 +3,17 @@ import { Queue, QueueScheduler } from "bullmq"; const QUEUE_NAME = "george-tasks"; +type GeorgeTaskArgument = string | number; + interface GeorgeTask { - command: string; - arguments: any[]; + name: string; + args: GeorgeTaskArgument[]; } -function buildGeorgeTask(command: string, taskArguments: any[]): GeorgeTask { +function buildGeorgeTask(task: string, taskArgs: GeorgeTaskArgument[]): GeorgeTask { return { - command, - arguments: taskArguments, + name: task, + args: taskArgs, }; } @@ -41,12 +43,12 @@ export function initJobQueue(redisConnection: IORedis.Redis | undefined): void { }); } -async function addToQueue(command: string, task: GeorgeTask): Promise { +async function addToQueue(taskName: string, task: GeorgeTask): Promise { if (!jobQueue) { return; } - await jobQueue.add(command, task); + await jobQueue.add(taskName, task); } async function addToQueueBulk( @@ -63,49 +65,49 @@ export async function updateDiscordRole( discordId: string, wpm: number ): Promise { - const command = "updateRole"; - const updateDiscordRoleTask = buildGeorgeTask(command, [discordId, wpm]); - await addToQueue(command, updateDiscordRoleTask); + const task = "updateRole"; + const updateDiscordRoleTask = buildGeorgeTask(task, [discordId, wpm]); + await addToQueue(task, updateDiscordRoleTask); } export async function linkDiscord( discordId: string, uid: string ): Promise { - const command = "linkDiscord"; - const linkDiscordTask = buildGeorgeTask(command, [discordId, uid]); - await addToQueue(command, linkDiscordTask); + const task = "linkDiscord"; + const linkDiscordTask = buildGeorgeTask(task, [discordId, uid]); + await addToQueue(task, linkDiscordTask); } export async function unlinkDiscord( discordId: string, uid: string ): Promise { - const command = "unlinkDiscord"; - const unlinkDiscordTask = buildGeorgeTask(command, [discordId, uid]); - await addToQueue(command, unlinkDiscordTask); + const task = "unlinkDiscord"; + const unlinkDiscordTask = buildGeorgeTask(task, [discordId, uid]); + await addToQueue(task, unlinkDiscordTask); } export async function awardChallenge( discordId: string, challengeName: string ): Promise { - const command = "awardChallenge"; - const awardChallengeTask = buildGeorgeTask(command, [ + const task = "awardChallenge"; + const awardChallengeTask = buildGeorgeTask(task, [ discordId, challengeName, ]); - await addToQueue(command, awardChallengeTask); + await addToQueue(task, awardChallengeTask); } -export async function announceLbUpdate( +export async function announceLeaderboardUpdate( newRecords: any[], leaderboardId: string ): Promise { - const command = "announceLbUpdate"; + const task = "announceLeaderboardUpdate"; const leaderboardUpdateTasks = newRecords.map((record) => { - const taskData = buildGeorgeTask(command, [ + const taskData = buildGeorgeTask(task, [ record.discordId ?? record.name, record.rank, leaderboardId, @@ -116,7 +118,7 @@ export async function announceLbUpdate( ]); return { - name: command, + name: task, data: taskData, }; }); diff --git a/backend/src/types/types.d.ts b/backend/src/types/types.d.ts index 3bd54fa7a..d37f2a9e3 100644 --- a/backend/src/types/types.d.ts +++ b/backend/src/types/types.d.ts @@ -26,9 +26,6 @@ declare namespace MonkeyTypes { enableSavingResults: { enabled: boolean; }; - useRedisForBotTasks: { - enabled: boolean; - }; favoriteQuotes: { maxFavorites: number; };