diff --git a/src/app/features/home/capture-tab/capture-item/capture-item.component.ts b/src/app/features/home/capture-tab/capture-item/capture-item.component.ts index cc1b6473c..9ce20305c 100644 --- a/src/app/features/home/capture-tab/capture-item/capture-item.component.ts +++ b/src/app/features/home/capture-tab/capture-item/capture-item.component.ts @@ -71,10 +71,7 @@ export class CaptureItemComponent { ) ); - readonly hasCaption$ = this.proof$.pipe( - switchMap(proof => this.diaBackendAssetRepository.fetchByProof$(proof)), - map(asset => asset.caption !== '') - ); + readonly hasCaption$ = this.proof$.pipe(map(proof => proof.caption !== '')); readonly isVideo$ = this.proof$.pipe( concatMap(proof => proof.getFirstAssetMeta()), diff --git a/src/app/features/home/details/edit-caption/edit-caption.page.ts b/src/app/features/home/details/edit-caption/edit-caption.page.ts index f5208a6e5..f3eb4e47b 100644 --- a/src/app/features/home/details/edit-caption/edit-caption.page.ts +++ b/src/app/features/home/details/edit-caption/edit-caption.page.ts @@ -3,13 +3,22 @@ import { DomSanitizer } from '@angular/platform-browser'; import { ActivatedRoute } from '@angular/router'; import { NavController } from '@ionic/angular'; import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; -import { combineLatest, fromEvent } from 'rxjs'; -import { map, tap } from 'rxjs/operators'; +import { combineLatest, fromEvent, of } from 'rxjs'; +import { + concatMap, + finalize, + first, + map, + tap as switchTap, +} from 'rxjs/operators'; import { DiaBackendAuthService } from '../../../../shared/dia-backend/auth/dia-backend-auth.service'; import { BUBBLE_IFRAME_URL } from '../../../../shared/dia-backend/secret'; import { BubbleToIonicPostMessage } from '../../../../shared/iframe/iframe'; import { IframeService } from '../../../../shared/iframe/iframe.service'; +import { getOldProof } from '../../../../shared/repositories/proof/old-proof-adapter'; +import { ProofRepository } from '../../../../shared/repositories/proof/proof-repository.service'; import { isNonNullable } from '../../../../utils/rx-operators/rx-operators'; +import { InformationSessionService } from '../information/session/information-session.service'; @UntilDestroy() @Component({ @@ -43,7 +52,9 @@ export class EditCaptionPage { private readonly sanitizer: DomSanitizer, private readonly navController: NavController, private readonly iframeService: IframeService, - private readonly diaBackendAuthService: DiaBackendAuthService + private readonly diaBackendAuthService: DiaBackendAuthService, + private readonly informationSessionService: InformationSessionService, + private readonly proofRepository: ProofRepository ) { this.processIframeEvents(); } @@ -51,7 +62,7 @@ export class EditCaptionPage { processIframeEvents() { fromEvent(window, 'message') .pipe( - tap(event => { + switchTap(event => { const postMessageEvent = event as MessageEvent; const data = postMessageEvent.data as BubbleToIonicPostMessage; switch (data) { @@ -60,7 +71,7 @@ export class EditCaptionPage { break; case BubbleToIonicPostMessage.EDIT_CAPTION_SAVE: this.iframeService.refreshDetailsPageIframe(); - this.navController.back(); + this.syncCaptionAndNavigateBack(); break; } }), @@ -68,4 +79,28 @@ export class EditCaptionPage { ) .subscribe(); } + + syncCaptionAndNavigateBack() { + if (this.informationSessionService.activatedDetailedCapture) { + combineLatest([ + this.informationSessionService.activatedDetailedCapture.proof$, + this.informationSessionService.activatedDetailedCapture.caption$, + ]) + .pipe( + first(), + concatMap(([proof, latestCaptionFromBackend]) => { + if (proof) { + proof.caption = latestCaptionFromBackend; + return this.proofRepository.update( + [proof], + (x, y) => getOldProof(x).hash === getOldProof(y).hash + ); + } + return of(null); + }), + finalize(() => this.navController.back()) + ) + .subscribe(); + } + } } diff --git a/src/app/shared/dia-backend/asset/downloading/dia-backend-downloading.service.ts b/src/app/shared/dia-backend/asset/downloading/dia-backend-downloading.service.ts index 30ea7f3d7..b91c94357 100644 --- a/src/app/shared/dia-backend/asset/downloading/dia-backend-downloading.service.ts +++ b/src/app/shared/dia-backend/asset/downloading/dia-backend-downloading.service.ts @@ -80,6 +80,7 @@ export class DiaBackendAssetDownloadingService { }, }); proof.diaBackendAssetId = diaBackendAsset.id; + proof.caption = diaBackendAsset.caption; if (diaBackendAsset.signed_metadata) proof.setSignatureVersion(); return this.proofRepository.add(proof, OnConflictStrategy.REPLACE); } diff --git a/src/app/shared/repositories/proof/proof.ts b/src/app/shared/repositories/proof/proof.ts index 0bc9cb69e..94bfa4861 100644 --- a/src/app/shared/repositories/proof/proof.ts +++ b/src/app/shared/repositories/proof/proof.ts @@ -26,6 +26,12 @@ export class Proof { diaBackendAssetId?: string = undefined; + /** + * When user uploades a capture we do not have option to set caption. Therefore caption is empty + * by default. Everytime caption is updated we need to update the caption in the proof as well. + */ + caption = ''; + isCollected = false; signatures: Signatures = {}; @@ -120,6 +126,7 @@ export class Proof { ); proof.setIndexedAssets(indexedProofView.indexedAssets); proof.diaBackendAssetId = indexedProofView.diaBackendAssetId; + proof.caption = indexedProofView.caption ?? ''; proof.isCollected = indexedProofView.isCollected ?? false; proof.signatureVersion = indexedProofView.signatureVersion; proof.integritySha = indexedProofView.integritySha; @@ -290,6 +297,7 @@ export class Proof { signatures: this.signatures, signatureVersion: this.signatureVersion, diaBackendAssetId: this.diaBackendAssetId, + caption: this.caption, isCollected: this.isCollected, integritySha: this.integritySha, cameraSource: this.cameraSource, @@ -424,6 +432,7 @@ export interface IndexedProofView extends Tuple { readonly signatures: Signatures; readonly signatureVersion?: string; readonly diaBackendAssetId?: string; + readonly caption?: string; readonly isCollected?: boolean; readonly integritySha?: string; readonly cameraSource: CameraSource;