mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-12 08:35:58 +08:00
Purge from dailies (#3635) Bruception
* Purge from dailies * Fix * Migrate logic to lua script * added test to pass coverage * typo * also purging user from daily lbs on personal bests clear Co-authored-by: Miodec <jack@monkeytype.com>
This commit is contained in:
parent
301db6f110
commit
a3b3387f01
4 changed files with 96 additions and 0 deletions
|
@ -121,4 +121,55 @@ describe("Validation", () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
it("isTestTooShort", () => {
|
||||
const testCases = [
|
||||
{
|
||||
result: {
|
||||
mode: "time",
|
||||
mode2: 10,
|
||||
customText: undefined,
|
||||
testDuration: 10,
|
||||
bailedOut: false,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
result: {
|
||||
mode: "time",
|
||||
mode2: 15,
|
||||
customText: undefined,
|
||||
testDuration: 15,
|
||||
bailedOut: false,
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
result: {
|
||||
mode: "time",
|
||||
mode2: 0,
|
||||
customText: undefined,
|
||||
testDuration: 20,
|
||||
bailedOut: false,
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
result: {
|
||||
mode: "time",
|
||||
mode2: 0,
|
||||
customText: undefined,
|
||||
testDuration: 2,
|
||||
bailedOut: false,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
];
|
||||
|
||||
testCases.forEach((testCase) => {
|
||||
//@ts-ignore
|
||||
expect(Validation.isTestTooShort(testCase.result)).toBe(
|
||||
testCase.expected
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
23
backend/redis-scripts/purge-results.lua
Normal file
23
backend/redis-scripts/purge-results.lua
Normal file
|
@ -0,0 +1,23 @@
|
|||
local redis_call = redis.call
|
||||
local string_match = string.match
|
||||
|
||||
local user_id = ARGV[1]
|
||||
local leaderboards_namespace = ARGV[2]
|
||||
|
||||
local current_cursor = '0'
|
||||
local match_pattern = leaderboards_namespace .. '*'
|
||||
|
||||
repeat
|
||||
local result = redis_call('SCAN', current_cursor, 'MATCH', match_pattern)
|
||||
local next_cursor, matched_keys = result[1], result[2]
|
||||
|
||||
for _, key in ipairs(matched_keys) do
|
||||
if (string_match(key, 'results')) then
|
||||
redis_call('HDEL', key, user_id)
|
||||
elseif (string_match(key, 'scores')) then
|
||||
redis_call('ZREM', key, user_id)
|
||||
end
|
||||
end
|
||||
|
||||
current_cursor = next_cursor
|
||||
until (current_cursor == '0')
|
|
@ -13,6 +13,7 @@ import { deleteAll as deleteAllResults } from "../../dal/result";
|
|||
import { deleteConfig } from "../../dal/config";
|
||||
import { verify } from "../../utils/captcha";
|
||||
import * as LeaderboardsDAL from "../../dal/leaderboards";
|
||||
import { purgeUserFromDailyLeaderboards } from "../../utils/daily-leaderboards";
|
||||
|
||||
async function verifyCaptcha(captcha: string): Promise<void> {
|
||||
if (!(await verify(captcha))) {
|
||||
|
@ -76,6 +77,10 @@ export async function resetUser(
|
|||
deleteAllPresets(uid),
|
||||
deleteAllResults(uid),
|
||||
deleteConfig(uid),
|
||||
purgeUserFromDailyLeaderboards(
|
||||
uid,
|
||||
req.ctx.configuration.dailyLeaderboards
|
||||
),
|
||||
]);
|
||||
Logger.logToDb("user_reset", `${userInfo.email} ${userInfo.name}`, uid);
|
||||
|
||||
|
@ -105,6 +110,10 @@ export async function clearPb(
|
|||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
await UserDAL.clearPb(uid);
|
||||
await purgeUserFromDailyLeaderboards(
|
||||
uid,
|
||||
req.ctx.configuration.dailyLeaderboards
|
||||
);
|
||||
Logger.logToDb("user_cleared_pbs", "", uid);
|
||||
|
||||
return new MonkeyResponse("User's PB cleared");
|
||||
|
|
|
@ -157,6 +157,19 @@ export class DailyLeaderboard {
|
|||
}
|
||||
}
|
||||
|
||||
export async function purgeUserFromDailyLeaderboards(
|
||||
uid: string,
|
||||
configuration: MonkeyTypes.Configuration["dailyLeaderboards"]
|
||||
): Promise<void> {
|
||||
const connection = RedisClient.getConnection();
|
||||
if (!connection || !configuration.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
await connection.purgeResults(0, uid, dailyLeaderboardNamespace);
|
||||
}
|
||||
|
||||
let DAILY_LEADERBOARDS: LRUCache<string, DailyLeaderboard>;
|
||||
|
||||
export function initializeDailyLeaderboardsCache(
|
||||
|
|
Loading…
Add table
Reference in a new issue