Test embeding image/png etc responses
This commit is contained in:
parent
9a3286b605
commit
2ef970a2f0
@ -54,12 +54,12 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
|||||||
channel_id: opts.channel_id,
|
channel_id: opts.channel_id,
|
||||||
attachments: opts.attachments || [],
|
attachments: opts.attachments || [],
|
||||||
embeds: opts.embeds || [],
|
embeds: opts.embeds || [],
|
||||||
reactions: /*opts.reactions ||*/ [],
|
reactions: /*opts.reactions ||*/[],
|
||||||
type: opts.type ?? 0
|
type: opts.type ?? 0
|
||||||
});
|
});
|
||||||
|
|
||||||
if (message.content && message.content.length > Config.get().limits.message.maxCharacters) {
|
if (message.content && message.content.length > Config.get().limits.message.maxCharacters) {
|
||||||
throw new HTTPError("Content length over max character limit")
|
throw new HTTPError("Content length over max character limit");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.author_id) {
|
if (opts.author_id) {
|
||||||
@ -152,6 +152,8 @@ export async function postHandleMessage(message: Message) {
|
|||||||
|
|
||||||
links = links.slice(0, 20); // embed max 20 links — TODO: make this configurable with instance policies
|
links = links.slice(0, 20); // embed max 20 links — TODO: make this configurable with instance policies
|
||||||
|
|
||||||
|
const { endpointPublic, resizeWidthMax, resizeHeightMax } = Config.get().cdn;
|
||||||
|
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
try {
|
try {
|
||||||
const request = await fetch(link, {
|
const request = await fetch(link, {
|
||||||
@ -159,6 +161,26 @@ export async function postHandleMessage(message: Message) {
|
|||||||
size: Config.get().limits.message.maxEmbedDownloadSize,
|
size: Config.get().limits.message.maxEmbedDownloadSize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let embed: Embed;
|
||||||
|
|
||||||
|
const type = request.headers.get("content-type");
|
||||||
|
if (type?.indexOf("image") == 0) {
|
||||||
|
embed = {
|
||||||
|
provider: {
|
||||||
|
url: link,
|
||||||
|
name: new URL(link).hostname,
|
||||||
|
},
|
||||||
|
image: {
|
||||||
|
// can't be bothered rn
|
||||||
|
proxy_url: `${endpointPublic}/external/resize/${encodeURIComponent(link)}?width=500&height=400`,
|
||||||
|
url: link,
|
||||||
|
width: 500,
|
||||||
|
height: 400
|
||||||
|
}
|
||||||
|
};
|
||||||
|
data.embeds.push(embed);
|
||||||
|
}
|
||||||
|
else {
|
||||||
const text = await request.text();
|
const text = await request.text();
|
||||||
const $ = cheerio.load(text);
|
const $ = cheerio.load(text);
|
||||||
|
|
||||||
@ -173,14 +195,13 @@ export async function postHandleMessage(message: Message) {
|
|||||||
|
|
||||||
const url = $('meta[property="og:url"]').attr("content");
|
const url = $('meta[property="og:url"]').attr("content");
|
||||||
// TODO: color
|
// TODO: color
|
||||||
const embed: Embed = {
|
embed = {
|
||||||
provider: {
|
provider: {
|
||||||
url: link,
|
url: link,
|
||||||
name: provider_name
|
name: provider_name
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const { endpointPublic, resizeWidthMax, resizeHeightMax } = Config.get().cdn;
|
|
||||||
const resizeWidth = Math.min(resizeWidthMax ?? 1, width ?? 100);
|
const resizeWidth = Math.min(resizeWidthMax ?? 1, width ?? 100);
|
||||||
const resizeHeight = Math.min(resizeHeightMax ?? 1, height ?? 100);
|
const resizeHeight = Math.min(resizeHeightMax ?? 1, height ?? 100);
|
||||||
if (author_name) embed.author = { name: author_name };
|
if (author_name) embed.author = { name: author_name };
|
||||||
@ -197,7 +218,8 @@ export async function postHandleMessage(message: Message) {
|
|||||||
if (title || description) {
|
if (title || description) {
|
||||||
data.embeds.push(embed);
|
data.embeds.push(embed);
|
||||||
}
|
}
|
||||||
} catch (error) {}
|
}
|
||||||
|
} catch (error) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
@ -218,7 +240,7 @@ export async function sendMessage(opts: MessageOptions) {
|
|||||||
emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message.toJSON() } as MessageCreateEvent)
|
emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message.toJSON() } as MessageCreateEvent)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
postHandleMessage(message).catch((e) => {}); // no await as it should catch error non-blockingly
|
postHandleMessage(message).catch((e) => { }); // no await as it should catch error non-blockingly
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
"target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
|
"target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
|
||||||
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
|
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
|
||||||
"lib": [
|
"lib": [
|
||||||
"ES2021"
|
"ES2021",
|
||||||
|
"DOM"
|
||||||
] /* Specify library files to be included in the compilation. */,
|
] /* Specify library files to be included in the compilation. */,
|
||||||
"allowJs": true /* Allow javascript files to be compiled. */,
|
"allowJs": true /* Allow javascript files to be compiled. */,
|
||||||
"checkJs": true /* Report errors in .js files. */,
|
"checkJs": true /* Report errors in .js files. */,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user