From daab20dbac8f8ddc3533b45aae062c86b2119b95 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 4 Jun 2024 22:22:04 -0400 Subject: [PATCH] fix: fetch should fill out partial objects --- src/collections/ChannelCollection.ts | 4 ++-- src/collections/EmojiCollection.ts | 4 ++-- src/collections/MessageCollection.ts | 4 ++-- src/collections/ServerCollection.ts | 4 ++-- src/collections/ServerMemberCollection.ts | 13 +++++++++++-- src/collections/UserCollection.ts | 4 ++-- src/storage/ObjectStorage.ts | 1 + 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/collections/ChannelCollection.ts b/src/collections/ChannelCollection.ts index 753ac087..26e2f063 100644 --- a/src/collections/ChannelCollection.ts +++ b/src/collections/ChannelCollection.ts @@ -27,7 +27,7 @@ export class ChannelCollection extends ClassCollection< */ async fetch(id: string): Promise { const channel = this.get(id); - if (channel) return channel; + if (channel && !this.isPartial(id)) return channel; const data = await this.client.api.get(`/channels/${id as ""}`); return this.getOrCreate(data._id, data); } @@ -39,7 +39,7 @@ export class ChannelCollection extends ClassCollection< * @param isNew Whether this object is new */ getOrCreate(id: string, data: API.Channel, isNew = false) { - if (this.has(id)) { + if (this.has(id) && !this.isPartial(id)) { return this.get(id)!; } else { const instance = new Channel(this, id); diff --git a/src/collections/EmojiCollection.ts b/src/collections/EmojiCollection.ts index f9ca975a..07eff0e4 100644 --- a/src/collections/EmojiCollection.ts +++ b/src/collections/EmojiCollection.ts @@ -14,7 +14,7 @@ export class EmojiCollection extends ClassCollection { */ async fetch(id: string): Promise { const emoji = this.get(id); - if (emoji) return emoji; + if (emoji && !this.isPartial(id)) return emoji; const data = await this.client.api.get(`/custom/emoji/${id as ""}`); return this.getOrCreate(data._id, data); } @@ -26,7 +26,7 @@ export class EmojiCollection extends ClassCollection { * @param isNew Whether this object is new */ getOrCreate(id: string, data: API.Emoji, isNew = false) { - if (this.has(id)) { + if (this.has(id) && !this.isPartial(id)) { return this.get(id)!; } else { const instance = new Emoji(this, id); diff --git a/src/collections/MessageCollection.ts b/src/collections/MessageCollection.ts index b46d8c0b..f121254c 100644 --- a/src/collections/MessageCollection.ts +++ b/src/collections/MessageCollection.ts @@ -18,7 +18,7 @@ export class MessageCollection extends ClassCollection< */ async fetch(channelId: string, messageId: string): Promise { const message = this.get(messageId); - if (message) return message; + if (message && !this.isPartial(messageId)) return message; const data = await this.client.api.get( `/channels/${channelId as ""}/messages/${messageId as ""}` @@ -34,7 +34,7 @@ export class MessageCollection extends ClassCollection< * @param isNew Whether this object is new */ getOrCreate(id: string, data: API.Message, isNew = false) { - if (this.has(id)) { + if (this.has(id) && !this.isPartial(id)) { return this.get(id)!; } else { const instance = new Message(this, id); diff --git a/src/collections/ServerCollection.ts b/src/collections/ServerCollection.ts index 701c5292..144a2995 100644 --- a/src/collections/ServerCollection.ts +++ b/src/collections/ServerCollection.ts @@ -20,7 +20,7 @@ export class ServerCollection extends ClassCollection { */ async fetch(id: string): Promise { const server = this.get(id); - if (server) return server; + if (server && !this.isPartial(id)) return server; const data = await this.client.api.get(`/servers/${id as ""}`, { include_channels: true, }); @@ -44,7 +44,7 @@ export class ServerCollection extends ClassCollection { * @param isNew Whether this object is new */ getOrCreate(id: string, data: API.Server, isNew = false) { - if (this.has(id)) { + if (this.has(id) && !this.isPartial(id)) { return this.get(id)!; } else { const instance = new Server(this, id); diff --git a/src/collections/ServerMemberCollection.ts b/src/collections/ServerMemberCollection.ts index 958e45e8..4c119bd0 100644 --- a/src/collections/ServerMemberCollection.ts +++ b/src/collections/ServerMemberCollection.ts @@ -28,6 +28,15 @@ export class ServerMemberCollection extends ClassCollection< return super.get(id.server + id.user); } + /** + * check partial status by composite key + * @param id Id + * @returns Member + */ + isPartialByKey(id: API.MemberCompositeKey) { + return super.isPartial(id.server + id.user); + } + /** * Fetch server member by Id * @param serverId Server Id @@ -36,7 +45,7 @@ export class ServerMemberCollection extends ClassCollection< */ async fetch(serverId: string, userId: string): Promise { const member = this.get(serverId + userId); - if (member) return member; + if (member && !this.isPartial(serverId + userId)) return member; const data = (await this.client.api.get( `/servers/${serverId as ""}/members/${userId as ""}`, @@ -55,7 +64,7 @@ export class ServerMemberCollection extends ClassCollection< * @param data Data */ getOrCreate(id: API.MemberCompositeKey, data: API.Member) { - if (this.hasByKey(id)) { + if (this.hasByKey(id) && !this.isPartialByKey(id)) { return this.getByKey(id)!; } else { const instance = new ServerMember(this, id); diff --git a/src/collections/UserCollection.ts b/src/collections/UserCollection.ts index 879e89e7..777cfb19 100644 --- a/src/collections/UserCollection.ts +++ b/src/collections/UserCollection.ts @@ -28,7 +28,7 @@ export class UserCollection extends ClassCollection { */ async fetch(id: string): Promise { const user = this.get(id); - if (user) return user; + if (user && !this.isPartial(id)) return user; const data = await this.client.api.get(`/users/${id as ""}`); return this.getOrCreate(data._id, data); } @@ -40,7 +40,7 @@ export class UserCollection extends ClassCollection { * @param isNew Whether this object is new */ getOrCreate(id: string, data: API.User) { - if (this.has(id)) { + if (this.has(id) && !this.isPartial(id)) { return this.get(id)!; } else { const instance = new User(this, id); diff --git a/src/storage/ObjectStorage.ts b/src/storage/ObjectStorage.ts index c0fd8977..a8d3cebe 100644 --- a/src/storage/ObjectStorage.ts +++ b/src/storage/ObjectStorage.ts @@ -37,6 +37,7 @@ export class ObjectStorage { */ hydrate(id: string, type: keyof Hydrators, context: unknown, data?: unknown) { if (data) { + data = { partial: false, ...data }; this.set(id, hydrate(type, data as never, context, true) as T); } }