Skip to content

Commit d9b5652

Browse files
authored
Merge pull request #3157 from numbersprotocol/fix-excessive-api-call
Fix excessive api call
2 parents 064e7cf + c4a0040 commit d9b5652

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

src/app/features/home/capture-tab/capture-item/capture-item.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ export class CaptureItemComponent {
7171
)
7272
);
7373

74-
readonly hasCaption$ = this.proof$.pipe(
75-
switchMap(proof => this.diaBackendAssetRepository.fetchByProof$(proof)),
76-
map(asset => asset.caption !== '')
77-
);
74+
readonly hasCaption$ = this.proof$.pipe(map(proof => proof.caption !== ''));
7875

7976
readonly isVideo$ = this.proof$.pipe(
8077
concatMap(proof => proof.getFirstAssetMeta()),

src/app/features/home/details/edit-caption/edit-caption.page.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ import { DomSanitizer } from '@angular/platform-browser';
33
import { ActivatedRoute } from '@angular/router';
44
import { NavController } from '@ionic/angular';
55
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
6-
import { combineLatest, fromEvent } from 'rxjs';
7-
import { map, tap } from 'rxjs/operators';
6+
import { combineLatest, fromEvent, of } from 'rxjs';
7+
import {
8+
concatMap,
9+
finalize,
10+
first,
11+
map,
12+
tap as switchTap,
13+
} from 'rxjs/operators';
814
import { DiaBackendAuthService } from '../../../../shared/dia-backend/auth/dia-backend-auth.service';
915
import { BUBBLE_IFRAME_URL } from '../../../../shared/dia-backend/secret';
1016
import { BubbleToIonicPostMessage } from '../../../../shared/iframe/iframe';
1117
import { IframeService } from '../../../../shared/iframe/iframe.service';
18+
import { getOldProof } from '../../../../shared/repositories/proof/old-proof-adapter';
19+
import { ProofRepository } from '../../../../shared/repositories/proof/proof-repository.service';
1220
import { isNonNullable } from '../../../../utils/rx-operators/rx-operators';
21+
import { InformationSessionService } from '../information/session/information-session.service';
1322

1423
@UntilDestroy()
1524
@Component({
@@ -43,15 +52,17 @@ export class EditCaptionPage {
4352
private readonly sanitizer: DomSanitizer,
4453
private readonly navController: NavController,
4554
private readonly iframeService: IframeService,
46-
private readonly diaBackendAuthService: DiaBackendAuthService
55+
private readonly diaBackendAuthService: DiaBackendAuthService,
56+
private readonly informationSessionService: InformationSessionService,
57+
private readonly proofRepository: ProofRepository
4758
) {
4859
this.processIframeEvents();
4960
}
5061

5162
processIframeEvents() {
5263
fromEvent(window, 'message')
5364
.pipe(
54-
tap(event => {
65+
switchTap(event => {
5566
const postMessageEvent = event as MessageEvent;
5667
const data = postMessageEvent.data as BubbleToIonicPostMessage;
5768
switch (data) {
@@ -60,12 +71,36 @@ export class EditCaptionPage {
6071
break;
6172
case BubbleToIonicPostMessage.EDIT_CAPTION_SAVE:
6273
this.iframeService.refreshDetailsPageIframe();
63-
this.navController.back();
74+
this.syncCaptionAndNavigateBack();
6475
break;
6576
}
6677
}),
6778
untilDestroyed(this)
6879
)
6980
.subscribe();
7081
}
82+
83+
syncCaptionAndNavigateBack() {
84+
if (this.informationSessionService.activatedDetailedCapture) {
85+
combineLatest([
86+
this.informationSessionService.activatedDetailedCapture.proof$,
87+
this.informationSessionService.activatedDetailedCapture.caption$,
88+
])
89+
.pipe(
90+
first(),
91+
concatMap(([proof, latestCaptionFromBackend]) => {
92+
if (proof) {
93+
proof.caption = latestCaptionFromBackend;
94+
return this.proofRepository.update(
95+
[proof],
96+
(x, y) => getOldProof(x).hash === getOldProof(y).hash
97+
);
98+
}
99+
return of(null);
100+
}),
101+
finalize(() => this.navController.back())
102+
)
103+
.subscribe();
104+
}
105+
}
71106
}

src/app/shared/dia-backend/asset/downloading/dia-backend-downloading.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class DiaBackendAssetDownloadingService {
8080
},
8181
});
8282
proof.diaBackendAssetId = diaBackendAsset.id;
83+
proof.caption = diaBackendAsset.caption;
8384
if (diaBackendAsset.signed_metadata) proof.setSignatureVersion();
8485
return this.proofRepository.add(proof, OnConflictStrategy.REPLACE);
8586
}

src/app/shared/repositories/proof/proof.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ export class Proof {
2626

2727
diaBackendAssetId?: string = undefined;
2828

29+
/**
30+
* When user uploades a capture we do not have option to set caption. Therefore caption is empty
31+
* by default. Everytime caption is updated we need to update the caption in the proof as well.
32+
*/
33+
caption = '';
34+
2935
isCollected = false;
3036

3137
signatures: Signatures = {};
@@ -120,6 +126,7 @@ export class Proof {
120126
);
121127
proof.setIndexedAssets(indexedProofView.indexedAssets);
122128
proof.diaBackendAssetId = indexedProofView.diaBackendAssetId;
129+
proof.caption = indexedProofView.caption ?? '';
123130
proof.isCollected = indexedProofView.isCollected ?? false;
124131
proof.signatureVersion = indexedProofView.signatureVersion;
125132
proof.integritySha = indexedProofView.integritySha;
@@ -290,6 +297,7 @@ export class Proof {
290297
signatures: this.signatures,
291298
signatureVersion: this.signatureVersion,
292299
diaBackendAssetId: this.diaBackendAssetId,
300+
caption: this.caption,
293301
isCollected: this.isCollected,
294302
integritySha: this.integritySha,
295303
cameraSource: this.cameraSource,
@@ -424,6 +432,7 @@ export interface IndexedProofView extends Tuple {
424432
readonly signatures: Signatures;
425433
readonly signatureVersion?: string;
426434
readonly diaBackendAssetId?: string;
435+
readonly caption?: string;
427436
readonly isCollected?: boolean;
428437
readonly integritySha?: string;
429438
readonly cameraSource: CameraSource;

0 commit comments

Comments
 (0)