feat: add models package

This commit is contained in:
Karol Sójko
2022-07-05 20:47:11 +02:00
parent 60d1554ff7
commit b614c71e79
199 changed files with 8772 additions and 22 deletions

View File

@@ -0,0 +1,60 @@
import { Uuid } from '@standardnotes/common'
import { ContextPayload } from './ContextPayload'
import { ItemContent } from '../Content/ItemContent'
import { DecryptedTransferPayload, EncryptedTransferPayload } from '../TransferPayload'
export interface BackupFileEncryptedContextualPayload extends ContextPayload {
auth_hash?: string
content: string
created_at_timestamp: number
created_at: Date
duplicate_of?: Uuid
enc_item_key: string
items_key_id: string | undefined
updated_at: Date
updated_at_timestamp: number
}
export interface BackupFileDecryptedContextualPayload<C extends ItemContent = ItemContent> extends ContextPayload {
content: C
created_at_timestamp: number
created_at: Date
duplicate_of?: Uuid
updated_at: Date
updated_at_timestamp: number
}
export function CreateEncryptedBackupFileContextPayload(
fromPayload: EncryptedTransferPayload,
): BackupFileEncryptedContextualPayload {
return {
auth_hash: fromPayload.auth_hash,
content_type: fromPayload.content_type,
content: fromPayload.content,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: false,
duplicate_of: fromPayload.duplicate_of,
enc_item_key: fromPayload.enc_item_key,
items_key_id: fromPayload.items_key_id,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
}
}
export function CreateDecryptedBackupFileContextPayload(
fromPayload: DecryptedTransferPayload,
): BackupFileDecryptedContextualPayload {
return {
content_type: fromPayload.content_type,
content: fromPayload.content,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: false,
duplicate_of: fromPayload.duplicate_of,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
}
}

View File

@@ -0,0 +1,24 @@
import { ItemContent } from '../Content/ItemContent'
import { DecryptedTransferPayload } from '../TransferPayload'
import { ContextPayload } from './ContextPayload'
/**
* Represents a payload with permissible fields for when a
* component wants to create a new item
*/
export interface ComponentCreateContextualPayload<C extends ItemContent = ItemContent> extends ContextPayload {
content: C
created_at?: Date
}
export function createComponentCreatedContextPayload(
fromPayload: DecryptedTransferPayload,
): ComponentCreateContextualPayload {
return {
content_type: fromPayload.content_type,
content: fromPayload.content,
created_at: fromPayload.created_at,
deleted: false,
uuid: fromPayload.uuid,
}
}

View File

@@ -0,0 +1,24 @@
import { ItemContent } from '../Content/ItemContent'
import { DecryptedTransferPayload } from '../TransferPayload'
import { ContextPayload } from './ContextPayload'
/**
* Represents a payload with permissible fields for when a
* payload is retrieved from a component for saving
*/
export interface ComponentRetrievedContextualPayload<C extends ItemContent = ItemContent> extends ContextPayload {
content: C
created_at?: Date
}
export function CreateComponentRetrievedContextPayload(
fromPayload: DecryptedTransferPayload,
): ComponentRetrievedContextualPayload {
return {
content_type: fromPayload.content_type,
content: fromPayload.content,
created_at: fromPayload.created_at,
deleted: false,
uuid: fromPayload.uuid,
}
}

View File

@@ -0,0 +1,9 @@
import { ContentType } from '@standardnotes/common'
import { ItemContent } from '../Content/ItemContent'
export interface ContextPayload<C extends ItemContent = ItemContent> {
uuid: string
content_type: ContentType
content: C | string | undefined
deleted: boolean
}

View File

@@ -0,0 +1,25 @@
import { ServerItemResponse } from '@standardnotes/responses'
import { isCorruptTransferPayload, isEncryptedTransferPayload } from '../TransferPayload'
export interface FilteredServerItem extends ServerItemResponse {
__passed_filter__: true
}
export function CreateFilteredServerItem(item: ServerItemResponse): FilteredServerItem {
return {
...item,
__passed_filter__: true,
}
}
export function FilterDisallowedRemotePayloadsAndMap(payloads: ServerItemResponse[]): FilteredServerItem[] {
return payloads.filter(isRemotePayloadAllowed).map(CreateFilteredServerItem)
}
export function isRemotePayloadAllowed(payload: ServerItemResponse): boolean {
if (isCorruptTransferPayload(payload)) {
return false
}
return isEncryptedTransferPayload(payload) || payload.content == undefined
}

