automatically embed links in message
This commit is contained in:
parent
3370ee50c5
commit
1383911771
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -29,7 +29,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
||||
"dependencies": {
|
||||
"@fosscord/server-util": "^1.3.5",
|
||||
"@fosscord/server-util": "^1.3.9",
|
||||
"@types/jest": "^26.0.22",
|
||||
"@types/json-schema": "^7.0.7",
|
||||
"ajv": "^8.4.0",
|
||||
@ -38,6 +38,7 @@
|
||||
"atomically": "^1.7.0",
|
||||
"bcrypt": "^5.0.1",
|
||||
"body-parser": "^1.19.0",
|
||||
"cheerio": "^1.0.0-rc.9",
|
||||
"dot-prop": "^6.0.1",
|
||||
"dotenv": "^8.2.0",
|
||||
"env-paths": "^2.2.1",
|
||||
|
||||
@ -4,7 +4,7 @@ import { HTTPError } from "lambert-server";
|
||||
import { MessageCreateSchema } from "../../../../../schema/Message";
|
||||
import { emitEvent } from "../../../../../util/Event";
|
||||
import { check } from "../../../../../util/instanceOf";
|
||||
import { handleMessage } from "../../../../../util/Message";
|
||||
import { handleMessage, postHandleMessage } from "../../../../../util/Message";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@ -40,6 +40,8 @@ router.patch("/", check(MessageCreateSchema), async (req, res) => {
|
||||
data: { ...toObject(message), nonce: undefined }
|
||||
} as MessageUpdateEvent);
|
||||
|
||||
postHandleMessage(message);
|
||||
|
||||
return res.json(toObject(message));
|
||||
});
|
||||
|
||||
|
||||
@ -1,14 +1,28 @@
|
||||
import { ChannelModel, MessageCreateEvent } from "@fosscord/server-util";
|
||||
import { ChannelModel, Embed, Message, MessageCreateEvent, MessageUpdateEvent } from "@fosscord/server-util";
|
||||
import { Snowflake } from "@fosscord/server-util";
|
||||
import { MessageModel } from "@fosscord/server-util";
|
||||
import { PublicMemberProjection } from "@fosscord/server-util";
|
||||
import { toObject } from "@fosscord/server-util";
|
||||
import { getPermission } from "@fosscord/server-util";
|
||||
import { Message } from "@fosscord/server-util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import fetch from "node-fetch";
|
||||
import cheerio from "cheerio";
|
||||
import { emitEvent } from "./Event";
|
||||
// TODO: check webhook, application, system author
|
||||
|
||||
const LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
|
||||
|
||||
const DEFAULT_FETCH_OPTIONS: any = {
|
||||
redirect: "follow",
|
||||
follow: 1,
|
||||
headers: {
|
||||
"user-agent": "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)"
|
||||
},
|
||||
size: 1024 * 1024 * 1,
|
||||
compress: true,
|
||||
method: "GET"
|
||||
};
|
||||
|
||||
export async function handleMessage(opts: Partial<Message>) {
|
||||
const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
|
||||
if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404);
|
||||
@ -43,6 +57,60 @@ export async function handleMessage(opts: Partial<Message>) {
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: cache link result in db
|
||||
export async function postHandleMessage(message: Message) {
|
||||
var links = message.content?.match(LINK_REGEX);
|
||||
if (!links) return;
|
||||
|
||||
const data = { ...message };
|
||||
data.embeds = data.embeds.filter((x) => x.type !== "link");
|
||||
|
||||
links = links.slice(0, 5); // embed max 5 links
|
||||
|
||||
for (const link of links) {
|
||||
try {
|
||||
const request = await fetch(link, DEFAULT_FETCH_OPTIONS);
|
||||
|
||||
const text = await request.text();
|
||||
const $ = cheerio.load(text);
|
||||
|
||||
const title = $('meta[property="og:title"]').attr("content");
|
||||
const provider_name = $('meta[property="og:site_name"]').text();
|
||||
const author_name = $('meta[property="article:author"]').attr("content");
|
||||
const description = $('meta[property="og:description"]').attr("content") || $('meta[property="description"]').attr("content");
|
||||
const image = $('meta[property="og:image"]').attr("content");
|
||||
const url = $('meta[property="og:url"]').attr("content");
|
||||
// TODO: color
|
||||
const embed: Embed = {
|
||||
provider: {
|
||||
url: link,
|
||||
name: provider_name
|
||||
}
|
||||
};
|
||||
|
||||
if (author_name) embed.author = { name: author_name };
|
||||
if (image) embed.thumbnail = { proxy_url: image, url: image };
|
||||
if (title) embed.title = title;
|
||||
if (url) embed.url = url;
|
||||
if (description) embed.description = description;
|
||||
|
||||
if (title || description) {
|
||||
data.embeds.push(embed);
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
emitEvent({
|
||||
event: "MESSAGE_UPDATE",
|
||||
guild_id: message.guild_id,
|
||||
channel_id: message.channel_id,
|
||||
data
|
||||
} as MessageUpdateEvent),
|
||||
MessageModel.updateOne({ id: message.id, channel_id: message.channel_id }, data).exec()
|
||||
]);
|
||||
}
|
||||
|
||||
export async function sendMessage(opts: Partial<Message>) {
|
||||
const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() });
|
||||
|
||||
@ -50,5 +118,7 @@ export async function sendMessage(opts: Partial<Message>) {
|
||||
|
||||
await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent);
|
||||
|
||||
postHandleMessage(data); // no await as it shouldnt block the message send function
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user