Skip to content

Commit

Permalink
Use gql for "downloader"
Browse files Browse the repository at this point in the history
  • Loading branch information
schroda committed Oct 27, 2023
1 parent ca18e75 commit 222f8f5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/manga/ChapterCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const ChapterCard: React.FC<IProps> = (props: IProps) => {
};

const downloadChapter = () => {
requestManager.addChapterToDownloadQueue(chapter.mangaId, chapter.index);
requestManager.addChapterToDownloadQueue(chapter.id);
handleClose();
};

Expand Down
108 changes: 86 additions & 22 deletions src/lib/requests/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
CategoryOrderBy,
CheckForServerUpdatesQuery,
CheckForServerUpdatesQueryVariables,
ClearDownloaderMutation,
ClearDownloaderMutationVariables,
CreateCategoryInput,
CreateCategoryMutation,
CreateCategoryMutationVariables,
Expand All @@ -50,6 +52,14 @@ import {
DeleteDownloadedChapterMutationVariables,
DeleteDownloadedChaptersMutation,
DeleteDownloadedChaptersMutationVariables,
DequeueChapterDownloadMutation,
DequeueChapterDownloadMutationVariables,
DequeueChapterDownloadsMutation,
DequeueChapterDownloadsMutationVariables,
EnqueueChapterDownloadMutation,
EnqueueChapterDownloadMutationVariables,
EnqueueChapterDownloadsMutation,
EnqueueChapterDownloadsMutationVariables,
GetAboutQuery,
GetAboutQueryVariables,
GetExtensionsFetchMutation,
Expand All @@ -66,13 +76,19 @@ import {
GetSourcesQueryVariables,
InstallExternalExtensionMutation,
InstallExternalExtensionMutationVariables,
ReorderChapterDownloadMutation,
ReorderChapterDownloadMutationVariables,
SetCategoryMetadataMutation,
SetCategoryMetadataMutationVariables,
SetChapterMetadataMutation,
SetChapterMetadataMutationVariables,
SetGlobalMetadataMutation,
SetGlobalMetadataMutationVariables,
SetMangaMetadataMutation,
StartDownloaderMutation,
StartDownloaderMutationVariables,
StopDownloaderMutation,
StopDownloaderMutationVariables,
UpdateCategoryMutation,
UpdateCategoryMutationVariables,
UpdateCategoryOrderMutation,
Expand Down Expand Up @@ -106,7 +122,18 @@ import { SET_MANGA_METADATA, UPDATE_MANGA, UPDATE_MANGA_CATEGORIES } from '@/lib
import { GET_MANGA, GET_MANGAS } from '@/lib/graphql/queries/MangaQuery.ts';
import { GET_CATEGORIES, GET_CATEGORY, GET_CATEGORY_MANGAS } from '@/lib/graphql/queries/CategoryQuery.ts';
import { GET_SOURCE_MANGAS_FETCH } from '@/lib/graphql/mutations/SourceMutation.ts';
import { DELETE_DOWNLOADED_CHAPTER, DELETE_DOWNLOADED_CHAPTERS } from '@/lib/graphql/mutations/DownloaderMutation.ts';
import {
CLEAR_DOWNLOADER,
DELETE_DOWNLOADED_CHAPTER,
DELETE_DOWNLOADED_CHAPTERS,
DEQUEUE_CHAPTER_DOWNLOAD,
DEQUEUE_CHAPTER_DOWNLOADS,
ENQUEUE_CHAPTER_DOWNLOAD,
ENQUEUE_CHAPTER_DOWNLOADS,
REORDER_CHAPTER_DOWNLOAD,
START_DOWNLOADER,
STOP_DOWNLOADER,
} from '@/lib/graphql/mutations/DownloaderMutation.ts';
import { GET_CHAPTER, GET_CHAPTERS } from '@/lib/graphql/queries/ChapterQuery.ts';
import { SET_CHAPTER_METADATA, UPDATE_CHAPTER, UPDATE_CHAPTERS } from '@/lib/graphql/mutations/ChapterMutation.ts';
import {
Expand All @@ -116,6 +143,7 @@ import {
UPDATE_CATEGORY,
UPDATE_CATEGORY_ORDER,
} from '@/lib/graphql/mutations/CategoryMutation.ts';
import { GET_DOWNLOAD_STATUS } from '@/lib/graphql/queries/DownloaderQuery.ts';

enum SWRHttpMethod {
SWR_GET,
Expand Down Expand Up @@ -923,43 +951,79 @@ export class RequestManager {
return this.getValidUrlFor('backup/export/file');
}

public startDownloads(): AbortableAxiosResponse {
return this.doRequest(HttpMethod.GET, 'downloads/start');
public startDownloads(): AbortableApolloMutationResponse<StartDownloaderMutation> {
return this.doRequestNew<StartDownloaderMutation, StartDownloaderMutationVariables>(
GQLMethod.MUTATION,
START_DOWNLOADER,
{},
);
}

public stopDownloads(): AbortableAxiosResponse {
return this.doRequest(HttpMethod.GET, 'downloads/stop');
public stopDownloads(): AbortableApolloMutationResponse<StopDownloaderMutation> {
return this.doRequestNew<StopDownloaderMutation, StopDownloaderMutationVariables>(
GQLMethod.MUTATION,
STOP_DOWNLOADER,
{},
);
}

public clearDownloads(): AbortableAxiosResponse {
return this.doRequest(HttpMethod.GET, 'downloads/clear');
public clearDownloads(): AbortableApolloMutationResponse<ClearDownloaderMutation> {
return this.doRequestNew<ClearDownloaderMutation, ClearDownloaderMutationVariables>(
GQLMethod.MUTATION,
CLEAR_DOWNLOADER,
{},
{ refetchQueries: [GET_DOWNLOAD_STATUS] },
);
}

public addChapterToDownloadQueue(mangaId: number | string, chapterIndex: number | string): AbortableAxiosResponse {
return this.doRequest(HttpMethod.GET, `download/${mangaId}/chapter/${chapterIndex}`);
public addChapterToDownloadQueue(id: number): AbortableApolloMutationResponse<EnqueueChapterDownloadMutation> {
return this.doRequestNew<EnqueueChapterDownloadMutation, EnqueueChapterDownloadMutationVariables>(
GQLMethod.MUTATION,
ENQUEUE_CHAPTER_DOWNLOAD,
{ input: { id } },
{ refetchQueries: [GET_DOWNLOAD_STATUS] },
);
}

public removeChapterFromDownloadQueue(
mangaId: number | string,
chapterIndex: number | string,
): AbortableAxiosResponse {
return this.doRequest(HttpMethod.DELETE, `download/${mangaId}/chapter/${chapterIndex}`);
public removeChapterFromDownloadQueue(id: number): AbortableApolloMutationResponse<DequeueChapterDownloadMutation> {
return this.doRequestNew<DequeueChapterDownloadMutation, DequeueChapterDownloadMutationVariables>(
GQLMethod.MUTATION,
DEQUEUE_CHAPTER_DOWNLOAD,
{ input: { id } },
{ refetchQueries: [GET_DOWNLOAD_STATUS] },
);
}

public reorderChapterInDownloadQueue(
mangaId: number | string,
chapterIndex: number | string,
chapterId: number,
position: number,
): AbortableAxiosResponse {
return this.doRequest(HttpMethod.PATCH, `download/${mangaId}/chapter/${chapterIndex}/reorder/${position}`);
): AbortableApolloMutationResponse<ReorderChapterDownloadMutation> {
return this.doRequestNew<ReorderChapterDownloadMutation, ReorderChapterDownloadMutationVariables>(
GQLMethod.MUTATION,
REORDER_CHAPTER_DOWNLOAD,
{ input: { chapterId, to: position } },
{ refetchQueries: [GET_DOWNLOAD_STATUS] },
);
}

public addChaptersToDownloadQueue(chapterIds: number[]): AbortableAxiosResponse {
return this.doRequest(HttpMethod.POST, 'download/batch', { data: { chapterIds } });
public addChaptersToDownloadQueue(ids: number[]): AbortableApolloMutationResponse<EnqueueChapterDownloadsMutation> {
return this.doRequestNew<EnqueueChapterDownloadsMutation, EnqueueChapterDownloadsMutationVariables>(
GQLMethod.MUTATION,
ENQUEUE_CHAPTER_DOWNLOADS,
{ input: { ids } },
{ refetchQueries: [GET_DOWNLOAD_STATUS] },
);
}

public removeChaptersFromDownloadQueue(chapterIds: number[]): AbortableAxiosResponse {
return this.doRequest(HttpMethod.DELETE, 'download/batch', { data: { chapterIds } });
public removeChaptersFromDownloadQueue(
ids: number[],
): AbortableApolloMutationResponse<DequeueChapterDownloadsMutation> {
return this.doRequestNew<DequeueChapterDownloadsMutation, DequeueChapterDownloadsMutationVariables>(
GQLMethod.MUTATION,
DEQUEUE_CHAPTER_DOWNLOADS,
{ input: { ids } },
{ refetchQueries: [GET_DOWNLOAD_STATUS] },
);
}

public useGetRecentlyUpdatedChapters(
Expand Down
2 changes: 1 addition & 1 deletion src/screens/DownloadQueue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const DownloadQueue: React.FC = () => {

await Promise.all([
// remove from download queue
requestManager.removeChapterFromDownloadQueue(chapter.mangaId, chapter.index).response,
requestManager.removeChapterFromDownloadQueue(chapter.id).response,
// delete partial download, should be handle server side?
// bug: The folder and the last image downloaded are not deleted
requestManager.deleteDownloadedChapter(chapter.id).response,
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Updates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const Updates: React.FC = () => {
};

const downloadChapter = (chapter: IChapter) => {
requestManager.addChapterToDownloadQueue(chapter.mangaId, chapter.index);
requestManager.addChapterToDownloadQueue(chapter.id);
};

const loadMore = useCallback(() => {
Expand Down

0 comments on commit 222f8f5

Please sign in to comment.