fix: consider mentions when calculating unreads

fix: always create a virtual unreads object if not available
This commit is contained in:
izzy
2025-09-12 14:19:31 +01:00
parent bbfddb0377
commit 482a3d98d0
3 changed files with 44 additions and 15 deletions
+11 -10
View File
@@ -296,11 +296,10 @@ export class Channel {
)
return false;
const unread = this.#collection.client.channelUnreads.for(this);
return (
(
this.#collection.client.channelUnreads.get(this.id)?.lastMessageId ??
"0"
).localeCompare(this.lastMessageId) === -1
(unread.lastMessageId ?? "0").localeCompare(this.lastMessageId) === -1 ||
unread.messageMentionIds.size > 0
);
}
@@ -720,17 +719,19 @@ export class Channel {
this.lastMessageId ??
ulid();
const unreads = this.#collection.client.channelUnreads;
const channelUnread = unreads.get(this.id);
if (channelUnread) {
unreads.updateUnderlyingObject(this.id, {
const channelUnread = this.#collection.client.channelUnreads.for(this);
batch(() => {
this.#collection.client.channelUnreads.updateUnderlyingObject(
this.id,
"lastMessageId",
lastMessageId,
});
);
if (channelUnread.messageMentionIds.size) {
channelUnread.messageMentionIds.clear();
}
}
});
// Skip request if not needed
if (skipRequest) return;
@@ -3,6 +3,7 @@ import { batch } from "solid-js";
import type { ChannelUnread as APIChannelUnread } from "revolt-api";
import { ChannelUnread } from "../classes/ChannelUnread.js";
import { Channel } from "../classes/index.js";
import type { HydratedChannelUnread } from "../hydration/channelUnread.js";
import { ClassCollection } from "./Collection.js";
@@ -48,4 +49,20 @@ export class ChannelUnreadCollection extends ClassCollection<
return instance;
}
}
/**
* Get channel unread data for a specific Channel
* @param channel Channel
* @returns Unread
*/
for(channel: Channel): ChannelUnread {
return this.getOrCreate(channel.id, {
_id: {
channel: channel.id,
user: this.client.user!.id,
},
last_id: null,
mentions: [],
});
}
}
+16 -5
View File
@@ -276,11 +276,22 @@ export async function handleEvent(
delete event.user;
client.messages.getOrCreate(event._id, event, true);
client.channels.updateUnderlyingObject(
event.channel,
"lastMessageId",
event._id,
);
if (
event.mentions?.includes(client.user!.id) &&
client.options.syncUnreads
) {
const channel = client.channels.get(event.channel);
if (!channel) return;
const unread = client.channelUnreads.for(channel);
unread.messageMentionIds.add(event._id);
client.channels.updateUnderlyingObject(
event.channel,
"lastMessageId",
event._id,
);
}
});
}
break;