diff --git a/api/src/util/handlers/Message.ts b/src/api/util/handlers/Message.ts
index 398b8914..7e91fb7b 100644
--- a/api/src/util/handlers/Message.ts
+++ b/src/api/util/handlers/Message.ts
@@ -21,11 +21,13 @@ import {
Webhook,
Attachment,
Config,
+ Sticker,
} from "@fosscord/util";
import { HTTPError } from "lambert-server";
import fetch from "node-fetch";
import cheerio from "cheerio";
import { MessageCreateSchema } from "../../routes/channels/#channel_id/messages";
+import { In } from "typeorm";
const allow_empty = false;
// TODO: check webhook, application, system author, stickers
// TODO: embed gifs/videos/images
@@ -47,9 +49,11 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
const channel = await Channel.findOneOrFail({ where: { id: opts.channel_id }, relations: ["recipients"] });
if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404);
- const message = new Message({
+ const stickers = opts.sticker_ids ? await Sticker.find({ where: { id: In(opts.sticker_ids) } }) : undefined;
+ const message = Message.create({
...opts,
- sticker_items: opts.sticker_ids?.map((x) => ({ id: x })),
+ id: Snowflake.generate(),
+ sticker_items: stickers,
guild_id: channel.guild_id,
channel_id: opts.channel_id,
attachments: opts.attachments || [],
@@ -68,10 +72,10 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
rights.hasThrow("SEND_MESSAGES");
}
if (opts.application_id) {
- message.application = await Application.findOneOrFail({ id: opts.application_id });
+ message.application = await Application.findOneOrFail({ where: { id: opts.application_id } });
}
if (opts.webhook_id) {
- message.webhook = await Webhook.findOneOrFail({ id: opts.webhook_id });
+ message.webhook = await Webhook.findOneOrFail({ where: { id: opts.webhook_id } });
}
const permission = await getPermission(opts.author_id, channel.guild_id, opts.channel_id);
@@ -85,7 +89,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
permission.hasThrow("READ_MESSAGE_HISTORY");
// code below has to be redone when we add custom message routing
if (message.guild_id !== null) {
- const guild = await Guild.findOneOrFail({ id: channel.guild_id });
+ const guild = await Guild.findOneOrFail({ where: { id: channel.guild_id } });
if (!guild.features.includes("CROSS_CHANNEL_REPLIES")) {
if (opts.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild");
if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel");
@@ -120,7 +124,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
await Promise.all(
Array.from(content.matchAll(ROLE_MENTION)).map(async ([_, mention]) => {
- const role = await Role.findOneOrFail({ id: mention, guild_id: channel.guild_id });
+ const role = await Role.findOneOrFail({ where: { id: mention, guild_id: channel.guild_id } });
if (role.mentionable || permission.has("MANAGE_ROLES")) {
mention_role_ids.push(mention);
}
@@ -132,9 +136,9 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
}
}
- message.mention_channels = mention_channel_ids.map((x) => new Channel({ id: x }));
- message.mention_roles = mention_role_ids.map((x) => new Role({ id: x }));
- message.mentions = mention_user_ids.map((x) => new User({ id: x }));
+ message.mention_channels = mention_channel_ids.map((x) => Channel.create({ id: x }));
+ message.mention_roles = mention_role_ids.map((x) => Role.create({ id: x }));
+ message.mentions = mention_user_ids.map((x) => User.create({ id: x }));
message.mention_everyone = mention_everyone;
// TODO: check and put it all in the body
@@ -278,6 +282,6 @@ interface MessageOptions extends MessageCreateSchema {
embeds?: Embed[];
channel_id?: string;
attachments?: Attachment[];
- edited_timestamp: Date | null;
+ edited_timestamp?: Date;
timestamp?: Date;
}
|