Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix excessive api call #3204

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading