Second attempt to selectively turn off APIs
This commit is contained in:
parent
54766bb59e
commit
5b35e552c5
3 changed files with 16 additions and 15 deletions
|
|
@ -1,14 +1,10 @@
|
||||||
import { APIModel } from './APIModel';
|
import { APIModel } from './APIModel';
|
||||||
import { MediaTypeModel } from '../models/MediaTypeModel';
|
import { MediaTypeModel } from '../models/MediaTypeModel';
|
||||||
import MediaDbPlugin from '../main';
|
|
||||||
|
|
||||||
|
|
||||||
export class APIManager {
|
export class APIManager {
|
||||||
plugin: MediaDbPlugin;
|
|
||||||
apis: APIModel[];
|
apis: APIModel[];
|
||||||
|
|
||||||
constructor(plugin: MediaDbPlugin) {
|
constructor() {
|
||||||
this.plugin = plugin;
|
|
||||||
this.apis = [];
|
this.apis = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -18,12 +14,13 @@ export class APIManager {
|
||||||
* @param query
|
* @param query
|
||||||
* @param apisToQuery
|
* @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}"`);
|
console.debug(`MDB | api manager queried with "${query}"`);
|
||||||
|
|
||||||
let res: MediaTypeModel[] = [];
|
let res: MediaTypeModel[] = [];
|
||||||
|
|
||||||
for (const api of this.apis) {
|
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);
|
const apiRes = await api.searchByTitle(query);
|
||||||
res = res.concat(apiRes);
|
res = res.concat(apiRes);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
import { MediaTypeModel } from '../models/MediaTypeModel';
|
import { MediaTypeModel } from '../models/MediaTypeModel';
|
||||||
import { MediaType } from '../utils/MediaType';
|
import { MediaType } from '../utils/MediaType';
|
||||||
|
import MediaDbPlugin from '../main';
|
||||||
|
|
||||||
export abstract class APIModel {
|
export abstract class APIModel {
|
||||||
apiName: string;
|
apiName: string;
|
||||||
apiUrl: string;
|
apiUrl: string;
|
||||||
apiDescription: string;
|
apiDescription: string;
|
||||||
types: MediaType[];
|
types: MediaType[];
|
||||||
|
plugin: MediaDbPlugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function should query the api and return a list of matches. The matches should be caped at 20.
|
* 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>;
|
abstract getById(id: string): Promise<MediaTypeModel>;
|
||||||
|
|
||||||
hasType(type: MediaType): boolean {
|
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 {
|
hasTypeOverlap(types: MediaType[]): boolean {
|
||||||
|
|
|
||||||
10
src/main.ts
10
src/main.ts
|
|
@ -39,7 +39,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
|
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
|
||||||
|
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
this.apiManager = new APIManager(this);
|
this.apiManager = new APIManager();
|
||||||
// register APIs
|
// register APIs
|
||||||
this.apiManager.registerAPI(new OMDbAPI(this));
|
this.apiManager.registerAPI(new OMDbAPI(this));
|
||||||
this.apiManager.registerAPI(new MALAPI(this));
|
this.apiManager.registerAPI(new MALAPI(this));
|
||||||
|
|
@ -156,7 +156,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
*/
|
*/
|
||||||
async createLinkWithSearchModal(): Promise<void> {
|
async createLinkWithSearchModal(): Promise<void> {
|
||||||
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
|
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) {
|
if (!apiSearchResults) {
|
||||||
|
|
@ -186,7 +186,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
let apiSearchResults: MediaTypeModel[] = await this.modalHelper.openSearchModal(searchModalOptions ?? {}, async searchModalData => {
|
let apiSearchResults: MediaTypeModel[] = await this.modalHelper.openSearchModal(searchModalOptions ?? {}, async searchModalData => {
|
||||||
types = searchModalData.types;
|
types = searchModalData.types;
|
||||||
const apis = this.apiManager.apis.filter(x => x.hasTypeOverlap(searchModalData.types)).map(x => x.apiName);
|
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) {
|
if (!apiSearchResults) {
|
||||||
|
|
@ -218,7 +218,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
|
|
||||||
async createEntryWithAdvancedSearchModal(): Promise<void> {
|
async createEntryWithAdvancedSearchModal(): Promise<void> {
|
||||||
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
|
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) {
|
if (!apiSearchResults) {
|
||||||
|
|
@ -570,7 +570,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
|
|
||||||
let results: MediaTypeModel[] = [];
|
let results: MediaTypeModel[] = [];
|
||||||
try {
|
try {
|
||||||
results = await this.apiManager.query(title, [selectedAPI], "");
|
results = await this.apiManager.query(title, [selectedAPI]);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
erroredFiles.push({ filePath: file.path, error: e.toString() });
|
erroredFiles.push({ filePath: file.path, error: e.toString() });
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue