summary refs log tree commit diff
path: root/src/api/util
diff options
context:
space:
mode:
authorMathMan05 <mathmanrm@gmail.com>2025-11-18 18:19:58 -0600
committerMathMan05 <mathmanrm@gmail.com>2025-11-18 18:19:58 -0600
commit77022c0afb654ead21e508f1bd1edb15c271fa92 (patch)
treec5e7d0745a62556aac7b3ddf453c13cb8aa7398f /src/api/util
parentMerge pull request #1391 from MathMan05/Dec (diff)
downloadserver-ts-77022c0afb654ead21e508f1bd1edb15c271fa92.tar.xz
implement mention_count
Diffstat (limited to 'src/api/util')
-rw-r--r--src/api/util/handlers/Message.ts31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts

index e3d5a99f..90d8153e 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"; const allow_empty = false; // TODO: check webhook, application, system author, stickers @@ -321,6 +324,32 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { 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<string>([ + ...(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;