✨ sendMessage() function
This commit is contained in:
parent
52b4a726f8
commit
6af753f4e0
@ -8,7 +8,7 @@ import {
|
|||||||
MessageDocument,
|
MessageDocument,
|
||||||
MessageModel,
|
MessageModel,
|
||||||
Snowflake,
|
Snowflake,
|
||||||
toObject,
|
toObject
|
||||||
} from "@fosscord/server-util";
|
} from "@fosscord/server-util";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { MessageCreateSchema } from "../../../../schema/Message";
|
import { MessageCreateSchema } from "../../../../schema/Message";
|
||||||
@ -18,6 +18,7 @@ import multer from "multer";
|
|||||||
import { emitEvent } from "../../../../util/Event";
|
import { emitEvent } from "../../../../util/Event";
|
||||||
import { Query } from "mongoose";
|
import { Query } from "mongoose";
|
||||||
import { PublicMemberProjection } from "../../../../util/Member";
|
import { PublicMemberProjection } from "../../../../util/Member";
|
||||||
|
import { sendMessage } from "../../../../util/Message";
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@ -48,7 +49,7 @@ router.get("/", async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
instanceOf({ $around: String, $after: String, $before: String, $limit: new Length(Number, 1, 100) }, req.query, {
|
instanceOf({ $around: String, $after: String, $before: String, $limit: new Length(Number, 1, 100) }, req.query, {
|
||||||
path: "query",
|
path: "query",
|
||||||
req,
|
req
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: 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)
|
else if (around)
|
||||||
query = MessageModel.find({
|
query = MessageModel.find({
|
||||||
channel_id,
|
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 {
|
else {
|
||||||
query = MessageModel.find({ channel_id }).sort({ id: -1 });
|
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
|
// TODO: trim and replace message content and every embed field
|
||||||
// Send message
|
// Send message
|
||||||
router.post("/", check(MessageCreateSchema), async (req, res) => {
|
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 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 = [];
|
const embeds = [];
|
||||||
if (body.embed) embeds.push(body.embed);
|
if (body.embed) embeds.push(body.embed);
|
||||||
|
const data = await sendMessage({ ...body, type: 0, pinned: false, author_id: req.user_id, embeds, channel_id });
|
||||||
// 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);
|
|
||||||
|
|
||||||
return res.send(data);
|
return res.send(data);
|
||||||
});
|
});
|
||||||
|
52
src/util/Message.ts
Normal file
52
src/util/Message.ts
Normal file
@ -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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user