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,71 @@
import { Uuid } from '@standardnotes/common'
import { Copy } from '@standardnotes/utils'
import { SyncResolvedParams, SyncResolvedPayload } from '../../../Runtime/Deltas/Utilities/SyncResolvedPayload'
import { FillItemContent, ItemContent } from '../../Content/ItemContent'
import { ContentReference } from '../../Reference/ContentReference'
import { DecryptedTransferPayload } from '../../TransferPayload/Interfaces/DecryptedTransferPayload'
import { DecryptedPayloadInterface } from '../Interfaces/DecryptedPayload'
import { PayloadSource } from '../Types/PayloadSource'
import { PurePayload } from './PurePayload'
export class DecryptedPayload<
C extends ItemContent = ItemContent,
T extends DecryptedTransferPayload<C> = DecryptedTransferPayload<C>,
>
extends PurePayload<T>
implements DecryptedPayloadInterface<C>
{
override readonly content: C
override readonly deleted: false
constructor(rawPayload: T, source = PayloadSource.Constructor) {
super(rawPayload, source)
this.content = Copy(FillItemContent<C>(rawPayload.content))
this.deleted = false
}
get references(): ContentReference[] {
return this.content.references || []
}
public getReference(uuid: Uuid): ContentReference {
const result = this.references.find((ref) => ref.uuid === uuid)
if (!result) {
throw new Error('Reference not found')
}
return result
}
override ejected(): DecryptedTransferPayload<C> {
return {
...super.ejected(),
content: this.content,
deleted: this.deleted,
}
}
copy(override?: Partial<T>, source = this.source): this {
const result = new DecryptedPayload(
{
...this.ejected(),
...override,
},
source,
)
return result as this
}
copyAsSyncResolved(override?: Partial<T> & SyncResolvedParams, source = this.source): SyncResolvedPayload {
const result = new DecryptedPayload(
{
...this.ejected(),
...override,
},
source,
)
return result as SyncResolvedPayload
}
}

View File

@@ -0,0 +1,54 @@
import { DeletedTransferPayload } from './../../TransferPayload/Interfaces/DeletedTransferPayload'
import { DeletedPayloadInterface } from '../Interfaces/DeletedPayload'
import { PayloadSource } from '../Types/PayloadSource'
import { PurePayload } from './PurePayload'
import { SyncResolvedParams, SyncResolvedPayload } from '../../../Runtime/Deltas/Utilities/SyncResolvedPayload'
export class DeletedPayload extends PurePayload<DeletedTransferPayload> implements DeletedPayloadInterface {
override readonly deleted: true
override readonly content: undefined
constructor(rawPayload: DeletedTransferPayload, source = PayloadSource.Constructor) {
super(rawPayload, source)
this.deleted = true
this.content = undefined
}
get discardable(): boolean | undefined {
return !this.dirty
}
override ejected(): DeletedTransferPayload {
return {
...super.ejected(),
deleted: this.deleted,
content: undefined,
}
}
copy(override?: Partial<DeletedTransferPayload>, source = this.source): this {
const result = new DeletedPayload(
{
...this.ejected(),
...override,
},
source,
)
return result as this
}
copyAsSyncResolved(
override?: Partial<DeletedTransferPayload> & SyncResolvedParams,
source = this.source,
): SyncResolvedPayload {
const result = new DeletedPayload(
{
...this.ejected(),
...override,
},
source,
)
return result as SyncResolvedPayload
}
}

View File

@@ -0,0 +1,68 @@
import { ProtocolVersion, protocolVersionFromEncryptedString } from '@standardnotes/common'
import { SyncResolvedParams, SyncResolvedPayload } from '../../../Runtime/Deltas/Utilities/SyncResolvedPayload'
import { EncryptedTransferPayload } from '../../TransferPayload/Interfaces/EncryptedTransferPayload'
import { EncryptedPayloadInterface } from '../Interfaces/EncryptedPayload'
import { PayloadSource } from '../Types/PayloadSource'
import { PurePayload } from './PurePayload'
export class EncryptedPayload extends PurePayload<EncryptedTransferPayload> implements EncryptedPayloadInterface {
override readonly content: string
override readonly deleted: false
readonly auth_hash?: string
readonly enc_item_key: string
readonly errorDecrypting: boolean
readonly items_key_id: string | undefined
readonly version: ProtocolVersion
readonly waitingForKey: boolean
constructor(rawPayload: EncryptedTransferPayload, source = PayloadSource.Constructor) {
super(rawPayload, source)
this.auth_hash = rawPayload.auth_hash
this.content = rawPayload.content
this.deleted = false
this.enc_item_key = rawPayload.enc_item_key
this.errorDecrypting = rawPayload.errorDecrypting
this.items_key_id = rawPayload.items_key_id
this.version = protocolVersionFromEncryptedString(this.content)
this.waitingForKey = rawPayload.waitingForKey
}
override ejected(): EncryptedTransferPayload {
return {
...super.ejected(),
enc_item_key: this.enc_item_key,
items_key_id: this.items_key_id,
auth_hash: this.auth_hash,
errorDecrypting: this.errorDecrypting,
waitingForKey: this.waitingForKey,
content: this.content,
deleted: this.deleted,
}
}
copy(override?: Partial<EncryptedTransferPayload>, source = this.source): this {
const result = new EncryptedPayload(
{
...this.ejected(),
...override,
},
source,
)
return result as this
}
copyAsSyncResolved(
override?: Partial<EncryptedTransferPayload> & SyncResolvedParams,
source = this.source,
): SyncResolvedPayload {
const result = new EncryptedPayload(
{
...this.ejected(),
...override,
},
source,
)
return result as SyncResolvedPayload
}
}

