From a12224536e8c4d0074ddc2c47a3917d61568a3eb Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Thu, 25 Jun 2026 01:18:06 +0200 Subject: [PATCH] feat: add route to expire a poll --- assets/openapi.json | Bin 978417 -> 981250 bytes .../#channel_id/polls/#poll_id/expire.ts | 49 ++++++++++++++++++ src/util/util/Constants.ts | 1 + 3 files changed, 50 insertions(+) create mode 100644 src/api/routes/channels/#channel_id/polls/#poll_id/expire.ts diff --git a/assets/openapi.json b/assets/openapi.json index 5de9ed291754cd3945adce3600947a7e6af66ec2..f9f6f540b27b0c3544df4a1d5fec5e511235e023 100644 GIT binary patch delta 169 zcmex(+q&tKbwdkd3sVbo3rh=Y3tJ0&3r7oQ3s(zw3(pqb6gAe=ih|6d=?@oh=uL09 z!!0>IA)NQ<^a=dj=e8?c=J96S&Zf<~lyQ5g8gCfebRa3tUY(d%T%HQl@`c?1rxqT@ t=?50^TC#xDPQU2I`3Ayr?B|G|zCnu5etNF~K91ORs)J?sDg delta 51 zcmZqrWc~5Bbwdkd3sVbo3rh=Y3tJ0&3r7oQ3s(zw3(pqb6t(S6+Pv;;?R(65x9>6M Hb2|tC=E4!g diff --git a/src/api/routes/channels/#channel_id/polls/#poll_id/expire.ts b/src/api/routes/channels/#channel_id/polls/#poll_id/expire.ts new file mode 100644 index 000000000..9ac128861 --- /dev/null +++ b/src/api/routes/channels/#channel_id/polls/#poll_id/expire.ts @@ -0,0 +1,49 @@ +/* + Spacebar: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2026 Spacebar and Spacebar Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +import { Request, Response, Router } from "express"; +import { route } from "@spacebar/api/util/handlers/route"; +import { Message } from "#database"; +import { DiscordApiErrors } from "#util"; + +const router: Router = Router({ mergeParams: true }); + +router.post("/", route({ permission: "VIEW_CHANNEL" }), async (req: Request, res: Response) => { + const { poll_id } = req.params as { [key: string]: string }; + + const message = await Message.findOne({ where: { id: poll_id } }); + + if (!message) { + throw DiscordApiErrors.UNKNOWN_MESSAGE; + } + + if (!message.poll) { + throw DiscordApiErrors.NON_POLL_MESSAGE_CANNOT_EXPIRE; + } + + if (new Date() > new Date(message.poll.expiry)) { + throw DiscordApiErrors.POLL_EXPIRED; + } + + message.poll.expiry = new Date(); + + await message.save(); + res.send(message); +}); + +export default router; diff --git a/src/util/util/Constants.ts b/src/util/util/Constants.ts index b605d1a9e..78c738974 100644 --- a/src/util/util/Constants.ts +++ b/src/util/util/Constants.ts @@ -663,6 +663,7 @@ export const DiscordApiErrors = { POLL_EXPIRED: new ApiError("Poll expired", 520001), POLL_INVALID_CHANNEL_TYPE: new ApiError("Invalid channel type for poll creation", 520002), POLL_CANNOT_EDIT_MESSAGE: new ApiError("Cannot edit a poll message", 520003), + NON_POLL_MESSAGE_CANNOT_EXPIRE: new ApiError("Cannot expire a non-poll message", 520006), //Other errors UNKNOWN_VOICE_STATE: new ApiError("Unknown Voice State", 10065, 404),