make spacebar compile again

This commit is contained in:
Oh64 authored and Rory& committed 2026-03-21 08:27:57 +01:00
1 parent a4e87dd792
commit 7a92946bed
12 files changed
+91 -50

No files matched your search

+3 -2
View File
@@ -90,11 +90,12 @@ router.post(
channel.last_message_id = message.id;
await message.save();
const publicMsg = message.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_CREATE",
data: message,
data: publicMsg,
channel_id,
} satisfies MessageCreateEvent),
channel.save(),
@@ -100,18 +100,15 @@ router.patch(
edited_timestamp: new Date(),
});
await Promise.all([
new_message.save(),
await emitEvent({
event: "MESSAGE_UPDATE",
channel_id,
data: {
...new_message.toJSON(),
nonce: undefined,
member: new_message.member?.toPublicMember(),
},
} satisfies MessageUpdateEvent),
]);
await new_message.save();
await emitEvent({
event: "MESSAGE_UPDATE",
channel_id,
data: {
...new_message.toJSON(),
nonce: undefined,
},
} satisfies MessageUpdateEvent);
postHandleMessage(new_message).catch((e) => console.error("[Message] post-message handler failed", e));
@@ -221,12 +218,13 @@ router.put(
//Fix for the client bug
delete message.member;
await message.save();
const publicMsg = message.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_CREATE",
channel_id: channel_id,
data: message,
data: publicMsg,
} satisfies MessageCreateEvent),
channel.save(),
]);
@@ -54,6 +54,8 @@ import {
MessageCreateAttachment,
MessageCreateCloudAttachment,
MessageCreateSchema,
PartialUser,
PublicMessage,
Reaction,
ReadStateType,
RelationshipType,
@@ -178,8 +180,8 @@ router.get(
await Message.fillReplies(messages);
const endpoint = Config.get().cdn.endpointPublic;
const ret = messages.map((x: Message) => {
x = x.toJSON();
const ret = messages.map((msg) => {
const x = msg.toJSON();
(x.reactions || []).forEach((y: Partial<Reaction>) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -188,12 +190,13 @@ router.get(
delete y.user_ids;
});
if (!x.author)
x.author = User.create({
x.author = {
id: "4",
discriminator: "0000",
username: "Spacebar Ghost",
public_flags: 0,
});
avatar: null,
} as PartialUser;
x.attachments?.forEach((y: Attachment) => {
// dynamically set attachment proxy_url in case the endpoint changed
const uri = y.proxy_url.startsWith("http") ? y.proxy_url : `https://example.org${y.proxy_url}`;
@@ -243,11 +246,15 @@ router.get(
});
//console.log(ret);
type MessageWithInteraction = PublicMessage & {
interaction_metadata?: { user?: User; user_id: string };
interaction?: { user?: User };
};
await Promise.all(
ret
.filter((x: MessageCreateSchema) => x.interaction_metadata && !x.interaction_metadata.user)
.map(async (x: MessageCreateSchema) => {
x.interaction_metadata!.user = x.interaction!.user = await User.findOneOrFail({ where: { id: (x as Message).interaction_metadata!.user_id } });
(ret as MessageWithInteraction[])
.filter((x) => x.interaction_metadata && !x.interaction_metadata.user)
.map(async (x) => {
x.interaction_metadata!.user = x.interaction!.user = await User.findOneOrFail({ where: { id: x.interaction_metadata!.user_id } });
}),
);
@@ -80,12 +80,14 @@ router.put(
mention_everyone: false,
});
await message.save();
const publicMsg = message.toJSON();
const publicSystem = systemPinMessage.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_UPDATE",
channel_id,
data: message,
data: publicMsg,
} satisfies MessageUpdateEvent),
emitEvent({
event: "CHANNEL_PINS_UPDATE",
@@ -100,7 +102,7 @@ router.put(
emitEvent({
event: "MESSAGE_CREATE",
channel_id: message.channel_id,
data: systemPinMessage,
data: publicSystem,
} satisfies MessageCreateEvent),
]);
@@ -133,12 +135,13 @@ router.delete(
message.pinned_at = null;
await message.save();
const publicMsg2 = message.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_UPDATE",
channel_id,
data: message,
data: publicMsg2,
} satisfies MessageUpdateEvent),
emitEvent({
event: "CHANNEL_PINS_UPDATE",
+8 -5
View File
@@ -81,12 +81,14 @@ router.put(
mention_everyone: false,
});
await message.save();
const publicMsg = message.toJSON();
const publicSystem = systemPinMessage.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_UPDATE",
channel_id,
data: message,
data: publicMsg,
} satisfies MessageUpdateEvent),
emitEvent({
event: "CHANNEL_PINS_UPDATE",
@@ -101,7 +103,7 @@ router.put(
emitEvent({
event: "MESSAGE_CREATE",
channel_id: message.channel_id,
data: systemPinMessage,
data: publicSystem,
} satisfies MessageCreateEvent),
]);
@@ -134,12 +136,13 @@ router.delete(
message.pinned_at = null;
await message.save();
const publicMsg2 = message.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_UPDATE",
channel_id,
data: message,
data: publicMsg2,
} satisfies MessageUpdateEvent),
emitEvent({
event: "CHANNEL_PINS_UPDATE",
@@ -18,7 +18,7 @@
import { route } from "@spacebar/api";
import { Channel, Member, Message } from "@spacebar/util";
import { PostDataSchema } from "@spacebar/schemas";
import { PostDataSchema, PublicMessage } from "@spacebar/schemas";
import { Request, Response, Router } from "express";
import { messageUpload } from "./messages";
@@ -83,7 +83,7 @@ router.post(
}),
]);
await Message.fillReplies(messages);
const objRet: { threads: Record<string, { first_message: null | Message; owner: null | Member }> } = { threads: {} };
const objRet: { threads: Record<string, { first_message: null | PublicMessage; owner: null | Member }> } = { threads: {} };
for (const thread of threads) {
const owner = members.find(({ id }) => id === thread.owner_id)?.toJSON() || null;
const first_message = messages.find(({ channel_id }) => channel_id === thread.id)?.toJSON() || null;
+11 -2
View File
@@ -147,8 +147,17 @@ router.post(
}
}
const embeds = body.message.embeds || [];
const message = await handleMessage({
const bodyMsg = {
...body.message,
allowed_mentions: body.message.allowed_mentions
? {
...body.message.allowed_mentions,
parse: body.message.allowed_mentions.parse as ("users" | "roles" | "everyone")[],
}
: undefined,
} as Parameters<typeof handleMessage>[0];
const message = await handleMessage({
...bodyMsg,
id: thread.id,
type: 0,
pinned: false,
@@ -189,7 +198,7 @@ router.post(
emitEvent({
event: "MESSAGE_CREATE",
channel_id: channel_id,
data: message,
data: message.toJSON(),
} satisfies MessageCreateEvent),
message.guild_id ? Member.update({ id: req.user_id, guild_id: message.guild_id }, { last_message_id: message.id }) : null,
]);
+1 -3
View File
@@ -58,11 +58,9 @@ router.post(
const filteredMessages = messages.map((message) => {
const x = message.toJSON();
// https://docs.discord.food/resources/message#preload-messages - reactions are not included in the response
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
x.reactions = undefined;
return x;
}) as PreloadMessagesResponseSchema;
}) as unknown as PreloadMessagesResponseSchema;
return res.status(200).send(filteredMessages);
},
+5 -3
View File
@@ -69,6 +69,7 @@ import {
UnfurledMediaItem,
BaseMessageComponents,
v1CompTypes,
PartialUser
} from "@spacebar/schemas";
const allow_empty = false;
// TODO: check webhook, application, system author, stickers
@@ -830,12 +831,13 @@ export async function postHandleMessage(message: Message) {
if (data.embeds != undefined) {
data.embeds = data.embeds?.filter((embed) => embed.type === "rich");
}
// author value is already included in message.toJSON()
const event = {
event: "MESSAGE_UPDATE",
channel_id: message.channel_id,
data: {
...data,
author,
...message.toJSON(),
embeds: data.embeds == undefined ? message.embeds || [] : data.embeds,
},
} satisfies MessageUpdateEvent;
const embeds = data.embeds == undefined ? [] : data.embeds;
@@ -900,7 +902,7 @@ export async function postHandleMessage(message: Message) {
emitEvent({
event: "MESSAGE_UPDATE",
channel_id: message.channel_id,
data,
data: message.toJSON(),
} satisfies MessageUpdateEvent),
Message.update({ id: message.id, channel_id: message.channel_id }, { embeds: embeds }),
...cachePromises,
+11 -2
View File
@@ -96,8 +96,17 @@ export const executeWebhook = async (req: Request, res: Response) => {
}
const embeds = body.embeds || [];
const message = await handleMessage({
const bodyMsg = {
...body,
allowed_mentions: body.allowed_mentions
? {
...body.allowed_mentions,
parse: body.allowed_mentions.parse as ("users" | "roles" | "everyone")[],
}
: undefined,
} as Parameters<typeof handleMessage>[0];
const message = await handleMessage({
...bodyMsg,
username: body.username || webhook.name,
avatar_url: body.avatar_url || webhook.avatar,
type: 0,
@@ -123,7 +132,7 @@ export const executeWebhook = async (req: Request, res: Response) => {
emitEvent({
event: "MESSAGE_CREATE",
channel_id: sendChannel.id,
data: message,
data: message.toJSON(),
} satisfies MessageCreateEvent),
]);
+3 -2
View File
@@ -442,12 +442,13 @@ export class Member extends BaseClassWithoutId {
channel.last_message_id = message.id;
await message.save();
const publicMsg = message.toJSON();
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_CREATE",
channel_id: message.channel_id,
data: message,
data: publicMsg,
} satisfies MessageCreateEvent),
channel.save(),
]);
+12 -2
View File
@@ -36,6 +36,7 @@ import {
MessageComponentType, MessageSnapshot, MessageType, PartialMessage, Poll, PublicMessage, Reaction,
UnfurledMediaItem,
} from "@spacebar/schemas";
import { PartialUser } from "@spacebar/schemas";
import { MessageFlags } from "@spacebar/util";
import { JsonRemoveEmpty } from "../util/Decorators";
@@ -251,15 +252,23 @@ export class Message extends BaseClass {
...this,
channel_id: this.channel_id ?? this.channel.id,
channel: undefined,
timestamp: this.timestamp.toISOString(),
edited_timestamp: this.edited_timestamp ? this.edited_timestamp.toISOString() : null,
author_id: undefined,
member_id: undefined,
webhook_id: this.webhook_id ?? undefined,
application_id: undefined,
mentions: this.mentions?.map((user) => {
if (user && !user.toPublicUser) console.trace("toPublic user missing!!!");
return user?.toPublicUser?.() ?? user ?? undefined;
return (user?.toPublicUser?.() ?? user ?? undefined) as unknown as PartialUser;
}),
mention_roles: this.mention_roles?.map((role) => role.id) ?? [],
mention_channels: this.mention_channels?.map((ch) => ch.toJSON()) ?? [],
attachments: this.attachments ?? [],
nonce: this.nonce ?? undefined,
tts: this.tts ?? false,
guild: this.guild ?? undefined,
@@ -269,6 +278,7 @@ export class Message extends BaseClass {
reactions: this.reactions ?? undefined,
sticker_items: this.sticker_items ?? undefined,
message_reference: this.message_reference ?? undefined,
mention_everyone: this.mention_everyone ?? false,
author: {
...(this.author?.toPublicUser() ?? undefined),
// Webhooks
@@ -277,7 +287,7 @@ export class Message extends BaseClass {
},
activity: this.activity ?? undefined,
application: this.application ?? undefined,
components: this.components ?? undefined,
components: this.components ?? [],
poll: this.poll ?? undefined,
content: this.content ?? "",
pinned: this.pinned,