This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add separate controls for MyUser
BREAKING CHANGE: `Client.users.fetchMe()` is now `Client.me.fetch()`.
- Loading branch information
1 parent
5a1274b
commit 57adf74
Showing
10 changed files
with
198 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { Client } from "../../../client"; | ||
import type { Comment } from "../../comment/object"; | ||
import type { Listing } from "../../listing/listing"; | ||
import type { Post } from "../../post/object"; | ||
import type { PostSort } from "../../post/types"; | ||
import type { Subreddit } from "../../subreddit/object"; | ||
import type { RedditObject } from "../../types"; | ||
import type { MyUserData } from "../my-user/object"; | ||
import type { OtherUserData } from "../other-user/object"; | ||
import type { UserItemsSort } from "../types"; | ||
import type { User, UserData } from "./object"; | ||
|
||
import { BaseControls } from "../../base-controls"; | ||
import { CommentListing } from "../../comment/listing/listing"; | ||
import { fakeListingAfter } from "../../listing/util"; | ||
import { PostListing } from "../../post/listing"; | ||
import { assertKind, fromRedditData } from "../../util"; | ||
import { MyUser } from "../my-user/object"; | ||
import { OtherUser } from "../other-user/object"; | ||
|
||
/** | ||
* The base controls for interacting with users. | ||
* | ||
* @category Controls | ||
*/ | ||
export class BaseUserControls extends BaseControls { | ||
/** @internal */ | ||
constructor(client: Client) { | ||
super(client, "u/"); | ||
} | ||
|
||
/** | ||
* Fetch a user's subreddit. | ||
* | ||
* @param username The user who's subreddit to fetch. | ||
* | ||
* @returns A promise that resolves to the user's subreddit. | ||
*/ | ||
async fetchSubreddit(username: string): Promise<Subreddit> { | ||
return this.client.subreddits.fetch(`u_${username}`); | ||
} | ||
|
||
/** | ||
* Get a Listing of all the posts a user has made. | ||
* | ||
* @param username The user to get posts from. | ||
* @param sort How to sort the posts. | ||
* | ||
* @returns A sorted Listing of posts. | ||
*/ | ||
getPosts(username: string, sort: PostSort = "new"): Listing<Post> { | ||
const request = { url: `user/${username}/submitted`, query: { sort } }; | ||
const context = { request, client: this.client }; | ||
return new PostListing(fakeListingAfter(""), context); | ||
} | ||
|
||
/** | ||
* Get a Listing of all the comments a user has made. | ||
* | ||
* @param username The user to get comments from. | ||
* @param sort How to sort the comments. | ||
* | ||
* @returns A sorted Listing of comments. | ||
*/ | ||
getSortedComments( | ||
username: string, | ||
sort: UserItemsSort = "new" | ||
): Listing<Comment> { | ||
const request = { url: `user/${username}/comments`, query: { sort } }; | ||
const context = { request, client: this.client }; | ||
return new CommentListing(fakeListingAfter(""), context); | ||
} | ||
|
||
/** @internal */ | ||
fromRaw(raw: RedditObject): User { | ||
assertKind("t2", raw); | ||
|
||
const rDat = raw.data; | ||
const data: UserData = fromRedditData(rDat); | ||
|
||
// "coins" is only set in the personal user response. | ||
return "coins" in rDat | ||
? new MyUser(this.client.me, data as MyUserData) | ||
: new OtherUser(this.client.users, data as OtherUserData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Data } from "../../../helper/types"; | ||
import type { RedditObject } from "../../types"; | ||
import type { MyUser } from "./object"; | ||
|
||
import { BaseUserControls } from "../base/controls"; | ||
|
||
/** | ||
* Various methods to allow you to interact with the authorized user. | ||
* | ||
* @category Controls | ||
*/ | ||
export class MyUserControls extends BaseUserControls { | ||
/** | ||
* Fetch the details of the authorized user. | ||
* | ||
* @returns The user. | ||
*/ | ||
async fetch(): Promise<MyUser> { | ||
const userData: Data = await this.gateway.get("api/v1/me"); | ||
// /me doesn't return a wrapped object, so we have to make it ourselves. | ||
const raw: RedditObject = { kind: "t2", data: userData }; | ||
return this.client.users.fromRaw(raw) as MyUser; | ||
} | ||
} |
Oops, something went wrong.