fix: extract encryption type split functions

This commit is contained in:
Karol Sójko
2022-07-05 12:22:38 +02:00
parent d026b659fb
commit 2ec1a4dbb4
4 changed files with 25 additions and 22 deletions

View File

@@ -1,27 +1,6 @@
import { DecryptedPayloadInterface, EncryptedPayloadInterface } from '@standardnotes/models'
import { ItemContentTypeUsesRootKeyEncryption } from '../Keys/RootKey/Functions'
export interface EncryptionTypeSplit<T = EncryptedPayloadInterface | DecryptedPayloadInterface> {
rootKeyEncryption?: T[]
itemsKeyEncryption?: T[]
}
export function SplitPayloadsByEncryptionType<T extends EncryptedPayloadInterface | DecryptedPayloadInterface>(
payloads: T[],
): EncryptionTypeSplit<T> {
const usesRootKey: T[] = []
const usesItemsKey: T[] = []
for (const item of payloads) {
if (ItemContentTypeUsesRootKeyEncryption(item.content_type)) {
usesRootKey.push(item)
} else {
usesItemsKey.push(item)
}
}
return {
rootKeyEncryption: usesRootKey.length > 0 ? usesRootKey : undefined,
itemsKeyEncryption: usesItemsKey.length > 0 ? usesItemsKey : undefined,
}
}

View File

@@ -0,0 +1,23 @@
import { DecryptedPayloadInterface, EncryptedPayloadInterface } from '@standardnotes/models'
import { ItemContentTypeUsesRootKeyEncryption } from '../Keys/RootKey/Functions'
import { EncryptionTypeSplit } from './EncryptionTypeSplit'
export function SplitPayloadsByEncryptionType<T extends EncryptedPayloadInterface | DecryptedPayloadInterface>(
payloads: T[],
): EncryptionTypeSplit<T> {
const usesRootKey: T[] = []
const usesItemsKey: T[] = []
for (const item of payloads) {
if (ItemContentTypeUsesRootKeyEncryption(item.content_type)) {
usesRootKey.push(item)
} else {
usesItemsKey.push(item)
}
}
return {
rootKeyEncryption: usesRootKey.length > 0 ? usesRootKey : undefined,
itemsKeyEncryption: usesItemsKey.length > 0 ? usesItemsKey : undefined,
}
}