feat: add voting route

This commit is contained in:
CyberL1
2026-06-24 23:46:23 +02:00
committed by Rory&
parent 305d1d87b2
commit 0efdb5c35a
5 changed files with 78 additions and 0 deletions
Binary file not shown.
Binary file not shown.
@@ -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;
+4
View File
@@ -46,3 +46,7 @@ export interface PollAnswerCount {
count: number;
me_voted: boolean;
}
export interface PollUserAnswersSchema {
answer_ids: string[];
}
+2
View File
@@ -659,6 +659,8 @@ export const DiscordApiErrors = {
STICKER_ANIMATION_DURATION_MAXIMUM: new ApiError("Sticker animation duration exceeds maximum of {} seconds", 170007, undefined, ["5"]),
AUTOMODERATOR_BLOCK: new ApiError("Message was blocked by automatic moderation", 200000),
BULK_BAN_FAILED: new ApiError("Failed to ban users", 500000),
POLL_VOTING_BLOCKED: new ApiError("Poll voting blocked", 520000),
POLL_EXPIRED: new ApiError("Poll expired", 520001),
//Other errors
UNKNOWN_VOICE_STATE: new ApiError("Unknown Voice State", 10065, 404),