({keyringId, allUsers = true})
| 360 | * @return {Array<Object>} list of recipients objects in the form {keyId, fingerprint, userId, email, name} |
| 361 | */ |
| 362 | export async function getKeyData({keyringId, allUsers = true}) { |
| 363 | await keyringInitialized; |
| 364 | let result = []; |
| 365 | let keyrings; |
| 366 | if (keyringId) { |
| 367 | keyrings = getPreferredKeyringQueue(keyringId); |
| 368 | } else { |
| 369 | keyrings = await getAll(); |
| 370 | } |
| 371 | for (const keyring of keyrings) { |
| 372 | const keyDataArray = await keyring.getKeyData({allUsers}); |
| 373 | for (const keyData of keyDataArray) { |
| 374 | keyData.keyring = keyring; |
| 375 | // check if key for this fingerprint already exists in result list |
| 376 | const keyIndex = result.findIndex(element => keyData.fingerprint === element.fingerprint); |
| 377 | if (keyIndex === -1) { |
| 378 | // key does not exist, add to result list |
| 379 | result.push(keyData); |
| 380 | continue; |
| 381 | } |
| 382 | // key already in result list |
| 383 | const existing = result[keyIndex]; |
| 384 | if (getLastModifiedDate(existing.key) < getLastModifiedDate(keyData.key)) { |
| 385 | // current key is more recent then existing key -> replace |
| 386 | result[keyIndex] = keyData; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | // filter out all invalid keys |
| 391 | result = await filterAsync(result, key => isValidEncryptionKey(key.key)); |
| 392 | // expand users |
| 393 | let expanded = []; |
| 394 | for (const keyData of result) { |
| 395 | for (const user of keyData.users) { |
| 396 | expanded.push({...keyData, ...user}); |
| 397 | } |
| 398 | } |
| 399 | if (prefs.keyserver.key_binding) { |
| 400 | expanded.sort((a, b) => a.email.localeCompare(b.email)); |
| 401 | expanded = await expanded.reduce(async (accPromise, current) => { |
| 402 | const accumulator = await accPromise; |
| 403 | const length = accumulator.length; |
| 404 | const last = length && accumulator[length - 1]; |
| 405 | if (length === 0 || last.email !== current.email) { |
| 406 | accumulator.push(current); |
| 407 | return accumulator; |
| 408 | } |
| 409 | last.binding = typeof last.binding === 'undefined' ? await isKeyBound(last.keyring, last.email, last.key) : last.binding; |
| 410 | last.lastModified = last.lastModified || getLastModifiedDate(last.key); |
| 411 | current.binding = typeof current.binding === 'undefined' ? await isKeyBound(current.keyring, current.email, current.key) : current.binding; |
| 412 | current.lastModified = current.lastModified || getLastModifiedDate(current.key); |
| 413 | if (!last.binding && current.binding || |
| 414 | last.binding === current.binding && last.lastModified < current.lastModified) { |
| 415 | accumulator.pop(); |
| 416 | accumulator.push(current); |
| 417 | } |
| 418 | return accumulator; |
| 419 | }, Promise.resolve([])); |
no test coverage detected