diff --git a/src/managers/MessageLikeManager.ts b/src/managers/MessageLikeManager.ts index e918192..c65b3d5 100644 --- a/src/managers/MessageLikeManager.ts +++ b/src/managers/MessageLikeManager.ts @@ -1,11 +1,58 @@ import { UserManager } from '..' +import type { Client, User } from '..' interface MessageLikeManagerInterface { + get ids(): string[] get mine(): boolean + fetchUsers(): Promise } export default class MessageLikeManager extends UserManager implements MessageLikeManagerInterface { + private readonly idSet: Set + constructor(client: Client, ids: string[]) { + super(client) + this.idSet = new Set(ids) + } + + _addID(id: string): this { + this.idSet.add(id) + const user = this.client.users.cache.get(id) + if (user) { + this.cache.set(id, user) + } + return this + } + + _deleteID(id: string): this { + this.idSet.delete(id) + this.cache.delete(id) + return this + } + + _patchIDs(ids: string[]): this { + this.idSet.clear() + this.cache.clear() + for (const id of ids) { + this._addID(id) + } + return this + } + + public get ids(): string[] { + const idArray: string[] = [] + this.idSet.forEach(id => idArray.push(id)) + return idArray + } + public get mine(): boolean { - return this.cache.has(this.client.user.id) + return this.idSet.has(this.client.user.id) + } + + public async fetchUsers(): Promise { + const userPromises: Promise[] = [] + // for each id: push a promise of fetching the user, and upserting into this.cache + this.idSet.forEach(id => userPromises.push(this.client.users.fetch(id).then(user => this._upsert(user)))) + const users = Promise.all(userPromises) + return users } } diff --git a/src/structures/Message.ts b/src/structures/Message.ts index a61012d..630aa29 100644 --- a/src/structures/Message.ts +++ b/src/structures/Message.ts @@ -1,6 +1,6 @@ import type { APIChatMessage, APIGroupMessage, DeleteGroupMessageResponse } from 'groupme-api-types' import type { Attachment, Channel, Client, User } from '..' -import { Base } from '..' +import { Base, MessageLikeManager } from '..' interface MessageInterface { fetch(): Promise @@ -18,7 +18,7 @@ export default abstract class Message extends Base implements createdAt: number sourceGuid: string system: boolean - likes: (User | string)[] + likes: MessageLikeManager attachments: Attachment[] constructor(client: Client, channel: T, data: APIGroupMessage | APIChatMessage) { super(client, data.id) @@ -32,7 +32,7 @@ export default abstract class Message extends Base implements this.createdAt = data.created_at this.sourceGuid = data.source_guid this.attachments = data.attachments - this.likes = data.favorited_by?.map(id => client.users.cache.get(id) || id) + this.likes = new MessageLikeManager(this.client, data.favorited_by) this.system = 'system' in data ? data.system : false } @@ -46,8 +46,7 @@ export default abstract class Message extends Base implements if (data.created_at !== undefined) this.createdAt = data.created_at if (data.source_guid !== undefined) this.sourceGuid = data.source_guid if (data.attachments !== undefined) this.attachments = data.attachments - if (data.favorited_by !== undefined) - this.likes = data.favorited_by.map(id => this.client.users.cache.get(id) || id) + if (data.favorited_by !== undefined) this.likes._patchIDs(data.favorited_by) if ('system' in data && data.system !== undefined) this.system = data.system return this diff --git a/src/structures/User.ts b/src/structures/User.ts index 0f06258..05d12af 100644 --- a/src/structures/User.ts +++ b/src/structures/User.ts @@ -5,7 +5,7 @@ import { Base } from '..' interface UserInterface {} export default class User extends Base implements UserInterface { - avatar: string | null + avatar?: string | null name: string constructor(client: Client, data: APIUser) { super(client, String(data.id))