Add functionality to save source browse searches

This commit is contained in:
schroda
2024-05-08 04:01:23 +02:00
parent 93f76d6b7e
commit 5cb541b684
11 changed files with 337 additions and 37 deletions

View File

@@ -65,6 +65,10 @@ export const PARTIAL_SOURCE_FIELDS = gql`
pkgName
repo
}
meta {
key
value
}
}
`;

File diff suppressed because one or more lines are too long

View File

@@ -83,3 +83,22 @@ export const UPDATE_SOURCE_PREFERENCES = gql`
}
}
`;
export const SET_SOURCE_METADATA = gql`
mutation SET_SOURCE_METADATA($input: SetSourceMetaInput!) {
setSourceMeta(input: $input) {
clientMutationId
meta {
key
value
source {
id
meta {
key
value
}
}
}
}
}
`;

View File

@@ -17,6 +17,7 @@ import {
TCategory,
TChapter,
TManga,
TPartialSource,
} from '@/typings.ts';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { MetaType } from '@/lib/graphql/generated/graphql.ts';
@@ -54,6 +55,9 @@ const GLOBAL_METADATA_KEYS: AppMetadataKeys[] = [
// updates
'webUIInformAvailableUpdate',
'serverInformAvailableUpdate',
// sources
'savedSearches',
];
/**
@@ -354,7 +358,7 @@ const wrapMetadataWithMetaKey = (wrap: boolean, metadata: Metadata): MetadataHol
};
};
type MetadataHolderType = 'manga' | 'chapter' | 'category' | 'global';
type MetadataHolderType = 'manga' | 'chapter' | 'category' | 'global' | 'source';
export const requestUpdateMetadataValue = async (
metadataHolder: GqlMetaHolder,
@@ -377,6 +381,9 @@ export const requestUpdateMetadataValue = async (
case 'manga':
await requestManager.setMangaMeta((metadataHolder as TManga).id, metadataKey, value).response;
break;
case 'source':
await requestManager.setSourceMeta((metadataHolder as TPartialSource).id, metadataKey, value).response;
break;
default:
throw new Error(`requestUpdateMetadataValue: unknown holderType "${holderType}"`);
}
@@ -406,3 +413,8 @@ export const requestUpdateCategoryMetadata = async (
category: TCategory,
keysToValues: MetadataKeyValuePair[],
): Promise<void[]> => requestUpdateMetadata(category, 'category', keysToValues);
export const requestUpdateSourceMetadata = async (
source: TPartialSource,
keysToValue: MetadataKeyValuePair[],
): Promise<void[]> => requestUpdateMetadata(source, 'source', keysToValue);

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import {
AllowedMetadataValueTypes,
AppMetadataKeys,
GqlMetaHolder,
ISourceMetadata,
Metadata,
SourceMetadataKeys,
TPartialSource,
} from '@/typings.ts';
import { jsonSaveParse } from '@/util/HelperFunctions.ts';
import { convertFromGqlMeta, getMetadataFrom, requestUpdateSourceMetadata } from '@/lib/metadata/metadata.ts';
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
const convertAppMetadataToGqlMetadata = (
metadata: Partial<ISourceMetadata>,
): Metadata<string, AllowedMetadataValueTypes> => ({
...metadata,
savedSearches: metadata.savedSearches ? JSON.stringify(metadata.savedSearches) : undefined,
});
export const convertGqlMetadataToAppMetadata = (
metadata: Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>,
): ISourceMetadata => ({
...(metadata as unknown as ISourceMetadata),
savedSearches: jsonSaveParse<ISourceMetadata['savedSearches']>(metadata.savedSearches as string) ?? undefined,
});
export const getSourceMetadata = ({ meta }: GqlMetaHolder = {}, applyMetadataMigration?: boolean): ISourceMetadata =>
convertGqlMetadataToAppMetadata(
getMetadataFrom({ meta: convertFromGqlMeta(meta) } ?? {}, { savedSearches: undefined }, applyMetadataMigration),
);
export const updateSourceMetadata = async <
MetadataKeys extends SourceMetadataKeys = SourceMetadataKeys,
MetadataKey extends MetadataKeys = MetadataKeys,
>(
source: TPartialSource,
metadataKey: MetadataKey,
value: ISourceMetadata[MetadataKey],
): Promise<void[]> =>
requestUpdateSourceMetadata(source, [
[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]],
]);
export const createUpdateSourceMetadata =
<Settings extends SourceMetadataKeys>(
source: TPartialSource,
handleError: (error: any) => void = defaultPromiseErrorHandler('createUpdateSourceMetadata'),
): ((...args: OmitFirst<Parameters<typeof updateSourceMetadata<Settings>>>) => Promise<void | void[]>) =>
(metadataKey, value) =>
updateSourceMetadata(source, metadataKey, value).catch(handleError);

View File

@@ -196,6 +196,9 @@ import {
TrackerUnbindMutationVariables,
TrackerFetchBindMutation,
TrackerFetchBindMutationVariables,
GetServerSettingsQueryVariables,
SetSourceMetadataMutation,
SetSourceMetadataMutationVariables,
} from '@/lib/graphql/generated/graphql.ts';
import { GET_GLOBAL_METADATAS } from '@/lib/graphql/queries/GlobalMetadataQuery.ts';
import { SET_GLOBAL_METADATA } from '@/lib/graphql/mutations/GlobalMetadataMutation.ts';
@@ -228,7 +231,11 @@ import {
GET_MIGRATABLE_SOURCE_MANGAS,
} from '@/lib/graphql/queries/MangaQuery.ts';
import { GET_CATEGORIES, GET_CATEGORY_MANGAS } from '@/lib/graphql/queries/CategoryQuery.ts';
import { GET_SOURCE_MANGAS_FETCH, UPDATE_SOURCE_PREFERENCES } from '@/lib/graphql/mutations/SourceMutation.ts';
import {
GET_SOURCE_MANGAS_FETCH,
SET_SOURCE_METADATA,
UPDATE_SOURCE_PREFERENCES,
} from '@/lib/graphql/mutations/SourceMutation.ts';
import {
CLEAR_DOWNLOADER,
DELETE_DOWNLOADED_CHAPTER,
@@ -1329,6 +1336,22 @@ export class RequestManager {
return this.doRequest(GQLMethod.USE_QUERY, GET_SOURCE, { id }, options);
}
public setSourceMeta(
sourceId: string,
key: string,
value: any,
options?: MutationOptions<SetSourceMetadataMutation, SetSourceMetadataMutationVariables>,
): AbortableApolloMutationResponse<SetSourceMetadataMutation> {
return this.doRequest(
GQLMethod.MUTATION,
SET_SOURCE_METADATA,
{
input: { meta: { sourceId, key, value: `${value}` } },
},
options,
);
}
public useGetSourceMangas(
input: FetchSourceMangaInput,
initialPages: number = 1,
@@ -2476,8 +2499,8 @@ export class RequestManager {
}
public useGetServerSettings(
options?: QueryHookOptions<GetServerSettingsQuery, GetSourcesQueryVariables>,
): AbortableApolloUseQueryResponse<GetServerSettingsQuery, GetSourcesQueryVariables> {
options?: QueryHookOptions<GetServerSettingsQuery, GetServerSettingsQueryVariables>,
): AbortableApolloUseQueryResponse<GetServerSettingsQuery, GetServerSettingsQueryVariables> {
return this.doRequest(GQLMethod.USE_QUERY, GET_SERVER_SETTINGS, undefined, options);
}