View File

@@ -0,0 +1,107 @@
import { Uuid } from '@standardnotes/common'
import { ContextPayload } from './ContextPayload'
import { ItemContent } from '../Content/ItemContent'
import { DecryptedPayloadInterface, DeletedPayloadInterface, EncryptedPayloadInterface } from '../Payload'
import { useBoolean } from '@standardnotes/utils'
import { EncryptedTransferPayload, isEncryptedTransferPayload } from '../TransferPayload'
export function isEncryptedLocalStoragePayload(
p: LocalStorageEncryptedContextualPayload | LocalStorageDecryptedContextualPayload,
): p is LocalStorageEncryptedContextualPayload {
return isEncryptedTransferPayload(p as EncryptedTransferPayload)
}
export interface LocalStorageEncryptedContextualPayload extends ContextPayload {
auth_hash?: string
auth_params?: unknown
content: string
deleted: false
created_at_timestamp: number
created_at: Date
dirty: boolean
duplicate_of: Uuid | undefined
enc_item_key: string
errorDecrypting: boolean
items_key_id: string | undefined
updated_at_timestamp: number
updated_at: Date
waitingForKey: boolean
}
export interface LocalStorageDecryptedContextualPayload<C extends ItemContent = ItemContent> extends ContextPayload {
content: C
created_at_timestamp: number
created_at: Date
deleted: false
dirty: boolean
duplicate_of?: Uuid
updated_at_timestamp: number
updated_at: Date
}
export interface LocalStorageDeletedContextualPayload extends ContextPayload {
content: undefined
created_at_timestamp: number
created_at: Date
deleted: true
dirty: true
duplicate_of?: Uuid
updated_at_timestamp: number
updated_at: Date
}
export function CreateEncryptedLocalStorageContextPayload(
fromPayload: EncryptedPayloadInterface,
): LocalStorageEncryptedContextualPayload {
return {
auth_hash: fromPayload.auth_hash,
content_type: fromPayload.content_type,
content: fromPayload.content,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: false,
dirty: fromPayload.dirty != undefined ? fromPayload.dirty : false,
duplicate_of: fromPayload.duplicate_of,
enc_item_key: fromPayload.enc_item_key,
errorDecrypting: fromPayload.errorDecrypting,
items_key_id: fromPayload.items_key_id,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
waitingForKey: fromPayload.waitingForKey,
}
}
export function CreateDecryptedLocalStorageContextPayload(
fromPayload: DecryptedPayloadInterface,
): LocalStorageDecryptedContextualPayload {
return {
content_type: fromPayload.content_type,
content: fromPayload.content,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: false,
duplicate_of: fromPayload.duplicate_of,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
dirty: useBoolean(fromPayload.dirty, false),
}
}
export function CreateDeletedLocalStorageContextPayload(
fromPayload: DeletedPayloadInterface,
): LocalStorageDeletedContextualPayload {
return {
content_type: fromPayload.content_type,
content: undefined,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: true,
dirty: true,
duplicate_of: fromPayload.duplicate_of,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
}
}

View File

@@ -0,0 +1,41 @@
import { Uuid } from '@standardnotes/common'
import { ItemContent } from '../Content/ItemContent'
import { DecryptedPayloadInterface, DeletedPayloadInterface, isDecryptedPayload } from '../Payload'
import { ContextPayload } from './ContextPayload'
export interface OfflineSyncPushContextualPayload extends ContextPayload {
content: ItemContent | undefined
created_at_timestamp: number
created_at: Date
duplicate_of?: Uuid
updated_at_timestamp: number
updated_at: Date
}
export function CreateOfflineSyncPushContextPayload(
fromPayload: DecryptedPayloadInterface | DeletedPayloadInterface,
): OfflineSyncPushContextualPayload {
const base: OfflineSyncPushContextualPayload = {
content: undefined,
content_type: fromPayload.content_type,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: false,
duplicate_of: fromPayload.duplicate_of,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
}
if (isDecryptedPayload(fromPayload)) {
return {
...base,
content: fromPayload.content,
}
} else {
return {
...base,
deleted: fromPayload.deleted,
}
}
}

