webhook fixes & username/avatar property for msg

This commit is contained in:
TomatoCake 2024-07-18 12:42:07 +02:00
parent 99d9bf563f
commit e7a98b6c46
9 changed files with 172 additions and 44 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,12 +1,14 @@
import { handleMessage, route } from "@spacebar/api"; import { handleMessage, postHandleMessage, route } from "@spacebar/api";
import { import {
Attachment, Attachment,
Config, Config,
DiscordApiErrors, DiscordApiErrors,
FieldErrors, FieldErrors,
Message, Message,
MessageCreateEvent,
Webhook, Webhook,
WebhookExecuteSchema, WebhookExecuteSchema,
emitEvent,
uploadFile, uploadFile,
} from "@spacebar/util"; } from "@spacebar/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
@ -93,7 +95,11 @@ router.post(
}, },
}), }),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { wait, thread_id } = req.query;
if (!wait) return res.status(204).send();
const { webhook_id, token } = req.params; const { webhook_id, token } = req.params;
const body = req.body as WebhookExecuteSchema; const body = req.body as WebhookExecuteSchema;
const attachments: Attachment[] = []; const attachments: Attachment[] = [];
@ -200,6 +206,7 @@ router.post(
webhook_id: webhook.id, webhook_id: webhook.id,
application_id: webhook.application?.id, application_id: webhook.application?.id,
embeds, embeds,
// TODO: Support thread_id/thread_name once threads are implemented
channel_id: webhook.channel_id, channel_id: webhook.channel_id,
attachments, attachments,
timestamp: new Date(), timestamp: new Date(),
@ -209,6 +216,22 @@ router.post(
message.edited_timestamp = null; message.edited_timestamp = null;
webhook.channel.last_message_id = message.id; webhook.channel.last_message_id = message.id;
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_CREATE",
channel_id: webhook.channel_id,
data: message,
} as MessageCreateEvent),
]);
// no await as it shouldnt block the message send function and silently catch error
postHandleMessage(message).catch((e) =>
console.error("[Message] post-message handler failed", e),
);
return res.json(message);
}, },
); );

View File

