diff --git a/src/service-worker/command/api/store/api-store-changes.command.ts b/src/service-worker/command/api/store/api-store-changes.command.ts index 24fe6d9..0031ab2 100644 --- a/src/service-worker/command/api/store/api-store-changes.command.ts +++ b/src/service-worker/command/api/store/api-store-changes.command.ts @@ -24,6 +24,9 @@ export class ApiStoreChangesCommand extends ApiCallBase implements ICommand> { + constructor() { + super(); + } async execute(): Promise<{ data: HashChangeResponse[] } | undefined> { await this.initTokenData(); if (!this.storeUrl) return; diff --git a/src/service-worker/command/api/store/api-store-commit.command.ts b/src/service-worker/command/api/store/api-store-commit.command.ts index 71fe2b4..a995c96 100644 --- a/src/service-worker/command/api/store/api-store-commit.command.ts +++ b/src/service-worker/command/api/store/api-store-commit.command.ts @@ -21,7 +21,7 @@ import { ICommand } from '../../../../common/model/shared/common.dto'; import { fnConsoleLog } from '../../../../common/fn/fn-console'; export class ApiStoreCommitCommand extends ApiCallBase implements ICommand> { - constructor(private tx: string) { + constructor(private tx: BeginTxResponse) { super(); } async execute(): Promise { @@ -29,7 +29,7 @@ export class ApiStoreCommitCommand extends ApiCallBase implements ICommand( - `${this.storeUrl}/api/v1/tx/${this.tx}/commit`, + `${this.storeUrl}/api/v1/tx/${this.tx.tx}/commit`, { type: 'TEXT', headers: this.getAuthHeaders() diff --git a/src/service-worker/command/api/store/api-store.model.ts b/src/service-worker/command/api/store/api-store.model.ts index 7aa94ac..9a9601b 100644 --- a/src/service-worker/command/api/store/api-store.model.ts +++ b/src/service-worker/command/api/store/api-store.model.ts @@ -16,7 +16,6 @@ */ export interface BeginTxResponse { tx: string; - redirectAddress?: string; locked: boolean; lockedBy?: string; lockExpire: number; diff --git a/src/service-worker/command/api/store/obj/api-add-obj.command.ts b/src/service-worker/command/api/store/obj/api-obj-add.command.ts similarity index 91% rename from src/service-worker/command/api/store/obj/api-add-obj.command.ts rename to src/service-worker/command/api/store/obj/api-obj-add.command.ts index a8f5209..f414391 100644 --- a/src/service-worker/command/api/store/obj/api-add-obj.command.ts +++ b/src/service-worker/command/api/store/obj/api-obj-add.command.ts @@ -19,6 +19,7 @@ import { ApiCallBase } from '../../api-call.base'; import { FetchService } from '@pinmenote/fetch-service'; import { ICommand, ServerErrorDto } from '../../../../../common/model/shared/common.dto'; import { ApiErrorCode } from '../../../../../common/model/shared/api.error-code'; +import { BeginTxResponse } from '../api-store.model'; export interface ObjAddRequest { type: ObjTypeDto; @@ -30,15 +31,15 @@ export interface ObjAddResponse { serverId: number; } -export class ApiAddObjCommand extends ApiCallBase implements ICommand> { - constructor(private obj: ObjDto, private hash: string, private tx: string) { +export class ApiObjAddCommand extends ApiCallBase implements ICommand> { + constructor(private obj: ObjDto, private hash: string, private tx: BeginTxResponse) { super(); } async execute(): Promise { await this.initTokenData(); if (!this.storeUrl) return { code: ApiErrorCode.INTERNAL, message: 'ApiStoreAddObjCommand' }; const resp = await FetchService.fetch( - `${this.storeUrl}/api/v1/obj/${this.tx}`, + `${this.storeUrl}/api/v1/obj/${this.tx.tx}`, { headers: this.getAuthHeaders(), method: 'POST', diff --git a/src/service-worker/command/api/store/obj/api-obj-get-by-hash.command.ts b/src/service-worker/command/api/store/obj/api-obj-get-by-hash.command.ts index 002f44e..bb2c256 100644 --- a/src/service-worker/command/api/store/obj/api-obj-get-by-hash.command.ts +++ b/src/service-worker/command/api/store/obj/api-obj-get-by-hash.command.ts @@ -20,6 +20,7 @@ import { ObjTypeDto } from '../../../../../common/model/obj/obj.dto'; import { ICommand, ServerErrorDto } from '../../../../../common/model/shared/common.dto'; import { ApiErrorCode } from '../../../../../common/model/shared/api.error-code'; import { FetchService } from '@pinmenote/fetch-service'; +import { BeginTxResponse } from '../api-store.model'; export interface ObjSingleChange { serverId: number; @@ -30,7 +31,7 @@ export interface ObjSingleChange { } export class ApiObjGetByHashCommand extends ApiCallBase implements ICommand> { - constructor(private hash: string) { + constructor(private hash: string, private tx: BeginTxResponse) { super(); } async execute(): Promise { diff --git a/src/service-worker/command/api/store/segment/api-segment-add.command.ts b/src/service-worker/command/api/store/segment/api-segment-add.command.ts index 8b32202..ccf7c89 100644 --- a/src/service-worker/command/api/store/segment/api-segment-add.command.ts +++ b/src/service-worker/command/api/store/segment/api-segment-add.command.ts @@ -19,6 +19,7 @@ import { BeginTxResponse } from '../api-store.model'; import { FetchService } from '@pinmenote/fetch-service'; import { ICommand } from '../../../../../common/model/shared/common.dto'; import { SyncHashType } from '../../../sync/sync.model'; +import { fnConsoleLog } from '../../../../../common/fn/fn-console'; export interface FileDataDto { parent?: string; @@ -28,16 +29,33 @@ export interface FileDataDto { } export class ApiSegmentAddCommand extends ApiCallBase implements ICommand> { - constructor(private tx: string, private file: string, private data: FileDataDto) { + constructor(private tx: BeginTxResponse, private file: string, private data: FileDataDto) { super(); } async execute(): Promise { await this.initTokenData(); if (!this.storeUrl) return false; - if (await this.hasSegment()) return true; + if (await this.hasSegment()) return this.addRef(); return await this.addSegment(); } + async addRef(): Promise { + if (!this.data.parent) return true; + const authHeaders = this.getAuthHeaders(false); + const resp = await FetchService.fetch( + `${this.storeUrl!}/api/v1/segment/ref/${this.tx.tx}/${this.data.hash}/${this.data.parent}`, + { + type: 'TEXT', + headers: { + ...authHeaders + } + }, + this.refreshParams() + ); + fnConsoleLog('ApiSegmentAddCommand->addRef', resp); + return true; + } + async hasSegment(): Promise { const authHeaders = this.getAuthHeaders(); const resp = await FetchService.fetch( @@ -63,9 +81,8 @@ export class ApiSegmentAddCommand extends ApiCallBase implements ICommand> { async execute(): Promise { fnConsoleLog('SyncObjectCommand', this.tx); if (this.obj.server?.id) return; - const resp: ObjAddResponse | ServerErrorDto = await new ApiAddObjCommand(this.obj, this.hash, this.tx.tx).execute(); + const resp: ObjAddResponse | ServerErrorDto = await new ApiObjAddCommand(this.obj, this.hash, this.tx).execute(); if ('serverId' in resp) { return await this.saveServerId(resp.serverId); } else if ('code' in resp && resp.code === ApiErrorCode.SYNC_DUPLICATED_HASH) { @@ -38,7 +38,7 @@ export class SyncObjectCommand implements ICommand> { } private async setByHash(): Promise { - const resp: ObjSingleChange | ServerErrorDto = await new ApiObjGetByHashCommand(this.hash).execute(); + const resp: ObjSingleChange | ServerErrorDto = await new ApiObjGetByHashCommand(this.hash, this.tx).execute(); if ('serverId' in resp) { await this.saveServerId(resp.serverId); return; diff --git a/src/service-worker/command/sync/obj/sync-snapshot.command.ts b/src/service-worker/command/sync/obj/sync-snapshot.command.ts index 75e449d..a0149f0 100644 --- a/src/service-worker/command/sync/obj/sync-snapshot.command.ts +++ b/src/service-worker/command/sync/obj/sync-snapshot.command.ts @@ -51,14 +51,14 @@ export class SyncSnapshotCommand implements ICommand> private async syncSnapshot(snapshot: PageSnapshotDto, parent: string): Promise { // snapshot->info - await new ApiSegmentAddCommand(this.tx.tx, JSON.stringify(snapshot.info), { + await new ApiSegmentAddCommand(this.tx, JSON.stringify(snapshot.info), { hash: snapshot.info.hash, parent, type: SyncHashType.PageSnapshotInfoDto, key: TEMP_KEY }).execute(); // snapshot->data - await new ApiSegmentAddCommand(this.tx.tx, JSON.stringify(snapshot.data), { + await new ApiSegmentAddCommand(this.tx, JSON.stringify(snapshot.data), { hash: snapshot.data.hash, parent, type: SyncHashType.PageSnapshotDataDto, @@ -74,7 +74,7 @@ export class SyncSnapshotCommand implements ICommand> const segment = await new PageSegmentGetCommand(hash).execute(); if (!segment) return; - const isSynchronized = await new ApiSegmentAddCommand(this.tx.tx, JSON.stringify(segment), { + const isSynchronized = await new ApiSegmentAddCommand(this.tx, JSON.stringify(segment), { hash, parent, type: this.convertSegmentTypeSyncHashType(segment.type), diff --git a/src/service-worker/command/sync/sync-tx.helper.ts b/src/service-worker/command/sync/sync-tx.helper.ts index f04974e..880fdc5 100644 --- a/src/service-worker/command/sync/sync-tx.helper.ts +++ b/src/service-worker/command/sync/sync-tx.helper.ts @@ -38,7 +38,7 @@ export class SyncTxHelper { const tx = await BrowserStorage.get(ObjectStoreKeys.SYNC_TX); if (!tx) return; fnConsoleLog('SyncServerCommand->commit', tx); - await new ApiStoreCommitCommand(tx.tx).execute(); + await new ApiStoreCommitCommand(tx).execute(); await BrowserStorage.remove(ObjectStoreKeys.SYNC_TX); }