Second attempt to selectively turn off APIs

This commit is contained in:
ltctceplrm 2024-02-06 02:29:24 +01:00
parent 54766bb59e
commit 5b35e552c5
3 changed files with 16 additions and 15 deletions

View file

@ -1,14 +1,10 @@
import { APIModel } from './APIModel';
import { MediaTypeModel } from '../models/MediaTypeModel';
import MediaDbPlugin from '../main';
export class APIManager {
plugin: MediaDbPlugin;
apis: APIModel[];
constructor(plugin: MediaDbPlugin) {
this.plugin = plugin;
constructor() {
this.apis = [];
}
@ -18,12 +14,13 @@ export class APIManager {
* @param query
* @param apisToQuery
*/
async query(query: string, apisToQuery: string[], mediaType: string): Promise<MediaTypeModel[]> {
async query(query: string, apisToQuery: string[]): Promise<MediaTypeModel[]> {
console.debug(`MDB | api manager queried with "${query}"`);
let res: MediaTypeModel[] = [];
for (const api of this.apis) {
if (apisToQuery.contains(api.apiName) && !(this.plugin.settings[[api.apiName, mediaType].filter(s => s).join('') as keyof typeof this.plugin.settings] === false)) {
if (apisToQuery.contains(api.apiName)) {
const apiRes = await api.searchByTitle(query);
res = res.concat(apiRes);
}

View file

@ -1,11 +1,13 @@
import { MediaTypeModel } from '../models/MediaTypeModel';
import { MediaType } from '../utils/MediaType';
import MediaDbPlugin from '../main';
export abstract class APIModel {
apiName: string;
apiUrl: string;
apiDescription: string;
types: MediaType[];
plugin: MediaDbPlugin;
/**
* This function should query the api and return a list of matches. The matches should be caped at 20.
@ -17,7 +19,9 @@ export abstract class APIModel {
abstract getById(id: string): Promise<MediaTypeModel>;
hasType(type: MediaType): boolean {
return this.types.contains(type);
if (this.types.contains(type) && !(this.plugin.settings[[this.apiName, type.toString()].filter(s => s).join('') as keyof typeof this.plugin.settings] === false)){
return true;
}
}
hasTypeOverlap(types: MediaType[]): boolean {
@ -28,4 +32,4 @@ export abstract class APIModel {
}
return false;
}
}
}

View file

@ -39,7 +39,7 @@ export default class MediaDbPlugin extends Plugin {
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
async onload(): Promise<void> {
this.apiManager = new APIManager(this);
this.apiManager = new APIManager();
// register APIs
this.apiManager.registerAPI(new OMDbAPI(this));
this.apiManager.registerAPI(new MALAPI(this));
@ -156,7 +156,7 @@ export default class MediaDbPlugin extends Plugin {
*/
async createLinkWithSearchModal(): Promise<void> {
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis, "");
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis);
});
if (!apiSearchResults) {
@ -186,7 +186,7 @@ export default class MediaDbPlugin extends Plugin {
let apiSearchResults: MediaTypeModel[] = await this.modalHelper.openSearchModal(searchModalOptions ?? {}, async searchModalData => {
types = searchModalData.types;
const apis = this.apiManager.apis.filter(x => x.hasTypeOverlap(searchModalData.types)).map(x => x.apiName);
return await this.apiManager.query(searchModalData.query, apis, searchModalData.types.toString());
return await this.apiManager.query(searchModalData.query, apis);
});
if (!apiSearchResults) {
@ -218,7 +218,7 @@ export default class MediaDbPlugin extends Plugin {
async createEntryWithAdvancedSearchModal(): Promise<void> {
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis, "");
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis);
});
if (!apiSearchResults) {
@ -570,7 +570,7 @@ export default class MediaDbPlugin extends Plugin {
let results: MediaTypeModel[] = [];
try {
results = await this.apiManager.query(title, [selectedAPI], "");
results = await this.apiManager.query(title, [selectedAPI]);
} catch (e) {
erroredFiles.push({ filePath: file.path, error: e.toString() });
continue;
@ -671,4 +671,4 @@ export default class MediaDbPlugin extends Plugin {
await this.saveData(this.settings);
}
}
}