summary refs log tree commit diff
path: root/src/api/util/handlers/Message.ts
blob: 6172a3d0c89d11b92a84b03a5ba413bacd75ad7c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Spacebar and Spacebar Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import {
	Channel,
	Embed,
	emitEvent,
	Guild,
	Message,
	MessageCreateEvent,
	MessageUpdateEvent,
	getPermission,
	getRights,
	//CHANNEL_MENTION,
	USER_MENTION,
	ROLE_MENTION,
	Role,
	EVERYONE_MENTION,
	HERE_MENTION,
	MessageType,
	User,
	Application,
	Webhook,
	Attachment,
	Config,
	Sticker,
	MessageCreateSchema,
	EmbedCache,
} from "@spacebar/util";
import { HTTPError } from "lambert-server";
import { In } from "typeorm";
import { EmbedHandlers } from "@spacebar/api";
import * as Sentry from "@sentry/node";
const allow_empty = false;
// TODO: check webhook, application, system author, stickers
// TODO: embed gifs/videos/images

const LINK_REGEX =
	/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g;

export async function handleMessage(opts: MessageOptions): Promise<Message> {
	const channel = await Channel.findOneOrFail({
		where: { id: opts.channel_id },
		relations: ["recipients"],
	});
	if (!channel || !opts.channel_id)
		throw new HTTPError("Channel not found", 404);

	const stickers = opts.sticker_ids
		? await Sticker.find({ where: { id: In(opts.sticker_ids) } })
		: undefined;
	const message = Message.create({
		...opts,
		sticker_items: stickers,
		guild_id: channel.guild_id,
		channel_id: opts.channel_id,
		attachments: opts.attachments || [],
		embeds: opts.embeds || [],
		reactions: /*opts.reactions ||*/ [],
		type: opts.type ?? 0,
	});

	if (
		message.content &&
		message.content.length > Config.get().limits.message.maxCharacters
	) {
		throw new HTTPError("Content length over max character limit");
	}

	if (opts.author_id) {
		message.author = await User.getPublicUser(opts.author_id);
		const rights = await getRights(opts.author_id);
		rights.hasThrow("SEND_MESSAGES");
	}
	if (opts.application_id) {
		message.application = await Application.findOneOrFail({
			where: { id: opts.application_id },
		});
	}
	if (opts.webhook_id) {
		message.webhook = await Webhook.findOneOrFail({
			where: { id: opts.webhook_id },
		});
	}

	const permission = await getPermission(
		opts.author_id,
		channel.guild_id,
		opts.channel_id,
	);
	permission.hasThrow("SEND_MESSAGES");
	if (permission.cache.member) {
		message.member = permission.cache.member;
	}

	if (opts.tts) permission.hasThrow("SEND_TTS_MESSAGES");
	if (opts.message_reference) {
		permission.hasThrow("READ_MESSAGE_HISTORY");
		// code below has to be redone when we add custom message routing
		if (message.guild_id !== null) {
			const guild = await Guild.findOneOrFail({
				where: { id: channel.guild_id },
			});
			if (!guild.features.includes("CROSS_CHANNEL_REPLIES")) {
				if (opts.message_reference.guild_id !== channel.guild_id)
					throw new HTTPError(
						"You can only reference messages from this guild",
					);
				if (opts.message_reference.channel_id !== opts.channel_id)
					throw new HTTPError(
						"You can only reference messages from this channel",
					);
			}
		}
		/** Q: should be checked if the referenced message exists? ANSWER: NO
		 otherwise backfilling won't work **/
		message.type = MessageType.REPLY;
	}

	// TODO: stickers/activity
	if (
		!allow_empty &&
		!opts.content &&
		!opts.embeds?.length &&
		!opts.attachments?.length &&
		!opts.sticker_ids?.length
	) {
		throw new HTTPError("Empty messages are not allowed", 50006);
	}

	let content = opts.content;

	// root@Rory - 20/02/2023 - This breaks channel mentions in test client. We're not sure this was used in older clients.
	//const mention_channel_ids = [] as string[];
	const mention_role_ids = [] as string[];
	const mention_user_ids = [] as string[];
	let mention_everyone = false;

	if (content) {
		// TODO: explicit-only mentions
		message.content = content.trim();
		content = content.replace(/ *`[^)]*` */g, ""); // remove codeblocks
		// root@Rory - 20/02/2023 - This breaks channel mentions in test client. We're not sure this was used in older clients.
		/*for (const [, mention] of content.matchAll(CHANNEL_MENTION)) {
			if (!mention_channel_ids.includes(mention))
				mention_channel_ids.push(mention);
		}*/

		for (const [, mention] of content.matchAll(USER_MENTION)) {
			if (!mention_user_ids.includes(mention))
				mention_user_ids.push(mention);
		}

		await Promise.all(
			Array.from(content.matchAll(ROLE_MENTION)).map(
				async ([, mention]) => {
					const role = await Role.findOneOrFail({
						where: { id: mention, guild_id: channel.guild_id },
					});
					if (role.mentionable || permission.has("MANAGE_ROLES")) {
						mention_role_ids.push(mention);
					}
				},
			),
		);

		if (permission.has("MENTION_EVERYONE")) {
			mention_everyone =
				!!content.match(EVERYONE_MENTION) ||
				!!content.match(HERE_MENTION);
		}
	}

	// root@Rory - 20/02/2023 - This breaks channel mentions in test client. We're not sure this was used in older clients.
	/*message.mention_channels = mention_channel_ids.map((x) =>
		Channel.create({ id: x }),
	);*/
	message.mention_roles = mention_role_ids.map((x) => Role.create({ id: x }));
	message.mentions = mention_user_ids.map((x) => User.create({ id: x }));
	message.mention_everyone = mention_everyone;

	// TODO: check and put it all in the body

	return message;
}

