cupidgpg/src/keys/providers/all.provider.ts

48 lines
1.5 KiB
TypeScript

import { Get, Injectable } from "@nestjs/common";
import { AbstractKeysProvider, type AdditionalData } from "./abstract.provider.js";
import { Indexes, InfoLine } from "../indexes.js";
import { OpenPGPKeysProvider } from "./openpgp.provider.js";
/**
* This provider searches all key providers and returns their combined result
*/
@Injectable()
export class AllKeysProvider implements AbstractKeysProvider {
constructor(
private openPgpKeysProvider: OpenPGPKeysProvider
) {}
getAll(): AbstractKeysProvider[] {
return [
this.openPgpKeysProvider
]
}
@Get()
async get(search: string, data: AdditionalData): Promise<string | 404> {
const all = this.getAll();
const promises = await Promise.all(all.map(x => x.get(search, data)))
if (promises.filter(x => x == 404).length == promises.length) { // all failed
return 404
} else {
// if there are multiple keys, join them together to avoid missing data
return promises
.filter(x => typeof x === 'string')
.join('\n')
}
}
@Get()
async index(search: string, data: AdditionalData): Promise<Indexes> {
const all = this.getAll();
const promises = await Promise.all(all.map(x => x.index(search, data)))
// merge indexes if there are multiple
const out = [ new InfoLine('info:1:1') ] as Indexes;
promises.forEach(x => x.filter(x => x.prefix !== 'info').forEach(y => out.push(y)));
return out;
}
}