refactor: use pinned_at to check if message is pinnned

This commit is contained in:
CyberL1 2025-07-13 10:56:30 +02:00 committed by Rory&
parent b8b461f0db
commit dc3a85f078
6 changed files with 39 additions and 29 deletions

View File

@ -133,7 +133,7 @@ router.patch(
mentions: new_message.embeds,
mention_roles: new_message.mention_roles,
mention_everyone: new_message.mention_everyone,
pinned: new_message.pinned,
pinned: new_message.pinned_at != null,
timestamp: new_message.timestamp,
edited_timestamp: new_message.edited_timestamp,

View File

@ -55,14 +55,18 @@ router.put(
// * in dm channels anyone can pin messages -> only check for guilds
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
const pinned_count = await Message.count({
where: { channel: { id: channel_id }, pinned: true },
});
const pinned_count = await Message.createQueryBuilder("message")
.leftJoinAndSelect("message.channel", "channel")
.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;
if (pinned_count >= maxPins)
throw DiscordApiErrors.MAXIMUM_PINS.withParams(maxPins);
message.pinned = true;
message.pinned_at = new Date();
const author = await User.getPublicUser(req.user_id);
@ -140,7 +144,7 @@ router.delete(
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
message.pinned = false;
message.pinned_at = null;
await Promise.all([
message.save(),
@ -180,15 +184,15 @@ router.get(
async (req: Request, res: Response) => {
const { channel_id } = req.params;
const pins = await Message.find({
where: { channel_id: channel_id, pinned: true },
relations: ["author"],
order: {
pinned_at: "DESC",
},
});
const pins = await Message.createQueryBuilder("message")
.leftJoinAndSelect("message.channel", "channel")
.leftJoinAndSelect("message.author", "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) => ({
const items = pins.map((message: Message) => ({
message,
pinned_at: message.pinned_at,
}));

View File

@ -56,14 +56,18 @@ router.put(
// * in dm channels anyone can pin messages -> only check for guilds
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
const pinned_count = await Message.count({
where: { channel: { id: channel_id }, pinned: true },
});
const pinned_count = await Message.createQueryBuilder("message")
.leftJoinAndSelect("message.channel", "channel")
.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;
if (pinned_count >= maxPins)
throw DiscordApiErrors.MAXIMUM_PINS.withParams(maxPins);
message.pinned = true;
message.pinned_at = new Date();
const author = await User.getPublicUser(req.user_id);
@ -140,7 +144,7 @@ router.delete(
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
message.pinned = false;
message.pinned_at = null;
await Promise.all([
message.save(),
@ -180,10 +184,13 @@ router.get(
async (req: Request, res: Response) => {
const { channel_id } = req.params;
const pins = await Message.find({
where: { channel_id: channel_id, pinned: true },
relations: ["author"],
});
const pins = await Message.createQueryBuilder("message")
.leftJoinAndSelect("message.channel", "channel")
.leftJoinAndSelect("message.author", "author")
.where("channel.id = :channelId", { channelId: channel_id })
.andWhere("message.pinned_at IS NOT NULL")
.orderBy("message.pinned_at", "DESC")
.getMany();
res.send(pins);
},

View File

@ -155,7 +155,7 @@ router.get(
embeds: x.embeds,
mentions: x.mentions,
mention_roles: x.mention_roles,
pinned: x.pinned,
pinned: x.pinned_at != null,
mention_everyone: x.mention_everyone,
tts: x.tts,
timestamp: x.timestamp,

View File

@ -185,11 +185,8 @@ export class Message extends BaseClass {
@Column({ type: "text", nullable: true })
nonce?: string;
@Column({ nullable: true })
pinned?: boolean;
@Column({ nullable: true })
pinned_at?: Date;
@Column({ type: "timestamp", nullable: true })
pinned_at: Date | null;
@Column({ type: "int" })
type: MessageType;

View File

@ -7,11 +7,13 @@ export class MessagePinnedAt1752383879533 implements MigrationInterface {
await queryRunner.query(
`ALTER TABLE "messages" ADD "pinned_at" TIMESTAMP`,
);
await queryRunner.query(`ALTER TABLE "messages" DROP COLUMN "pinned"`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "messages" DROP COLUMN "pinned_at"`,
);
await queryRunner.query(`ALTER TABLE "messages" ADD "pinned" boolean`);
}
}