Skip to content

Commit 5b35e55

Browse files
committed
Second attempt to selectively turn off APIs
1 parent 54766bb commit 5b35e55

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/api/APIManager.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { APIModel } from './APIModel';
22
import { MediaTypeModel } from '../models/MediaTypeModel';
3-
import MediaDbPlugin from '../main';
4-
53

64
export class APIManager {
7-
plugin: MediaDbPlugin;
85
apis: APIModel[];
96

10-
constructor(plugin: MediaDbPlugin) {
11-
this.plugin = plugin;
7+
constructor() {
128
this.apis = [];
139
}
1410

@@ -18,12 +14,13 @@ export class APIManager {
1814
* @param query
1915
* @param apisToQuery
2016
*/
21-
async query(query: string, apisToQuery: string[], mediaType: string): Promise<MediaTypeModel[]> {
17+
async query(query: string, apisToQuery: string[]): Promise<MediaTypeModel[]> {
2218
console.debug(`MDB | api manager queried with "${query}"`);
2319

2420
let res: MediaTypeModel[] = [];
21+
2522
for (const api of this.apis) {
26-
if (apisToQuery.contains(api.apiName) && !(this.plugin.settings[[api.apiName, mediaType].filter(s => s).join('') as keyof typeof this.plugin.settings] === false)) {
23+
if (apisToQuery.contains(api.apiName)) {
2724
const apiRes = await api.searchByTitle(query);
2825
res = res.concat(apiRes);
2926
}

src/api/APIModel.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { MediaTypeModel } from '../models/MediaTypeModel';
22
import { MediaType } from '../utils/MediaType';
3+
import MediaDbPlugin from '../main';
34

45
export abstract class APIModel {
56
apiName: string;
67
apiUrl: string;
78
apiDescription: string;
89
types: MediaType[];
10+
plugin: MediaDbPlugin;
911

1012
/**
1113
* 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 {
1719
abstract getById(id: string): Promise<MediaTypeModel>;
1820

1921
hasType(type: MediaType): boolean {
20-
return this.types.contains(type);
22+
if (this.types.contains(type) && !(this.plugin.settings[[this.apiName, type.toString()].filter(s => s).join('') as keyof typeof this.plugin.settings] === false)){
23+
return true;
24+
}
2125
}
2226

2327
hasTypeOverlap(types: MediaType[]): boolean {
@@ -28,4 +32,4 @@ export abstract class APIModel {
2832
}
2933
return false;
3034
}
31-
}
35+
}

src/main.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class MediaDbPlugin extends Plugin {
3939
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
4040

4141
async onload(): Promise<void> {
42-
this.apiManager = new APIManager(this);
42+
this.apiManager = new APIManager();
4343
// register APIs
4444
this.apiManager.registerAPI(new OMDbAPI(this));
4545
this.apiManager.registerAPI(new MALAPI(this));
@@ -156,7 +156,7 @@ export default class MediaDbPlugin extends Plugin {
156156
*/
157157
async createLinkWithSearchModal(): Promise<void> {
158158
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
159-
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis, "");
159+
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis);
160160
});
161161

162162
if (!apiSearchResults) {
@@ -186,7 +186,7 @@ export default class MediaDbPlugin extends Plugin {
186186
let apiSearchResults: MediaTypeModel[] = await this.modalHelper.openSearchModal(searchModalOptions ?? {}, async searchModalData => {
187187
types = searchModalData.types;
188188
const apis = this.apiManager.apis.filter(x => x.hasTypeOverlap(searchModalData.types)).map(x => x.apiName);
189-
return await this.apiManager.query(searchModalData.query, apis, searchModalData.types.toString());
189+
return await this.apiManager.query(searchModalData.query, apis);
190190
});
191191

192192
if (!apiSearchResults) {
@@ -218,7 +218,7 @@ export default class MediaDbPlugin extends Plugin {
218218

219219
async createEntryWithAdvancedSearchModal(): Promise<void> {
220220
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal({}, async advancedSearchModalData => {
221-
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis, "");
221+
return await this.apiManager.query(advancedSearchModalData.query, advancedSearchModalData.apis);
222222
});
223223

224224
if (!apiSearchResults) {
@@ -570,7 +570,7 @@ export default class MediaDbPlugin extends Plugin {
570570

571571
let results: MediaTypeModel[] = [];
572572
try {
573-
results = await this.apiManager.query(title, [selectedAPI], "");
573+
results = await this.apiManager.query(title, [selectedAPI]);
574574
} catch (e) {
575575
erroredFiles.push({ filePath: file.path, error: e.toString() });
576576
continue;
@@ -671,4 +671,4 @@ export default class MediaDbPlugin extends Plugin {
671671

672672
await this.saveData(this.settings);
673673
}
674-
}
674+
}

0 commit comments

Comments
 (0)