summary refs log tree commit diff
path: root/src/util/Message.ts
blob: 2779699753785d660fc04ff201bbc721d2d39d5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 { 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);
	// TODO: are tts messages allowed in dm channels? should permission be checked?

	const permissions = await getPermission(opts.author_id, channel.guild_id, opts.channel_id, { channel });
	permissions.hasThrow("SEND_MESSAGES");
	if (opts.tts) permissions.hasThrow("SEND_TTS_MESSAGES");
	if (opts.message_reference) {
		permissions.hasThrow("READ_MESSAGE_HISTORY");
		if (opts.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild");
	}

	if (opts.message_reference) {
		if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel");
		// TODO: should be checked if the referenced message exists?
	}

	// TODO: check and put it all in the body
	return {
		...opts,
		guild_id: channel.guild_id,
		channel_id: opts.channel_id,
		// TODO: generate mentions and check permissions
		mention_channels_ids: [],
		mention_role_ids: [],
		mention_user_ids: [],
		attachments: [], // TODO: message attachments
		embeds: opts.embeds || [],
		reactions: opts.reactions || [],
		type: opts.type ?? 0
	};
}

// 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() });

	const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save());

	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;
}