diff --git a/src/classes/ChannelUnread.ts b/src/classes/ChannelUnread.ts new file mode 100644 index 00000000..b41f252d --- /dev/null +++ b/src/classes/ChannelUnread.ts @@ -0,0 +1,33 @@ +import { ChannelUnreadCollection } from "../collections"; + +/** + * Channel Unread Class + */ +export class ChannelUnread { + readonly #collection: ChannelUnreadCollection; + readonly id: string; + + /** + * Construct Channel + * @param collection Collection + * @param id Channel Id + */ + constructor(collection: ChannelUnreadCollection, id: string) { + this.#collection = collection; + this.id = id; + } + + /** + * Last read message id + */ + get lastMessageId() { + return this.#collection.getUnderlyingObject(this.id).lastMessageId; + } + + /** + * List of message IDs that we were mentioned in + */ + get messageMentionIds() { + return this.#collection.getUnderlyingObject(this.id).lastMessageId; + } +} diff --git a/src/collections/ChannelUnreadCollection.ts b/src/collections/ChannelUnreadCollection.ts new file mode 100644 index 00000000..d7eb34fa --- /dev/null +++ b/src/collections/ChannelUnreadCollection.ts @@ -0,0 +1,25 @@ +import { API } from ".."; +import { ChannelUnread } from "../classes/ChannelUnread"; +import { HydratedChannelUnread } from "../hydration"; + +import { ClassCollection } from "."; + +export class ChannelUnreadCollection extends ClassCollection< + ChannelUnread, + HydratedChannelUnread +> { + /** + * Get or create + * @param id Id + * @param data Data + */ + getOrCreate(id: string, data: API.ChannelUnread) { + if (this.has(id)) { + return this.get(id)!; + } else { + const instance = new ChannelUnread(this, id); + this.create(id, "channelUnread", instance, data); + return instance; + } + } +} diff --git a/src/collections/index.ts b/src/collections/index.ts index aa6cddb9..a2feffb4 100644 --- a/src/collections/index.ts +++ b/src/collections/index.ts @@ -15,6 +15,7 @@ export class ClassCollection extends StoreCollection { } export { ChannelCollection } from "./ChannelCollection"; +export { ChannelUnreadCollection } from "./ChannelUnreadCollection"; export { EmojiCollection } from "./EmojiCollection"; export { MessageCollection } from "./MessageCollection"; export { ServerCollection } from "./ServerCollection"; diff --git a/src/hydration/channelUnread.ts b/src/hydration/channelUnread.ts new file mode 100644 index 00000000..a8de27bb --- /dev/null +++ b/src/hydration/channelUnread.ts @@ -0,0 +1,31 @@ +import { ReactiveSet } from "@solid-primitives/set"; + +import { API } from ".."; +import type { Merge } from "../lib/merge"; + +import { Hydrate } from "."; + +export type HydratedChannelUnread = { + id: string; + lastMessageId?: string; + messageMentionIds: ReactiveSet; +}; + +export const channelUnreadHydration: Hydrate< + Merge, + HydratedChannelUnread +> = { + keyMapping: { + _id: "id", + last_id: "lastMessageId", + mentions: "messageMentionIds", + }, + functions: { + id: (unread) => unread._id.channel, + lastMessageId: (unread) => unread.last_id!, + messageMentionIds: (unread) => new ReactiveSet(unread.mentions!), + }, + initialHydration: () => ({ + messageMentionIds: new ReactiveSet(), + }), +}; diff --git a/src/hydration/index.ts b/src/hydration/index.ts index 45e910b7..e3e424d4 100644 --- a/src/hydration/index.ts +++ b/src/hydration/index.ts @@ -1,4 +1,5 @@ import { channelHydration } from "./channel"; +import { channelUnreadHydration } from "./channelUnread"; import { emojiHydration } from "./emoji"; import { messageHydration } from "./message"; import { serverHydration } from "./server"; @@ -6,6 +7,7 @@ import { serverMemberHydration } from "./serverMember"; import { userHydration } from "./user"; export type { HydratedChannel } from "./channel"; +export type { HydratedChannelUnread } from "./channelUnread"; export type { HydratedEmoji } from "./emoji"; export type { HydratedMessage } from "./message"; export type { HydratedServer } from "./server"; @@ -64,6 +66,7 @@ function hydrateInternal( const hydrators = { channel: channelHydration, + channelUnread: channelUnreadHydration, emoji: emojiHydration, message: messageHydration, server: serverHydration,