View File

@@ -0,0 +1,104 @@
import { ContentType } from '@standardnotes/common'
import { deepFreeze, useBoolean } from '@standardnotes/utils'
import { PayloadInterface } from '../Interfaces/PayloadInterface'
import { PayloadSource } from '../Types/PayloadSource'
import { TransferPayload } from '../../TransferPayload/Interfaces/TransferPayload'
import { ItemContent } from '../../Content/ItemContent'
import { SyncResolvedParams, SyncResolvedPayload } from '../../../Runtime/Deltas/Utilities/SyncResolvedPayload'
type RequiredKeepUndefined<T> = { [K in keyof T]-?: [T[K]] } extends infer U
? U extends Record<keyof U, [unknown]>
? { [K in keyof U]: U[K][0] }
: never
: never
export abstract class PurePayload<T extends TransferPayload<C>, C extends ItemContent = ItemContent>
implements PayloadInterface<T>
{
readonly source: PayloadSource
readonly uuid: string
readonly content_type: ContentType
readonly deleted: boolean
readonly content: C | string | undefined
readonly created_at: Date
readonly updated_at: Date
readonly created_at_timestamp: number
readonly updated_at_timestamp: number
readonly dirtyIndex?: number
readonly globalDirtyIndexAtLastSync?: number
readonly dirty?: boolean
readonly lastSyncBegan?: Date
readonly lastSyncEnd?: Date
readonly duplicate_of?: string
constructor(rawPayload: T, source = PayloadSource.Constructor) {
this.source = source
this.uuid = rawPayload.uuid
if (!this.uuid) {
throw Error(
`Attempting to construct payload with null uuid
Content type: ${rawPayload.content_type}`,
)
}
this.content = rawPayload.content
this.content_type = rawPayload.content_type
this.deleted = useBoolean(rawPayload.deleted, false)
this.dirty = rawPayload.dirty
this.duplicate_of = rawPayload.duplicate_of
this.created_at = new Date(rawPayload.created_at || new Date())
this.updated_at = new Date(rawPayload.updated_at || 0)
this.created_at_timestamp = rawPayload.created_at_timestamp || 0
this.updated_at_timestamp = rawPayload.updated_at_timestamp || 0
this.lastSyncBegan = rawPayload.lastSyncBegan ? new Date(rawPayload.lastSyncBegan) : undefined
this.lastSyncEnd = rawPayload.lastSyncEnd ? new Date(rawPayload.lastSyncEnd) : undefined
this.dirtyIndex = rawPayload.dirtyIndex
this.globalDirtyIndexAtLastSync = rawPayload.globalDirtyIndexAtLastSync
const timeToAllowSubclassesToFinishConstruction = 0
setTimeout(() => {
deepFreeze(this)
}, timeToAllowSubclassesToFinishConstruction)
}
ejected(): TransferPayload {
const comprehensive: RequiredKeepUndefined<TransferPayload> = {
uuid: this.uuid,
content: this.content,
deleted: this.deleted,
content_type: this.content_type,
created_at: this.created_at,
updated_at: this.updated_at,
created_at_timestamp: this.created_at_timestamp,
updated_at_timestamp: this.updated_at_timestamp,
dirty: this.dirty,
duplicate_of: this.duplicate_of,
dirtyIndex: this.dirtyIndex,
globalDirtyIndexAtLastSync: this.globalDirtyIndexAtLastSync,
lastSyncBegan: this.lastSyncBegan,
lastSyncEnd: this.lastSyncEnd,
}
return comprehensive
}
public get serverUpdatedAt(): Date {
return this.updated_at
}
public get serverUpdatedAtTimestamp(): number {
return this.updated_at_timestamp
}
abstract copy(override?: Partial<TransferPayload>, source?: PayloadSource): this
abstract copyAsSyncResolved(override?: Partial<T> & SyncResolvedParams, source?: PayloadSource): SyncResolvedPayload
}