From eec44b313ce99f9140129e6a1376a42da52d3fb0 Mon Sep 17 00:00:00 2001 From: NeKz Date: Wed, 15 May 2024 05:23:20 +0200 Subject: [PATCH] Support new board API --- docker/compose/autorender.portal2.local.yml | 9 ++++++ src/server/.env.example | 3 ++ src/server/deno.json | 4 +-- src/server/deno.lock | 1 + src/server/main.ts | 36 ++++++++++++++++++++- src/server/tests/mel_board_test.ts | 35 ++++++++++++++++++++ 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/server/tests/mel_board_test.ts diff --git a/docker/compose/autorender.portal2.local.yml b/docker/compose/autorender.portal2.local.yml index aa1bf02..a98c996 100644 --- a/docker/compose/autorender.portal2.local.yml +++ b/docker/compose/autorender.portal2.local.yml @@ -9,6 +9,11 @@ services: restart: always links: - database:autorender.database + networks: + - default + - board-net + external_links: + - board-net volumes: - ./docker/volumes/logs/server:/logs/server:rw - ./docker/volumes/storage:/storage:rw @@ -47,6 +52,7 @@ services: volumes: - ./docker/volumes/initdb:/docker-entrypoint-initdb.d - ./docker/volumes/mysql:/var/lib/mysql + - ./docker/volumes/backups:/backups proxy: image: nginx:stable-alpine3.17 container_name: "${PROJECT_NAME}-proxy" @@ -61,3 +67,6 @@ services: - ./docker/volumes/ssl:/ssl:r - ./docker/volumes/nginx:/etc/nginx/templates - ./docker/volumes/logs/nginx:/var/log/nginx:rw +networks: + board-net: + external: true diff --git a/src/server/.env.example b/src/server/.env.example index c88a083..0b3a587 100644 --- a/src/server/.env.example +++ b/src/server/.env.example @@ -29,6 +29,9 @@ AUTORENDER_RUN_DEMO_REPAIR=true AUTORENDER_RUN_SKIP_COOP_VIDEOS_CHECK=true COOKIE_SECRET_KEY="" +MEL_BOARD_DOMAIN=board-server +MEL_BOARD_API_TOKEN="" + B2_ENABLED=false B2_BUCKET_ID=none B2_KEY_ID=none diff --git a/src/server/deno.json b/src/server/deno.json index 2e0b3cc..6fb7623 100644 --- a/src/server/deno.json +++ b/src/server/deno.json @@ -1,8 +1,8 @@ { "tasks": { - "dev": "deno task dev:stale & deno task dev:processing & deno run --import-map=../import_map.json --no-prompt --allow-env --allow-read=.env,.env.defaults,.env.example,app/assets,/logs/server,/storage --allow-write=/logs/server,/storage --allow-run=ffprobe --allow-net --watch main.ts", + "dev": "deno task dev:stale & deno task dev:processing & deno run --import-map=../import_map.json --no-prompt --allow-env --allow-read=.env,.env.defaults,.env.example,app/assets,/logs/server,/storage --allow-write=/logs/server,/storage --allow-run=ffprobe --allow-net --unsafely-ignore-certificate-errors=board-server --watch main.ts", "prod": "deno task stale & deno task board & deno task processing & deno run --import-map=../import_map.json --no-prompt --allow-env --allow-read=.env,.env.defaults,.env.example,app/assets,/logs/server,/storage --allow-write=/logs/server,/storage --allow-run=ffprobe --allow-net main.ts", - "test": "deno test --import-map=../import_map.json --allow-net --allow-read --allow-env --unsafely-ignore-certificate-errors=autorender.portal2.local", + "test": "deno test --import-map=../import_map.json --allow-net --allow-read --allow-env --unsafely-ignore-certificate-errors=autorender.portal2.local,board-server", "perm": "deno run --import-map=../import_map.json --no-prompt --allow-env --allow-read=.env,.env.defaults,.env.example --allow-net=autorender.database:3307 tasks/perm.ts", "migrate": "deno run --import-map=../import_map.json --no-prompt --allow-env --allow-read=.env,.env.defaults,.env.example,/logs/server/migrate_info.log,/logs/server/migrate_error.log,/storage --allow-write=/logs/server/migrate_info.log,/logs/server/migrate_error.log --allow-write=/logs/server/migrate_info.log,/logs/server/migrate_error.log,/storage --allow-net=autorender.database:3307,autorender.portal2.sr,board.portal2.sr,mel.board.portal2.sr tasks/migrate.ts", "stale": "deno run --import-map=../import_map.json --no-prompt --allow-env --allow-read=.env,.env.defaults,.env.example,/logs/server/stale_info.log,/logs/server/stale_error.log --allow-write=/logs/server/stale_info.log,/logs/server/stale_error.log --allow-net=autorender.database:3307 tasks/stale.ts", diff --git a/src/server/deno.lock b/src/server/deno.lock index 6e7d4c5..1cf9cbf 100644 --- a/src/server/deno.lock +++ b/src/server/deno.lock @@ -388,6 +388,7 @@ "https://deno.land/std@0.221.0/assert/assertion_error.ts": "9f689a101ee586c4ce92f52fa7ddd362e86434ffdf1f848e45987dc7689976b8", "https://deno.land/std@0.221.0/fmt/colors.ts": "d239d84620b921ea520125d778947881f62c50e78deef2657073840b8af9559a", "https://deno.land/std@0.221.0/fs/exists.ts": "3d38cb7dcbca3cf313be343a7b8af18a87bddb4b5ca1bd2314be12d06533b50f", + "https://deno.land/std@0.221.0/io/types.ts": "acecb3074c730b5ff487ba4fe9ce51e67bd982aa07c95e5f5679b7b2f24ad129", "https://deno.land/std@0.221.0/io/write_all.ts": "24aac2312bb21096ae3ae0b102b22c26164d3249dff96dbac130958aa736f038", "https://deno.land/std@0.221.0/log/_config.ts": "489e11b6d3c917bf5fc954c5e914c095d3480efd924d1e85f2fc576468581c54", "https://deno.land/std@0.221.0/log/_state.ts": "314c0c31ab9c8f4fb33326ad446757d35f75e5bb21746b7720ed4e3f3a939da1", diff --git a/src/server/main.ts b/src/server/main.ts index 03848a4..2df9611 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -89,6 +89,8 @@ const AUTORENDER_BOT_TOKEN_HASH = await bcrypt.hash( const AUTORENDER_MAX_DEMO_FILE_SIZE = Number(Deno.env.get('AUTORENDER_MAX_DEMO_FILE_SIZE')) * 1_000_000; const AUTORENDER_MAX_VIDEO_FILE_SIZE = Number(Deno.env.get('AUTORENDER_MAX_VIDEO_FILE_SIZE')) * 1_000_000; const DISCORD_BOARD_INTEGRATION_WEBHOOK_URL = Deno.env.get('DISCORD_BOARD_INTEGRATION_WEBHOOK_URL')!; +const MEL_BOARD_DOMAIN = Deno.env.get('MEL_BOARD_DOMAIN')!; +const MEL_BOARD_API_TOKEN = Deno.env.get('MEL_BOARD_API_TOKEN')!; const B2_ENABLED = Deno.env.get('B2_ENABLED')!.toLowerCase() === 'true'; const B2_BUCKET_ID = Deno.env.get('B2_BUCKET_ID')!; const BOARD_INTEGRATION_START_DATE = '2023-08-25'; @@ -623,6 +625,38 @@ apiV1 return; } + try { + if (video.board_changelog_id && video.board_source === BoardSource.Mel && MEL_BOARD_API_TOKEN) { + logger.info(`Sending autorender to ${MEL_BOARD_DOMAIN}`, video.share_id); + + const res = await fetch(`https://${MEL_BOARD_DOMAIN}/api-v3/set-autorender`, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${MEL_BOARD_API_TOKEN}`, + 'Content-Type': 'application/json', + 'User-Agent': Deno.env.get('USER_AGENT')!, + }, + body: JSON.stringify({ + changelog_id: video.board_changelog_id, + autorender_id: video.share_id, + }), + }); + + logger.info(`Sent autorender to ${MEL_BOARD_DOMAIN} for`, video.video_id, ':', res.statusText); + + if (!res.ok) { + logger.error( + `Failed to send autorender to ${MEL_BOARD_DOMAIN} for video`, + video.video_id, + ':', + await res.text(), + ); + } + } + } catch (err) { + logger.error(err); + } + try { if (video.board_changelog_id !== null) { if (video.board_rank === 1) { @@ -995,7 +1029,7 @@ apiV1 }) // Get the video of a leaderboard run. .get('/video/:boardChangelogId(\\d+)/video', async (ctx) => { - const source = Number(ctx.request.url.searchParams.get('source')) ?? BoardSource.Portal2; + const source = Number(ctx.request.url.searchParams.get('source') ?? BoardSource.Portal2); if (isNaN(source) || ![BoardSource.Portal2, BoardSource.Mel].includes(source)) { return Err(ctx, Status.BadRequest, 'Bad value for source parameter.'); } diff --git a/src/server/tests/mel_board_test.ts b/src/server/tests/mel_board_test.ts new file mode 100644 index 0000000..bf54ce6 --- /dev/null +++ b/src/server/tests/mel_board_test.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023-2024, NeKz + * + * SPDX-License-Identifier: MIT + */ + +import { assert } from 'testing/asserts.ts'; + +const MEL_BOARD_DOMAIN = Deno.env.get('MEL_BOARD_DOMAIN')!; +const MEL_BOARD_API_TOKEN = Deno.env.get('MEL_BOARD_API_TOKEN')!; + +Deno.test('Can set autorender on board.portal2.local', async () => { + if (!MEL_BOARD_API_TOKEN) { + return console.log('Mel board disabled. Skipped test.'); + } + + const res = await fetch(`https://${MEL_BOARD_DOMAIN}/api-v3/set-autorender`, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${MEL_BOARD_API_TOKEN}`, + 'Content-Type': 'application/json', + 'User-Agent': Deno.env.get('USER_AGENT')!, + }, + body: JSON.stringify({ + changelog_id: -1, + autorender_id: '0123456789A', + }), + }); + + assert(res.ok); + + const status = await res.json(); + assert(status); + assert(status.affected === 0); +});