From 7012b77275bcf62593bece3aa37cee6b07d941a3 Mon Sep 17 00:00:00 2001 From: MCGallaspy Date: Sat, 19 Oct 2024 15:36:04 -0700 Subject: [PATCH 1/7] Add replays batch endpoint --- src/actions.ts | 11 ++++++++++- src/replays.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/actions.ts b/src/actions.ts index 3448155..7681940 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -904,7 +904,16 @@ export const actions: {[k: string]: QueryHandler} = { } return {password: pw}; }, - + async 'replays/batch.json'(params) { + if (!params.ids) { + throw new ActionError("Invalid batch replay request, must provide ids"); + } + const ids: string[] = params.ids.split(','); + const results = await Replays.getBatch(ids); + console.log(params, ids, results); + this.response.setHeader('Content-Type', 'application/json'); + return JSON.stringify(results); + }, // sent by ps server async 'smogon/validate'(params) { if (this.getIp() !== Config.restartip) { diff --git a/src/replays.ts b/src/replays.ts index 8f03c9d..f006c39 100644 --- a/src/replays.ts +++ b/src/replays.ts @@ -233,6 +233,18 @@ export const Replays = new class { SQL`uploadtime, id, format, players, rating` )`WHERE private = 0 ORDER BY uploadtime DESC LIMIT 51`.then(this.toReplays); } + + getBatch(ids: string[]) { + let idsForQuery = ''; + for (var i = 0; i < ids.length; i++) { + const id = ids[i]; + idsForQuery += `'${id}'`; + if (i !== ids.length - 1) idsForQuery += ","; + } + return replays.selectAll( + SQL`*` + )`WHERE private = 0 AND id IN (${idsForQuery}) LIMIT 51`.then(this.toReplays); + } }; export default Replays; From 3628b6296415d761a406f453a27e81ead6a77482 Mon Sep 17 00:00:00 2001 From: MCGallaspy Date: Sun, 20 Oct 2024 08:20:26 -0700 Subject: [PATCH 2/7] Include id list appropriately SQL magic can just take a list, and the right thing will happen. No need to construct a string. --- src/actions.ts | 1 - src/replays.ts | 10 ++-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/actions.ts b/src/actions.ts index 7681940..d9f9c93 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -910,7 +910,6 @@ export const actions: {[k: string]: QueryHandler} = { } const ids: string[] = params.ids.split(','); const results = await Replays.getBatch(ids); - console.log(params, ids, results); this.response.setHeader('Content-Type', 'application/json'); return JSON.stringify(results); }, diff --git a/src/replays.ts b/src/replays.ts index f006c39..a9e9fcc 100644 --- a/src/replays.ts +++ b/src/replays.ts @@ -235,16 +235,10 @@ export const Replays = new class { } getBatch(ids: string[]) { - let idsForQuery = ''; - for (var i = 0; i < ids.length; i++) { - const id = ids[i]; - idsForQuery += `'${id}'`; - if (i !== ids.length - 1) idsForQuery += ","; - } return replays.selectAll( SQL`*` - )`WHERE private = 0 AND id IN (${idsForQuery}) LIMIT 51`.then(this.toReplays); - } + )`WHERE private = 0 AND id IN (${ids}) LIMIT 51`.then(this.toReplays); + } }; export default Replays; From aab5c12ab7114e82c4c49b73957e66d6b4fbe07c Mon Sep 17 00:00:00 2001 From: MCGallaspy Date: Sun, 20 Oct 2024 15:06:00 -0700 Subject: [PATCH 3/7] Address review comments --- src/actions.ts | 7 +++---- src/replays.ts | 4 +--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/actions.ts b/src/actions.ts index d9f9c93..ccb575d 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -904,14 +904,13 @@ export const actions: {[k: string]: QueryHandler} = { } return {password: pw}; }, - async 'replays/batch.json'(params) { + async 'replays/batch'(params) { if (!params.ids) { throw new ActionError("Invalid batch replay request, must provide ids"); } - const ids: string[] = params.ids.split(','); + const ids: string[] = params.ids.split(',').slice(0, 51); const results = await Replays.getBatch(ids); - this.response.setHeader('Content-Type', 'application/json'); - return JSON.stringify(results); + return results; }, // sent by ps server async 'smogon/validate'(params) { diff --git a/src/replays.ts b/src/replays.ts index a9e9fcc..37ac258 100644 --- a/src/replays.ts +++ b/src/replays.ts @@ -235,9 +235,7 @@ export const Replays = new class { } getBatch(ids: string[]) { - return replays.selectAll( - SQL`*` - )`WHERE private = 0 AND id IN (${ids}) LIMIT 51`.then(this.toReplays); + return replays.selectAll()`WHERE private = 0 AND id IN (${ids}) LIMIT 51`.then(this.toReplays); } }; From 0fb1f11b35cb8bf4c575332c6044d9d00b4918c6 Mon Sep 17 00:00:00 2001 From: Michael Gallaspy Date: Mon, 28 Oct 2024 15:58:55 -0700 Subject: [PATCH 4/7] Update src/actions.ts Co-authored-by: Mia <49593536+mia-pi-git@users.noreply.github.com> --- src/actions.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/actions.ts b/src/actions.ts index ccb575d..1ece327 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -909,8 +909,7 @@ export const actions: {[k: string]: QueryHandler} = { throw new ActionError("Invalid batch replay request, must provide ids"); } const ids: string[] = params.ids.split(',').slice(0, 51); - const results = await Replays.getBatch(ids); - return results; + return Replays.getBatch(ids); }, // sent by ps server async 'smogon/validate'(params) { From 567da8bbfaaf63199c51b816db109b78b708e1e3 Mon Sep 17 00:00:00 2001 From: Michael Gallaspy Date: Mon, 28 Oct 2024 15:59:01 -0700 Subject: [PATCH 5/7] Update src/actions.ts Co-authored-by: Mia <49593536+mia-pi-git@users.noreply.github.com> --- src/actions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/actions.ts b/src/actions.ts index 1ece327..7ccabc6 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -908,7 +908,7 @@ export const actions: {[k: string]: QueryHandler} = { if (!params.ids) { throw new ActionError("Invalid batch replay request, must provide ids"); } - const ids: string[] = params.ids.split(',').slice(0, 51); + const ids = params.ids.split(',').slice(0, 51); return Replays.getBatch(ids); }, // sent by ps server From c8c149d84faeeb46ca05731b89259125298474bf Mon Sep 17 00:00:00 2001 From: Michael Gallaspy Date: Tue, 12 Nov 2024 09:07:20 -0800 Subject: [PATCH 6/7] Update src/actions.ts Co-authored-by: Guangcong Luo --- src/actions.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/actions.ts b/src/actions.ts index 7ccabc6..1c78ad9 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -908,7 +908,8 @@ export const actions: {[k: string]: QueryHandler} = { if (!params.ids) { throw new ActionError("Invalid batch replay request, must provide ids"); } - const ids = params.ids.split(',').slice(0, 51); + const ids = params.ids.split(','); + if (ids.length > 51) throw new ActionError(`Limit 51 IDs (you have ${ids.length}).`); return Replays.getBatch(ids); }, // sent by ps server From a3b9c0409b5c47b7d5f559a83c4cc5fed60ad6a4 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Tue, 12 Nov 2024 21:10:59 -0800 Subject: [PATCH 7/7] Fix trailing replays --- src/replays.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/replays.ts b/src/replays.ts index 37ac258..6113c76 100644 --- a/src/replays.ts +++ b/src/replays.ts @@ -233,10 +233,9 @@ export const Replays = new class { SQL`uploadtime, id, format, players, rating` )`WHERE private = 0 ORDER BY uploadtime DESC LIMIT 51`.then(this.toReplays); } - getBatch(ids: string[]) { return replays.selectAll()`WHERE private = 0 AND id IN (${ids}) LIMIT 51`.then(this.toReplays); - } + } }; export default Replays;