implement mention_count

This commit is contained in:
MathMan05 2025-11-18 18:19:58 -06:00
parent 5c5d62a174
commit 77022c0afb
5 changed files with 43 additions and 26 deletions

View File

@ -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();

View File

@ -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(),

View File

@ -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();
}),

View File

@ -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;

View File

@ -16,14 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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";