mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-21 04:25:27 -04:00
feat: create ChannelUnread class
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
@@ -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(),
|
||||
}),
|
||||
};
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user