mirror of
https://github.com/spacebarchat/server.git
synced 2026-07-01 02:51:52 +00:00
feat: get non-expired polls on start
This commit is contained in:
+108
-3
@@ -21,14 +21,15 @@ import { Request, Response, Router } from "express";
|
||||
import morgan from "morgan";
|
||||
import { Server, ServerOptions } from "lambert-server/Server";
|
||||
import { red } from "picocolors";
|
||||
import { getDatabase, initDatabase } from "@spacebar/database";
|
||||
import { Config, ConnectionConfig, ConnectionLoader, Email, JSONReplacer, WebAuthn, initEvent, registerRoutes, getRevInfoOrFail } from "@spacebar/util";
|
||||
import { getDatabase, initDatabase, Message } from "@spacebar/database";
|
||||
import { Config, ConnectionConfig, ConnectionLoader, Email, JSONReplacer, WebAuthn, initEvent, registerRoutes, getRevInfoOrFail, pendingPolls } from "@spacebar/util";
|
||||
import { ProcessLifecycle } from "../util/util/ProcessLifecycle";
|
||||
import { Monitoring } from "../util/monitoring/Monitoring";
|
||||
import { BcryptWorkerPool } from "../util/util/workers/bcrypt/BcryptWorkerPool";
|
||||
import { Authentication, CORS, ImageProxy, BodyParser, ErrorHandler, initRateLimits, initTranslation } from "./middlewares";
|
||||
import { initInstance } from "./util/handlers/Instance";
|
||||
import { route } from "./util";
|
||||
import { route, sendMessage } from "./util";
|
||||
import { EmbedType, MessageReferenceType, MessageType, PollAnswerCount } from "#schemas";
|
||||
|
||||
const ASSETS_FOLDER = path.join(__dirname, "..", "..", "assets");
|
||||
const PUBLIC_ASSETS_FOLDER = path.join(ASSETS_FOLDER, "public");
|
||||
@@ -189,6 +190,110 @@ export class SpacebarServer extends Server {
|
||||
});
|
||||
});
|
||||
|
||||
// Pickup non-expired polls
|
||||
const nonExpiredPolls = await Message.createQueryBuilder("message").where("message.poll->>'expiry' > :now", { now: new Date().toISOString() }).getMany();
|
||||
console.log(nonExpiredPolls);
|
||||
|
||||
for (const message of nonExpiredPolls) {
|
||||
if (!message.poll) {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingPolls.set(message.id, {
|
||||
timeout: setTimeout(
|
||||
async () => {
|
||||
if (!message.poll?.results) {
|
||||
return;
|
||||
}
|
||||
|
||||
const allAnswerCounts = message.poll.results.answer_counts as unknown as (Omit<PollAnswerCount, "me_voted"> & { voters: string[] })[];
|
||||
|
||||
const totalVotes = allAnswerCounts.map((a) => a.voters).length;
|
||||
const winningAnswerCounts = allAnswerCounts.filter((a) => (a.count * totalVotes) / 100);
|
||||
|
||||
const pollResultsMessage = {
|
||||
type: MessageType.POLL_RESULT,
|
||||
channel_id: message.channel_id,
|
||||
author_id: message.author_id,
|
||||
message_reference: {
|
||||
type: MessageReferenceType.DEFAULT,
|
||||
message_id: message.id,
|
||||
channel_id: message.channel_id,
|
||||
},
|
||||
embeds: [
|
||||
{
|
||||
type: EmbedType.poll_result,
|
||||
id: message.id,
|
||||
fields: [
|
||||
{
|
||||
name: "poll_question_text",
|
||||
value: message.poll.question.text!,
|
||||
},
|
||||
{
|
||||
name: "total_votes",
|
||||
value: totalVotes.toString(),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
if (winningAnswerCounts) {
|
||||
const winningAnswer = message.poll.answers.find((a) => a.answer_id === Number(winningAnswerCounts[0]?.id))!;
|
||||
|
||||
if (winningAnswerCounts.length === 0) {
|
||||
pollResultsMessage.embeds[0].fields.push({
|
||||
name: "victor_answer_votes",
|
||||
value: "0",
|
||||
});
|
||||
} else if (winningAnswerCounts.length === 1) {
|
||||
pollResultsMessage.embeds[0].fields.push(
|
||||
{
|
||||
name: "victor_answer_votes",
|
||||
value: winningAnswerCounts[0].count.toString(),
|
||||
},
|
||||
{
|
||||
name: "victor_answer_id",
|
||||
value: winningAnswerCounts[0].id,
|
||||
},
|
||||
{
|
||||
name: "victor_answer_text",
|
||||
value: winningAnswer.poll_media.text!,
|
||||
},
|
||||
);
|
||||
} else if (winningAnswerCounts.length > 1) {
|
||||
pollResultsMessage.embeds[0].fields.push({
|
||||
name: "victor_answer_votes",
|
||||
value: winningAnswerCounts[0].count.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
if (winningAnswer?.poll_media.emoji) {
|
||||
pollResultsMessage.embeds[0].fields.push(
|
||||
{
|
||||
name: "victor_answer_emoji_id",
|
||||
value: winningAnswer.poll_media.emoji.id!.toString()!,
|
||||
},
|
||||
{
|
||||
name: "victor_answer_emoji_name",
|
||||
value: winningAnswer.poll_media.emoji.name!,
|
||||
},
|
||||
{
|
||||
name: "victor_answer_emoji_animated",
|
||||
value: `${winningAnswer.poll_media.emoji.animated}`,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await sendMessage(pollResultsMessage);
|
||||
pendingPolls.delete(message.id);
|
||||
},
|
||||
new Date(message.poll.expiry).getTime() - Date.now(),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
function isReady(req: Request, res: Response) {
|
||||
if (!getDatabase()) return res.sendStatus(503);
|
||||
return res.sendStatus(200);
|
||||
|
||||
Reference in New Issue
Block a user