mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-14 06:32:36 +01:00
🚧 [Route] /messages
This commit is contained in:
parent
6a1e0594dc
commit
e902b805c1
@ -8,6 +8,8 @@ import { check } from "../../../../../../util/instanceOf";
|
|||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|
||||||
|
// TODO: should users be able to bulk delete messages or only bots?
|
||||||
// TODO: should this request fail, if you provide messages older than 14 days/invalid ids?
|
// TODO: should this request fail, if you provide messages older than 14 days/invalid ids?
|
||||||
// https://discord.com/developers/docs/resources/channel#bulk-delete-messages
|
// https://discord.com/developers/docs/resources/channel#bulk-delete-messages
|
||||||
router.post("/", check({ messages: [BigInt] }), async (req, res) => {
|
router.post("/", check({ messages: [BigInt] }), async (req, res) => {
|
||||||
@ -15,7 +17,7 @@ router.post("/", check({ messages: [BigInt] }), async (req, res) => {
|
|||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec();
|
||||||
if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
|
if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
|
||||||
|
|
||||||
const permission = await getPermission(req.userid, channel?.guild_id, channel_id, { channel });
|
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
|
||||||
if (!permission.has("MANAGE_MESSAGES")) throw new HTTPError("You are missing the MANAGE_MESSAGES permissions");
|
if (!permission.has("MANAGE_MESSAGES")) throw new HTTPError("You are missing the MANAGE_MESSAGES permissions");
|
||||||
|
|
||||||
const { maxBulkDelete } = Config.get().limits.message;
|
const { maxBulkDelete } = Config.get().limits.message;
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { ChannelModel, ChannelType, getPermission, MessageModel } from "fosscord-server-util";
|
import { ChannelModel, ChannelType, getPermission, Message, MessageCreateEvent, MessageModel, Snowflake } from "fosscord-server-util";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
|
import { MessageCreateSchema } from "../../../../../../schema/Message";
|
||||||
import { check, instanceOf, Length } from "../../../../../../util/instanceOf";
|
import { check, instanceOf, Length } from "../../../../../../util/instanceOf";
|
||||||
import { PublicUserProjection } from "../../../../../../util/User";
|
import { PublicUserProjection } from "../../../../../../util/User";
|
||||||
|
import multer from "multer";
|
||||||
|
import { emitEvent } from "../../../../../../util/Event";
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@ -21,6 +24,8 @@ function isTextChannel(type: ChannelType): boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#create-message
|
||||||
|
// get messages
|
||||||
router.get("/", async (req, res) => {
|
router.get("/", async (req, res) => {
|
||||||
const channel_id = BigInt(req.params.channel_id);
|
const channel_id = BigInt(req.params.channel_id);
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
|
||||||
@ -44,12 +49,12 @@ router.get("/", async (req, res) => {
|
|||||||
throw new HTTPError("Not a text channel");
|
throw new HTTPError("Not a text channel");
|
||||||
|
|
||||||
if (channel.guild_id) {
|
if (channel.guild_id) {
|
||||||
const permissions = await getPermission(req.userid, channel.guild_id, channel_id, { channel });
|
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id, { channel });
|
||||||
if (!permissions.has("VIEW_CHANNEL")) throw new HTTPError("You don't have permission to view this channel", 401);
|
if (!permissions.has("VIEW_CHANNEL")) throw new HTTPError("You don't have permission to view this channel", 401);
|
||||||
if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
|
if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
|
||||||
} else if (channel.recipients) {
|
} else if (channel.recipients) {
|
||||||
// group/dm channel
|
// group/dm channel
|
||||||
if (!channel.recipients.includes(req.userid)) throw new HTTPError("You don't have permission to view this channel", 401);
|
if (!channel.recipients.includes(req.user_id)) throw new HTTPError("You don't have permission to view this channel", 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
var query: any;
|
var query: any;
|
||||||
@ -72,14 +77,50 @@ router.get("/", async (req, res) => {
|
|||||||
return res.json(messages);
|
return res.json(messages);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/", check(), async (req, res) => {
|
// TODO: config max upload size
|
||||||
|
const messageUpload = multer({ limits: { fieldSize: 1024 * 1024 * 1024 * 50 } }); // max upload 50 mb
|
||||||
|
|
||||||
|
// TODO: dynamically change limit of MessageCreateSchema with config
|
||||||
|
// TODO: check: sum of all characters in an embed structure must not exceed 6000 characters
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/channel#create-message
|
||||||
|
// TODO: text channel slowdown
|
||||||
|
// TODO: trim and replace message content and every embed field
|
||||||
|
// Send message
|
||||||
|
router.post("/", check(MessageCreateSchema), async (req, res) => {
|
||||||
const channel_id = BigInt(req.params.channel_id);
|
const channel_id = BigInt(req.params.channel_id);
|
||||||
|
const body = req.body as MessageCreateSchema;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
|
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);
|
if (!channel) throw new HTTPError("Channel not found", 404);
|
||||||
|
|
||||||
if (channel.guild_id) {
|
if (channel.guild_id) {
|
||||||
const permissions = await getPermission(req.userid, channel.guild_id, channel_id, { channel });
|
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id, { channel });
|
||||||
if (!permissions.has("SEND_MESSAGES")) throw new HTTPError("You don't have the SEND_MESSAGES permission");
|
if (!permissions.has("SEND_MESSAGES")) throw new HTTPError("You don't have the SEND_MESSAGES permission");
|
||||||
|
if (body.tts && !permissions.has("SEND_TTS_MESSAGES")) throw new HTTPError("You are missing the SEND_TTS_MESSAGES permission");
|
||||||
|
if (body.message_reference) {
|
||||||
|
if (!permissions.has("READ_MESSAGE_HISTORY"))
|
||||||
|
throw new HTTPError("You are missing the READ_MESSAGE_HISTORY permission to reply");
|
||||||
|
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 it be checked if the message exists?
|
||||||
|
}
|
||||||
|
|
||||||
|
const message: Message = {
|
||||||
|
id: Snowflake.generate(),
|
||||||
|
channel_id,
|
||||||
|
guild_id: channel.guild_id,
|
||||||
|
author_id: req.user_id,
|
||||||
|
content: req.body,
|
||||||
|
timestamp: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
await new MessageModel().save();
|
||||||
|
|
||||||
|
await emitEvent({ event: "MESSAGE_CREATE", channel_id, data: {} } as MessageCreateEvent);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user