Merge pull request #1394 from MathMan05/readStates

This commit is contained in:
Cyber 2025-11-19 15:20:19 +01:00 committed by GitHub
commit 1917bec1f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 43 additions and 26 deletions

View File

@ -17,12 +17,7 @@
*/ */
import { route } from "@spacebar/api"; import { route } from "@spacebar/api";
import { import { emitEvent, getPermission, MessageAckEvent, ReadState } from "@spacebar/util";
emitEvent,
getPermission,
MessageAckEvent,
ReadState,
} from "@spacebar/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });
@ -43,19 +38,16 @@ router.post(
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { channel_id, message_id } = req.params; const { channel_id, message_id } = req.params;
const permission = await getPermission( const permission = await getPermission(req.user_id, undefined, channel_id);
req.user_id,
undefined,
channel_id,
);
permission.hasThrow("VIEW_CHANNEL"); permission.hasThrow("VIEW_CHANNEL");
let read_state = await ReadState.findOne({ let read_state = await ReadState.findOne({
where: { user_id: req.user_id, channel_id }, where: { user_id: req.user_id, channel_id },
}); });
if (!read_state) if (!read_state) read_state = ReadState.create({ user_id: req.user_id, channel_id });
read_state = ReadState.create({ user_id: req.user_id, channel_id });
read_state.last_message_id = message_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(); await read_state.save();

View File

@ -324,7 +324,7 @@ router.post(
} }
// handle blocked users in dms // 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; const otherUser = channel.recipients.find((r) => r.user_id != req.user_id)?.user;
if (otherUser) { if (otherUser) {
const relationship = await Relationship.findOne({ const relationship = await Relationship.findOne({
@ -335,12 +335,11 @@ router.post(
}); });
if (relationship?.type === RelationshipType.blocked) { if (relationship?.type === RelationshipType.blocked) {
throw DiscordApiErrors.CANNOT_MESSAGE_USER; throw DiscordApiErrors.CANNOT_MESSAGE_USER;
} }
} }
} }
if (body.nonce) { if (body.nonce) {
const existing = await Message.findOne({ const existing = await Message.findOne({
where: { where: {
@ -499,6 +498,8 @@ router.post(
}); });
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; 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([ await Promise.all([
read_state.save(), read_state.save(),

View File

@ -19,7 +19,7 @@
import { route } from "@spacebar/api"; import { route } from "@spacebar/api";
import { ReadState } from "@spacebar/util"; import { ReadState } from "@spacebar/util";
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { AckBulkSchema } from "@spacebar/schemas" import { AckBulkSchema } from "@spacebar/schemas";
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });
router.post( router.post(
@ -56,6 +56,8 @@ router.post(
}); });
ret.last_message_id = x.message_id; 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(); return ret.save();
}), }),

View File

@ -44,9 +44,12 @@ import {
normalizeUrl, normalizeUrl,
} from "@spacebar/util"; } from "@spacebar/util";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { In } from "typeorm"; import { In, Or, Equal, IsNull } from "typeorm";
import fetch from "node-fetch-commonjs"; import fetch from "node-fetch-commonjs";
import { CloudAttachment } from "../../../util/entities/CloudAttachment"; 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 { Embed, MessageCreateAttachment, MessageCreateCloudAttachment, MessageCreateSchema, MessageType, Reaction } from "@spacebar/schemas";
import { EmbedType } from "../../../schemas/api/messages/Embeds"; import { EmbedType } from "../../../schemas/api/messages/Embeds";
const allow_empty = false; const allow_empty = false;
@ -322,6 +325,32 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
message.mention_everyone = mention_everyone; 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 // TODO: check and put it all in the body
return message; return message;

View File

@ -16,14 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { import { Column, Entity, Index, JoinColumn, ManyToOne, RelationId } from "typeorm";
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
RelationId,
} from "typeorm";
import { BaseClass } from "./BaseClass"; import { BaseClass } from "./BaseClass";
import { Channel } from "./Channel"; import { Channel } from "./Channel";
import { User } from "./User"; import { User } from "./User";