Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
599 changes: 320 additions & 279 deletions src/api/routes/channels/#channel_id/messages/#message_id/index.ts

Large diffs are not rendered by default.

192 changes: 114 additions & 78 deletions src/api/routes/channels/#channel_id/messages/#message_id/reactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ import {
MessageReactionRemoveEmojiEvent,
MessageReactionRemoveEvent,
User,
arrayRemove,
} from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
import { In } from "typeorm";
import { PartialEmoji, PublicMemberProjection, PublicUserProjection } from "@spacebar/schemas";
import {
resolveMessageInChannel,
computeProjectionsForMessage,
getIntimacyBroadcastRuleForChannel,
filterReactionsForIntimacyBroadcast,
} from "../../../../../util/helpers/MessageProjection";

const router = Router({ mergeParams: true });
// TODO: check if emoji is really an unicode emoji or a properly encoded external emoji
Expand Down Expand Up @@ -74,17 +79,26 @@ router.delete(
where: { id: channel_id },
});

await Message.update({ id: message_id, channel_id }, { reactions: [] });
const message = await resolveMessageInChannel(message_id, channel_id, req.user_id);

await emitEvent({
event: "MESSAGE_REACTION_REMOVE_ALL",
channel_id,
data: {
channel_id,
message_id,
guild_id: channel.guild_id,
},
} as MessageReactionRemoveAllEvent);
message.reactions = [];
await message.save();

const projections = await computeProjectionsForMessage(message);

await Promise.all(
projections.map((projection) =>
emitEvent({
event: "MESSAGE_REACTION_REMOVE_ALL",
channel_id: projection.channelId,
data: {
channel_id: projection.channelId,
message_id,
guild_id: channel.guild_id,
},
} as MessageReactionRemoveAllEvent),
),
);

res.sendStatus(204);
},
Expand All @@ -107,27 +121,30 @@ router.delete(
const { message_id, channel_id } = req.params;
const emoji = getEmoji(req.params.emoji);

const message = await Message.findOneOrFail({
where: { id: message_id, channel_id },
});
const message = await resolveMessageInChannel(message_id, channel_id, req.user_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);
arrayRemove(message.reactions, 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),
]);
message.reactions.remove(already_added);

await message.save();

const projections = await computeProjectionsForMessage(message);

await Promise.all(
projections.map((projection) =>
emitEvent({
event: "MESSAGE_REACTION_REMOVE_EMOJI",
channel_id: projection.channelId,
data: {
channel_id: projection.channelId,
message_id,
guild_id: message.guild_id,
emoji,
},
} as MessageReactionRemoveEmojiEvent),
),
);

res.sendStatus(204);
},
Expand All @@ -152,10 +169,17 @@ router.get(
const { message_id, channel_id } = req.params;
const emoji = getEmoji(req.params.emoji);

const message = await Message.findOneOrFail({
where: { id: message_id, channel_id },
});
const reaction = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
const message = await resolveMessageInChannel(message_id, channel_id, req.user_id);

const intimacyRule = await getIntimacyBroadcastRuleForChannel(message.source_channel_id || message.channel_id!);
const isIntimacyBroadcast = intimacyRule !== null;

let reactions = message.reactions;
if (isIntimacyBroadcast) {
reactions = filterReactionsForIntimacyBroadcast(reactions, req.user_id, message.author_id);
}

const reaction = 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 users = (
Expand Down Expand Up @@ -193,9 +217,7 @@ router.put(
const channel = await Channel.findOneOrFail({
where: { id: channel_id },
});
const message = await Message.findOneOrFail({
where: { id: message_id, channel_id },
});
const message = await resolveMessageInChannel(message_id, channel_id, req.user_id);
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);

if (!already_added) req.permission?.hasThrow("ADD_REACTIONS");
Expand Down Expand Up @@ -231,18 +253,24 @@ router.put(
})
).toPublicMember();

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);
const projections = await computeProjectionsForMessage(message);

await Promise.all(
projections.map((projection) =>
emitEvent({
event: "MESSAGE_REACTION_ADD",
channel_id: projection.channelId,
data: {
user_id: req.user_id,
channel_id: projection.channelId,
message_id,
guild_id: channel.guild_id,
emoji,
member,
},
} as MessageReactionAddEvent),
),
);

res.sendStatus(204);
},
Expand All @@ -269,9 +297,7 @@ router.delete(
const channel = await Channel.findOneOrFail({
where: { id: channel_id },
});
const message = await Message.findOneOrFail({
where: { id: message_id, channel_id },
});
const message = await resolveMessageInChannel(message_id, channel_id, req.user_id);

if (user_id === "@me") user_id = req.user_id;
else {
Expand All @@ -284,22 +310,28 @@ router.delete(

already_added.count--;

if (already_added.count <= 0) arrayRemove(message.reactions, already_added);
if (already_added.count <= 0) message.reactions.remove(already_added);
else already_added.user_ids.splice(already_added.user_ids.indexOf(user_id), 1);

await message.save();

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);
const projections = await computeProjectionsForMessage(message);

await Promise.all(
projections.map((projection) =>
emitEvent({
event: "MESSAGE_REACTION_REMOVE",
channel_id: projection.channelId,
data: {
user_id: req.user_id,
channel_id: projection.channelId,
message_id,
guild_id: channel.guild_id,
emoji,
},
} as MessageReactionRemoveEvent),
),
);

res.sendStatus(204);
},
Expand All @@ -326,9 +358,7 @@ router.delete(
const channel = await Channel.findOneOrFail({
where: { id: channel_id },
});
const message = await Message.findOneOrFail({
where: { id: message_id, channel_id },
});
const message = await resolveMessageInChannel(message_id, channel_id, req.user_id);

if (user_id === "@me") user_id = req.user_id;
else {
Expand All @@ -341,22 +371,28 @@ router.delete(

already_added.count--;

if (already_added.count <= 0) arrayRemove(message.reactions, already_added);
if (already_added.count <= 0) message.reactions.remove(already_added);
else already_added.user_ids.splice(already_added.user_ids.indexOf(user_id), 1);

await message.save();

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);
const projections = await computeProjectionsForMessage(message);

await Promise.all(
projections.map((projection) =>
emitEvent({
event: "MESSAGE_REACTION_REMOVE",
channel_id: projection.channelId,
data: {
user_id: req.user_id,
channel_id: projection.channelId,
message_id,
guild_id: channel.guild_id,
emoji,
},
} as MessageReactionRemoveEvent),
),
);

res.sendStatus(204);
},
Expand Down
Loading
Loading