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

57 lines
1.8 KiB
TypeScript

import { Get, Injectable } from '@nestjs/common'
import {
AbstractKeysProvider,
GetOperationResult,
type AdditionalData,
} from './abstract.provider.js'
import { Indexes, InfoLine } from '../indexes.js'
import { OpenPGPKeysProvider } from './openpgp.provider.js'
import { UbuntuKeysProvider } from './ubuntu.provider.js'
/**
* This provider searches all key providers and returns their combined result
*/
@Injectable()
export class AllKeysProvider implements AbstractKeysProvider {
readonly url = Object.freeze('http://none')
constructor(
private openPgpKeysProvider: OpenPGPKeysProvider,
private ubuntuKeysProvider: UbuntuKeysProvider
) {}
getAll(): AbstractKeysProvider[] {
return [this.openPgpKeysProvider, this.ubuntuKeysProvider]
}
@Get()
async get(search: string, data: AdditionalData): Promise<GetOperationResult> {
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
}
}