// TODO: cache link result in db
export async function postHandleMessage(message: Message) {
	const content = message.content?.replace(/ *`[^)]*` */g, ""); // remove markdown
	let links = content?.match(LINK_REGEX);
	if (!links) return;

	const data = { ...message };
	data.embeds = data.embeds.filter((x) => x.type !== "link");

	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) {
		const url = new URL(link);

		const cached = await EmbedCache.findOne({ where: { url: link } });
		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 {
			let res = await handler(url);
			if (!res) continue;
			// tried to use shorthand but types didn't like me L
			if (!Array.isArray(res)) res = [res];

			for (const embed of res) {
				const cache = EmbedCache.create({
					url: link,
					embed: embed,
				});
				cachePromises.push(cache.save());
				data.embeds.push(embed);
			}
		} catch (e) {
			console.error(`[Embeds] Error while generating embed`, e);
			Sentry.captureException(e, (scope) => {
				scope.clear();
				scope.setContext("request", { url });
				return scope;
			});
			continue;
		}
	}

	if (!data.embeds) return;

	await Promise.all([
		emitEvent({
			event: "MESSAGE_UPDATE",
			channel_id: message.channel_id,
			data,
		} as MessageUpdateEvent),
		Message.update(
			{ id: message.id, channel_id: message.channel_id },
			{ embeds: data.embeds },
		),
		...cachePromises,
	]);
}

export async function sendMessage(opts: MessageOptions) {
	const message = await handleMessage({ ...opts, timestamp: new Date() });

	await Promise.all([
		Message.insert(message),
		emitEvent({
			event: "MESSAGE_CREATE",
			channel_id: opts.channel_id,
			data: message.toJSON(),
		} as MessageCreateEvent),
	]);

	// no await as it should catch error non-blockingly
	postHandleMessage(message).catch((e) =>
		console.error("[Message] post-message handler failed", e),
	);

	return message;
}

interface MessageOptions extends MessageCreateSchema {
	id?: string;
	type?: MessageType;
	pinned?: boolean;
	author_id?: string;
	webhook_id?: string;
	application_id?: string;
	embeds?: Embed[];
	channel_id?: string;
	attachments?: Attachment[];
	edited_timestamp?: Date;
	timestamp?: Date;
}