|
| 1 | +import { sendErrorResponse } from "../utils.js"; |
| 2 | +/** |
| 3 | + * Builds a strongly typed runtime view for the SponsorBlock route. |
| 4 | + * |
| 5 | + * @param nodelink - Router-facing runtime instance. |
| 6 | + * @returns Runtime compatible with the route, or `null` when the required |
| 7 | + * session manager field is unavailable. |
| 8 | + */ |
| 9 | +function getSponsorBlockRuntime(nodelink) { |
| 10 | + const runtime = nodelink; |
| 11 | + if (!runtime.sessions || typeof runtime.sessions.get !== 'function') { |
| 12 | + return null; |
| 13 | + } |
| 14 | + return runtime; |
| 15 | +} |
| 16 | +/** |
| 17 | + * Extracts and validates the path parameters used by the route. |
| 18 | + * |
| 19 | + * @param parsedUrl - Parsed request URL. |
| 20 | + * @returns Validated path parameters, or `null` when validation fails. |
| 21 | + */ |
| 22 | +function getPathParams(parsedUrl) { |
| 23 | + const pathParts = parsedUrl.pathname.split('/'); |
| 24 | + const sessionId = pathParts[3]; |
| 25 | + const guildId = pathParts[5]; |
| 26 | + if (!sessionId || !guildId) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + if (!/^\d{17,20}$/.test(guildId)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + return { |
| 33 | + sessionId, |
| 34 | + guildId |
| 35 | + }; |
| 36 | +} |
| 37 | +/** |
| 38 | + * Handles `GET /sessions/:id/players/:guildId/sponsorblock`. |
| 39 | + */ |
| 40 | +async function handleGetSponsorBlock(req, res, pathParams, runtime, sendResponse) { |
| 41 | + const session = runtime.sessions.get(pathParams.sessionId); |
| 42 | + if (!session) { |
| 43 | + sendErrorResponse(req, res, 404, 'Not Found', "The provided sessionId doesn't exist.", req.url || ''); |
| 44 | + return; |
| 45 | + } |
| 46 | + try { |
| 47 | + const state = session.players.getSponsorBlock(pathParams.guildId); |
| 48 | + sendResponse(req, res, state, 200); |
| 49 | + } |
| 50 | + catch (error) { |
| 51 | + const errorMessage = error instanceof Error ? error.message : 'Player not found'; |
| 52 | + sendErrorResponse(req, res, 404, 'Not Found', errorMessage, req.url || ''); |
| 53 | + } |
| 54 | +} |
| 55 | +/** |
| 56 | + * Handles `PATCH /sessions/:id/players/:guildId/sponsorblock`. |
| 57 | + */ |
| 58 | +async function handlePatchSponsorBlock(req, res, pathParams, runtime, sendResponse) { |
| 59 | + const session = runtime.sessions.get(pathParams.sessionId); |
| 60 | + if (!session) { |
| 61 | + sendErrorResponse(req, res, 404, 'Not Found', "The provided sessionId doesn't exist.", req.url || ''); |
| 62 | + return; |
| 63 | + } |
| 64 | + const body = req.body; |
| 65 | + if (!body || typeof body !== 'object' || Array.isArray(body)) { |
| 66 | + sendErrorResponse(req, res, 400, 'Bad Request', 'Invalid body', req.url || ''); |
| 67 | + return; |
| 68 | + } |
| 69 | + try { |
| 70 | + session.players.updateSponsorBlock(pathParams.guildId, { |
| 71 | + enabled: body.enabled, |
| 72 | + categories: body.categories, |
| 73 | + actionTypes: body.actionTypes, |
| 74 | + skipMarginMs: body.skipMarginMs |
| 75 | + }); |
| 76 | + sendResponse(req, res, session.players.getSponsorBlock(pathParams.guildId), 200); |
| 77 | + } |
| 78 | + catch (error) { |
| 79 | + const errorMessage = error instanceof Error ? error.message : 'Player not found'; |
| 80 | + sendErrorResponse(req, res, 404, 'Not Found', errorMessage, req.url || ''); |
| 81 | + } |
| 82 | +} |
| 83 | +/** |
| 84 | + * Handles `POST /sessions/:id/players/:guildId/sponsorblock`. |
| 85 | + */ |
| 86 | +async function handlePostSponsorBlock(req, res, pathParams, runtime, sendResponse) { |
| 87 | + const session = runtime.sessions.get(pathParams.sessionId); |
| 88 | + if (!session) { |
| 89 | + sendErrorResponse(req, res, 404, 'Not Found', "The provided sessionId doesn't exist.", req.url || ''); |
| 90 | + return; |
| 91 | + } |
| 92 | + const body = req.body; |
| 93 | + if (!body?.segments || !Array.isArray(body.segments)) { |
| 94 | + sendErrorResponse(req, res, 400, 'Bad Request', 'Invalid segments array', req.url || ''); |
| 95 | + return; |
| 96 | + } |
| 97 | + try { |
| 98 | + session.players.setSponsorBlockSegments(pathParams.guildId, body.segments); |
| 99 | + sendResponse(req, res, session.players.getSponsorBlock(pathParams.guildId), 200); |
| 100 | + } |
| 101 | + catch (error) { |
| 102 | + const errorMessage = error instanceof Error ? error.message : 'Player not found'; |
| 103 | + sendErrorResponse(req, res, 404, 'Not Found', errorMessage, req.url || ''); |
| 104 | + } |
| 105 | +} |
| 106 | +/** |
| 107 | + * Handles `DELETE /sessions/:id/players/:guildId/sponsorblock`. |
| 108 | + */ |
| 109 | +async function handleDeleteSponsorBlock(req, res, pathParams, runtime) { |
| 110 | + const session = runtime.sessions.get(pathParams.sessionId); |
| 111 | + if (!session) { |
| 112 | + sendErrorResponse(req, res, 404, 'Not Found', "The provided sessionId doesn't exist.", req.url || ''); |
| 113 | + return; |
| 114 | + } |
| 115 | + try { |
| 116 | + session.players.clearSponsorBlock(pathParams.guildId); |
| 117 | + res.writeHead(204); |
| 118 | + res.end(); |
| 119 | + } |
| 120 | + catch (error) { |
| 121 | + const errorMessage = error instanceof Error ? error.message : 'Player not found'; |
| 122 | + sendErrorResponse(req, res, 404, 'Not Found', errorMessage, req.url || ''); |
| 123 | + } |
| 124 | +} |
| 125 | +/** |
| 126 | + * Handles requests for the SponsorBlock route. |
| 127 | + */ |
| 128 | +async function handler(nodelink, req, res, sendResponse, parsedUrl) { |
| 129 | + const runtime = getSponsorBlockRuntime(nodelink); |
| 130 | + if (!runtime) { |
| 131 | + sendErrorResponse(req, res, 500, 'Internal Server Error', 'SponsorBlock runtime contract is incomplete.', parsedUrl.pathname, true); |
| 132 | + return; |
| 133 | + } |
| 134 | + const pathParams = getPathParams(parsedUrl); |
| 135 | + if (!pathParams) { |
| 136 | + sendErrorResponse(req, res, 400, 'Bad Request', 'Invalid path parameters', parsedUrl.pathname, true); |
| 137 | + return; |
| 138 | + } |
| 139 | + if (req.method === 'GET') { |
| 140 | + await handleGetSponsorBlock(req, res, pathParams, runtime, sendResponse); |
| 141 | + return; |
| 142 | + } |
| 143 | + if (req.method === 'PATCH') { |
| 144 | + await handlePatchSponsorBlock(req, res, pathParams, runtime, sendResponse); |
| 145 | + return; |
| 146 | + } |
| 147 | + if (req.method === 'POST') { |
| 148 | + await handlePostSponsorBlock(req, res, pathParams, runtime, sendResponse); |
| 149 | + return; |
| 150 | + } |
| 151 | + if (req.method === 'DELETE') { |
| 152 | + await handleDeleteSponsorBlock(req, res, pathParams, runtime); |
| 153 | + return; |
| 154 | + } |
| 155 | + sendErrorResponse(req, res, 405, 'Method Not Allowed', 'Method Not Allowed', parsedUrl.pathname); |
| 156 | +} |
| 157 | +const sponsorBlockRoute = { |
| 158 | + handler, |
| 159 | + methods: ['GET', 'POST', 'PATCH', 'DELETE'] |
| 160 | +}; |
| 161 | +export default sponsorBlockRoute; |
0 commit comments