From 5679318be0d4bc96002864f21be1b966d578a567 Mon Sep 17 00:00:00 2001 From: Cyber Date: Sun, 18 Aug 2024 11:17:19 +0200 Subject: [PATCH 1/4] feat: message pinned message --- src/api/routes/channels/#channel_id/pins.ts | 33 ++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/api/routes/channels/#channel_id/pins.ts b/src/api/routes/channels/#channel_id/pins.ts index 1e1da018..9a806b5a 100644 --- a/src/api/routes/channels/#channel_id/pins.ts +++ b/src/api/routes/channels/#channel_id/pins.ts @@ -1,23 +1,24 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Copyright (C) 2023 Spacebar and Spacebar Contributors - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import { route } from "@spacebar/api"; import { + Channel, ChannelPinsUpdateEvent, Config, DiscordApiErrors, @@ -77,6 +78,20 @@ router.put( last_pin_timestamp: undefined, }, } as ChannelPinsUpdateEvent), + + Message.create({ + type: 6, + embeds: [], + reactions: [], + channel_id: message.channel_id, + guild_id: message.guild_id, + author_id: req.user_id, + message_reference: { + message_id: message.id, + channel_id: message.channel_id, + guild_id: message.guild_id, + }, + }).save(), ]); res.sendStatus(204); @@ -99,27 +114,31 @@ router.delete( async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; + const channel = await Channel.findOneOrFail({ + where: { id: channel_id }, + }); + if (channel.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); + const message = await Message.findOneOrFail({ where: { id: message_id }, }); - - if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); - message.pinned = false; await Promise.all([ message.save(), + emitEvent({ event: "MESSAGE_UPDATE", channel_id, data: message, } as MessageUpdateEvent), + emitEvent({ event: "CHANNEL_PINS_UPDATE", channel_id, data: { channel_id, - guild_id: message.guild_id, + guild_id: channel.guild_id, last_pin_timestamp: undefined, }, } as ChannelPinsUpdateEvent), From 7853ad3acf8f79d904a808382aa4ccc6d5df78fd Mon Sep 17 00:00:00 2001 From: Cyber Date: Sun, 18 Aug 2024 11:31:15 +0200 Subject: [PATCH 2/4] fix: update the code with latest commit --- src/api/routes/channels/#channel_id/pins.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/api/routes/channels/#channel_id/pins.ts b/src/api/routes/channels/#channel_id/pins.ts index 9a806b5a..7b2236e8 100644 --- a/src/api/routes/channels/#channel_id/pins.ts +++ b/src/api/routes/channels/#channel_id/pins.ts @@ -1,24 +1,23 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Copyright (C) 2023 Spacebar and Spacebar Contributors - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import { route } from "@spacebar/api"; import { - Channel, ChannelPinsUpdateEvent, Config, DiscordApiErrors, @@ -114,31 +113,27 @@ router.delete( async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; - const channel = await Channel.findOneOrFail({ - where: { id: channel_id }, - }); - if (channel.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); - const message = await Message.findOneOrFail({ where: { id: message_id }, }); + + if (message.guild_id) req.permission?.hasThrow("MANAGE_MESSAGES"); + message.pinned = false; await Promise.all([ message.save(), - emitEvent({ event: "MESSAGE_UPDATE", channel_id, data: message, } as MessageUpdateEvent), - emitEvent({ event: "CHANNEL_PINS_UPDATE", channel_id, data: { channel_id, - guild_id: channel.guild_id, + guild_id: message.guild_id, last_pin_timestamp: undefined, }, } as ChannelPinsUpdateEvent), From a401f63318ededb7a20c88dc5a06a7dd73f27076 Mon Sep 17 00:00:00 2001 From: Cyber Date: Sun, 18 Aug 2024 12:08:04 +0200 Subject: [PATCH 3/4] fix: message timestamp --- src/api/routes/channels/#channel_id/pins.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/routes/channels/#channel_id/pins.ts b/src/api/routes/channels/#channel_id/pins.ts index 7b2236e8..b6bb66b9 100644 --- a/src/api/routes/channels/#channel_id/pins.ts +++ b/src/api/routes/channels/#channel_id/pins.ts @@ -79,6 +79,7 @@ router.put( } as ChannelPinsUpdateEvent), Message.create({ + timestamp: new Date(), type: 6, embeds: [], reactions: [], From 39e3b5ad7a8eb8f33d3d2278ba70c3747affd4ee Mon Sep 17 00:00:00 2001 From: Cyber Date: Sun, 18 Aug 2024 13:32:19 +0200 Subject: [PATCH 4/4] feat: emit `MESSAGE_CREATE` --- src/api/routes/channels/#channel_id/pins.ts | 45 ++++++++++++++------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/api/routes/channels/#channel_id/pins.ts b/src/api/routes/channels/#channel_id/pins.ts index b6bb66b9..d43db6ec 100644 --- a/src/api/routes/channels/#channel_id/pins.ts +++ b/src/api/routes/channels/#channel_id/pins.ts @@ -23,7 +23,9 @@ import { DiscordApiErrors, emitEvent, Message, + MessageCreateEvent, MessageUpdateEvent, + User, } from "@spacebar/util"; import { Request, Response, Router } from "express"; @@ -61,6 +63,30 @@ router.put( message.pinned = true; + const author = await User.getPublicUser(req.user_id); + + const systemPinMessage = Message.create({ + timestamp: new Date(), + type: 6, + guild_id: message.guild_id, + channel_id: message.channel_id, + author, + message_reference: { + message_id: message.id, + channel_id: message.channel_id, + guild_id: message.guild_id, + }, + reactions: [], + attachments: [], + embeds: [], + sticker_items: [], + edited_timestamp: undefined, + mentions: [], + mention_channels: [], + mention_roles: [], + mention_everyone: false, + }); + await Promise.all([ message.save(), emitEvent({ @@ -77,21 +103,12 @@ router.put( last_pin_timestamp: undefined, }, } as ChannelPinsUpdateEvent), - - Message.create({ - timestamp: new Date(), - type: 6, - embeds: [], - reactions: [], + systemPinMessage.save(), + emitEvent({ + event: "MESSAGE_CREATE", channel_id: message.channel_id, - guild_id: message.guild_id, - author_id: req.user_id, - message_reference: { - message_id: message.id, - channel_id: message.channel_id, - guild_id: message.guild_id, - }, - }).save(), + data: systemPinMessage, + } as MessageCreateEvent), ]); res.sendStatus(204);