1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

empty messages check + allowed mentions

This commit is contained in:
Flam3rboy 2021-06-23 19:23:41 +02:00
parent 295fbabf0d
commit 2be07d2f73
5 changed files with 32 additions and 10 deletions

View File

@ -22,6 +22,8 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne
code = httpcode = 500;
}
if (httpcode > 511) httpcode = 400;
res.status(httpcode).json({ code: code, message, errors });
return;

View File

@ -99,13 +99,16 @@ const messageUpload = multer({
// https://discord.com/developers/docs/resources/channel#create-message
// TODO: text channel slowdown
// TODO: trim and replace message content and every embed field
// TODO: check allowed_mentions
// Send message
router.post("/", check(MessageCreateSchema), messageUpload.single("file"), async (req: Request, res: Response) => {
router.post("/", messageUpload.single("file"), async (req: Request, res: Response) => {
const { channel_id } = req.params;
var body = req.body as MessageCreateSchema;
const attachments: Attachment[] = [];
console.log(body);
if (req.file) {
try {
const file = await uploadFile(`/attachments/${channel_id}`, req.file);
@ -117,10 +120,11 @@ router.post("/", check(MessageCreateSchema), messageUpload.single("file"), async
if (body.payload_json) {
body = JSON.parse(body.payload_json);
const errors = instanceOf(MessageCreateSchema, body, { req });
if (errors !== true) throw errors;
}
const errors = instanceOf(MessageCreateSchema, body, { req });
if (errors !== true) throw errors;
const embeds = [];
if (body.embed) embeds.push(body.embed);
const data = await sendMessage({ ...body, type: 0, pinned: false, author_id: req.user_id, embeds, channel_id, attachments });

View File

@ -21,7 +21,7 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number,
if (!(await RoleModel.exists({ id: overwrite_id }))) throw new HTTPError("role not found", 404);
} else if (body.type === 1) {
if (!(await MemberModel.exists({ id: overwrite_id }))) throw new HTTPError("user not found", 404);
} else throw new HTTPError("type not supported");
} else throw new HTTPError("type not supported", 501);
// @ts-ignore
var overwrite: ChannelPermissionOverwrite = channel.permission_overwrites.find((x) => x.id === overwrite_id);

View File

@ -43,7 +43,12 @@ export const MessageCreateSchema = {
25
)
},
$allowed_mentions: [],
$allowed_mentions: {
$parse: [String],
$roles: [String],
$users: [String],
$replied_user: Boolean
},
$message_reference: {
message_id: String,
channel_id: String,
@ -60,7 +65,12 @@ export interface MessageCreateSchema {
tts?: boolean;
flags?: bigint;
embed?: Embed & { timestamp?: string };
allowed_mentions?: [];
allowed_mentions?: {
parse?: string[];
roles?: string[];
users?: string[];
replied_user?: boolean;
};
message_reference?: {
message_id: string;
channel_id: string;

View File

@ -8,6 +8,7 @@ import { HTTPError } from "lambert-server";
import fetch from "node-fetch";
import cheerio from "cheerio";
import { emitEvent } from "./Event";
import { MessageType } from "@fosscord/server-util/dist/util/Constants";
// TODO: check webhook, application, system author
const LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
@ -34,11 +35,14 @@ export async function handleMessage(opts: Partial<Message>) {
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?
// @ts-ignore
opts.type = MessageType.REPLY;
}
if (!opts.content && !opts.embeds?.length) {
throw new HTTPError("Empty messages are not allowed", 50006);
}
// TODO: check and put it all in the body
@ -114,7 +118,9 @@ export async function postHandleMessage(message: Message) {
export async function sendMessage(opts: Partial<Message>) {
const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() });
const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save());
const data = toObject(
await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).populate("referenced_message").save()
);
await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent);