Skip to content

Commit

Permalink
Fixed bugs, MORE comments, version bump (#13)
Browse files Browse the repository at this point in the history
* Fixed bugs, Fix search bug, MORE comments, version bump

- Added more comments, hopefully improving readability
- Fixed fetching bug
- fixed updated section from starting on 0 to 1
- Version bump (2.0.1)
- Search function, makes sure to check every source for error. This should stop paperback from returning an error in case of individual source failures.
  • Loading branch information
ofelizestevez authored Nov 15, 2023
1 parent 9497693 commit 9f26d63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/TachiDesk/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export async function testRequest(stateManager: SourceStateManager, requestManag
// ! Requests End

// ! Categories Start
// Fetch Categories from server and returns them as an object
// Fetch Categories from server and returns them as a record
export async function fetchServerCategories(stateManager: SourceStateManager, requestManager: RequestManager) {
let categories: Record<string, tachiCategory> = {};

Expand Down Expand Up @@ -388,6 +388,7 @@ export function getCategoryFromId(categories: Record<string, tachiCategory>, id:
return categories[id] ?? DEFAULT_SERVER_CATEGORY
}

// categoryName is used to give a name to old entries which are no longer in the server
export function getCategoryNameFromId(categories: Record<string, tachiCategory>, id: string) {
let categoryName = "OLD ENTRY OR ERROR"
Object.values(categories).forEach(category => {
Expand All @@ -401,7 +402,7 @@ export function getCategoryNameFromId(categories: Record<string, tachiCategory>,
// ! Categories End

// ! Sources Start
// Fetch Sources from server
// Fetch Sources from server and return as record
export async function fetchServerSources(stateManager: SourceStateManager, requestManager: RequestManager) {
let sources: Record<string, tachiSources> = {};

Expand Down Expand Up @@ -442,6 +443,7 @@ export function getSourceFromId(sources: Record<string, tachiSources>, id: strin
return sources[id] ?? DEFAULT_SERVER_SOURCE
}

// SourceName is used to give a name to old entries which are no longer in the server
export function getSourceNameFromId(sources: Record<string, tachiSources>, id: string) {
let sourceName = "OLD ENTRY OR ERROR"
Object.values(sources).forEach(source => {
Expand Down
3 changes: 2 additions & 1 deletion src/TachiDesk/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
testRequest
} from "./Common"

// 2 Sections -> 1 for server url, another for auth
// 2 Sections 1 page, -> 1 for server url, another for auth
export const serverAddressSettings = (stateManager: SourceStateManager, requestManager: RequestManager): DUINavigationButton => {
// Label that shows test response
let label = "Click on the button!"
Expand Down Expand Up @@ -387,6 +387,7 @@ export const languageSettings = async (stateManager: SourceStateManager): Promis
})
}

// Button which runs a function from common which sets every Paperback value back to their default values
export const resetSettingsButton = async (stateManager: SourceStateManager): Promise<DUIButton> => {
return App.createDUIButton({
id: "resetSettingsButton",
Expand Down
51 changes: 39 additions & 12 deletions src/TachiDesk/TachiDesk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export const TachiDeskInfo: SourceInfo = {
description: 'Paperback extension which aims to bridge all of Tachidesks features and the Paperback App.',
icon: 'icon.png',
name: 'Tachidesk',
version: '2.0',
version: '2.0.1',
websiteBaseURL: "https://github.com/Suwayomi/Tachidesk-Server",
contentRating: ContentRating.ADULT,
contentRating: ContentRating.EVERYONE,
sourceTags: [
{
text: "Self-hosted",
Expand All @@ -84,6 +84,7 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
requestsPerSecond: 4,
requestTimeout: 20000,
interceptor: {
// Intercepts request to add basic auth
interceptRequest: async (request: Request) => {
const authEnabled = await getAuthState(this.stateManager);

Expand All @@ -102,7 +103,7 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
}
})

// Variable used for share URL
// Variable used for share URL, updated by getChapters()
serverAddress = ""

// Settings
Expand Down Expand Up @@ -161,7 +162,18 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S

// Chapter list, sets the share URl address
async getChapters(mangaId: string): Promise<Chapter[]> {
const chaptersData: tachiChapter[] = await makeRequest(this.stateManager, this.requestManager, "manga/" + mangaId + "/chapters")
// Fetches manga first to use to check last fetched at
const manga: tachiManga = await makeRequest(this.stateManager, this.requestManager, "manga/" + mangaId)
let chaptersQueryString = "manga/" + mangaId + "/chapters"

// If last fetched is older than a day ago, do an online fetch for the manga and the chapter list
// Online fetch manga to update the manga.lastFetchedAt. Seems redundant but now idea how to improve
if (manga.lastFetchedAt < Math.floor(Date.now() / 1000) - 86400){
makeRequest(this.stateManager, this.requestManager, "manga/" + mangaId + "?onlineFetch=true")
chaptersQueryString += "?onlineFetch=true"
}

const chaptersData: tachiChapter[] = await makeRequest(this.stateManager, this.requestManager, chaptersQueryString)
this.serverAddress = await getServerURL(this.stateManager)

const chapters: Chapter[] = []
Expand All @@ -188,7 +200,8 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S

const pages: string[] = []

// Tachidesk uses page count, so for keys makes it easy to provide the links
// Tachidesk uses page count, so make an array of length pageCount then use the keys of array LOL
// pretty much a for i in range() from python
for (const pageIndex of Array(chapterData.pageCount).keys()) {
pages.push(apiURL + "manga/" + mangaId + "/chapter/" + chapterId + "/page/" + pageIndex)
}
Expand Down Expand Up @@ -223,7 +236,9 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
return;
}

// Fetches sources and categories since it runs every time anyway
// Fetches sources and categories since it runs every time anyway, including after installing the extension
// Useful because it fetches the sources and categories from the server, so you won't have to fetch them for settings
// Makes settings a lot more stable (as long as homepage sections are loaded before entering settings)
const serverURL = await getServerURL(this.stateManager);
const serverSources = await getServerSources(this.stateManager);
const serverCategories = await getServerCategories(this.stateManager);
Expand All @@ -248,6 +263,7 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
}

// Gets the settings values to set the type of rows
// Allows for customization of each type of row (updated, category, sources)
const mangaPerRow = await getMangaPerRow(this.stateManager);
const updatedRowState = await getUpdatedRowState(this.stateManager);
const categoryRowState = await getCategoryRowState(this.stateManager);
Expand All @@ -257,6 +273,7 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
const sourceRowStyle = (await getSourceRowStyle(this.stateManager))[0];

// Push Sections
// Uses regular paperback request syntax... could be changed to use the function makeRequest.
if (updatedRowState) {
sections.push({
section: App.createHomeSection({
Expand All @@ -276,7 +293,7 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
const serverCategories = await fetchServerCategories(this.stateManager, this.requestManager)
const selectedCategories: Array<string> = await getSelectedCategories(this.stateManager)

// Gets the information of selected sources to compare their order on the tachidesk server
//Gets server categories with all request info, filters out to only include selected categories, then compares their order to sort
const orderedSelectedCategories = Object.keys(serverCategories)
.filter((key) => selectedCategories.includes(key))
.sort((a, b) => {
Expand Down Expand Up @@ -312,7 +329,9 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
const serverSources = await getServerSources(this.stateManager);
const selectedSources = await getSelectedSources(this.stateManager);

// Adds popular and latest... there might be a way to handle this better (option) but... thats a lot of sources
// Adds popular and latest... We could add an option to turn each on or off but no idea how to set it up
// Should we allow each source to have an option for both? That sounds messy.
// Should we allow to turn each type of row on/off entirely? idk.
for (const sourceId of selectedSources) {
sections.push({
section: App.createHomeSection({
Expand Down Expand Up @@ -411,13 +430,13 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
// uses type of source to determine where to get the manga list and the api link
switch (type) {
case "updated":
page = metadata?.page ?? 0
page = metadata?.page ?? 1
apiEndpoint = "update/recentChapters/" + page;
response = (await makeRequest(this.stateManager, this.requestManager, apiEndpoint));
tileData = response.page
break
case "category":
page = metadata?.page ?? undefined
page = metadata?.page ?? undefined // Categories don't have pages
apiEndpoint = "category/" + sourceId;
response = (await makeRequest(this.stateManager, this.requestManager, apiEndpoint));
tileData = response
Expand Down Expand Up @@ -458,10 +477,11 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
results: tiles,
metadata: metadata
})

}

// For now only supports searching sources. I think this has something to do with genres / tags as well but honestly haven't looked into it
// For now only supports searching sources.
// Could support filters but it's too complicated since each source has their own set of filters
// and paperback considers tachidesk as 1 source.
async getSearchResults(query: SearchRequest, metadata: any): Promise<PagedResults> {
const serverSources = await getServerSources(this.stateManager)
const selectedSources = await getSelectedSources(this.stateManager)
Expand All @@ -484,6 +504,13 @@ export class TachiDesk implements HomePageSectionsProviding, ChapterProviding, S
}

const mangaResults = await makeRequest(this.stateManager, this.requestManager, "source/" + source + "/search" + paramsString)

// If request result is an error (evaluated by makeRequest), then skip source
// This stops individual sources from messing up the whole search process.
if (mangaResults instanceof Error){
continue
}

for (const manga of mangaResults.mangaList) {
tiles.push(
App.createPartialSourceManga({
Expand Down

0 comments on commit 9f26d63

Please sign in to comment.