summary refs log tree commit diff
path: root/src/api/routes/channels/#channel_id/messages/#message_id/index.ts
blob: d7e27062c42c804d56686f4143780be219e0ab78 (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
import {
	Attachment,
	Channel,
	Embed,
	DiscordApiErrors,
	emitEvent,
	FosscordApiErrors,
	getPermission,
	getRights,
 	Message,
	MessageCreateEvent,
	MessageDeleteEvent,
	MessageUpdateEvent,
	Snowflake,
	uploadFile, 
	MessageCreateSchema
} from "@fosscord/util";
import { Router, Response, Request } from "express";
import multer from "multer";
import { route } from "@fosscord/api";
import { handleMessage, postHandleMessage } from "@fosscord/api";
import { HTTPError } from "@fosscord/util";

const router = Router();
// TODO: message content/embed string length limit

const messageUpload = multer({
	limits: {
		fileSize: 1024 * 1024 * 100,
		fields: 10,
		files: 1
	},
	storage: multer.memoryStorage()
}); // max upload 50 mb

router.patch("/", route({ body: "MessageCreateSchema", permission: "SEND_MESSAGES", right: "SEND_MESSAGES" }), async (req: Request, res: Response) => {
	const { message_id, channel_id } = req.params;
	let body = req.body as MessageCreateSchema;

	const message = await Message.findOneOrFail({ where: { id: message_id, channel_id }, relations: ["attachments"] });

	const permissions = await getPermission(req.user_id, undefined, channel_id);
	
	const rights = await getRights(req.user_id);

	if ((req.user_id !== message.author_id)) {
		if (!rights.has("MANAGE_MESSAGES")) {
			permissions.hasThrow("MANAGE_MESSAGES");
			body = { flags: body.flags };
// guild admins can only suppress embeds of other messages, no such restriction imposed to instance-wide admins
		}
	} else rights.hasThrow("SELF_EDIT_MESSAGES");

	const new_message = await handleMessage({
		...message,
		// TODO: should message_reference be overridable?
		// @ts-ignore
		message_reference: message.message_reference,
		...body,
		author_id: message.author_id,
		channel_id,
		id: message_id,
		edited_timestamp: new Date()
	});

	await Promise.all([
		new_message!.save(),
		await emitEvent({
			event: "MESSAGE_UPDATE",
			channel_id,
			data: { ...new_message, nonce: undefined }
		} as MessageUpdateEvent)
	]);

	postHandleMessage(message);

	return res.json(message);
});


// Backfill message with specific timestamp
router.put(
	"/",
	messageUpload.single("file"),
	async (req, res, next) => {
		if (req.body.payload_json) {
			req.body = JSON.parse(req.body.payload_json);
		}

		next();
	},
	route({ body: "MessageCreateSchema", permission: "SEND_MESSAGES", right: "SEND_BACKDATED_EVENTS" }),
	async (req: Request, res: Response) => {
		const { channel_id, message_id } = req.params;
		let body = req.body as MessageCreateSchema;
		const attachments: Attachment[] = [];
		
		const rights = await getRights(req.user_id);
		rights.hasThrow("SEND_MESSAGES");

		// regex to check if message contains anything other than numerals ( also no decimals )
		if (!message_id.match(/^\+?\d+$/)) {
			throw new HTTPError("Message IDs must be positive integers", 400);
		}

		const snowflake = Snowflake.deconstruct(message_id)
		if (Date.now() < snowflake.timestamp) {
			// message is in the future
			throw FosscordApiErrors.CANNOT_BACKFILL_TO_THE_FUTURE;
		}

		const exists = await Message.findOne({ where: { id: message_id, channel_id: channel_id }});
		if (exists) {
			throw FosscordApiErrors.CANNOT_REPLACE_BY_BACKFILL;
		}

		if (req.file) {
			try {
				const file: any = await uploadFile(`/attachments/${req.params.channel_id}`, req.file);
				attachments.push({ ...file, proxy_url: file.url });
			} catch (error) {
				return res.status(400).json(error);
			}
		}
		const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients", "recipients.user"] });

		const embeds = body.embeds || [];
		if (body.embed) embeds.push(body.embed);
		let message = await handleMessage({
			...body,
			type: 0,
			pinned: false,
			author_id: req.user_id,
			id: message_id,
			embeds,
			channel_id,
			attachments,
			edited_timestamp: undefined,
			timestamp: new Date(snowflake.timestamp),
		});

		//Fix for the client bug
		delete message.member
		
		await Promise.all([
			message.save(),
			emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent),
			channel.save()
		]);

		postHandleMessage(message).catch((e) => { }); // no await as it shouldnt block the message send function and silently catch error

		return res.json(message);
	}
);

router.get("/", route({ permission: "VIEW_CHANNEL" }), async (req: Request, res: Response) => {
	const { message_id, channel_id } = req.params;

	const message = await Message.findOneOrFail({ where: { id: message_id, channel_id }, relations: ["attachments"] });

	const permissions = await getPermission(req.user_id, undefined, channel_id);
	
	if (message.author_id !== req.user_id) permissions.hasThrow("READ_MESSAGE_HISTORY");

	return res.json(message);
});

router.delete("/", route({}), async (req: Request, res: Response) => {
	const { message_id, channel_id } = req.params;

	const channel = await Channel.findOneOrFail({ where: { id: channel_id } });
	const message = await Message.findOneOrFail({ where: { id: message_id } });
	
	const rights = await getRights(req.user_id);

	if ((message.author_id !== req.user_id)) {
		if (!rights.has("MANAGE_MESSAGES")) {
			const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
			permission.hasThrow("MANAGE_MESSAGES");
		}
	} else rights.hasThrow("SELF_DELETE_MESSAGES");

	await Message.delete({ id: message_id });

	await emitEvent({
		event: "MESSAGE_DELETE",
		channel_id,
		data: {
			id: message_id,
			channel_id,
			guild_id: channel.guild_id
		}
	} as MessageDeleteEvent);

	res.sendStatus(204);
});

export default router;