summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-21 21:58:41 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-21 21:58:41 +0200
commit6af753f4e05dd769a52af084d45e2b00dfc09830 (patch)
tree28ecb502634370e44bf5d4bb7ff7ac03039d4ab3 /src
parentMerge pull request #143 from DarrenAlex/patch-1 (diff)
downloadserver-6af753f4e05dd769a52af084d45e2b00dfc09830.tar.xz
:sparkles: sendMessage() function
Diffstat (limited to 'src')
-rw-r--r--src/routes/channels/#channel_id/messages/index.ts51
-rw-r--r--src/util/Message.ts52
2 files changed, 58 insertions, 45 deletions
diff --git a/src/routes/channels/#channel_id/messages/index.ts b/src/routes/channels/#channel_id/messages/index.ts

index b186343e..7bf6412d 100644 --- a/src/routes/channels/#channel_id/messages/index.ts +++ b/src/routes/channels/#channel_id/messages/index.ts
@@ -8,7 +8,7 @@ import { MessageDocument, MessageModel, Snowflake, - toObject, + toObject } from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; import { MessageCreateSchema } from "../../../../schema/Message"; @@ -18,6 +18,7 @@ import multer from "multer"; import { emitEvent } from "../../../../util/Event"; import { Query } from "mongoose"; import { PublicMemberProjection } from "../../../../util/Member"; +import { sendMessage } from "../../../../util/Message"; const router: Router = Router(); export default router; @@ -48,7 +49,7 @@ router.get("/", async (req, res) => { try { instanceOf({ $around: String, $after: String, $before: String, $limit: new Length(Number, 1, 100) }, req.query, { path: "query", - req, + req }); } catch (error) { return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); @@ -67,7 +68,7 @@ router.get("/", async (req, res) => { else if (around) query = MessageModel.find({ channel_id, - id: { $gt: (BigInt(around) - BigInt(halfLimit)).toString(), $lt: (BigInt(around) + BigInt(halfLimit)).toString() }, + id: { $gt: (BigInt(around) - BigInt(halfLimit)).toString(), $lt: (BigInt(around) + BigInt(halfLimit)).toString() } }); else { query = MessageModel.find({ channel_id }).sort({ id: -1 }); @@ -89,52 +90,12 @@ const messageUpload = multer({ limits: { fieldSize: 1024 * 1024 * 1024 * 50 } }) // TODO: trim and replace message content and every embed field // Send message router.post("/", check(MessageCreateSchema), async (req, res) => { - const channel_id = req.params.channel_id; + const { channel_id } = req.params; const body = req.body as MessageCreateSchema; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); - // TODO: are tts messages allowed in dm channels? should permission be checked? - - const permissions = await getPermission(req.user_id, channel.guild_id, channel_id, { channel }); - permissions.hasThrow("SEND_MESSAGES"); - if (body.tts) permissions.hasThrow("SEND_TTS_MESSAGES"); - if (body.message_reference) { - permissions.hasThrow("READ_MESSAGE_HISTORY"); - if (body.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild"); - } - - if (body.message_reference) { - if (body.message_reference.channel_id !== channel_id) throw new HTTPError("You can only reference messages from this channel"); - // TODO: should be checked if the referenced message exists? - } - const embeds = []; if (body.embed) embeds.push(body.embed); - - // TODO: check and put it all in the body - const message: Message = { - id: Snowflake.generate(), - channel_id, - guild_id: channel.guild_id, - author_id: req.user_id, - content: body.content, - timestamp: new Date(), - mention_channels_ids: [], - mention_role_ids: [], - mention_user_ids: [], - attachments: [], - embeds: [], - reactions: [], - type: 0, - tts: body.tts, - nonce: body.nonce, - pinned: false, - }; - - const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); - - await emitEvent({ event: "MESSAGE_CREATE", channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); + const data = await sendMessage({ ...body, type: 0, pinned: false, author_id: req.user_id, embeds, channel_id }); return res.send(data); }); diff --git a/src/util/Message.ts b/src/util/Message.ts new file mode 100644
index 00000000..8e9f98cd --- /dev/null +++ b/src/util/Message.ts
@@ -0,0 +1,52 @@ +import { ChannelModel, MessageCreateEvent } from "@fosscord/server-util"; +import { Snowflake } from "@fosscord/server-util"; +import { MessageModel } from "@fosscord/server-util"; +import { PublicMemberProjection } from "@fosscord/server-util"; +import { toObject } from "@fosscord/server-util"; +import { getPermission } from "@fosscord/server-util"; +import { Message } from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { emitEvent } from "./Event"; +// TODO: check webhook, application, system author + +export async function sendMessage(opts: Partial<Message>) { + const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); + if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); + // TODO: are tts messages allowed in dm channels? should permission be checked? + + const permissions = await getPermission(opts.author_id, channel.guild_id, opts.channel_id, { channel }); + permissions.hasThrow("SEND_MESSAGES"); + if (opts.tts) permissions.hasThrow("SEND_TTS_MESSAGES"); + if (opts.message_reference) { + permissions.hasThrow("READ_MESSAGE_HISTORY"); + if (opts.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild"); + } + + if (opts.message_reference) { + if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel"); + // TODO: should be checked if the referenced message exists? + } + + // TODO: check and put it all in the body + const message: Message = { + ...opts, + id: Snowflake.generate(), + timestamp: new Date(), + guild_id: channel.guild_id, + channel_id: opts.channel_id, + // TODO: generate mentions and check permissions + mention_channels_ids: [], + mention_role_ids: [], + mention_user_ids: [], + attachments: [], // TODO: message attachments + embeds: opts.embeds || [], + reactions: opts.reactions || [], + type: opts.type ?? 0 + }; + + const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); + + await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); + + return data; +}