diff --git a/src/api/routes/channels/#channel_id/messages/#message_id/index.ts b/src/api/routes/channels/#channel_id/messages/#message_id/index.ts
index 32f51bea..52271c83 100644
--- a/src/api/routes/channels/#channel_id/messages/#message_id/index.ts
+++ b/src/api/routes/channels/#channel_id/messages/#message_id/index.ts
@@ -190,7 +190,7 @@ router.put(
if (req.file) {
try {
const file = await uploadFile(`/attachments/${req.params.channel_id}`, req.file);
- attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
+ attachments.push(Attachment.create(file));
} catch (error) {
return res.status(400).json(error);
}
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts
index 20b3ab22..a484c35c 100644
--- a/src/api/routes/channels/#channel_id/messages/index.ts
+++ b/src/api/routes/channels/#channel_id/messages/index.ts
@@ -178,8 +178,6 @@ router.get(
}
await Message.fillReplies(messages);
- const endpoint = Config.get().cdn.endpointPublic;
-
const ret = messages.map((msg) => {
const x = msg.toJSON();
@@ -197,40 +195,32 @@ router.get(
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}`;
-
- const url = new URL(uri);
- if (endpoint) {
- const newBase = new URL(endpoint);
- url.protocol = newBase.protocol;
- url.hostname = newBase.hostname;
- url.port = newBase.port;
- }
+ x.attachments =
+ msg.attachments?.map((y: Attachment) => {
+ const att = y.toJSON();
- y.proxy_url = url.toString();
+ att.proxy_url = getUrlSignature(
+ new NewUrlSignatureData({
+ url: att.proxy_url,
+ userAgent: req.headers["user-agent"],
+ ip: req.ip,
+ }),
+ )
+ .applyToUrl(att.proxy_url)
+ .toString();
- y.proxy_url = getUrlSignature(
- new NewUrlSignatureData({
- url: y.proxy_url,
- userAgent: req.headers["user-agent"],
- ip: req.ip,
- }),
- )
- .applyToUrl(y.proxy_url)
- .toString();
+ att.url = getUrlSignature(
+ new NewUrlSignatureData({
+ url: att.url,
+ userAgent: req.headers["user-agent"],
+ ip: req.ip,
+ }),
+ )
+ .applyToUrl(att.url)
+ .toString();
- y.url = getUrlSignature(
- new NewUrlSignatureData({
- url: y.url,
- userAgent: req.headers["user-agent"],
- ip: req.ip,
- }),
- )
- .applyToUrl(y.url)
- .toString();
- });
+ return att;
+ }) ?? [];
/**
Some clients ( discord.js ) only check if a property exists within the response,
@@ -312,6 +302,7 @@ router.post(
async (req: Request, res: Response) => {
const { channel_id } = req.params as { [key: string]: string };
const body = req.body as MessageCreateSchema;
+ const messageId = Snowflake.generate();
const attachments: (Attachment | MessageCreateAttachment | MessageCreateCloudAttachment)[] = body.attachments ?? [];
const channel = await Channel.findOneOrFail({
@@ -418,8 +409,8 @@ router.post(
const files = (req.files as Express.Multer.File[]) ?? [];
for (const currFile of files) {
try {
- const file = await uploadFile(`/attachments/${channel.id}`, currFile);
- attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
+ const file = await uploadFile(`/attachments/${channel.id}/${messageId}`, currFile);
+ attachments.push(Attachment.create(file));
} catch (error) {
return res.status(400).json({ message: error?.toString() });
}
@@ -429,6 +420,7 @@ router.post(
if (body.embed) embeds.push(body.embed);
const message = await handleMessage({
...body,
+ id: messageId,
type: 0,
pinned: false,
author_id: req.user_id,
diff --git a/src/api/routes/channels/#channel_id/threads.ts b/src/api/routes/channels/#channel_id/threads.ts
index 2af92f53..ce7a9435 100644
--- a/src/api/routes/channels/#channel_id/threads.ts
+++ b/src/api/routes/channels/#channel_id/threads.ts
@@ -31,6 +31,7 @@ import {
ThreadMember,
Message,
ChannelFlags,
+ Snowflake,
} from "@spacebar/util";
import { ChannelType, MessageType, ThreadCreationSchema, MessageCreateAttachment, MessageCreateCloudAttachment } from "@spacebar/schemas";
@@ -140,8 +141,8 @@ router.post(
const attachments: (Attachment | MessageCreateAttachment | MessageCreateCloudAttachment)[] = body.message.attachments ?? [];
for (const currFile of files) {
try {
- const file = await uploadFile(`/attachments/${channel.id}`, currFile);
- attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
+ const file = await uploadFile(`/attachments/${channel.id}/${thread.id}`, currFile);
+ attachments.push(Attachment.create(file));
} catch (error) {
return res.status(400).json({ message: error?.toString() });
}
diff --git a/src/api/routes/channels/#channel_id/webhooks.ts b/src/api/routes/channels/#channel_id/webhooks.ts
index 0f686173..ee801db7 100644
--- a/src/api/routes/channels/#channel_id/webhooks.ts
+++ b/src/api/routes/channels/#channel_id/webhooks.ts
@@ -46,7 +46,7 @@ router.get(
return res.json(
webhooks.map((webhook) => ({
...webhook,
- url: Config.get().api.endpointPublic + "/webhooks/" + webhook.id + "/" + webhook.token,
+ url: Config.get().api.endpointPublic + "/api/webhooks/" + webhook.id + "/" + webhook.token,
})),
);
},
diff --git a/src/api/routes/interactions/#interaction_id/#interaction_token/callback.ts b/src/api/routes/interactions/#interaction_id/#interaction_token/callback.ts
index 7a781267..d8ec9c26 100644
--- a/src/api/routes/interactions/#interaction_id/#interaction_token/callback.ts
+++ b/src/api/routes/interactions/#interaction_id/#interaction_token/callback.ts
@@ -47,9 +47,9 @@ router.post(
user_id: interaction?.userId,
data: {
id: interactionId,
- nonce: interaction.nonce ?? "", // TODO: did i do this right?
+ nonce: interaction.nonce ?? "", // TODO: did i do this right?
},
- } satisfies InteractionSuccessEvent);
+ } satisfies InteractionSuccessEvent);
switch (body.type) {
case InteractionCallbackType.PONG:
@@ -70,7 +70,7 @@ router.post(
for (const currFile of files) {
try {
const file = await uploadFile(`/attachments/${interaction.channelId}`, currFile);
- attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
+ attachments.push(Attachment.create(file));
} catch (error) {
return res.status(400).json({ message: error?.toString() });
}
diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts
index 1fb9a080..afb585c9 100644
--- a/src/api/util/handlers/Message.ts
+++ b/src/api/util/handlers/Message.ts
@@ -171,12 +171,11 @@ async function processMedia(media: UnfurledMediaItem, messageId: string, batchId
const realAtt = Attachment.create({
filename: attEnt.userFilename,
- url: media.url,
- proxy_url: media.proxy_url,
size: attEnt.size,
height: attEnt.height,
width: attEnt.width,
content_type: attEnt.contentType || attEnt.userOriginalContentType,
+ channel_id: channel.id,
});
await realAtt.save();
@@ -396,12 +395,11 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
const realAtt = Attachment.create({
filename: attEnt.userFilename,
- url: `${conf.cdn.endpointPublic}/${cloneRespBody.new_path}`,
- proxy_url: `${conf.cdn.endpointPublic}/${cloneRespBody.new_path}`,
size: attEnt.size,
height: attEnt.height,
width: attEnt.width,
content_type: attEnt.contentType || attEnt.userOriginalContentType,
+ channel_id: channel.id,
});
await realAtt.save();
return { attachment: realAtt, index: att.index };
@@ -744,22 +742,22 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
const footer = embed.footer;
const footerAttachment = fetchAttachment(footer?.icon_url);
if (footerAttachment !== undefined) {
- footer!.icon_url = footerAttachment.url;
- footer!.proxy_icon_url = footerAttachment.proxy_url;
+ footer!.icon_url = footerAttachment.toJSON().url;
+ footer!.proxy_icon_url = footerAttachment.toJSON().proxy_url;
}
const image = embed.image;
const imageAttachment = fetchAttachment(image?.url);
if (imageAttachment !== undefined) {
- image!.url = imageAttachment.url;
- image!.proxy_url = imageAttachment.proxy_url;
+ image!.url = imageAttachment.toJSON().url;
+ image!.proxy_url = imageAttachment.toJSON().proxy_url;
}
const author = embed.author;
const authorAttachment = fetchAttachment(author?.icon_url);
if (authorAttachment !== undefined) {
- author!.icon_url = authorAttachment.url;
- author!.proxy_icon_url = authorAttachment.proxy_url;
+ author!.icon_url = authorAttachment.toJSON().url;
+ author!.proxy_icon_url = authorAttachment.toJSON().proxy_url;
}
}
message.attachments = message.attachments?.filter((_, index) => {
diff --git a/src/api/util/handlers/Webhook.ts b/src/api/util/handlers/Webhook.ts
index deb0331f..73e609d8 100644
--- a/src/api/util/handlers/Webhook.ts
+++ b/src/api/util/handlers/Webhook.ts
@@ -1,5 +1,5 @@
import { handleMessage, postHandleMessage } from "@spacebar/api";
-import { Attachment, Channel, Config, DiscordApiErrors, emitEvent, FieldErrors, Message, MessageCreateEvent, uploadFile, ValidateName, Webhook } from "@spacebar/util";
+import { Attachment, Channel, Config, DiscordApiErrors, emitEvent, FieldErrors, Message, MessageCreateEvent, Snowflake, uploadFile, ValidateName, Webhook } from "@spacebar/util";
import { Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { MoreThan } from "typeorm";
@@ -7,6 +7,7 @@ import { WebhookExecuteSchema } from "@spacebar/schemas";
export const executeWebhook = async (req: Request, res: Response) => {
const body = req.body as WebhookExecuteSchema;
+ const messageId = Snowflake.generate();
const { webhook_id, token } = req.params as { [key: string]: string };
@@ -87,8 +88,8 @@ export const executeWebhook = async (req: Request, res: Response) => {
const files = (req.files as Express.Multer.File[]) ?? [];
for (const currFile of files) {
try {
- const file = await uploadFile(`/attachments/${sendChannel.id}`, currFile);
- attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
+ const file = await uploadFile(`/attachments/${sendChannel.id}/${messageId}`, currFile);
+ attachments.push(Attachment.create(file));
} catch (error) {
if (wait) res.status(400).json({ message: error?.toString() });
return;
@@ -106,6 +107,7 @@ export const executeWebhook = async (req: Request, res: Response) => {
: undefined,
} as Parameters<typeof handleMessage>[0];
const message = await handleMessage({
+ id: messageId,
...bodyMsg,
username: body.username || webhook.name,
avatar_url: body.avatar_url || webhook.avatar,
|