-
-
Notifications
You must be signed in to change notification settings - Fork 638
/
unreadPmsReducer.js
73 lines (58 loc) · 1.76 KB
/
unreadPmsReducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* @flow strict-local */
import type { Action } from '../types';
import type { UnreadPmsState } from './unreadModelTypes';
import {
REALM_INIT,
LOGOUT,
ACCOUNT_SWITCH,
EVENT_NEW_MESSAGE,
EVENT_MESSAGE_DELETE,
EVENT_UPDATE_MESSAGE_FLAGS,
} from '../actionConstants';
import { addItemsToPmArray, removeItemsDeeply } from './unreadHelpers';
import { NULL_ARRAY } from '../nullObjects';
import { recipientsOfPrivateMessage } from '../utils/recipient';
const initialState: UnreadPmsState = NULL_ARRAY;
const eventNewMessage = (state, action) => {
if (action.message.type !== 'private') {
return state;
}
if (recipientsOfPrivateMessage(action.message).length !== 2) {
return state;
}
if (action.message.flags?.includes('read')) {
return state;
}
return addItemsToPmArray(state, [action.message.id], action.message.sender_id);
};
const eventUpdateMessageFlags = (state, action) => {
if (action.flag !== 'read') {
return state;
}
if (action.all) {
return initialState;
}
if (action.op === 'add') {
return removeItemsDeeply(state, action.messages);
} else if (action.op === 'remove') {
// we do not support that operation
}
return state;
};
export default (state: UnreadPmsState = initialState, action: Action): UnreadPmsState => {
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
return initialState;
case REALM_INIT:
return (action.data.unread_msgs && action.data.unread_msgs.pms) || initialState;
case EVENT_NEW_MESSAGE:
return eventNewMessage(state, action);
case EVENT_MESSAGE_DELETE:
return removeItemsDeeply(state, action.messageIds);
case EVENT_UPDATE_MESSAGE_FLAGS:
return eventUpdateMessageFlags(state, action);
default:
return state;
}
};