diff --git a/src/api/routes/channels/#channel_id/messages/#message_id/ack.ts b/src/api/routes/channels/#channel_id/messages/#message_id/ack.ts index e702482c..c7c96fea 100644 --- a/src/api/routes/channels/#channel_id/messages/#message_id/ack.ts +++ b/src/api/routes/channels/#channel_id/messages/#message_id/ack.ts @@ -17,12 +17,7 @@ */ import { route } from "@spacebar/api"; -import { - emitEvent, - getPermission, - MessageAckEvent, - ReadState, -} from "@spacebar/util"; +import { emitEvent, getPermission, MessageAckEvent, ReadState } from "@spacebar/util"; import { Request, Response, Router } from "express"; const router = Router({ mergeParams: true }); @@ -43,19 +38,16 @@ router.post( async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; - const permission = await getPermission( - req.user_id, - undefined, - channel_id, - ); + const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("VIEW_CHANNEL"); let read_state = await ReadState.findOne({ where: { user_id: req.user_id, channel_id }, }); - if (!read_state) - read_state = ReadState.create({ user_id: req.user_id, channel_id }); + if (!read_state) read_state = ReadState.create({ user_id: req.user_id, channel_id }); read_state.last_message_id = message_id; + //It's a little more complicated but this'll do :P + read_state.mention_count = 0; await read_state.save(); diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index 8de86792..d0d445b9 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -324,7 +324,7 @@ router.post( } // handle blocked users in dms - if(channel.recipients?.length == 2) { + if (channel.recipients?.length == 2) { const otherUser = channel.recipients.find((r) => r.user_id != req.user_id)?.user; if (otherUser) { const relationship = await Relationship.findOne({ @@ -335,12 +335,11 @@ router.post( }); if (relationship?.type === RelationshipType.blocked) { - throw DiscordApiErrors.CANNOT_MESSAGE_USER; + throw DiscordApiErrors.CANNOT_MESSAGE_USER; } } } - if (body.nonce) { const existing = await Message.findOne({ where: { @@ -499,6 +498,8 @@ router.post( }); if (!read_state) read_state = ReadState.create({ user_id: req.user_id, channel_id }); read_state.last_message_id = message.id; + //It's a little more complicated than this but this'll do + read_state.mention_count = 0; await Promise.all([ read_state.save(), diff --git a/src/api/routes/read-states/ack-bulk.ts b/src/api/routes/read-states/ack-bulk.ts index 475b223a..a597e4bc 100644 --- a/src/api/routes/read-states/ack-bulk.ts +++ b/src/api/routes/read-states/ack-bulk.ts @@ -19,7 +19,7 @@ import { route } from "@spacebar/api"; import { ReadState } from "@spacebar/util"; import { Request, Response, Router } from "express"; -import { AckBulkSchema } from "@spacebar/schemas" +import { AckBulkSchema } from "@spacebar/schemas"; const router = Router({ mergeParams: true }); router.post( @@ -56,6 +56,8 @@ router.post( }); ret.last_message_id = x.message_id; + //It's a little more complicated than this but this'll do + ret.mention_count = 0; return ret.save(); }), diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index f558a8b1..70bbc479 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -44,9 +44,12 @@ import { normalizeUrl, } from "@spacebar/util"; import { HTTPError } from "lambert-server"; -import { In } from "typeorm"; +import { In, Or, Equal, IsNull } from "typeorm"; import fetch from "node-fetch-commonjs"; import { CloudAttachment } from "../../../util/entities/CloudAttachment"; +import { ReadState } from "../../../util/entities/ReadState"; +import { Member } from "../../../util/entities/Member"; +import { ChannelType } from "@spacebar/schemas"; import { Embed, MessageCreateAttachment, MessageCreateCloudAttachment, MessageCreateSchema, MessageType, Reaction } from "@spacebar/schemas"; import { EmbedType } from "../../../schemas/api/messages/Embeds"; const allow_empty = false; @@ -322,6 +325,32 @@ export async function handleMessage(opts: MessageOptions): Promise { message.mention_everyone = mention_everyone; + if (mention_everyone || channel.type === ChannelType.DM || channel.type === ChannelType.GROUP_DM) { + const repository = ReadState.getRepository(); + const condition = { channel_id: channel.id }; + repository.update({ ...condition, mention_count: IsNull() }, { mention_count: 0 }); + repository.increment(condition, "mention_count", 1); + } else { + const users = new Set([ + ...(message.mention_roles.length + ? await Member.find({ + where: [ + ...message.mention_roles.map((role) => { + return { roles: { id: role.id } }; + }), + ], + }) + : [] + ).map((member) => member.id), + ...message.mentions.map((user) => user.id), + ]); + + const repository = ReadState.getRepository(); + const condition = { user_id: Or(...[...users].map((id) => Equal(id))), channel_id: channel.id }; + repository.update({ ...condition, mention_count: IsNull() }, { mention_count: 0 }); + repository.increment(condition, "mention_count", 1); + } + // TODO: check and put it all in the body return message; diff --git a/src/util/entities/ReadState.ts b/src/util/entities/ReadState.ts index 36916e11..be3de5d1 100644 --- a/src/util/entities/ReadState.ts +++ b/src/util/entities/ReadState.ts @@ -16,14 +16,7 @@ along with this program. If not, see . */ -import { - Column, - Entity, - Index, - JoinColumn, - ManyToOne, - RelationId, -} from "typeorm"; +import { Column, Entity, Index, JoinColumn, ManyToOne, RelationId } from "typeorm"; import { BaseClass } from "./BaseClass"; import { Channel } from "./Channel"; import { User } from "./User";