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
|
import {
Channel,
emitEvent,
Emoji,
getPermission,
Member,
Message,
MessageReactionAddEvent,
MessageReactionRemoveAllEvent,
MessageReactionRemoveEmojiEvent,
MessageReactionRemoveEvent,
PartialEmoji,
PublicUserProjection,
User
} from "@fosscord/util";
import { Router, Response, Request } from "express";
import { HTTPError } from "lambert-server";
import { In } from "typeorm";
const router = Router();
// TODO: check if emoji is really an unicode emoji or a prperly encoded external emoji
function getEmoji(emoji: string): PartialEmoji {
emoji = decodeURIComponent(emoji);
const parts = emoji.includes(":") && emoji.split(":");
if (parts)
return {
name: parts[0],
id: parts[1]
};
return {
id: undefined,
name: emoji
};
}
router.delete("/", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const channel = await Channel.findOneOrFail({ id: channel_id });
const permissions = await getPermission(req.user_id, undefined, channel_id);
permissions.hasThrow("MANAGE_MESSAGES");
await Message.update({ id: message_id, channel_id }, { reactions: [] });
await emitEvent({
event: "MESSAGE_REACTION_REMOVE_ALL",
channel_id,
data: {
channel_id,
message_id,
guild_id: channel.guild_id
}
} as MessageReactionRemoveAllEvent);
res.sendStatus(204);
});
router.delete("/:emoji", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const emoji = getEmoji(req.params.emoji);
const permissions = await getPermission(req.user_id, undefined, channel_id);
permissions.hasThrow("MANAGE_MESSAGES");
const message = await Message.findOneOrFail({ id: message_id, channel_id });
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
if (!already_added) throw new HTTPError("Reaction not found", 404);
message.reactions.remove(already_added);
await Promise.all([
message.save(),
emitEvent({
event: "MESSAGE_REACTION_REMOVE_EMOJI",
channel_id,
data: {
channel_id,
message_id,
guild_id: message.guild_id,
emoji
}
} as MessageReactionRemoveEmojiEvent)
]);
res.sendStatus(204);
});
router.get("/:emoji", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const emoji = getEmoji(req.params.emoji);
const message = await Message.findOneOrFail({ id: message_id, channel_id });
const reaction = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
if (!reaction) throw new HTTPError("Reaction not found", 404);
const permissions = await getPermission(req.user_id, undefined, channel_id);
permissions.hasThrow("VIEW_CHANNEL");
const users = await User.find({
where: {
id: In(reaction.user_ids)
},
select: PublicUserProjection
});
res.json(users);
});
router.put("/:emoji/:user_id", async (req: Request, res: Response) => {
const { message_id, channel_id, user_id } = req.params;
if (user_id !== "@me") throw new HTTPError("Invalid user");
const emoji = getEmoji(req.params.emoji);
const channel = await Channel.findOneOrFail({ id: channel_id });
const message = await Message.findOneOrFail({ id: message_id, channel_id });
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
const permissions = await getPermission(req.user_id, undefined, channel_id);
permissions.hasThrow("READ_MESSAGE_HISTORY");
if (!already_added) permissions.hasThrow("ADD_REACTIONS");
if (emoji.id) {
const external_emoji = await Emoji.findOneOrFail({ id: emoji.id });
if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS");
emoji.animated = external_emoji.animated;
emoji.name = external_emoji.name;
}
if (already_added) {
if (already_added.user_ids.includes(req.user_id)) return res.sendStatus(204); // Do not throw an error ¯\_(ツ)_/¯ as discord also doesn't throw any error
already_added.count++;
} else message.reactions.push({ count: 1, emoji, user_ids: [req.user_id] });
await Message.update({ id: message_id, channel_id }, message);
const member = channel.guild_id && (await Member.findOneOrFail({ id: req.user_id }));
await emitEvent({
event: "MESSAGE_REACTION_ADD",
channel_id,
data: {
user_id: req.user_id,
channel_id,
message_id,
guild_id: channel.guild_id,
emoji,
member
}
} as MessageReactionAddEvent);
res.sendStatus(204);
});
router.delete("/:emoji/:user_id", async (req: Request, res: Response) => {
var { message_id, channel_id, user_id } = req.params;
const emoji = getEmoji(req.params.emoji);
const channel = await Channel.findOneOrFail({ id: channel_id });
const message = await Message.findOneOrFail({ id: message_id, channel_id });
const permissions = await getPermission(req.user_id, undefined, channel_id);
if (user_id === "@me") user_id = req.user_id;
else permissions.hasThrow("MANAGE_MESSAGES");
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
if (!already_added || !already_added.user_ids.includes(user_id)) throw new HTTPError("Reaction not found", 404);
already_added.count--;
if (already_added.count <= 0) message.reactions.remove(already_added);
await Message.update({ id: message_id, channel_id }, message);
await emitEvent({
event: "MESSAGE_REACTION_REMOVE",
channel_id,
data: {
user_id: req.user_id,
channel_id,
message_id,
guild_id: channel.guild_id,
emoji
}
} as MessageReactionRemoveEvent);
res.sendStatus(204);
});
export default router;
|