Extract "MetadataReader" functions
This commit is contained in:
45
src/modules/metadata/Metadata.utils.ts
Normal file
45
src/modules/metadata/Metadata.utils.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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 { APP_METADATA_KEY_PREFIX, GLOBAL_METADATA_KEYS } from '@/modules/metadata/Metadata.constants.ts';
|
||||||
|
import { AppMetadataKeys, Metadata } from '@/modules/metadata/Metadata.types.ts';
|
||||||
|
import { getActiveDevice } from '@/modules/device/services/Device.ts';
|
||||||
|
|
||||||
|
export const extractOriginalKey = (key: string) => key.split('_').slice(-1)[0];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the key with the provided prefixes.
|
||||||
|
*
|
||||||
|
* In case the key is not a global key ({@link GLOBAL_METADATA_KEYS}), the active device name ({@link getActiveDevice})
|
||||||
|
* will be added as the second prefix.
|
||||||
|
*
|
||||||
|
* **Important**
|
||||||
|
*
|
||||||
|
* "default" (case-insensitive) is a reserved value and will be handled as if there is no prefix to add
|
||||||
|
*
|
||||||
|
* The format is <prefixes>\_<key>
|
||||||
|
*
|
||||||
|
* e.g.: <base_prefix>\_[device_name]\_[optional_prefix1]\_[optional_prefix2]\_<key>
|
||||||
|
*/
|
||||||
|
export const getMetadataKey = (key: string, prefixes: string[] = [], appPrefix: string = APP_METADATA_KEY_PREFIX) => {
|
||||||
|
const isGlobalMetadataKey = GLOBAL_METADATA_KEYS.includes(key as AppMetadataKeys);
|
||||||
|
const addActiveDevicePrefix = !isGlobalMetadataKey;
|
||||||
|
|
||||||
|
const finalPrefix = [appPrefix, ...(addActiveDevicePrefix ? [getActiveDevice()] : []), ...prefixes].filter(
|
||||||
|
(prefix) => prefix.toLowerCase() !== 'default',
|
||||||
|
);
|
||||||
|
|
||||||
|
return `${finalPrefix.join('_')}_${key}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const doesMetadataKeyExistIn = (
|
||||||
|
meta: Metadata | undefined,
|
||||||
|
key: string,
|
||||||
|
prefixes?: string[],
|
||||||
|
appPrefix?: string,
|
||||||
|
): boolean => Object.prototype.hasOwnProperty.call(meta ?? {}, getMetadataKey(key, prefixes, appPrefix));
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import { APP_METADATA_KEY_PREFIX, METADATA_MIGRATIONS } from '@/modules/metadata/Metadata.constants.ts';
|
import { APP_METADATA_KEY_PREFIX, METADATA_MIGRATIONS } from '@/modules/metadata/Metadata.constants.ts';
|
||||||
import { IMetadataMigration, Metadata } from '@/modules/metadata/Metadata.types.ts';
|
import { IMetadataMigration, Metadata } from '@/modules/metadata/Metadata.types.ts';
|
||||||
import { doesMetadataKeyExistIn, getMetadataKey } from '@/modules/metadata/services/MetadataReader.ts';
|
import { doesMetadataKeyExistIn, getMetadataKey } from '@/modules/metadata/Metadata.utils.ts';
|
||||||
|
|
||||||
export const getAppKeyPrefixForMigration = (migrationId: number): string => {
|
export const getAppKeyPrefixForMigration = (migrationId: number): string => {
|
||||||
const appKeyPrefix = METADATA_MIGRATIONS.slice(0, migrationId)
|
const appKeyPrefix = METADATA_MIGRATIONS.slice(0, migrationId)
|
||||||
|
|||||||
@@ -7,13 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import {
|
import { METADATA_MIGRATIONS, VALID_APP_METADATA_KEYS } from '@/modules/metadata/Metadata.constants.ts';
|
||||||
APP_METADATA_KEY_PREFIX,
|
|
||||||
GLOBAL_METADATA_KEYS,
|
|
||||||
METADATA_MIGRATIONS,
|
|
||||||
VALID_APP_METADATA_KEYS,
|
|
||||||
} from '@/modules/metadata/Metadata.constants.ts';
|
|
||||||
import { getActiveDevice } from '@/modules/device/services/Device.ts';
|
|
||||||
import { applyMetadataMigrations } from '@/modules/metadata/services/MetadataMigrations.ts';
|
import { applyMetadataMigrations } from '@/modules/metadata/services/MetadataMigrations.ts';
|
||||||
import { convertToGqlMeta, convertValueFromMetadata } from '@/modules/metadata/services/MetadataConverter.ts';
|
import { convertToGqlMeta, convertValueFromMetadata } from '@/modules/metadata/services/MetadataConverter.ts';
|
||||||
import {
|
import {
|
||||||
@@ -41,40 +35,7 @@ import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts'
|
|||||||
import { ChapterIdInfo } from '@/modules/chapter/services/Chapters.ts';
|
import { ChapterIdInfo } from '@/modules/chapter/services/Chapters.ts';
|
||||||
import { CategoryIdInfo } from '@/modules/category/Category.types.ts';
|
import { CategoryIdInfo } from '@/modules/category/Category.types.ts';
|
||||||
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
||||||
|
import { doesMetadataKeyExistIn, extractOriginalKey, getMetadataKey } from '@/modules/metadata/Metadata.utils.ts';
|
||||||
export const extractOriginalKey = (key: string) => key.split('_').slice(-1)[0];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the key with the provided prefixes.
|
|
||||||
*
|
|
||||||
* In case the key is not a global key ({@link GLOBAL_METADATA_KEYS}), the active device name ({@link getActiveDevice})
|
|
||||||
* will be added as the second prefix.
|
|
||||||
*
|
|
||||||
* **Important**
|
|
||||||
*
|
|
||||||
* "default" (case-insensitive) is a reserved value and will be handled as if there is no prefix to add
|
|
||||||
*
|
|
||||||
* The format is <prefixes>\_<key>
|
|
||||||
*
|
|
||||||
* e.g.: <base_prefix>\_[device_name]\_[optional_prefix1]\_[optional_prefix2]\_<key>
|
|
||||||
*/
|
|
||||||
export const getMetadataKey = (key: string, prefixes: string[] = [], appPrefix: string = APP_METADATA_KEY_PREFIX) => {
|
|
||||||
const isGlobalMetadataKey = GLOBAL_METADATA_KEYS.includes(key as AppMetadataKeys);
|
|
||||||
const addActiveDevicePrefix = !isGlobalMetadataKey;
|
|
||||||
|
|
||||||
const finalPrefix = [appPrefix, ...(addActiveDevicePrefix ? [getActiveDevice()] : []), ...prefixes].filter(
|
|
||||||
(prefix) => prefix.toLowerCase() !== 'default',
|
|
||||||
);
|
|
||||||
|
|
||||||
return `${finalPrefix.join('_')}_${key}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const doesMetadataKeyExistIn = (
|
|
||||||
meta: Metadata | undefined,
|
|
||||||
key: string,
|
|
||||||
prefixes?: string[],
|
|
||||||
appPrefix?: string,
|
|
||||||
): boolean => Object.prototype.hasOwnProperty.call(meta ?? {}, getMetadataKey(key, prefixes, appPrefix));
|
|
||||||
|
|
||||||
export const getMetadataValueFrom = <Key extends AppMetadataKeys, Value extends AllowedMetadataValueTypes>(
|
export const getMetadataValueFrom = <Key extends AppMetadataKeys, Value extends AllowedMetadataValueTypes>(
|
||||||
{ meta }: MetadataHolder,
|
{ meta }: MetadataHolder,
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|||||||
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
||||||
import { ChapterIdInfo } from '@/modules/chapter/services/Chapters.ts';
|
import { ChapterIdInfo } from '@/modules/chapter/services/Chapters.ts';
|
||||||
import { CategoryIdInfo } from '@/modules/category/Category.types.ts';
|
import { CategoryIdInfo } from '@/modules/category/Category.types.ts';
|
||||||
import { getMetadataKey } from '@/modules/metadata/services/MetadataReader.ts';
|
|
||||||
import {
|
import {
|
||||||
AllowedMetadataValueTypes,
|
AllowedMetadataValueTypes,
|
||||||
AppMetadataKeys,
|
AppMetadataKeys,
|
||||||
@@ -19,6 +18,7 @@ import {
|
|||||||
MetadataKeyValuePair,
|
MetadataKeyValuePair,
|
||||||
} from '@/modules/metadata/Metadata.types.ts';
|
} from '@/modules/metadata/Metadata.types.ts';
|
||||||
import { MangaIdInfo } from '@/modules/manga/Manga.types.ts';
|
import { MangaIdInfo } from '@/modules/manga/Manga.types.ts';
|
||||||
|
import { getMetadataKey } from '@/modules/metadata/Metadata.utils.ts';
|
||||||
|
|
||||||
export const requestUpdateMetadataValue = async (
|
export const requestUpdateMetadataValue = async (
|
||||||
metadataHolder: GqlMetaHolder,
|
metadataHolder: GqlMetaHolder,
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import {
|
|||||||
updateReaderSettings,
|
updateReaderSettings,
|
||||||
} from '@/modules/reader/services/ReaderSettingsMetadata.ts';
|
} from '@/modules/reader/services/ReaderSettingsMetadata.ts';
|
||||||
import { MangaIdInfo } from '@/modules/manga/Manga.types.ts';
|
import { MangaIdInfo } from '@/modules/manga/Manga.types.ts';
|
||||||
import { getMetadataKey } from '@/modules/metadata/services/MetadataReader.ts';
|
|
||||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||||
import { MANGA_META_FIELDS } from '@/lib/graphql/fragments/MangaFragments.ts';
|
import { MANGA_META_FIELDS } from '@/lib/graphql/fragments/MangaFragments.ts';
|
||||||
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
||||||
@@ -51,6 +50,7 @@ import { Queue } from '@/lib/Queue.ts';
|
|||||||
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
||||||
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
||||||
import { FALLBACK_MANGA } from '@/modules/manga/Manga.constants.ts';
|
import { FALLBACK_MANGA } from '@/modules/manga/Manga.constants.ts';
|
||||||
|
import { getMetadataKey } from '@/modules/metadata/Metadata.utils.ts';
|
||||||
|
|
||||||
const DIRECTION_TO_INVERTED: Record<Direction, Direction> = {
|
const DIRECTION_TO_INVERTED: Record<Direction, Direction> = {
|
||||||
ltr: 'rtl',
|
ltr: 'rtl',
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
import { MangaType } from '@/lib/graphql/generated/graphql.ts';
|
import { MangaType } from '@/lib/graphql/generated/graphql.ts';
|
||||||
import { IReaderSettings, IReaderSettingsWithDefaultFlag, ReadingMode } from '@/modules/reader/types/Reader.types.ts';
|
import { IReaderSettings, IReaderSettingsWithDefaultFlag, ReadingMode } from '@/modules/reader/types/Reader.types.ts';
|
||||||
import { convertFromGqlMeta } from '@/modules/metadata/services/MetadataConverter.ts';
|
import { convertFromGqlMeta } from '@/modules/metadata/services/MetadataConverter.ts';
|
||||||
import { extractOriginalKey, getMetadataFrom } from '@/modules/metadata/services/MetadataReader.ts';
|
import { getMetadataFrom } from '@/modules/metadata/services/MetadataReader.ts';
|
||||||
import {
|
import {
|
||||||
AllowedMetadataValueTypes,
|
AllowedMetadataValueTypes,
|
||||||
AppMetadataKeys,
|
AppMetadataKeys,
|
||||||
@@ -33,6 +33,7 @@ import {
|
|||||||
} from '@/modules/reader/constants/ReaderSettings.constants.tsx';
|
} from '@/modules/reader/constants/ReaderSettings.constants.tsx';
|
||||||
import { DEFAULT_DEVICE, getActiveDevice } from '@/modules/device/services/Device.ts';
|
import { DEFAULT_DEVICE, getActiveDevice } from '@/modules/device/services/Device.ts';
|
||||||
import { APP_METADATA_KEY_PREFIX } from '@/modules/metadata/Metadata.constants.ts';
|
import { APP_METADATA_KEY_PREFIX } from '@/modules/metadata/Metadata.constants.ts';
|
||||||
|
import { extractOriginalKey } from '@/modules/metadata/Metadata.utils.ts';
|
||||||
|
|
||||||
export const convertFromReaderSettingsWithDefaultFlag = (settings: IReaderSettingsWithDefaultFlag): IReaderSettings =>
|
export const convertFromReaderSettingsWithDefaultFlag = (settings: IReaderSettingsWithDefaultFlag): IReaderSettings =>
|
||||||
Object.fromEntries(
|
Object.fromEntries(
|
||||||
|
|||||||
Reference in New Issue
Block a user