mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-21 04:25:27 -04:00
52 lines
1.1 KiB
TypeScript
52 lines
1.1 KiB
TypeScript
import { API, User } from "..";
|
|
import { HydratedUser } from "../hydration";
|
|
|
|
import { ClassCollection } from ".";
|
|
|
|
export class UserCollection extends ClassCollection<User, HydratedUser> {
|
|
/**
|
|
* Fetch user by ID
|
|
* @param id Id
|
|
* @returns User
|
|
*/
|
|
async fetch(id: string): Promise<User> {
|
|
const user = this.get(id);
|
|
if (user) return user;
|
|
const data = await this.client.api.get(`/users/${id as ""}`);
|
|
return this.getOrCreate(data._id, data);
|
|
}
|
|
|
|
/**
|
|
* Get or create
|
|
* @param id Id
|
|
* @param data Data
|
|
* @param isNew Whether this object is new
|
|
*/
|
|
getOrCreate(id: string, data: API.User) {
|
|
if (this.has(id)) {
|
|
return this.get(id)!;
|
|
} else {
|
|
const instance = new User(this, id);
|
|
this.create(id, "user", instance, data);
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get or return partial
|
|
* @param id Id
|
|
*/
|
|
getOrPartial(id: string) {
|
|
if (this.has(id)) {
|
|
return this.get(id)!;
|
|
} else if (this.client.options.partials) {
|
|
const instance = new User(this, id);
|
|
this.create(id, "user", instance, {
|
|
id,
|
|
partial: true,
|
|
});
|
|
return instance;
|
|
}
|
|
}
|
|
}
|