Skip to content

Commit

Permalink
style: code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ACK72 committed Apr 20, 2024
1 parent 11deeee commit 66a2f69
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Kavya ![Generic badge](https://img.shields.io/badge/version-1.3.5-green.svg)
# Kavya ![Generic badge](https://img.shields.io/badge/version-1.3.6-green.svg)
Kavya, A [Kavita](https://www.kavitareader.com/) client extension, for [Paperback](https://paperback.moe/)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "extensions-main",
"repositoryName": "ACK72's Extensions",
"version": "1.3.5",
"version": "1.3.6",
"description": "ACK72's Paperback extension repository",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 0 additions & 2 deletions src/Kavya/CacheManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export class CacheManager {
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
private cachedData: { [key: number]: { time: Date, data: any } };

constructor() {
Expand Down Expand Up @@ -32,7 +31,6 @@ export class CacheManager {
return this.cachedData[key]?.data;
}

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
setCachedData(str: string, data: any) {
const hash = this.getHash(str);
let cacheTime = this.cachedData[hash]?.time ?? new Date();
Expand Down
9 changes: 4 additions & 5 deletions src/Kavya/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export function searchRequestToString(searchQuery: SearchRequest): string {
//
// Kavya Setting State Methods
//
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
export const DEFAULT_VALUES: any = {
kavitaAddress: 'https://demo.kavitareader.com',
kavitaAPIUrl: 'https://demo.kavitareader.com/api',
Expand All @@ -176,7 +175,7 @@ export const DEFAULT_VALUES: any = {
showOnDeck: true,
showRecentlyUpdated: true,
showNewlyAdded: true,
excludeBookTypeLibrary: false,
excludeUnsupportedLibrary: false,

enableRecursiveSearch: false
}
Expand Down Expand Up @@ -212,16 +211,16 @@ export async function getOptions(
showOnDeck: boolean;
showRecentlyUpdated: boolean;
showNewlyAdded: boolean;
excludeBookTypeLibrary: boolean;
excludeUnsupportedLibrary: boolean;
enableRecursiveSearch: boolean;
}> {
const pageSize = (await stateManager.retrieve('pageSize') as number) ?? DEFAULT_VALUES.pageSize;
const showOnDeck = (await stateManager.retrieve('showOnDeck') as boolean) ?? DEFAULT_VALUES.showOnDeck;
const showRecentlyUpdated = (await stateManager.retrieve('showRecentlyUpdated') as boolean) ?? DEFAULT_VALUES.showRecentlyUpdated;
const showNewlyAdded = (await stateManager.retrieve('showNewlyAdded') as boolean) ?? DEFAULT_VALUES.showNewlyAdded;
const excludeBookTypeLibrary = (await stateManager.retrieve('excludeBookTypeLibrary') as boolean) ?? DEFAULT_VALUES.excludeBookTypeLibrary;
const excludeUnsupportedLibrary = (await stateManager.retrieve('excludeUnsupportedLibrary') as boolean) ?? DEFAULT_VALUES.excludeUnsupportedLibrary;

const enableRecursiveSearch = (await stateManager.retrieve('enableRecursiveSearch') as boolean) ?? DEFAULT_VALUES.enableRecursiveSearch;

return { pageSize, showOnDeck, showRecentlyUpdated, showNewlyAdded, excludeBookTypeLibrary, enableRecursiveSearch };
return { pageSize, showOnDeck, showRecentlyUpdated, showNewlyAdded, excludeUnsupportedLibrary, enableRecursiveSearch };
}
35 changes: 10 additions & 25 deletions src/Kavya/Kavya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ import {
import { searchRequest } from './Search';
import { CacheManager } from './CacheManager';

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const sortHelper = (a: any, b: any) => {
if (a.volume === b.volume) return a.chapNum === b.chapNum ? a._index - b._index : a.chapNum - b.chapNum;
return a.volume === 0 || b.volume === 0 ? b.volume - a.volume : a.volume - b.volume;
}

export const KavyaInfo: SourceInfo = {
version: '1.3.5',
version: '1.3.6',
name: 'Kavya',
icon: 'icon.png',
author: 'ACK72',
Expand Down Expand Up @@ -105,8 +104,6 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const response = await this.requestManager.schedule(request, 1);
const result = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
// rome-ignore lint/style/useSingleVarDeclarator: <explanation>
const chapters: any[] = [], specials: any[] = [];

let i = 0;
Expand All @@ -117,14 +114,13 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const title: string = chapter.range.endsWith('.epub') ? chapter.range.slice(0, -5) : chapter.range.slice(0, -4);
const progress: string = chapter.pagesRead === 0 ? '' : chapter.pages === chapter.pagesRead ? '· Read' : `· Reading ${chapter.pagesRead} page`;

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const item: any = {
id: `${chapter.id}`,
mangaId: mangaId,
chapNum: chapter.number === "-100000" ? 1 : (chapter.isSpecial ? j++ : parseFloat(chapter.number)), // chapter.number is 0 when it's a special
chapNum: chapter.number === '-100000' ? 1 : (chapter.isSpecial ? j++ : parseFloat(chapter.number)), // chapter.number is 0 when it's a special
name: chapter.isSpecial ? title : name,
time: new Date(chapter.releaseDate === '0001-01-01T00:00:00' ? chapter.created : chapter.releaseDate),
volume: chapter.isSpecial ? 0 : volume.name === "-100000" ? 0 : parseFloat(volume.name) , // assign both special and chapters w/o volumes w/ volume 0 as it's hidden by paperback
volume: chapter.isSpecial ? 0 : volume.name === '-100000' ? 0 : parseFloat(volume.name) , // assign both special and chapters w/o volumes w/ volume 0 as it's hidden by paperback
group: `${(chapter.isSpecial ? 'Specials · ' : '')}${chapter.pages} pages ${progress}`,
_index: i++,
// sortIndex is unused, as it seems to have an issue when changing the sort order
Expand Down Expand Up @@ -173,7 +169,6 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga

async getSearchResults(
searchQuery: SearchRequest,
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
metadata: any
): Promise<PagedResults> {
return await searchRequest(searchQuery, metadata, this.requestManager, this.interceptor, this.stateManager, this.cacheManager);
Expand All @@ -186,7 +181,7 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
}

const kavitaAPI = await getKavitaAPI(this.stateManager);
const { excludeBookTypeLibrary } = await getOptions(this.stateManager);
const { excludeUnsupportedLibrary } = await getOptions(this.stateManager);

const includeLibraryIds: string[] = [];

Expand All @@ -199,12 +194,11 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const libraryResult = JSON.parse(libraryResponse.data ?? '[]');

for (const library of libraryResult) {
if (excludeBookTypeLibrary && library.type === 2) continue;
if (excludeUnsupportedLibrary && library.type === 2) continue;
includeLibraryIds.push(library.id);
}

const tagNames: string[] = ['genres', 'people', 'tags'];
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const tagSections: any = [];

const promises: Promise<void>[] = [];
Expand All @@ -223,7 +217,6 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const names: string[] = [];
const tags: Tag[] = [];

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
result.forEach(async (item: any) => {
switch (tagName) {
case 'people':
Expand Down Expand Up @@ -270,7 +263,7 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
// We won't use `await this.getKavitaAPI()` as we do not want to throw an error on
// the homepage when server settings are not set
const kavitaAPI = await getKavitaAPI(this.stateManager);
const { showOnDeck, showRecentlyUpdated, showNewlyAdded, excludeBookTypeLibrary } = await getOptions(this.stateManager);
const { showOnDeck, showRecentlyUpdated, showNewlyAdded, excludeUnsupportedLibrary } = await getOptions(this.stateManager);
const pageSize = (await getOptions(this.stateManager)).pageSize / 2;

// The source define two homepage sections: new and latest
Expand Down Expand Up @@ -314,7 +307,7 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const excludeLibraryIds: number[] = [];

for (const library of result) {
if (excludeBookTypeLibrary && library.type === 2) {
if (excludeUnsupportedLibrary && library.type === 2) {
excludeLibraryIds.push(library.id);
continue;
}
Expand All @@ -334,8 +327,6 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
}

for (const section of sections) {
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
// rome-ignore lint/style/useSingleVarDeclarator: <explanation>
let apiPath: string, body: any = {}, id: string = 'id', title: string = 'name';
switch (section.id) {
case 'ondeck':
Expand Down Expand Up @@ -374,7 +365,7 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const tiles: PartialSourceManga[] = [];

for (const series of result) {
if (excludeBookTypeLibrary && excludeLibraryIds.includes(series.libraryId)) continue;
if (excludeUnsupportedLibrary && excludeLibraryIds.includes(series.libraryId)) continue;
tiles.push(App.createPartialSourceManga({
title: series[title],
image: `${kavitaAPI.url}/image/series-cover?seriesId=${series[id]}&apiKey=${kavitaAPI.key}`,
Expand All @@ -396,16 +387,13 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga

async getViewMoreItems(
homepageSectionId: string,
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
metadata: any
): Promise<PagedResults> {
const kavitaAPI = await getKavitaAPI(this.stateManager);
const { pageSize, excludeBookTypeLibrary } = await getOptions(this.stateManager);
const { pageSize, excludeUnsupportedLibrary } = await getOptions(this.stateManager);
const excludeLibraryIds: number[] = [];
const page: number = (metadata?.page ?? 0) + 1;

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
// rome-ignore lint/style/useSingleVarDeclarator: <explanation>
let apiPath: string, body: any = {}, id: string = 'id', title: string = 'name', checkLibraryId = false, useBuiltInCache: boolean = false;
switch (homepageSectionId) {
case 'ondeck':
Expand Down Expand Up @@ -439,7 +427,6 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
method: 'POST'
});

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
let result: any;
if (useBuiltInCache) {
result = this.cacheManager.getCachedData(reqeustToString(request));
Expand Down Expand Up @@ -471,7 +458,7 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga

const tiles: PartialSourceManga[] = [];
for (const series of result) {
if (checkLibraryId && excludeBookTypeLibrary && excludeLibraryIds.includes(series.libraryId)) continue;
if (checkLibraryId && excludeUnsupportedLibrary && excludeLibraryIds.includes(series.libraryId)) continue;
tiles.push(App.createPartialSourceManga({
title: series[title],
image: `${kavitaAPI.url}/image/series-cover?seriesId=${series[id]}&apiKey=${kavitaAPI.key}`,
Expand All @@ -498,13 +485,11 @@ export class Kavya implements ChapterProviding, HomePageSectionsProviding, Manga
const response = await this.requestManager.schedule(request, 1);
const result = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const chapters: any[] = [];

let i = 0;
for (const volume of result) {
for (const chapter of volume.chapters) {
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const item: any = {
chapNum: parseFloat(chapter.number),
volume: volume.number,
Expand Down
8 changes: 2 additions & 6 deletions src/Kavya/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
searchRequestToString
} from './Common';

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const KAVITA_PERSON_ROLES: any = {
'1': 'other',
'2': 'artist',
Expand All @@ -32,7 +31,6 @@ const KAVITA_PERSON_ROLES: any = {

export async function searchRequest(
searchQuery: SearchRequest,
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
metadata: any,
requestManager: RequestManager,
interceptor: KavitaRequestInterceptor,
Expand All @@ -47,12 +45,12 @@ export async function searchRequest(
}

const kavitaAPI = await getKavitaAPI(stateManager);
const { enableRecursiveSearch, excludeBookTypeLibrary, pageSize } = await getOptions(stateManager);
const { enableRecursiveSearch, excludeUnsupportedLibrary, pageSize } = await getOptions(stateManager);
const page: number = metadata?.page ?? 0;

const excludeLibraryIds: number[] = [];

if (excludeBookTypeLibrary) {
if (excludeUnsupportedLibrary) {
const request = App.createRequest({
url: `${kavitaAPI.url}/Library/libraries`,
method: 'GET'
Expand All @@ -73,7 +71,6 @@ export async function searchRequest(
const tagSearchTiles: PartialSourceManga[] = [];
const titleSearchTiles: PartialSourceManga[] = [];

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
let result: any = cacheManager.getCachedData(searchRequestToString(searchQuery));
if (result === undefined) {
if (typeof searchQuery.title === 'string' && searchQuery.title !== '') {
Expand Down Expand Up @@ -148,7 +145,6 @@ export async function searchRequest(
}

if (typeof searchQuery.includedTags !== 'undefined') {
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
const body: any = {};
const peopleTags: string[] = [];

Expand Down
11 changes: 5 additions & 6 deletions src/Kavya/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ export const serverSettingsMenu = (
})
}),
App.createDUISwitch({
id: 'excludeBookTypeLibrary',
id: 'excludeUnsupportedLibrary',
label: 'Exclude Book & Novel Type Libraries',
value: App.createDUIBinding({
async get() {
return values.excludeBookTypeLibrary;
return values.excludeUnsupportedLibrary;
},
async set(value) {
values.excludeBookTypeLibrary = value;
values.excludeUnsupportedLibrary = value;
await setStateData(stateManager, interceptor, values);
}
})
Expand Down Expand Up @@ -169,14 +169,13 @@ export async function retrieveStateData(stateManager: SourceStateManager) {
const showOnDeck = (await stateManager.retrieve('showOnDeck') as boolean) ?? DEFAULT_VALUES.showOnDeck;
const showRecentlyUpdated = (await stateManager.retrieve('showRecentlyUpdated') as boolean) ?? DEFAULT_VALUES.showRecentlyUpdated;
const showNewlyAdded = (await stateManager.retrieve('showNewlyAdded') as boolean) ?? DEFAULT_VALUES.showNewlyAdded;
const excludeBookTypeLibrary = (await stateManager.retrieve('excludeBookTypeLibrary') as boolean) ?? DEFAULT_VALUES.excludeBookTypeLibrary;
const excludeUnsupportedLibrary = (await stateManager.retrieve('excludeUnsupportedLibrary') as boolean) ?? DEFAULT_VALUES.excludeUnsupportedLibrary;

const enableRecursiveSearch = (await stateManager.retrieve('enableRecursiveSearch') as boolean) ?? DEFAULT_VALUES.enableRecursiveSearch;

return { kavitaAddress, kavitaAPIKey, pageSize, showOnDeck, showRecentlyUpdated, showNewlyAdded, excludeBookTypeLibrary, enableRecursiveSearch }
return { kavitaAddress, kavitaAPIKey, pageSize, showOnDeck, showRecentlyUpdated, showNewlyAdded, excludeUnsupportedLibrary, enableRecursiveSearch }
}

// rome-ignore lint/suspicious/noExplicitAny: <explanation>
export async function setStateData(stateManager: SourceStateManager, interceptor: KavitaRequestInterceptor, data: Record<string, any>) {
const promises: Promise<void>[] = [];
const prevStateData: any = await retrieveStateData(stateManager);
Expand Down

0 comments on commit 66a2f69

Please sign in to comment.