From 6fee42285ce57f70e2ebbc5a2d645960c776419f Mon Sep 17 00:00:00 2001 From: metaphor Date: Wed, 17 Aug 2022 18:07:10 -0300 Subject: [PATCH 1/2] test(user): get favorites --- test/users.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/users.ts b/test/users.ts index dc7030d..b7c97e8 100644 --- a/test/users.ts +++ b/test/users.ts @@ -88,8 +88,20 @@ describe("Users", function() { // @TODO Get Upload Limit Test it.skip("get upload limit", async function() {}); - // @TODO Get Favorites Test - it.skip("get favorites", async function() {}); + it("get favorites", async function() { + const limit = 5; + const favorites = await E6Client.users.getFavorites({ id: 323290, limit }); + expect(favorites.length).to.not.equal(0, "get favorites returned zero results"); + expect(favorites.length).to.lte(limit, "get favorites not respecting limit"); + }); + + it("get favorites from user object", async function() { + const limit = 5; + const user = await E6Client.users.get(323290); + const favorites = await user?.getFavorites({ limit }) || []; + expect(favorites.length).to.not.equal(0, "get favorites from user returned zero results"); + expect(favorites.length).to.lte(limit, "get favorites not respecting limit"); + }); // @TODO Add Favorite Test it.skip("add favorite", async function() {}); From 692213326f8342367643cbf581c01f3e8e191caa Mon Sep 17 00:00:00 2001 From: metaphor Date: Wed, 17 Aug 2022 18:08:16 -0300 Subject: [PATCH 2/2] feat(user): getFavorites() with options --- src/modules/Users.ts | 19 ++++++++++++++----- src/structures/User.ts | 8 ++++++-- src/types/users.d.ts | 4 ++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/modules/Users.ts b/src/modules/Users.ts index 81c55f6..1879715 100755 --- a/src/modules/Users.ts +++ b/src/modules/Users.ts @@ -11,7 +11,8 @@ import type { SearchUsersOrder, Timezones, DefaultImageSize, - PostProperties + PostProperties, + GetFavoritesOptions } from "../types"; import FormHelper from "../util/FormHelper"; import { APIError } from "../util/RequestHandler"; @@ -197,14 +198,22 @@ export default class Users { /** * Get the favorites of a user * - * @param {number} [id] - the id of the user to get favorites for, or none to get the authenticated user, if provided + * @param {object} [options] + * @param {number} [options.id] - the id of the user to get favorites for, or none to get the authenticated user, if provided + * @param {number} [options.page] - page of results to get + * @param {number} [options.limit] - limit the maximum amount of results returned */ - async getFavorites(id?: number) { - if (!id) { + async getFavorites(options?: GetFavoritesOptions) { + options = options ?? {}; + if (!options.id) { const auth = this.main.request.authCheck.call(this, "Users#getFavorites", false); if (!auth) throw new Error("calling Users#getFavorites without an id requires authentication"); } - const res = await this.main.request.get<{ posts: Array; }>(`/favorites.json${!id ? "" : `?user_id=${id}`}`); + const qs = new FormHelper(); + if (typeof options.id !== "undefined") qs.add("user_id", options.id); + if (typeof options.page !== "undefined") qs.add("page", options.page); + if (typeof options.limit === "number" || typeof options.limit === "string") qs.add("limit", options.limit); + const res = await this.main.request.get<{ posts: Array; }>(`/favorites.json?${qs.build()}`); return res!.posts.map(info => new Post(this.main, info)); } diff --git a/src/structures/User.ts b/src/structures/User.ts index a8b51f0..7e4188e 100755 --- a/src/structures/User.ts +++ b/src/structures/User.ts @@ -5,7 +5,8 @@ import type { SearchUserFeedbackOptions, CreateUserFeedbackOptions, UserFeedbackProperties, - FeedbackCategories + FeedbackCategories, + GenericSearchOptions } from "../types"; export default class User implements UserProperties { @@ -76,9 +77,12 @@ export default class User implements UserProperties { /** * Get this users favorites * + * @param {object} [options] + * @param {number} [options.page] - page of results to get + * @param {number} [options.limit] - limit the maximum amount of results returned * @returns {Promise>} */ - async getFavorites() { return this.main.users.getFavorites(this.id); } + async getFavorites(options?: GenericSearchOptions) { return this.main.users.getFavorites({ id: this.id, ...options }); } async asAuthenticatedUser() { this.main.request.authCheck("User#asAuthenticatedUser"); diff --git a/src/types/users.d.ts b/src/types/users.d.ts index 9579ea2..54faf80 100755 --- a/src/types/users.d.ts +++ b/src/types/users.d.ts @@ -275,3 +275,7 @@ export interface EditSelfUserOptions { disable_responsive_mode?: boolean; custom_css_style?: string; } + +export interface GetFavoritesOptions extends GenericSearchOptions { + id?: number; +}