refactor: don't use query builder

This commit is contained in:
CyberL1 2025-09-29 07:40:26 +02:00 committed by Rory&
parent 6902e1d19c
commit 29c4a6695c
2 changed files with 14 additions and 21 deletions

View File

@ -28,6 +28,7 @@ import {
User, User,
} from "@spacebar/util"; } from "@spacebar/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { IsNull, Not } from "typeorm";
const router: Router = Router(); const router: Router = Router();
@ -184,13 +185,10 @@ router.get(
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { channel_id } = req.params; const { channel_id } = req.params;
const pins = await Message.createQueryBuilder("message") const pins = await Message.find({
.leftJoinAndSelect("message.channel", "channel") where: { channel_id: channel_id, pinned_at: Not(IsNull()) },
.leftJoinAndSelect("message.author", "author") relations: ["author"],
.where("channel.id = :channelId", { channelId: channel_id }) });
.andWhere("message.pinned_at IS NOT NULL")
.orderBy("message.pinned_at", "DESC")
.getMany();
const items = pins.map((message: Message) => ({ const items = pins.map((message: Message) => ({
message, message,

View File

@ -28,6 +28,7 @@ import {
User, User,
} from "@spacebar/util"; } from "@spacebar/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { IsNull, Not } from "typeorm";
const router: Router = Router(); const router: Router = Router();
@ -56,13 +57,10 @@ router.put(
// * in dm channels anyone can pin messages -> only check for guilds // * in dm channels anyone can pin messages -> only check for guilds
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
const pinned_count = await Message.createQueryBuilder("message") const pinned_count = await Message.count({
.leftJoinAndSelect("message.channel", "channel") where: { channel: { id: channel_id }, pinned_at: Not(IsNull()) },
.leftJoinAndSelect("message.author", "author") });
.where("channel.id = :channelId", { channelId: channel_id })
.andWhere("message.pinned_at IS NOT NULL")
.orderBy("message.pinned_at", "DESC")
.getCount();
const { maxPins } = Config.get().limits.channel; const { maxPins } = Config.get().limits.channel;
if (pinned_count >= maxPins) if (pinned_count >= maxPins)
throw DiscordApiErrors.MAXIMUM_PINS.withParams(maxPins); throw DiscordApiErrors.MAXIMUM_PINS.withParams(maxPins);
@ -184,13 +182,10 @@ router.get(
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { channel_id } = req.params; const { channel_id } = req.params;
const pins = await Message.createQueryBuilder("message") const pins = await Message.find({
.leftJoinAndSelect("message.channel", "channel") where: { channel_id: channel_id, pinned_at: Not(IsNull()) },
.leftJoinAndSelect("message.author", "author") relations: ["author"],
.where("channel.id = :channelId", { channelId: channel_id }) });
.andWhere("message.pinned_at IS NOT NULL")
.orderBy("message.pinned_at", "DESC")
.getMany();
res.send(pins); res.send(pins);
}, },