This repository has been archived on 2023-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-overlay/src/helpers/validateSteamId.js

41 lines
1017 B
JavaScript

import { default as LeaderboardType } from "../consts/LeaderboardType";
import { getValue, setValue, valueExists } from "../utils/redisUtils";
import Utils from "../utils/utils";
const KEY = "VALID_STEAM_ID_";
const TO_CHECK = [
LeaderboardType.ScoreSaber.ApiUrl.PlayerData,
LeaderboardType.BeatLeader.ApiUrl.PlayerData,
];
export async function isValidSteamId(steamId) {
if (!steamId) {
return false;
}
if (steamId.length !== 17) {
return false;
}
const exists = await valueExists(`${KEY}${steamId}`);
if (exists) {
const data = await getValue(`${KEY}${steamId}`);
return Boolean(data);
}
const before = Date.now();
let valid = false;
for (const url of TO_CHECK) {
const isValid = await Utils.checkLeaderboard(url, steamId);
if (isValid) {
valid = true;
break;
}
}
await setValue(`${KEY}${steamId}`, valid, valid ? 86400 * 7 : 86400); // Expire in a week if is valid
console.log(
`[Cache]: Cached Steam ID for id ${steamId} in ${Date.now() - before}ms`
);
return valid;
}