From ededed1cc8332c5c271f5b9dcd369fdef97a8071 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Wed, 3 Mar 2021 20:18:01 +0100 Subject: [PATCH] Re-export classes, fix imports and manage session --- src/index.ts | 133 +++++++++++++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 57 deletions(-) diff --git a/src/index.ts b/src/index.ts index e205f02..30ef426 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import shared from "./scripts/shared.js"; import search from "./scripts/search.js"; import { authenticate, urlExists, isF95URL } from "./scripts/network-helper.js"; -import { getUserData as retrieveUserData } from "./scripts/scrape-data/scrape-user.js"; import fetchLatestHandiworkURLs from "./scripts/fetch-data/fetch-latest.js"; import fetchPlatformData from "./scripts/fetch-data/fetch-platform-data.js"; import getHandiworkInformation from "./scripts/scrape-data/handiwork-parse.js"; @@ -13,21 +12,34 @@ import { IBasic } from "./scripts/interfaces.js"; // Classes from file import Credentials from "./scripts/classes/credentials.js"; import LoginResult from "./scripts/classes/login-result.js"; -import UserData from "./scripts/classes/user-data.js"; +import UserProfile from "./scripts/classes/mapping/user-profile.js"; import LatestSearchQuery from "./scripts/classes/query/latest-search-query.js"; import HandiworkSearchQuery from "./scripts/classes/query/handiwork-search-query.js"; import HandiWork from "./scripts/classes/handiwork/handiwork.js"; +import { UserNotLogged } from "./scripts/classes/errors.js"; //#region Global variables + const USER_NOT_LOGGED = "User not authenticated, unable to continue"; + //#endregion -//#region Export classes -// module.exports.GameInfo = GameInfo; -// module.exports.LoginResult = LoginResult; -// module.exports.UserData = UserData; -// module.exports.PrefixParser = PrefixParser; -//#endregion Export classes +//#region Re-export classes +export { default as Animation } from "./scripts/classes/handiwork/animation.js"; +export { default as Asset } from "./scripts/classes/handiwork/asset.js"; +export { default as Comic } from "./scripts/classes/handiwork/comic.js"; +export { default as Game } from "./scripts/classes/handiwork/game.js"; +export { default as Handiwork } from "./scripts/classes/handiwork/handiwork.js"; + +export { default as PlatformUser } from "./scripts/classes/mapping/platform-user.js"; +export { default as Post } from "./scripts/classes/mapping/post.js"; +export { default as Thread } from "./scripts/classes/mapping/thread.js"; +export { default as UserProfile } from "./scripts/classes/mapping/user-profile.js"; + +export { default as HandiworkSearchQuery } from "./scripts/classes/query/handiwork-search-query.js"; +export { default as LatestSearchQuery } from "./scripts/classes/query/latest-search-query.js"; +export { default as ThreadSearchQuery } from "./scripts/classes/query/thread-search-query.js"; +//#endregion Re-export classes //#region Export properties /** @@ -40,24 +52,32 @@ shared.logger.level = "warn"; // By default log only the warn messages /** * Indicates whether a user is logged in to the F95Zone platform or not. */ -export function isLogged(): boolean { - return shared.isLogged; -}; +export function isLogged(): boolean { return shared.isLogged; }; //#endregion Export properties //#region Export methods + /** * Log in to the F95Zone platform. + * * This **must** be the first operation performed before accessing any other script functions. - * @returns {Promise} Result of the operation */ export async function login(username: string, password: string): Promise { - /* istanbul ignore next */ - if (shared.isLogged) { - shared.logger.info(`${username} already authenticated`); - return new LoginResult(true, `${username} already authenticated`); + // Try to load a previous session + await shared.session.load(); + + // If the session is valid, return + if (shared.session.isValid(username, password)) { + shared.logger.info(`Loading previous session for ${username}`); + + // Load platform data + await fetchPlatformData(); + + shared.setIsLogged(true); + return new LoginResult(true, `${username} already authenticated (session)`); } + // Creating credentials and fetch unique platform token shared.logger.trace("Fetching token..."); const creds = new Credentials(username, password); await creds.fetchToken(); @@ -66,15 +86,16 @@ export async function login(username: string, password: string): Promise // Local variables let hasUpdate = false; - /* istanbul ignore next */ - if (!shared.isLogged) { - shared.logger.warn(USER_NOT_LOGGED); - return false; - } + // Check if the user is logged + if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED); // F95 change URL at every game update, // so if the URL is different an update is available @@ -108,35 +126,28 @@ export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise }; /** - * Starting from the name, it gets all the information about the game you are looking for. + * Search for one or more handiworks identified by a specific query. + * * You **must** be logged in to the portal before calling this method. - * @param {String} name Name of the game searched - * @param {Boolean} mod Indicate if you are looking for mods or games - * @returns {Promise} List of information obtained where each item corresponds to - * an identified game (in the case of homonymy of titles) + * + * @param {HandiworkSearchQuery} query Parameters used for the search. + * @param {Number} limit Maximum number of results. Default: 10 */ -export async function getHandiwork(query: HandiworkSearchQuery, limit: number = 30): Promise { - /* istanbul ignore next */ - if (!shared.isLogged) { - shared.logger.warn(USER_NOT_LOGGED); - return null; - } +export async function searchHandiwork(query: HandiworkSearchQuery, limit: number = 10): Promise { + // Check if the user is logged + if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED); return await search(query, limit); }; /** - * Starting from the url, it gets all the information - * about the handiwork requested. + * Given the url, it gets all the information about the handiwork requested. * * You **must** be logged in to the portal before calling this method. */ export async function getHandiworkFromURL(url: string): Promise { - /* istanbul ignore next */ - if (!shared.isLogged) { - shared.logger.warn(USER_NOT_LOGGED); - return null; - } + // Check if the user is logged + if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED); // Check URL validity const exists = await urlExists(url); @@ -149,30 +160,37 @@ export async function getHandiworkFromURL(url: string): Promis /** * Gets the data of the currently logged in user. + * * You **must** be logged in to the portal before calling this method. - * @returns {Promise} Data of the user currently logged in + * + * @returns {Promise} Data of the user currently logged in */ -export async function getUserData(): Promise { - /* istanbul ignore next */ - if (!shared.isLogged) { - shared.logger.warn(USER_NOT_LOGGED); - return null; - } +export async function getUserData(): Promise { + // Check if the user is logged + if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED); - return await retrieveUserData(); + // Create and fetch profile data + const profile = new UserProfile(); + await profile.fetch(); + + return profile; }; /** * Gets the latest updated games that match the specified parameters. + * * You **must** be logged in to the portal before calling this method. - * @param {LatestSearchQuery} query - * Parameters used for the search. - * @param {Number} limit Maximum number of results + * + * @param {LatestSearchQuery} query Parameters used for the search. + * @param {Number} limit Maximum number of results. Default: 10 */ -export async function getLatestUpdates(query: LatestSearchQuery, limit: number): Promise { +export async function getLatestUpdates(query: LatestSearchQuery, limit: number = 10): Promise { // Check limit value if (limit <= 0) throw new Error("limit must be greater than 0"); + // Check if the user is logged + if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED); + // Fetch the results const urls = await fetchLatestHandiworkURLs(query, limit); @@ -180,4 +198,5 @@ export async function getLatestUpdates(query: LatestSearchQuer const promiseList = urls.map((u: string) => getHandiworkInformation(u)); return await Promise.all(promiseList); }; + //#endregion