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, mentions: new_message.embeds,
mention_roles: new_message.mention_roles, mention_roles: new_message.mention_roles,
mention_everyone: new_message.mention_everyone, mention_everyone: new_message.mention_everyone,
pinned: new_message.pinned, pinned: new_message.pinned_at != null,
timestamp: new_message.timestamp, timestamp: new_message.timestamp,
edited_timestamp: new_message.edited_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 // * 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.count({ const pinned_count = await Message.createQueryBuilder("message")
where: { channel: { id: channel_id }, pinned: true }, .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; 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);
message.pinned = true;
message.pinned_at = new Date(); message.pinned_at = new Date();
const author = await User.getPublicUser(req.user_id); const author = await User.getPublicUser(req.user_id);
@ -140,7 +144,7 @@ router.delete(
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
message.pinned = false; message.pinned_at = null;
await Promise.all([ await Promise.all([
message.save(), message.save(),
@ -180,15 +184,15 @@ 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.find({ const pins = await Message.createQueryBuilder("message")
where: { channel_id: channel_id, pinned: true }, .leftJoinAndSelect("message.channel", "channel")
relations: ["author"], .leftJoinAndSelect("message.author", "author")
order: { .where("channel.id = :channelId", { channelId: channel_id })
pinned_at: "DESC", .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, message,
pinned_at: message.pinned_at, pinned_at: message.pinned_at,
})); }));

View File

@ -56,14 +56,18 @@ 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.count({ const pinned_count = await Message.createQueryBuilder("message")
where: { channel: { id: channel_id }, pinned: true }, .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; 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);
message.pinned = true; message.pinned_at = new Date();
const author = await User.getPublicUser(req.user_id); const author = await User.getPublicUser(req.user_id);
@ -140,7 +144,7 @@ router.delete(
if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES");
message.pinned = false; message.pinned_at = null;
await Promise.all([ await Promise.all([
message.save(), message.save(),
@ -180,10 +184,13 @@ 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.find({ const pins = await Message.createQueryBuilder("message")
where: { channel_id: channel_id, pinned: true }, .leftJoinAndSelect("message.channel", "channel")
relations: ["author"], .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); res.send(pins);
}, },

View File

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

View File

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

View File

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