Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): count background unreads with badge and mute settings

Hampusca79fec0b38b
diff --git a/fluxer_app/src/features/gateway/transport/BackgroundReadStateReducer.ts b/fluxer_app/src/features/gateway/transport/BackgroundReadStateReducer.ts
index f739ac875..172b066be 100644
--- a/fluxer_app/src/features/gateway/transport/BackgroundReadStateReducer.ts
+++ b/fluxer_app/src/features/gateway/transport/BackgroundReadStateReducer.ts
@@ -2,16 +2,23 @@
import {
BackgroundMentionCountMode,
+ type BackgroundMessageNotification,
type BackgroundSnapshotSink,
type BackgroundSnapshotSinkConfig,
type ObserveMentionCounts,
type ObserveMessageNotification,
} from '@app/features/gateway/transport/BackgroundSnapshotSink';
import {resolveReadStateMention} from '@app/features/read_state/state/read_states/ReadStateMentionMachine';
+import {compareMessageIds, snowflakeTimestamp} from '@app/features/read_state/state/read_states/shared';
import {ME} from '@fluxer/constants/src/AppConstants';
-import {MessageFlags, MessageTypes} from '@fluxer/constants/src/ChannelConstants';
+import {
+ ChannelTypes,
+ MessageFlags,
+ MessageTypes,
+ TEXT_BASED_CHANNEL_TYPES,
+} from '@fluxer/constants/src/ChannelConstants';
import {LARGE_GUILD_THRESHOLD} from '@fluxer/constants/src/GatewayConstants';
-import {GuildFeatures} from '@fluxer/constants/src/GuildConstants';
+import {ContentWarningLevel, GuildFeatures} from '@fluxer/constants/src/GuildConstants';
import {MessageNotifications} from '@fluxer/constants/src/NotificationConstants';
import {RelationshipTypes} from '@fluxer/constants/src/UserConstants';
import type {Message as WireMessage} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
@@ -28,6 +35,7 @@ interface MuteConfig {
interface ChannelNotificationOverride {
readonly mute: MuteConfig;
readonly messageNotifications: number;
+ readonly unreadBadges: number | null;
}
interface GuildNotificationSettings {
@@ -35,6 +43,7 @@ interface GuildNotificationSettings {
readonly messageNotifications: number;
readonly suppressEveryone: boolean;
readonly suppressRoles: boolean;
+ readonly unreadBadges: number | null;
readonly channelOverrides: ReadonlyMap<string, ChannelNotificationOverride>;
}
@@ -43,12 +52,17 @@ interface GuildDetails {
readonly defaultMessageNotifications: number;
readonly memberCount: number | null;
readonly largeFeature: boolean;
+ readonly joinedAt: number | null;
+ readonly matureContent: boolean;
+ readonly contentWarning: boolean;
}
interface ChannelDetails {
readonly name: string | null;
readonly parentId: string | null;
readonly type: number | null;
+ readonly matureContent: boolean | null;
+ readonly contentWarning: boolean | null;
}
const UNMUTED: MuteConfig = {muted: false, mutedUntil: null};
@@ -58,18 +72,45 @@ const DEFAULT_GUILD_SETTINGS: GuildNotificationSettings = {
messageNotifications: MessageNotifications.NULL,
suppressEveryone: false,
suppressRoles: false,
+ unreadBadges: null,
channelOverrides: new Map(),
};
const NOTIFIABLE_MESSAGE_TYPES: ReadonlySet<number> = new Set([MessageTypes.DEFAULT, MessageTypes.REPLY]);
-export class BackgroundReadStateCapacityError extends Error {
+const UNREAD_TRACKED_CHANNEL_TYPES: ReadonlySet<number> = new Set([
+ ...TEXT_BASED_CHANNEL_TYPES,
+ ChannelTypes.ANNOUNCEMENT_THREAD,
+ ChannelTypes.PUBLIC_THREAD,
+ ChannelTypes.PRIVATE_THREAD,
+]);
+
+class ReadStateCapacityExceeded extends Error {
constructor(kind: 'channels' | 'guilds' | 'users', capacity: number) {
super(`Background read state exceeded its ${capacity} tracked ${kind} bound`);
- this.name = 'BackgroundReadStateCapacityError';
+ this.name = 'ReadStateCapacityExceeded';
}
}
+export interface BackgroundChannelActivity {
+ readonly channelId: string;
+ readonly guildId: string | null;
+ readonly parentId: string | null;
+ readonly type: number | null;
+ readonly name: string | null;
+ readonly mentionCount: number;
+ readonly unread: boolean;
+ readonly lastMessageId: string | null;
+ readonly ackMessageId: string | null;
+}
+
+export interface BackgroundReadStateTotals {
+ readonly mentionCount: number;
+ readonly unreadChannelCount: number;
+ readonly lastMessageId: string | null;
+ readonly degraded: boolean;
+}
+
function asRecord(value: unknown): Record<string, unknown> | null {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return null;
@@ -93,7 +134,7 @@ function asInteger(value: unknown): number | null {
return typeof value === 'number' && Number.isInteger(value) ? value : null;
}
-function isExplicitNotificationLevel(level: number | undefined): level is number {
+function isExplicitNotificationLevel(level: number | null | undefined): level is number {
return (
level === MessageNotifications.ALL_MESSAGES ||
level === MessageNotifications.ONLY_MENTIONS ||
@@ -150,6 +191,7 @@ function readChannelOverrides(value: unknown): ReadonlyMap<string, ChannelNotifi
overrides.set(channelId, {
mute: readMuteConfig(override),
messageNotifications: asInteger(override.message_notifications) ?? MessageNotifications.NULL,
+ unreadBadges: asInteger(override.unread_badges),
});
}
return overrides;
@@ -171,6 +213,7 @@ function readGuildSettings(value: unknown): {guildId: string; settings: GuildNot
messageNotifications: asInteger(source.message_notifications) ?? MessageNotifications.NULL,
suppressEveryone: source.suppress_everyone === true,
suppressRoles: source.suppress_roles === true,
+ unreadBadges: asInteger(source.unread_badges),
channelOverrides: readChannelOverrides(source.channel_overrides),
},
};
@@ -180,6 +223,8 @@ function readGuildDetails(source: Record<string, unknown>, prior: GuildDetails |
const properties = asRecord(source.properties) ?? source;
const features = asIdSet(properties.features);
const memberCount = asInteger(source.member_count) ?? asInteger(properties.member_count);
+ const joinedAt = asString(source.joined_at) ?? asString(properties.joined_at);
+ const parsedJoinedAt = joinedAt === null ? Number.NaN : Date.parse(joinedAt);
return {
name: asString(properties.name) ?? prior?.name ?? null,
defaultMessageNotifications:
@@ -188,6 +233,12 @@ function readGuildDetails(source: Record<string, unknown>, prior: GuildDetails |
MessageNotifications.ALL_MESSAGES,
memberCount: memberCount ?? prior?.memberCount ?? null,
largeFeature: features.has(GuildFeatures.LARGE_GUILD_OVERRIDE) || features.has(GuildFeatures.VERY_LARGE_GUILD),
+ joinedAt: Number.isNaN(parsedJoinedAt) ? (prior?.joinedAt ?? null) : parsedJoinedAt,
+ matureContent: typeof properties.nsfw === 'boolean' ? properties.nsfw : (prior?.matureContent ?? false),
+ contentWarning:
+ typeof properties.content_warning_level === 'number'
+ ? properties.content_warning_level === ContentWarningLevel.CONTENT_WARNING
+ : (prior?.contentWarning ?? false),
};
}
@@ -204,19 +255,39 @@ function effectiveGuildMessageNotifications(details: GuildDetails | undefined):
return details.defaultMessageNotifications;
}
+function readContentWarning(value: unknown): boolean | null {
+ const level = asInteger(value);
+ return level === null || level === ContentWarningLevel.INHERIT ? null : level === ContentWarningLevel.CONTENT_WARNING;
+}
+
function readChannelDetails(source: Record<string, unknown>): ChannelDetails {
return {
name: asString(source.name),
parentId: asString(source.parent_id),
type: asInteger(source.type),
+ matureContent: typeof source.nsfw_override === 'boolean' ? source.nsfw_override : null,
+ contentWarning: readContentWarning(source.content_warning_level),
};
}
+const THREAD_CHANNEL_TYPES: ReadonlySet<number> = new Set([
+ ChannelTypes.ANNOUNCEMENT_THREAD,
+ ChannelTypes.PUBLIC_THREAD,
+ ChannelTypes.PRIVATE_THREAD,
+]);
+
export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
private readonly userId: string;
private readonly observeMentionCounts: ObserveMentionCounts;
private readonly observeMessageNotification: ObserveMessageNotification;
+ private readonly observeMention: ObserveMessageNotification | null;
private readonly mentionCounts = new Map<string, number>();
+ private readonly lastMessageIds = new Map<string, string>();
+ private readonly ackMessageIds = new Map<string, string>();
+ private readonly unreadChannels = new Set<string>();
+ private newestMessageId: string | null = null;
+ private degraded = false;
+ private unreadBadgeCustomizationEnabled = false;
private readonly channelGuilds = new Map<string, string>();
private readonly guildChannels = new Map<string, Set<string>>();
private readonly guildSettings = new Map<string, GuildNotificationSettings>();
@@ -225,13 +296,123 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
private readonly channelDetails = new Map<string, ChannelDetails>();
private readonly blockedUserIds = new Set<string>();
- constructor({userId, observeMentionCounts, observeMessageNotification}: BackgroundSnapshotSinkConfig) {
+ constructor({
+ userId,
+ observeMentionCounts,
+ observeMessageNotification,
+ observeMention,
+ }: BackgroundSnapshotSinkConfig) {
this.userId = userId;
this.observeMentionCounts = observeMentionCounts;
this.observeMessageNotification = observeMessageNotification;
+ this.observeMention = observeMention ?? null;
+ }
+
+ get isDegraded(): boolean {
+ return this.degraded;
+ }
+
+ totals(): BackgroundReadStateTotals {
+ let mentionCount = 0;
+ for (const [channelId, count] of this.mentionCounts) {
+ if (this.channelDetails.has(channelId)) {
+ mentionCount += count;
+ }
+ }
+ return {
+ mentionCount,
+ unreadChannelCount: this.unreadChannels.size,
+ lastMessageId: this.newestMessageId,
+ degraded: this.degraded,
+ };
+ }
+
+ mentionCountsSnapshot(): Map<string, number> {
+ return new Map(this.mentionCounts);
+ }
+
+ *channelActivity(): IterableIterator<BackgroundChannelActivity> {
+ const channelIds = new Set<string>(this.unreadChannels);
+ for (const [channelId, count] of this.mentionCounts) {
+ if (count > 0 && this.channelDetails.has(channelId)) {
+ channelIds.add(channelId);
+ }
+ }
+ for (const channelId of channelIds) {
+ const details = this.channelDetails.get(channelId);
+ yield {
+ channelId,
+ guildId: this.channelGuilds.get(channelId) ?? null,
+ parentId: details?.parentId ?? null,
+ type: details?.type ?? null,
+ name: details?.name ?? null,
+ mentionCount: this.mentionCounts.get(channelId) ?? 0,
+ unread: this.unreadChannels.has(channelId),
+ lastMessageId: this.lastMessageIds.get(channelId) ?? null,
+ ackMessageId: this.ackMessageIds.get(channelId) ?? null,
+ };
+ }
+ }
+
+ guildName(guildId: string): string | null {
+ return this.guildDetails.get(guildId)?.name ?? null;
+ }
+
+ refreshUnreadState(): void {
+ this.refreshAllUnread();
+ }
+
+ setUnreadBadgeCustomizationEnabled(enabled: boolean): void {
+ if (this.unreadBadgeCustomizationEnabled === enabled) {
+ return;
+ }
+ this.unreadBadgeCustomizationEnabled = enabled;
+ this.refreshAllUnread();
+ }
+
+ isChannelGated(channelId: string, guildId: string | null): boolean {
+ let channel = this.channelDetails.get(channelId);
+ if (channel?.type != null && THREAD_CHANNEL_TYPES.has(channel.type) && channel.parentId !== null) {
+ channel = this.channelDetails.get(channel.parentId);
+ }
+ const parent = channel?.parentId == null ? undefined : this.channelDetails.get(channel.parentId);
+ const resolvedGuildId = guildId ?? this.channelGuilds.get(channelId) ?? null;
+ const guild = resolvedGuildId === null ? undefined : this.guildDetails.get(resolvedGuildId);
+ const matureContent = channel?.matureContent ?? parent?.matureContent ?? guild?.matureContent ?? false;
+ const contentWarning = channel?.contentWarning ?? parent?.contentWarning ?? guild?.contentWarning ?? false;
+ return matureContent || contentWarning;
}
applyReady(data: unknown): void {
+ this.degraded = false;
+ try {
+ this.applyReadyUnguarded(data);
+ } catch (error) {
+ this.degradeOnCapacity(error);
+ }
+ }
+
+ applyDispatch(type: string, data: unknown): void {
+ if (this.degraded) {
+ return;
+ }
+ try {
+ this.applyDispatchUnguarded(type, data);
+ } catch (error) {
+ this.degradeOnCapacity(error);
+ }
+ }
+
+ private degradeOnCapacity(error: unknown): void {
+ if (!(error instanceof ReadStateCapacityExceeded)) {
+ throw error;
+ }
+ this.clearState();
+ this.degraded = true;
+ this.observeMentionCounts(new Map(), BackgroundMentionCountMode.REPLACE);
+ }
+
+ private applyReadyUnguarded(data: unknown): void {
const ready = asRecord(data);
this.clearState();
if (ready === null) {
@@ -253,11 +434,20 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
for (const entry of asArray(ready.read_states)) {
const readState = asRecord(entry);
const channelId = asString(readState?.id);
+ if (channelId === null) {
+ continue;
+ }
const mentionCount = asCount(readState?.mention_count);
- if (channelId !== null && mentionCount > 0) {
+ if (mentionCount > 0) {
this.storeMentionCount(channelId, mentionCount);
}
+ const ackMessageId = asString(readState?.last_message_id);
+ if (ackMessageId !== null) {
+ this.assertChannelCapacity(this.ackMessageIds, channelId);
+ this.ackMessageIds.set(channelId, ackMessageId);
+ }
}
+ this.refreshAllUnread();
const visible = new Map<string, number>();
for (const [channelId, mentionCount] of this.mentionCounts) {
if (this.channelDetails.has(channelId)) {
@@ -267,7 +457,7 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
this.observeMentionCounts(visible, BackgroundMentionCountMode.REPLACE);
}
- applyDispatch(type: string, data: unknown): void {
+ private applyDispatchUnguarded(type: string, data: unknown): void {
const payload = asRecord(data);
if (payload === null) {
return;
@@ -300,11 +490,17 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
case 'GUILD_UPDATE':
this.storeGuildDetails(payload);
return;
+ case 'CHANNEL_UPDATE_BULK':
+ for (const entry of asArray(payload.channels)) {
+ this.storeChannel(entry);
+ }
+ return;
case 'GUILD_DELETE':
this.applyGuildDelete(payload);
return;
case 'USER_GUILD_SETTINGS_UPDATE':
this.storeGuildSettings(payload);
+ this.refreshAllUnread();
return;
case 'RELATIONSHIP_ADD':
case 'RELATIONSHIP_UPDATE':
@@ -329,6 +525,10 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
private clearState(): void {
this.mentionCounts.clear();
+ this.lastMessageIds.clear();
+ this.ackMessageIds.clear();
+ this.unreadChannels.clear();
+ this.newestMessageId = null;
this.channelGuilds.clear();
this.guildChannels.clear();
this.guildSettings.clear();
@@ -420,10 +620,90 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
private storeChannelDetails(channelId: string, channel: Record<string, unknown>): number {
this.assertChannelCapacity(this.channelDetails, channelId);
const known = this.channelDetails.has(channelId);
- this.channelDetails.set(channelId, readChannelDetails(channel));
+ const prior = this.channelDetails.get(channelId);
+ const details = readChannelDetails(channel);
+ this.channelDetails.set(channelId, {
+ name: 'name' in channel ? details.name : (prior?.name ?? null),
+ parentId: 'parent_id' in channel ? details.parentId : (prior?.parentId ?? null),
+ type: 'type' in channel ? details.type : (prior?.type ?? null),
+ matureContent: 'nsfw_override' in channel ? details.matureContent : (prior?.matureContent ?? null),
+ contentWarning: 'content_warning_level' in channel ? details.contentWarning : (prior?.contentWarning ?? null),
+ });
+ const lastMessageId = asString(channel.last_message_id);
+ if (lastMessageId !== null) {
+ this.recordLastMessage(channelId, lastMessageId);
+ } else {
+ this.refreshUnread(channelId);
+ }
return known ? 0 : (this.mentionCounts.get(channelId) ?? 0);
}
+ private recordLastMessage(channelId: string, messageId: string): void {
+ const previous = this.lastMessageIds.get(channelId);
+ if (previous === undefined || compareMessageIds(previous, messageId) < 0) {
+ this.assertChannelCapacity(this.lastMessageIds, channelId);
+ this.lastMessageIds.set(channelId, messageId);
+ }
+ if (this.newestMessageId === null || compareMessageIds(this.newestMessageId, messageId) < 0) {
+ this.newestMessageId = messageId;
+ }
+ this.refreshUnread(channelId);
+ }
+
+ private recordAck(channelId: string, messageId: string): void {
+ const previous = this.ackMessageIds.get(channelId);
+ if (previous === undefined || compareMessageIds(previous, messageId) < 0) {
+ this.assertChannelCapacity(this.ackMessageIds, channelId);
+ this.ackMessageIds.set(channelId, messageId);
+ }
+ this.refreshUnread(channelId);
+ }
+
+ private refreshAllUnread(): void {
+ this.unreadChannels.clear();
+ for (const channelId of this.lastMessageIds.keys()) {
+ this.refreshUnread(channelId);
+ }
+ }
+
+ private refreshUnread(channelId: string): void {
+ if (this.isChannelUnread(channelId)) {
+ this.unreadChannels.add(channelId);
+ } else {
+ this.unreadChannels.delete(channelId);
+ }
+ }
+
+ private isChannelUnread(channelId: string): boolean {
+ const lastMessageId = this.lastMessageIds.get(channelId);
+ const details = this.channelDetails.get(channelId);
+ if (lastMessageId === undefined || details === undefined) {
+ return false;
+ }
+ if (details.type !== null && !UNREAD_TRACKED_CHANNEL_TYPES.has(details.type)) {
+ return false;
+ }
+ const guildId = this.channelGuilds.get(channelId) ?? null;
+ if (this.isGuildOrChannelMuted(guildId, channelId)) {
+ return false;
+ }
+ if (guildId !== null) {
+ const unreadBadges = this.resolveUnreadBadges(guildId, channelId, details);
+ if (unreadBadges === MessageNotifications.NO_MESSAGES || unreadBadges === MessageNotifications.ONLY_MENTIONS) {
+ return false;
+ }
+ }
+ const ackMessageId = this.ackMessageIds.get(channelId);
+ if (ackMessageId !== undefined) {
+ return compareMessageIds(ackMessageId, lastMessageId) < 0;
+ }
+ if (guildId === null) {
+ return true;
+ }
+ const joinedAt = this.guildDetails.get(guildId)?.joinedAt ?? null;
+ return joinedAt !== null && joinedAt < snowflakeTimestamp(lastMessageId);
+ }
+
private storeRelationship(value: unknown): void {
const relationship = asRecord(value);
const userId = asString(relationship?.id);
@@ -435,7 +715,7 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
return;
}
if (!this.blockedUserIds.has(userId) && this.blockedUserIds.size >= MAX_BACKGROUND_READ_STATE_USERS) {
- throw new BackgroundReadStateCapacityError('users', MAX_BACKGROUND_READ_STATE_USERS);
+ throw new ReadStateCapacityExceeded('users', MAX_BACKGROUND_READ_STATE_USERS);
}
this.blockedUserIds.add(userId);
}
@@ -454,10 +734,7 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
return;
}
const authorId = asString(asRecord(message.author)?.id);
- if (authorId === this.userId) {
- this.setMentionCount(channelId, 0);
- return;
- }
+ const messageId = asString(message.id);
const guildId = asString(message.guild_id);
if (guildId !== null) {
this.linkChannel(guildId, channelId);
@@ -465,22 +742,39 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
if (!this.channelDetails.has(channelId)) {
this.revealChannel(channelId, {});
}
+ if (authorId === this.userId) {
+ if (messageId !== null) {
+ this.recordLastMessage(channelId, messageId);
+ this.recordAck(channelId, messageId);
+ }
+ this.setMentionCount(channelId, 0);
+ return;
+ }
+ if (messageId !== null) {
+ this.recordLastMessage(channelId, messageId);
+ }
const muted = this.isGuildOrChannelMuted(guildId, channelId);
const authorBlocked = authorId !== null && this.blockedUserIds.has(authorId);
const mentioned = this.messageMentionsUser(message, guildId, muted, authorBlocked);
+ const channel = this.channelDetails.get(channelId);
+ const notification: BackgroundMessageNotification = {
+ message: message as unknown as WireMessage,
+ guildName: guildId === null ? null : (this.guildDetails.get(guildId)?.name ?? null),
+ channelName: channel?.name ?? null,
+ channelType: channel?.type ?? null,
+ mentioned,
+ gated: this.isChannelGated(channelId, guildId),
+ };
if (mentioned) {
this.setMentionCount(channelId, (this.mentionCounts.get(channelId) ?? 0) + 1);
+ if (messageId !== null) {
+ this.observeMention?.(notification);
+ }
}
if (authorId === null || authorBlocked || muted || !this.shouldNotify(message, guildId, channelId, mentioned)) {
return;
}
- const channel = this.channelDetails.get(channelId);
- this.observeMessageNotification({
- message: message as unknown as WireMessage,
- guildName: guildId === null ? null : (this.guildDetails.get(guildId)?.name ?? null),
- channelName: channel?.name ?? null,
- channelType: channel?.type ?? null,
- });
+ this.observeMessageNotification(notification);
}
private isGuildOrChannelMuted(guildId: string | null, channelId: string): boolean {
@@ -526,6 +820,35 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
return mentioned || guildId === null;
}
+ private resolveUnreadBadges(guildId: string, channelId: string, details: ChannelDetails): number | null {
+ const settings = this.settingsFor(guildId);
+ const guildLevel = isExplicitNotificationLevel(settings.messageNotifications)
+ ? settings.messageNotifications
+ : effectiveGuildMessageNotifications(this.guildDetails.get(guildId));
+ if (guildLevel === MessageNotifications.ALL_MESSAGES || guildLevel === MessageNotifications.NO_MESSAGES) {
+ return guildLevel;
+ }
+ if (!this.unreadBadgeCustomizationEnabled) {
+ return null;
+ }
+ const targetId =
+ details.type !== null && THREAD_CHANNEL_TYPES.has(details.type) && details.parentId !== null
+ ? details.parentId
+ : channelId;
+ const direct = settings.channelOverrides.get(targetId)?.unreadBadges;
+ if (isExplicitNotificationLevel(direct)) {
+ return direct;
+ }
+ const parentId = this.channelDetails.get(targetId)?.parentId ?? null;
+ if (parentId !== null) {
+ const parent = settings.channelOverrides.get(parentId)?.unreadBadges;
+ if (isExplicitNotificationLevel(parent)) {
+ return parent;
+ }
+ }
+ return isExplicitNotificationLevel(settings.unreadBadges) ? settings.unreadBadges : null;
+ }
+
private resolveMessageNotifications(guildId: string | null, channelId: string): number {
const settings = this.settingsFor(guildId ?? ME);
const direct = settings.channelOverrides.get(channelId)?.messageNotifications;
@@ -585,9 +908,14 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
private applyMessageAck(payload: Record<string, unknown>): void {
const channelId = asString(payload.channel_id);
- if (channelId !== null) {
- this.setMentionCount(channelId, asCount(payload.mention_count));
+ if (channelId === null) {
+ return;
}
+ const messageId = asString(payload.message_id);
+ if (messageId !== null) {
+ this.recordAck(channelId, messageId);
+ }
+ this.setMentionCount(channelId, asCount(payload.mention_count));
}
private applyChannelDelete(payload: Record<string, unknown>): void {
@@ -610,6 +938,7 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
this.channelGuilds.delete(channelId);
this.channelDetails.delete(channelId);
this.mentionCounts.delete(channelId);
+ this.forgetChannelReadState(channelId);
cleared.set(channelId, 0);
}
this.guildChannels.delete(guildId);
@@ -621,8 +950,15 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
}
}
+ private forgetChannelReadState(channelId: string): void {
+ this.lastMessageIds.delete(channelId);
+ this.ackMessageIds.delete(channelId);
+ this.unreadChannels.delete(channelId);
+ }
+
private forgetChannel(channelId: string): void {
const guildId = this.channelGuilds.get(channelId);
+ this.forgetChannelReadState(channelId);
this.channelGuilds.delete(channelId);
this.channelDetails.delete(channelId);
if (guildId !== undefined) {
@@ -667,13 +1003,13 @@ export class BackgroundReadStateReducer implements BackgroundSnapshotSink {
private assertChannelCapacity(map: ReadonlyMap<string, unknown>, channelId: string): void {
if (!map.has(channelId) && map.size >= MAX_BACKGROUND_READ_STATE_CHANNELS) {
- throw new BackgroundReadStateCapacityError('channels', MAX_BACKGROUND_READ_STATE_CHANNELS);
+ throw new ReadStateCapacityExceeded('channels', MAX_BACKGROUND_READ_STATE_CHANNELS);
}
}
private assertGuildCapacity(map: ReadonlyMap<string, unknown>, guildId: string): void {
if (!map.has(guildId) && map.size >= MAX_BACKGROUND_READ_STATE_GUILDS) {
- throw new BackgroundReadStateCapacityError('guilds', MAX_BACKGROUND_READ_STATE_GUILDS);
+ throw new ReadStateCapacityExceeded('guilds', MAX_BACKGROUND_READ_STATE_GUILDS);
}
}
}