summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorCyberL1 <cyber.hf18@gmail.com>2026-06-24 23:46:23 +0200
committerRory& <root@rory.gay>2026-06-25 17:56:49 +0200
commit0efdb5c35a452f81184fc64d72b9bdd4aede360d (patch)
tree2715c40c202483cbc248da11e7b2128788247180 /src/api
parentfix: poll answer id should never be 0 (diff)
downloadserver-ts-0efdb5c35a452f81184fc64d72b9bdd4aede360d.tar.xz
feat: add voting route
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/channels/#channel_id/polls/#poll_id/answers/@me.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/api/routes/channels/#channel_id/polls/#poll_id/answers/@me.ts b/src/api/routes/channels/#channel_id/polls/#poll_id/answers/@me.ts
new file mode 100644

index 000000000..95660c308 --- /dev/null +++ b/src/api/routes/channels/#channel_id/polls/#poll_id/answers/@me.ts
@@ -0,0 +1,72 @@ +/* + 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 <https://www.gnu.org/licenses/>. +*/ + +import { Request, Response, Router } from "express"; +import { route } from "@spacebar/api/util/handlers/route"; +import { PollAnswerCount, PollUserAnswersSchema } from "@spacebar/schemas"; +import { Message } from "#database"; +import { DiscordApiErrors, ErrorList, FieldError, makeObjectErrorContent } from "#util"; + +const router: Router = Router({ mergeParams: true }); + +router.put("/", route({ requestBody: "PollUserAnswersSchema", permission: "VIEW_CHANNEL" }), async (req: Request, res: Response) => { + const payload = req.body as PollUserAnswersSchema; + const { poll_id } = req.params as { [key: string]: string }; + + const message = await Message.findOne({ where: { id: poll_id } }); + + if (!message || !message.poll || !message.poll.results) { + throw DiscordApiErrors.UNKNOWN_MESSAGE; + } + + if (new Date() > new Date(message.poll.expiry)) { + throw DiscordApiErrors.POLL_EXPIRED; + } + + if (!message.poll.allow_multiselect && payload.answer_ids.length > 1) { + const errors: ErrorList = {}; + errors["answer_ids"] = makeObjectErrorContent("CANNOT_ADD_MULTIPLE_POLL_ANSWERS", "Multiple votes are not allowed for this poll."); + throw new FieldError(50035, "Invalid form body", errors); + } + + const allAnswerCounts = message.poll.results.answer_counts as unknown as (Omit<PollAnswerCount, "me_voted"> & { voters: string[] })[]; + + for (const answer_id of payload.answer_ids) { + let answerCount = allAnswerCounts.find((a) => a.id === answer_id); + + if (!answerCount) { + allAnswerCounts.push({ id: answer_id, count: 0, voters: [] }); + answerCount = allAnswerCounts.find((a) => a.id === answer_id)!; + } + + if (!answerCount.voters.includes(req.user_id)) { + answerCount.voters.push(req.user_id); + answerCount.count = answerCount.voters.length; + } + } + + for (const answerCount of allAnswerCounts.filter((a) => !payload.answer_ids.includes(a.id))) { + answerCount.voters = answerCount.voters.filter((voter) => voter != req.user_id); + answerCount.count = answerCount.voters.length; + } + + await message.save(); + res.send(); +}); + +export default router;