Embed cache
This commit is contained in:
parent
69ff9c944c
commit
c885fd5503
@ -22,6 +22,7 @@ import {
|
|||||||
Config,
|
Config,
|
||||||
Sticker,
|
Sticker,
|
||||||
MessageCreateSchema,
|
MessageCreateSchema,
|
||||||
|
EmbedCache,
|
||||||
} from "@fosscord/util";
|
} from "@fosscord/util";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { In } from "typeorm";
|
import { In } from "typeorm";
|
||||||
@ -187,28 +188,43 @@ export async function postHandleMessage(message: Message) {
|
|||||||
|
|
||||||
links = links.slice(0, 20) as RegExpMatchArray; // embed max 20 links — TODO: make this configurable with instance policies
|
links = links.slice(0, 20) as RegExpMatchArray; // embed max 20 links — TODO: make this configurable with instance policies
|
||||||
|
|
||||||
|
const cachePromises = [];
|
||||||
|
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
let embed: Embed;
|
|
||||||
const url = new URL(link);
|
const url = new URL(link);
|
||||||
|
|
||||||
|
const cached = await EmbedCache.findOne({ where: { url: `${url.host}${url.pathname}` } });
|
||||||
|
if (cached) {
|
||||||
|
data.embeds.push(cached.embed);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// bit gross, but whatever!
|
// bit gross, but whatever!
|
||||||
const endpointPublic = Config.get().cdn.endpointPublic || "http://127.0.0.1"; // lol
|
const endpointPublic = Config.get().cdn.endpointPublic || "http://127.0.0.1"; // lol
|
||||||
const handler = url.hostname == new URL(endpointPublic).hostname ? EmbedHandlers["self"] : EmbedHandlers[url.hostname] || EmbedHandlers["default"];
|
const handler = url.hostname == new URL(endpointPublic).hostname ? EmbedHandlers["self"] : EmbedHandlers[url.hostname] || EmbedHandlers["default"];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await handler(url);
|
let res = await handler(url);
|
||||||
if (!res) continue;
|
if (!res) continue;
|
||||||
// tried to use shorthand but types didn't like me L
|
// tried to use shorthand but types didn't like me L
|
||||||
if (Array.isArray(res))
|
if (!Array.isArray(res)) res = [res];
|
||||||
data.embeds.push(...res)
|
|
||||||
else
|
for (var embed of res) {
|
||||||
data.embeds.push(res);
|
var cache = EmbedCache.create({
|
||||||
|
url: `${url.host}${url.pathname}`,
|
||||||
|
embed: embed,
|
||||||
|
});
|
||||||
|
cachePromises.push(cache.save());
|
||||||
|
data.embeds.push(embed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!data.embeds) return;
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
emitEvent({
|
emitEvent({
|
||||||
event: "MESSAGE_UPDATE",
|
event: "MESSAGE_UPDATE",
|
||||||
@ -219,6 +235,7 @@ export async function postHandleMessage(message: Message) {
|
|||||||
{ id: message.id, channel_id: message.channel_id },
|
{ id: message.id, channel_id: message.channel_id },
|
||||||
{ embeds: data.embeds },
|
{ embeds: data.embeds },
|
||||||
),
|
),
|
||||||
|
...cachePromises,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
src/util/entities/EmbedCache.ts
Normal file
12
src/util/entities/EmbedCache.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { BaseClass } from "./BaseClass";
|
||||||
|
import { Entity, Column } from "typeorm";
|
||||||
|
import { Embed } from "./Message";
|
||||||
|
|
||||||
|
@Entity("embed_cache")
|
||||||
|
export class EmbedCache extends BaseClass {
|
||||||
|
@Column()
|
||||||
|
url: string;
|
||||||
|
|
||||||
|
@Column({ type: "simple-json" })
|
||||||
|
embed: Embed;
|
||||||
|
}
|
@ -7,6 +7,7 @@ export * from "./Categories";
|
|||||||
export * from "./Channel";
|
export * from "./Channel";
|
||||||
export * from "./Config";
|
export * from "./Config";
|
||||||
export * from "./ConnectedAccount";
|
export * from "./ConnectedAccount";
|
||||||
|
export * from "./EmbedCache";
|
||||||
export * from "./Emoji";
|
export * from "./Emoji";
|
||||||
export * from "./Guild";
|
export * from "./Guild";
|
||||||
export * from "./Invite";
|
export * from "./Invite";
|
||||||
@ -29,4 +30,4 @@ export * from "./VoiceState";
|
|||||||
export * from "./Webhook";
|
export * from "./Webhook";
|
||||||
export * from "./ClientRelease";
|
export * from "./ClientRelease";
|
||||||
export * from "./BackupCodes";
|
export * from "./BackupCodes";
|
||||||
export * from "./Note";
|
export * from "./Note";
|
Loading…
x
Reference in New Issue
Block a user