summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/api/util/handlers/Message.ts29
-rw-r--r--src/util/entities/EmbedCache.ts12
-rw-r--r--src/util/entities/index.ts3
3 files changed, 37 insertions, 7 deletions
diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts
index d4f0e995..e4b74f48 100644
--- a/src/api/util/handlers/Message.ts
+++ b/src/api/util/handlers/Message.ts
@@ -22,6 +22,7 @@ import {
 	Config,
 	Sticker,
 	MessageCreateSchema,
+	EmbedCache,
 } from "@fosscord/util";
 import { HTTPError } from "lambert-server";
 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
 
+	const cachePromises = [];
+
 	for (const link of links) {
-		let embed: Embed;
 		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!
 		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"];
 
 		try {
-			const res = await handler(url);
+			let res = await handler(url);
 			if (!res) continue;
 			// tried to use shorthand but types didn't like me L
-			if (Array.isArray(res))
-				data.embeds.push(...res)
-			else
-				data.embeds.push(res);
+			if (!Array.isArray(res)) res = [res];
+
+			for (var embed of res) {
+				var cache = EmbedCache.create({
+					url: `${url.host}${url.pathname}`,
+					embed: embed,
+				});
+				cachePromises.push(cache.save());
+				data.embeds.push(embed);
+			}
 		}
 		catch (e) {
 			continue;
 		}
 	}
 
+	if (!data.embeds) return;
+
 	await Promise.all([
 		emitEvent({
 			event: "MESSAGE_UPDATE",
@@ -219,6 +235,7 @@ export async function postHandleMessage(message: Message) {
 			{ id: message.id, channel_id: message.channel_id },
 			{ embeds: data.embeds },
 		),
+		...cachePromises,
 	]);
 }
 
diff --git a/src/util/entities/EmbedCache.ts b/src/util/entities/EmbedCache.ts
new file mode 100644
index 00000000..14881ccf
--- /dev/null
+++ b/src/util/entities/EmbedCache.ts
@@ -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;
+}
\ No newline at end of file
diff --git a/src/util/entities/index.ts b/src/util/entities/index.ts
index c439a4b7..04f3bace 100644
--- a/src/util/entities/index.ts
+++ b/src/util/entities/index.ts
@@ -7,6 +7,7 @@ export * from "./Categories";
 export * from "./Channel";
 export * from "./Config";
 export * from "./ConnectedAccount";
+export * from "./EmbedCache";
 export * from "./Emoji";
 export * from "./Guild";
 export * from "./Invite";
@@ -29,4 +30,4 @@ export * from "./VoiceState";
 export * from "./Webhook";
 export * from "./ClientRelease";
 export * from "./BackupCodes";
-export * from "./Note";
+export * from "./Note";
\ No newline at end of file