Skip to content

Commit

Permalink
Merge pull request #3204 from numbersprotocol/fix-excessive-api-call
Browse files Browse the repository at this point in the history
Fix excessive api call
  • Loading branch information
sultanmyrza authored Feb 6, 2024
2 parents 8b6b6c7 + 6441b2a commit 6f79c12
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
45 changes: 40 additions & 5 deletions src/app/features/home/details/edit-caption/edit-caption.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -43,15 +52,17 @@ 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();
}

processIframeEvents() {
fromEvent(window, 'message')
.pipe(
tap(event => {
switchTap(event => {
const postMessageEvent = event as MessageEvent;
const data = postMessageEvent.data as BubbleToIonicPostMessage;
switch (data) {
Expand All @@ -60,12 +71,36 @@ export class EditCaptionPage {
break;
case BubbleToIonicPostMessage.EDIT_CAPTION_SAVE:
this.iframeService.refreshDetailsPageIframe();
this.navController.back();
this.syncCaptionAndNavigateBack();
break;
}
}),
untilDestroyed(this)
)
.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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions src/app/shared/repositories/proof/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6f79c12

Please sign in to comment.