1
1
import { Actions , Effect } from "@ngrx/effects" ;
2
2
import { EMPTY , Subject , from , fromEvent , merge , of , timer } from "rxjs" ;
3
3
import { Injectable } from "@angular/core" ;
4
- import { Store } from "@ngrx/store" ;
5
- import { catchError , concatMap , debounce , filter , finalize , map , mergeMap , takeUntil , tap } from "rxjs/operators" ;
4
+ import { Store , select } from "@ngrx/store" ;
5
+ import { catchError , concatMap , debounce , filter , finalize , map , mergeMap , takeUntil , tap , withLatestFrom } from "rxjs/operators" ;
6
6
7
7
import { ACCOUNTS_ACTIONS , CORE_ACTIONS , OPTIONS_ACTIONS , unionizeActionFilter } from "src/web/src/app/store/actions" ;
8
8
import { AccountTypeAndLoginFieldContainer } from "src/shared/model/container" ;
9
+ import { AccountsSelectors } from "src/web/src/app/store/selectors" ;
9
10
import { ElectronService } from "src/web/src/app/_core/electron.service" ;
11
+ import { IPC_MAIN_API_NOTIFICATION_ACTIONS } from "src/shared/api/main" ;
10
12
import { ONE_SECOND_MS } from "src/shared/constants" ;
11
13
import { State } from "src/web/src/app/store/reducers/accounts" ;
12
14
import { TutanotaNotificationOutput } from "src/shared/api/webview/tutanota" ;
@@ -38,8 +40,8 @@ export class AccountsEffects {
38
40
unionizeActionFilter ( ACCOUNTS_ACTIONS . is . SetupNotificationChannel ) ,
39
41
map ( logActionTypeAndBoundLoggerWithActionType ( { _logger} ) ) ,
40
42
mergeMap ( ( { payload, logger} ) => {
41
- const { account, webView, finishPromise} = payload ;
42
- const { type, login, entryUrl} = account . accountConfig ;
43
+ const { account : payloadAccount , webView, finishPromise} = payload ;
44
+ const { type, login, entryUrl} = payloadAccount . accountConfig ;
43
45
const dispose$ = from ( finishPromise ) . pipe ( tap ( ( ) => logger . info ( "dispose" ) ) ) ;
44
46
45
47
logger . info ( "setup" ) ;
@@ -48,16 +50,23 @@ export class AccountsEffects {
48
50
merged$ ,
49
51
this . api . webViewClient ( webView , type , { finishPromise} ) . pipe (
50
52
mergeMap ( ( webViewClient ) => webViewClient ( "notification" ) ( { entryUrl, zoneName : logger . zoneName ( ) } ) ) ,
51
- mergeMap ( ( notification ) => {
52
- if ( type !== "tutanota"
53
- || typeof ( notification as TutanotaNotificationOutput ) . batchEntityUpdatesCounter === "undefined" ) {
54
- return of ( ACCOUNTS_ACTIONS . Patch ( { login, patch : { notifications : notification } } ) ) ;
53
+ withLatestFrom ( this . store . pipe ( select ( AccountsSelectors . ACCOUNTS . pickAccount ( { login} ) ) ) ) ,
54
+ mergeMap ( ( [ notification , account ] ) => {
55
+ const batchEntityUpdatesNotification = type === "tutanota"
56
+ && typeof ( notification as TutanotaNotificationOutput ) . batchEntityUpdatesCounter === "number" ;
57
+
58
+ if ( batchEntityUpdatesNotification ) {
59
+ this . fireSyncingIteration$ . next ( { type, login} ) ;
60
+ return EMPTY ;
55
61
}
56
62
57
- this . fireSyncingIteration$ . next ( { type, login} ) ;
58
- // skipping patching "notifications" by the "batchEntityUpdatesCounter" value
59
- return EMPTY ;
63
+ // app derives "unread" value form the database in case of activated database syncing
64
+ // as "unread" notification should be ignored
65
+ if ( account && account . syncingActivated && typeof notification . unread === "number" ) {
66
+ return EMPTY ;
67
+ }
60
68
69
+ return of ( ACCOUNTS_ACTIONS . Patch ( { login, patch : { notifications : notification } } ) ) ;
61
70
} ) ,
62
71
takeUntil ( dispose$ ) ,
63
72
) ,
@@ -99,6 +108,12 @@ export class AccountsEffects {
99
108
this . api . webViewClient ( webView , type , { finishPromise} ) . pipe (
100
109
mergeMap ( ( webViewClient ) => merge (
101
110
of ( ACCOUNTS_ACTIONS . Patch ( { login, patch : { syncingActivated : true } } ) ) ,
111
+ this . api . ipcMainClient ( { finishPromise} ) ( "notification" ) ( ) . pipe (
112
+ filter ( IPC_MAIN_API_NOTIFICATION_ACTIONS . is . DbPatchAccount ) ,
113
+ mergeMap ( ( { payload : statPayload } ) => {
114
+ return of ( ACCOUNTS_ACTIONS . Patch ( { login, patch : { notifications : { unread : statPayload . stat . unread } } } ) ) ;
115
+ } ) ,
116
+ ) ,
102
117
merge (
103
118
timer ( 0 , ONE_SECOND_MS * 60 * 5 ) . pipe (
104
119
tap ( ( ) => logger . verbose ( `triggered by: timer` ) ) ,
0 commit comments