From d93fd8dbd6eb443c7369fae4d754f3f8c5bd820d Mon Sep 17 00:00:00 2001 From: Bux42 Date: Sat, 7 Oct 2023 18:30:50 +0200 Subject: [PATCH] Get map info (map name) from cache instead of querying tm.io API --- server/src/cache.ts | 10 ++++++++++ server/src/routes/maps.ts | 12 +++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/cache.ts b/server/src/cache.ts index 6a64c643..50c3dbe8 100644 --- a/server/src/cache.ts +++ b/server/src/cache.ts @@ -54,3 +54,13 @@ export const deleteReplay = async (replay: any) => { dbCache.set('maps', cachedMaps); } }; + +export const getMapNameByUId = async (mapUId: string) => { + const cachedMaps = await getMapsCache(); + const mapCacheMatch = cachedMaps.find((map: any) => map.mapUId === mapUId); + + if (mapCacheMatch) { + return mapCacheMatch.mapName; + } + return null; +}; diff --git a/server/src/routes/maps.ts b/server/src/routes/maps.ts index 5bbfefbe..be099196 100644 --- a/server/src/routes/maps.ts +++ b/server/src/routes/maps.ts @@ -9,10 +9,9 @@ import { Request, Response } from 'express'; import * as express from 'express'; -import axios from 'axios'; - import * as db from '../lib/db'; import * as artefacts from '../lib/artefacts'; +import { getMapNameByUId } from '../cache'; const router = express.Router(); /** @@ -56,13 +55,8 @@ router.get('/:mapUID/info', async (req: Request, res: Response) => { // fetch tm.io data try { - const tmxRes = await axios.get(`https://trackmania.io/api/map/${req.params.mapUID}`, { - withCredentials: true, - headers: { 'User-Agent': 'TMDojo API - https://github.com/Bux42/TMDojo' }, - }); - - const tmioData = tmxRes.data; - mapData = { ...mapData, ...tmioData }; + const mapName = await getMapNameByUId(req.params.mapUID); + mapData = { name: mapName }; } catch (error) { req.log.error(`mapsRouter: tm.io request failed with error ${error.toString()}`); }