Add login check

pull/77/head
MillenniumEarl 2021-03-10 10:00:41 +01:00
parent df7141227b
commit fbe2faf53b
4 changed files with 72 additions and 41 deletions

View File

@ -13,6 +13,8 @@ import { DateTime } from "luxon";
import { urls } from "../../constants/url";
import { fetchHTML } from "../../network-helper";
import { GENERIC, MEMBER } from "../../constants/css-selector";
import shared from "../../shared";
import { UserNotLogged, USER_NOT_LOGGED } from "../errors";
/**
* Represents a generic user registered on the platform.
@ -144,6 +146,9 @@ export default class PlatformUser {
}
public async fetch(): Promise<void> {
// Check login
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Check ID
if (!this.id && this.id < 1) throw new Error("Invalid user ID");
@ -152,10 +157,17 @@ export default class PlatformUser {
// Fetch the page
const htmlResponse = await fetchHTML(url);
const result = htmlResponse.applyOnSuccess(this.elaborateResponse);
if (result.isFailure()) throw htmlResponse.value;
}
if (htmlResponse.isSuccess()) {
//#endregion Public methods
//#region Private methods
private elaborateResponse(html: string): void {
// Prepare cheerio
const $ = cheerio.load(htmlResponse.value);
const $ = cheerio.load(html);
// Check if the profile is private
this._private =
@ -189,8 +201,7 @@ export default class PlatformUser {
const donation = $(MEMBER.AMOUNT_DONATED)?.text().replace("$", "");
this._amountDonated = donation ? parseInt(donation, 10) : 0;
}
} else throw htmlResponse.value;
}
//#endregion Public method
//#endregion Private methods
}

View File

@ -14,6 +14,8 @@ import { IPostElement, parseF95ThreadPost } from "../../scrape-data/post-parse";
import { POST, THREAD } from "../../constants/css-selector";
import { urls } from "../../constants/url";
import { fetchHTML } from "../../network-helper";
import shared from "../../shared";
import { UserNotLogged, USER_NOT_LOGGED } from "../errors";
/**
* Represents a post published by a user on the F95Zone platform.
@ -95,6 +97,9 @@ export default class Post {
* Gets the post data starting from its unique ID for the entire platform.
*/
public async fetch(): Promise<void> {
// Check login
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Fetch HTML page containing the post
const url = new URL(this.id.toString(), urls.POSTS).toString();
const htmlResponse = await fetchHTML(url);

View File

@ -20,10 +20,13 @@ import Shared from "../../shared";
import {
GenericAxiosError,
ParameterError,
UnexpectedResponseContentType
UnexpectedResponseContentType,
UserNotLogged,
USER_NOT_LOGGED
} from "../errors";
import { Result } from "../result";
import { getJSONLD, TJsonLD } from "../../scrape-data/json-ld";
import shared from "../../shared";
/**
* Represents a generic F95Zone platform thread.
@ -237,6 +240,9 @@ export default class Thread {
* Gets information about this thread.
*/
public async fetch(): Promise<void> {
// Check login
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Prepare the url
this._url = new URL(this.id.toString(), urls.THREADS).toString();

View File

@ -14,8 +14,14 @@ import PlatformUser from "./platform-user";
import { urls } from "../../constants/url";
import { GENERIC, WATCHED_THREAD } from "../../constants/css-selector";
import { fetchHTML } from "../../network-helper";
import { GenericAxiosError, UnexpectedResponseContentType } from "../errors";
import {
GenericAxiosError,
UnexpectedResponseContentType,
UserNotLogged,
USER_NOT_LOGGED
} from "../errors";
import { Result } from "../result";
import shared from "../../shared";
// Interfaces
interface IWatchedThread {
@ -88,6 +94,9 @@ export default class UserProfile extends PlatformUser {
//#region Public methods
public async fetch(): Promise<void> {
// Check login
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// First get the user ID and set it
const id = await this.fetchUserID();
super.setID(id);