View File

@@ -0,0 +1,30 @@
import { ContentType } from '@standardnotes/common'
import { DecryptedPayloadInterface, DeletedPayloadInterface, isDeletedPayload } from '../Payload'
/**
* The saved sync item payload represents the payload we want to map
* when mapping saved_items from the server or local sync mechanism. We only want to map the
* updated_at value the server returns for the item, and basically
* nothing else.
*/
export interface OfflineSyncSavedContextualPayload {
content_type: ContentType
created_at_timestamp: number
deleted: boolean
updated_at_timestamp?: number
updated_at: Date
uuid: string
}
export function CreateOfflineSyncSavedPayload(
fromPayload: DecryptedPayloadInterface | DeletedPayloadInterface,
): OfflineSyncSavedContextualPayload {
return {
content_type: fromPayload.content_type,
created_at_timestamp: fromPayload.created_at_timestamp,
deleted: isDeletedPayload(fromPayload),
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
}
}

View File

@@ -0,0 +1,50 @@
import { Uuid } from '@standardnotes/common'
import { DeletedPayloadInterface, EncryptedPayloadInterface } from '../Payload'
import { ContextPayload } from './ContextPayload'
export interface ServerSyncPushContextualPayload extends ContextPayload {
auth_hash?: string
content: string | undefined
created_at_timestamp: number
created_at: Date
duplicate_of?: Uuid
enc_item_key?: string
items_key_id?: string
updated_at_timestamp: number
updated_at: Date
}
export function CreateEncryptedServerSyncPushPayload(
fromPayload: EncryptedPayloadInterface,
): ServerSyncPushContextualPayload {
return {
content_type: fromPayload.content_type,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: false,
duplicate_of: fromPayload.duplicate_of,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
content: fromPayload.content,
enc_item_key: fromPayload.enc_item_key,
items_key_id: fromPayload.items_key_id,
auth_hash: fromPayload.auth_hash,
}
}
export function CreateDeletedServerSyncPushPayload(
fromPayload: DeletedPayloadInterface,
): ServerSyncPushContextualPayload {
return {
content_type: fromPayload.content_type,
created_at_timestamp: fromPayload.created_at_timestamp,
created_at: fromPayload.created_at,
deleted: true,
duplicate_of: fromPayload.duplicate_of,
updated_at_timestamp: fromPayload.updated_at_timestamp,
updated_at: fromPayload.updated_at,
uuid: fromPayload.uuid,
content: undefined,
}
}

View File

@@ -0,0 +1,31 @@
import { useBoolean } from '@standardnotes/utils'
import { FilteredServerItem } from './FilteredServerItem'
import { ContentType } from '@standardnotes/common'
/**
* The saved sync item payload represents the payload we want to map
* when mapping saved_items from the server. We only want to map the
* updated_at value the server returns for the item, and basically
* nothing else.
*/
export interface ServerSyncSavedContextualPayload {
content_type: ContentType
created_at_timestamp: number
created_at: Date
deleted: boolean
updated_at_timestamp: number
updated_at: Date
uuid: string
}
export function CreateServerSyncSavedPayload(from: FilteredServerItem): ServerSyncSavedContextualPayload {
return {
content_type: from.content_type,
created_at_timestamp: from.created_at_timestamp,
created_at: from.created_at,
deleted: useBoolean(from.deleted, false),
updated_at_timestamp: from.updated_at_timestamp,
updated_at: from.updated_at,
uuid: from.uuid,
}
}

View File

@@ -0,0 +1,7 @@
import { ItemContent } from '../Content/ItemContent'
import { ContextPayload } from './ContextPayload'
export interface SessionHistoryContextualPayload<C extends ItemContent = ItemContent> extends ContextPayload {
content: C
updated_at: Date
}

View File

@@ -0,0 +1,10 @@
export * from './ComponentCreate'
export * from './ComponentRetrieved'
export * from './BackupFile'
export * from './LocalStorage'
export * from './OfflineSyncPush'
export * from './OfflineSyncSaved'
export * from './ServerSyncPush'
export * from './SessionHistory'
export * from './ServerSyncSaved'
export * from './FilteredServerItem'