summary refs log tree commit diff
path: root/src/api/routes/guilds/#guild_id/messages/search.ts
blob: 601167eeed79ced53d644ee0f5e010e40aa8704a (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
/*
	Fosscord: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Fosscord and Fosscord 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/>.
*/

/* eslint-disable @typescript-eslint/ban-ts-comment */

import { Request, Response, Router } from "express";
import { route } from "@fosscord/api";
import { getPermission, FieldErrors, Message, Channel } from "@fosscord/util";
import { HTTPError } from "lambert-server";
import { FindManyOptions, In, Like } from "typeorm";

const router: Router = Router();

router.get("/", route({}), async (req: Request, res: Response) => {
	const {
		channel_id,
		content,
		// include_nsfw, // TODO
		offset,
		sort_order,
		// sort_by, // TODO: Handle 'relevance'
		limit,
		author_id,
	} = req.query;

	const parsedLimit = Number(limit) || 50;
	if (parsedLimit < 1 || parsedLimit > 100)
		throw new HTTPError("limit must be between 1 and 100", 422);

	if (sort_order) {
		if (
			typeof sort_order != "string" ||
			["desc", "asc"].indexOf(sort_order) == -1
		)
			throw FieldErrors({
				sort_order: {
					message: "Value must be one of ('desc', 'asc').",
					code: "BASE_TYPE_CHOICES",
				},
			}); // todo this is wrong
	}

	const permissions = await getPermission(
		req.user_id,
		req.params.guild_id,
		channel_id as string | undefined,
	);
	permissions.hasThrow("VIEW_CHANNEL");
	if (!permissions.has("READ_MESSAGE_HISTORY"))
		return res.json({ messages: [], total_results: 0 });

	const query: FindManyOptions<Message> = {
		order: {
			timestamp: sort_order
				? (sort_order.toUpperCase() as "ASC" | "DESC")
				: "DESC",
		},
		take: parsedLimit || 0,
		where: {
			guild: {
				id: req.params.guild_id,
			},
		},
		relations: [
			"author",
			"webhook",
			"application",
			"mentions",
			"mention_roles",
			"mention_channels",
			"sticker_items",
			"attachments",
		],
		skip: offset ? Number(offset) : 0,
	};
	//@ts-ignore
	if (channel_id) query.where.channel = { id: channel_id };
	else {
		// get all channel IDs that this user can access
		const channels = await Channel.find({
			where: { guild_id: req.params.guild_id },
			select: ["id"],
		});
		const ids = [];

		for (const channel of channels) {
			const perm = await getPermission(
				req.user_id,
				req.params.guild_id,
				channel.id,
			);
			if (!perm.has("VIEW_CHANNEL") || !perm.has("READ_MESSAGE_HISTORY"))
				continue;
			ids.push(channel.id);
		}

		//@ts-ignore
		query.where.channel = { id: In(ids) };
	}
	//@ts-ignore
	if (author_id) query.where.author = { id: author_id };
	//@ts-ignore
	if (content) query.where.content = Like(`%${content}%`);

	const messages: Message[] = await Message.find(query);

	const messagesDto = messages.map((x) => [
		{
			id: x.id,
			type: x.type,
			content: x.content,
			channel_id: x.channel_id,
			author: {
				id: x.author?.id,
				username: x.author?.username,
				avatar: x.author?.avatar,
				avatar_decoration: null,
				discriminator: x.author?.discriminator,
				public_flags: x.author?.public_flags,
			},
			attachments: x.attachments,
			embeds: x.embeds,
			mentions: x.mentions,
			mention_roles: x.mention_roles,
			pinned: x.pinned,
			mention_everyone: x.mention_everyone,
			tts: x.tts,
			timestamp: x.timestamp,
			edited_timestamp: x.edited_timestamp,
			flags: x.flags,
			components: x.components,
			hit: true,
		},
	]);

	return res.json({
		messages: messagesDto,
		total_results: messages.length,
	});
});

export default router;