@ -41,11 +41,13 @@ import {
Sticker, Sticker,
MessageCreateSchema, MessageCreateSchema,
EmbedCache, EmbedCache,
handleFile,
} from "@spacebar/util"; } from "@spacebar/util";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { In } from "typeorm"; import { In } from "typeorm";
import { EmbedHandlers } from "@spacebar/api"; import { EmbedHandlers } from "@spacebar/api";
import * as Sentry from "@sentry/node"; import * as Sentry from "@sentry/node";
import fetch from "node-fetch";
const allow_empty = false; const allow_empty = false;
// TODO: check webhook, application, system author, stickers // TODO: check webhook, application, system author, stickers
// TODO: embed gifs/videos/images // TODO: embed gifs/videos/images
@ -92,13 +94,57 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
where: { id: opts.application_id }, where: { id: opts.application_id },
}); });
} }
let permission: any;
if (opts.webhook_id) { if (opts.webhook_id) {
message.webhook = await Webhook.findOneOrFail({ message.webhook = await Webhook.findOneOrFail({
where: { id: opts.webhook_id }, where: { id: opts.webhook_id },
}); });
message.author = (await User.findOne({
where: { id: opts.webhook_id },
})) || undefined;
if (!message.author) {
message.author = User.create({
id: opts.webhook_id,
username: message.webhook.name,
discriminator: "0000",
avatar: message.webhook.avatar,
public_flags: 0,
premium: false,
premium_type: 0,
bot: true,
created_at: new Date(),
verified: true,
rights: "0",
data: {
valid_tokens_since: new Date(),
},
});
await message.author.save();
} }
const permission = await getPermission( if (opts.username) {
message.username = opts.username;
message.author.username = message.username;
}
if (opts.avatar_url) {
const avatarData = await fetch(opts.avatar_url);
const base64 = await avatarData.buffer().then((x) => x.toString("base64"));
const dataUri = "data:" + avatarData.headers.get("content-type") + ";base64," + base64;
message.avatar = await handleFile(
`/avatars/${opts.webhook_id}`,
dataUri as string,
);
console.log(message.avatar);
message.author.avatar = message.avatar;
}
} else {
permission = await getPermission(
opts.author_id, opts.author_id,
channel.guild_id, channel.guild_id,
opts.channel_id, opts.channel_id,
@ -131,6 +177,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
otherwise backfilling won't work **/ otherwise backfilling won't work **/
message.type = MessageType.REPLY; message.type = MessageType.REPLY;
} }
}
// TODO: stickers/activity // TODO: stickers/activity
if ( if (
@ -172,14 +219,14 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
const role = await Role.findOneOrFail({ const role = await Role.findOneOrFail({
where: { id: mention, guild_id: channel.guild_id }, where: { id: mention, guild_id: channel.guild_id },
}); });
if (role.mentionable || permission.has("MANAGE_ROLES")) { if (role.mentionable || (opts.webhook_id || permission.has("MANAGE_ROLES"))) {
mention_role_ids.push(mention); mention_role_ids.push(mention);
} }
}, },
), ),
); );
if (permission.has("MENTION_EVERYONE")) { if (opts.webhook_id || permission.has("MENTION_EVERYONE")) {
mention_everyone = mention_everyone =
!!content.match(EVERYONE_MENTION) || !!content.match(EVERYONE_MENTION) ||
!!content.match(HERE_MENTION); !!content.match(HERE_MENTION);
@ -302,4 +349,6 @@ interface MessageOptions extends MessageCreateSchema {
attachments?: Attachment[]; attachments?: Attachment[];
edited_timestamp?: Date; edited_timestamp?: Date;
timestamp?: Date; timestamp?: Date;
username?: string;
avatar_url?: string;
} }

View File

@ -218,6 +218,12 @@ export class Message extends BaseClass {
@Column({ type: "simple-json", nullable: true }) @Column({ type: "simple-json", nullable: true })
components?: MessageComponent[]; components?: MessageComponent[];
@Column({ nullable: true })
username?: string;
@Column({ nullable: true })
avatar?: string;
toJSON(): Message { toJSON(): Message {
return { return {
...this, ...this,
@ -234,7 +240,12 @@ export class Message extends BaseClass {
reactions: this.reactions ?? undefined, reactions: this.reactions ?? undefined,
sticker_items: this.sticker_items ?? undefined, sticker_items: this.sticker_items ?? undefined,
message_reference: this.message_reference ?? undefined, message_reference: this.message_reference ?? undefined,
author: this.author?.toPublicUser() ?? undefined, author: {
...this.author?.toPublicUser() ?? undefined,
// Webhooks
username: this.username ?? this.author?.username,
avatar: this.avatar ?? this.author?.avatar,
},
activity: this.activity ?? undefined, activity: this.activity ?? undefined,
application: this.application ?? undefined, application: this.application ?? undefined,
components: this.components ?? undefined, components: this.components ?? undefined,

View File

@ -35,7 +35,7 @@ export class Webhook extends BaseClass {
type: WebhookType; type: WebhookType;
@Column({ nullable: true }) @Column({ nullable: true })
name?: string; name: string;
@Column({ nullable: true }) @Column({ nullable: true })
avatar?: string; avatar?: string;

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class WebhookMessageProperties1721298824927 implements MigrationInterface {
name = "WebhookMessageProperties1721298824927";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE `messages` ADD `username` text NULL");
await queryRunner.query("ALTER TABLE `messages` ADD `avatar` text NULL");
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE `messages` DROP COLUMN `username`");
await queryRunner.query("ALTER TABLE `messages` DROP COLUMN `avatar`");
}
}

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class WebhookMessageProperties1721298824927 implements MigrationInterface {
name = "WebhookMessageProperties1721298824927";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE `messages` ADD `username` text NULL");
await queryRunner.query("ALTER TABLE `messages` ADD `avatar` text NULL");
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE `messages` DROP COLUMN `username`");
await queryRunner.query("ALTER TABLE `messages` DROP COLUMN `avatar`");
}
}

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class WebhookMessageProperties1721298824927 implements MigrationInterface {
name = "WebhookMessageProperties1721298824927";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE messages ADD username text NULL");
await queryRunner.query("ALTER TABLE messages ADD avatar text NULL");
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE messages DROP COLUMN username");
await queryRunner.query("ALTER TABLE messages DROP COLUMN avatar");
}
}