1
1
/* @flow strict-local */
2
+ import Immutable from 'immutable' ;
2
3
import differenceInSeconds from 'date-fns/differenceInSeconds' ;
3
4
import differenceInDays from 'date-fns/differenceInDays' ;
4
5
import formatDistanceToNow from 'date-fns/formatDistanceToNow' ;
@@ -24,7 +25,6 @@ import {
24
25
REGISTER_COMPLETE ,
25
26
RESET_ACCOUNT_DATA ,
26
27
} from '../actionConstants' ;
27
- import { NULL_OBJECT } from '../nullObjects' ;
28
28
29
29
//
30
30
//
@@ -34,7 +34,7 @@ import { NULL_OBJECT } from '../nullObjects';
34
34
export const getPresence = ( state : PerAccountState ) : PresenceState => state . presence ;
35
35
36
36
export function getUserPresenceByEmail ( state : PresenceState , email : string ) : UserPresence | void {
37
- return state [ email ] ;
37
+ return state . byEmail . get ( email ) ;
38
38
}
39
39
40
40
//
@@ -196,7 +196,9 @@ export const getPresenceStatusForUserId = (
196
196
// Reducer.
197
197
//
198
198
199
- const initialState : PresenceState = NULL_OBJECT ;
199
+ const initialState : PresenceState = {
200
+ byEmail : Immutable . Map ( ) ,
201
+ } ;
200
202
201
203
export function reducer (
202
204
state : PresenceState = initialState , // eslint-disable-line default-param-last
@@ -206,22 +208,23 @@ export function reducer(
206
208
case RESET_ACCOUNT_DATA :
207
209
return initialState ;
208
210
209
- case REGISTER_COMPLETE :
210
- return (
211
- action . data . presences
212
- // TODO(#5102): Delete fallback once we enforce any threshold for
213
- // ancient servers we refuse to connect to. It was added in
214
- // #2878 (2018-11-16), but it wasn't clear even then, it seems,
215
- // whether any servers actually omit the data. The API doc
216
- // doesn't mention any servers that omit it, and our Flow types
217
- // mark it required.
218
- || initialState
219
- ) ;
211
+ case REGISTER_COMPLETE : {
212
+ // TODO(#5102): Delete fallback once we enforce any threshold for
213
+ // ancient servers we refuse to connect to. It was added in
214
+ // #2878 (2018-11-16), but it wasn't clear even then, it seems,
215
+ // whether any servers actually omit the data. The API doc
216
+ // doesn't mention any servers that omit it, and our Flow types
217
+ // mark it required.
218
+ const data = action . data . presences ?? { } ;
219
+ return {
220
+ byEmail : Immutable . Map ( data ) ,
221
+ } ;
222
+ }
220
223
221
224
case PRESENCE_RESPONSE :
222
225
return {
223
226
...state ,
224
- ... action . presence ,
227
+ byEmail : state . byEmail . merge ( action . presence ) ,
225
228
} ;
226
229
227
230
case EVENT_PRESENCE : {
@@ -233,25 +236,23 @@ export function reducer(
233
236
return state ;
234
237
}
235
238
236
- const oldUserPresence = getUserPresenceByEmail ( state , action . email ) ;
237
239
return {
238
240
...state ,
239
- // Flow bug (unresolved):
240
- // https://github.com/facebook/flow/issues/8276
241
- // $FlowIssue[cannot-spread-indexer] #8276
242
- [ action . email ] : {
243
- ...oldUserPresence ,
244
- ...action . presence ,
245
- // Flow bug (unresolved):
246
- // https://github.com/facebook/flow/issues/8276
247
- // $FlowIssue[cannot-spread-indexer] #8276
248
- aggregated : getAggregatedPresence ( {
249
- ...oldUserPresence ,
241
+ byEmail : state . byEmail . update ( action . email , ( userPresence : UserPresence ) : UserPresence =>
242
+ // $FlowIssue[cannot-spread-indexer] https://github.com/facebook/flow/issues/8276
243
+ ( {
244
+ ...userPresence ,
250
245
...action . presence ,
246
+ // $FlowIssue[cannot-spread-indexer] https://github.com/facebook/flow/issues/8276
247
+ aggregated : getAggregatedPresence ( {
248
+ ...userPresence ,
249
+ ...action . presence ,
250
+ } ) ,
251
251
} ) ,
252
- } ,
252
+ ) ,
253
253
} ;
254
254
}
255
+
255
256
default :
256
257
return state ;
257
258
}
0 commit comments