mirror of
https://github.com/spacebarchat/server.git
synced 2026-07-04 06:41:52 +00:00
feat: send vote poll events
This commit is contained in:
@@ -20,7 +20,7 @@ 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";
|
||||
import { DiscordApiErrors, emitEvent, ErrorList, FieldError, makeObjectErrorContent, MessagePollVoteAddEvent, MessagePollVoteRemoveEvent } from "#util";
|
||||
|
||||
const router: Router = Router({ mergeParams: true });
|
||||
|
||||
@@ -44,6 +44,9 @@ router.put("/", route({ requestBody: "PollUserAnswersSchema", permission: "VIEW_
|
||||
throw new FieldError(50035, "Invalid form body", errors);
|
||||
}
|
||||
|
||||
const channel_id = message.channel_id!;
|
||||
const guild_id = message.guild_id;
|
||||
|
||||
const allAnswerCounts = message.poll.results.answer_counts as unknown as (Omit<PollAnswerCount, "me_voted"> & { voters: string[] })[];
|
||||
|
||||
for (const answer_id of payload.answer_ids) {
|
||||
@@ -57,12 +60,36 @@ router.put("/", route({ requestBody: "PollUserAnswersSchema", permission: "VIEW_
|
||||
if (!answerCount.voters.includes(req.user_id)) {
|
||||
answerCount.voters.push(req.user_id);
|
||||
answerCount.count = answerCount.voters.length;
|
||||
|
||||
await emitEvent({
|
||||
event: "MESSAGE_POLL_VOTE_ADD",
|
||||
channel_id,
|
||||
data: {
|
||||
answer_id: Number(answerCount.id),
|
||||
channel_id: channel_id,
|
||||
message_id: poll_id,
|
||||
user_id: req.user_id,
|
||||
guild_id,
|
||||
},
|
||||
} satisfies MessagePollVoteAddEvent);
|
||||
}
|
||||
}
|
||||
|
||||
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 emitEvent({
|
||||
event: "MESSAGE_POLL_VOTE_REMOVE",
|
||||
channel_id,
|
||||
data: {
|
||||
answer_id: Number(answerCount.id),
|
||||
channel_id,
|
||||
message_id: poll_id,
|
||||
user_id: req.user_id,
|
||||
guild_id,
|
||||
},
|
||||
} satisfies MessagePollVoteRemoveEvent);
|
||||
}
|
||||
|
||||
await message.save();
|
||||
|
||||
@@ -366,6 +366,29 @@ export interface MessageDeleteBulkEvent extends Event {
|
||||
guild_id?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessagePollVoteAddEvent extends Event {
|
||||
event: "MESSAGE_POLL_VOTE_ADD";
|
||||
data: {
|
||||
answer_id: number;
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
message_id: string;
|
||||
user_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessagePollVoteRemoveEvent extends Event {
|
||||
event: "MESSAGE_POLL_VOTE_REMOVE";
|
||||
data: {
|
||||
answer_id: number;
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
message_id: string;
|
||||
user_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export const enum ReactionType {
|
||||
normal = 0,
|
||||
burst = 1,
|
||||
@@ -685,6 +708,8 @@ export type EventData =
|
||||
| MessageUpdateEvent
|
||||
| MessageDeleteEvent
|
||||
| MessageDeleteBulkEvent
|
||||
| MessagePollVoteAddEvent
|
||||
| MessagePollVoteRemoveEvent
|
||||
| MessageReactionAddEvent
|
||||
| MessageReactionRemoveEvent
|
||||
| MessageReactionRemoveAllEvent
|
||||
@@ -746,6 +771,8 @@ export enum EVENTEnum {
|
||||
MessageUpdate = "MESSAGE_UPDATE",
|
||||
MessageDelete = "MESSAGE_DELETE",
|
||||
MessageDeleteBulk = "MESSAGE_DELETE_BULK",
|
||||
MessagePollVoteAdd = "MESSAGE_POLL_VOTE_ADD",
|
||||
MessageePollVoteRemove = "MESSAGE_POLL_VOTE_REMOVE",
|
||||
MessageReactionAdd = "MESSAGE_REACTION_ADD",
|
||||
MessageReactionRemove = "MESSAGE_REACTION_REMOVE",
|
||||
MessageReactionRemoveAll = "MESSAGE_REACTION_REMOVE_ALL",
|
||||
@@ -804,6 +831,8 @@ export type EVENT =
|
||||
| "MESSAGE_UPDATE"
|
||||
| "MESSAGE_DELETE"
|
||||
| "MESSAGE_DELETE_BULK"
|
||||
| "MESSAGE_POLL_VOTE_ADD"
|
||||
| "MESSAGE_POLL_VOTE_REMOVE"
|
||||
| "MESSAGE_REACTION_ADD"
|
||||
// TODO: add a new event: bulk add reaction:
|
||||
// | "MESSAGE_REACTION_BULK_ADD"
|
||||
|
||||
Reference in New Issue
Block a user