From 719d9c5f0bcc8d67e8f271db6ae5a66cd414a4b1 Mon Sep 17 00:00:00 2001 From: izzy Date: Thu, 16 Oct 2025 17:28:53 +0100 Subject: [PATCH] fix: ensure `lastMessageId` is updated on incoming messages --- src/events/v1.ts | 174 ++++++++++++++++++++++++----------------------- 1 file changed, 88 insertions(+), 86 deletions(-) diff --git a/src/events/v1.ts b/src/events/v1.ts index 069bc729..1764bb6e 100644 --- a/src/events/v1.ts +++ b/src/events/v1.ts @@ -42,21 +42,21 @@ export type ProtocolV1 = { type ClientMessage = | { type: "Authenticate"; token: string } | { - type: "BeginTyping"; - channel: string; - } + type: "BeginTyping"; + channel: string; + } | { - type: "EndTyping"; - channel: string; - } + type: "EndTyping"; + channel: string; + } | { - type: "Ping"; - data: number; - } + type: "Ping"; + data: number; + } | { - type: "Pong"; - data: number; - }; + type: "Pong"; + data: number; + }; /** * Messages sent from the server @@ -70,46 +70,46 @@ type ServerMessage = | { type: "Pong"; data: number } | ({ type: "Message" } & Message) | { - type: "MessageUpdate"; - id: string; - channel: string; - data: Partial; - } + type: "MessageUpdate"; + id: string; + channel: string; + data: Partial; + } | { - type: "MessageAppend"; - id: string; - channel: string; - append: Pick, "embeds">; - } + type: "MessageAppend"; + id: string; + channel: string; + append: Pick, "embeds">; + } | { type: "MessageDelete"; id: string; channel: string } | { - type: "MessageReact"; - id: string; - channel_id: string; - user_id: string; - emoji_id: string; - } + type: "MessageReact"; + id: string; + channel_id: string; + user_id: string; + emoji_id: string; + } | { - type: "MessageUnreact"; - id: string; - channel_id: string; - user_id: string; - emoji_id: string; - } + type: "MessageUnreact"; + id: string; + channel_id: string; + user_id: string; + emoji_id: string; + } | { - type: "MessageRemoveReaction"; - id: string; - channel_id: string; - emoji_id: string; - } + type: "MessageRemoveReaction"; + id: string; + channel_id: string; + emoji_id: string; + } | { type: "BulkMessageDelete"; channel: string; ids: string[] } | ({ type: "ChannelCreate" } & Channel) | { - type: "ChannelUpdate"; - id: string; - data: Partial; - clear?: FieldsChannel[]; - } + type: "ChannelUpdate"; + id: string; + data: Partial; + clear?: FieldsChannel[]; + } | { type: "ChannelDelete"; id: string } | { type: "ChannelGroupJoin"; id: string; user: string } | { type: "ChannelGroupLeave"; id: string; user: string } @@ -117,62 +117,62 @@ type ServerMessage = | { type: "ChannelStopTyping"; id: string; user: string } | { type: "ChannelAck"; id: string; user: string; message_id: string } | { - type: "ServerCreate"; - id: string; - server: Server; - channels: Channel[]; - } + type: "ServerCreate"; + id: string; + server: Server; + channels: Channel[]; + } | { - type: "ServerUpdate"; - id: string; - data: Partial; - clear?: FieldsServer[]; - } + type: "ServerUpdate"; + id: string; + data: Partial; + clear?: FieldsServer[]; + } | { type: "ServerDelete"; id: string } | { - type: "ServerMemberUpdate"; - id: MemberCompositeKey; - data: Partial; - clear?: FieldsMember[]; - } + type: "ServerMemberUpdate"; + id: MemberCompositeKey; + data: Partial; + clear?: FieldsMember[]; + } | { type: "ServerMemberJoin"; id: string; user: string } | { type: "ServerMemberLeave"; id: string; user: string } | { - type: "ServerRoleUpdate"; - id: string; - role_id: string; - data: Partial; - } + type: "ServerRoleUpdate"; + id: string; + role_id: string; + data: Partial; + } | { type: "ServerRoleDelete"; id: string; role_id: string } | { - type: "UserUpdate"; - id: string; - data: Partial; - clear?: FieldsUser[]; - } + type: "UserUpdate"; + id: string; + data: Partial; + clear?: FieldsUser[]; + } | { type: "UserRelationship"; user: User; status: RelationshipStatus } | { type: "UserPresence"; id: string; online: boolean } | { - type: "UserSettingsUpdate"; - id: string; - update: { [key: string]: [number, string] }; - } + type: "UserSettingsUpdate"; + id: string; + update: { [key: string]: [number, string] }; + } | { type: "UserPlatformWipe"; user_id: string; flags: number } | ({ type: "EmojiCreate" } & Emoji) | { type: "EmojiDelete"; id: string } | ({ - type: "Auth"; - } & ( + type: "Auth"; + } & ( | { - event_type: "DeleteSession"; - user_id: string; - session_id: string; - } + event_type: "DeleteSession"; + user_id: string; + session_id: string; + } | { - event_type: "DeleteAllSessions"; - user_id: string; - exclude_session_id: string; - } + event_type: "DeleteAllSessions"; + user_id: string; + exclude_session_id: string; + } )); /** @@ -277,13 +277,15 @@ export async function handleEvent( client.messages.getOrCreate(event._id, event, true); + const channel = client.channels.get(event.channel); + if (!channel) return; + + client.channels.updateUnderlyingObject(channel.id, "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(