You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wattpad-deno/src/classes/User.ts

62 lines
2.2 KiB

import Story from "./Story.ts";
import { SearchResults, Session, UserJSON } from "../types.d.ts";
export default class User {
#session: Session;
#searchLimit: number;
username: string;
displayName?: string;
description?: string;
createDate?: Date;
userJSON!: UserJSON;
stories: Story[] = [];
constructor(session: Session, username: string, limit: number = 20) {
this.#session = session;
this.username = username;
this.#searchLimit = limit;
}
async init() {
this.userJSON = await (await this.#session.get(
`/api/v3/users/${this.username}`,
false,
{
params: new URLSearchParams({
fields:
"username,description,avatar,name,email,genderCode,language,birthdate,verified,isPrivate,ambassador,is_staff,follower,following,backgroundUrl,votesReceived,numFollowing,numFollowers,createDate,followerRequest,website,facebook,twitter,followingRequest,numStoriesPublished,numLists,location,externalId,programs,showSocialNetwork,verified_email,has_accepted_latest_tos,email_reverification_status,highlight_colour,safety(isMuted,isBlocked),has_writer_subscription",
}),
},
))
.json() as UserJSON;
this.displayName = this.userJSON.name;
this.description = this.userJSON.description;
this.createDate = new Date(this.userJSON.createDate);
}
async updateStories(pageNum: number) {
const res = await (await this.#session.get(
`/v4/users/${this.username}/stories/published`,
false,
{
params: new URLSearchParams({
fields: "stories(id)",
limit: this.#searchLimit.toString(),
offset: (pageNum * this.#searchLimit).toString(),
mature: "1",
}),
},
)).json() as SearchResults;
for (let i = 0; i < res.stories.length; i++) {
const story = new Story(
res.stories[i].id,
this.#session,
);
this.stories.push(story);
}
}
}