feat: create ChannelUnread class

This commit is contained in:
Paul Makles
2023-04-09 15:05:35 +01:00
parent 5ce43e0ef8
commit 3c48a79980
5 changed files with 93 additions and 0 deletions
+33
View File
@@ -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;
}
}
@@ -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;
}
}
}
+1
View File
@@ -15,6 +15,7 @@ export class ClassCollection<T, V> extends StoreCollection<T, V> {
}
export { ChannelCollection } from "./ChannelCollection";
export { ChannelUnreadCollection } from "./ChannelUnreadCollection";
export { EmojiCollection } from "./EmojiCollection";
export { MessageCollection } from "./MessageCollection";
export { ServerCollection } from "./ServerCollection";
+31
View File
@@ -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<string>;
};
export const channelUnreadHydration: Hydrate<
Merge<API.ChannelUnread>,
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(),
}),
};
+3
View File
@@ -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<Input extends object, Output>(
const hydrators = {
channel: channelHydration,
channelUnread: channelUnreadHydration,
emoji: emojiHydration,
message: messageHydration,
server: serverHydration,