/
fetchActions.js
218 lines (194 loc) · 6.3 KB
/
fetchActions.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* @flow */
import type {
Narrow,
Dispatch,
GetState,
Message,
FetchMessagesAction,
MessageFetchStartAction,
MessageFetchCompleteAction,
MarkMessagesReadAction,
InitialFetchStartAction,
InitialFetchCompleteAction,
} from '../types';
import { getMessages, getStreams, registerForEvents, uploadFile } from '../api';
import {
getAuth,
getSession,
getFirstMessageId,
getLastMessageId,
getCaughtUpForActiveNarrow,
getFetchingForActiveNarrow,
getPushToken,
getTopMostNarrow,
} from '../selectors';
import config from '../config';
import {
INITIAL_FETCH_START,
INITIAL_FETCH_COMPLETE,
MESSAGE_FETCH_START,
MESSAGE_FETCH_COMPLETE,
MARK_MESSAGES_READ,
} from '../actionConstants';
import { LAST_MESSAGE_ANCHOR } from '../constants';
import timing from '../utils/timing';
import { ALL_PRIVATE_NARROW } from '../utils/narrow';
import { tryUntilSuccessful } from '../utils/async';
import { refreshNotificationToken } from '../utils/notifications';
import {
addToOutbox,
initNotifications,
initStreams,
realmInit,
sendFocusPing,
startEventPolling,
trySendMessages,
} from '../actions';
export const messageFetchStart = (
narrow: Narrow,
numBefore: number,
numAfter: number,
): MessageFetchStartAction => ({
type: MESSAGE_FETCH_START,
narrow,
numBefore,
numAfter,
});
export const messageFetchComplete = (
messages: Message[],
narrow: Narrow,
anchor: number,
numBefore: number,
numAfter: number,
): MessageFetchCompleteAction => ({
type: MESSAGE_FETCH_COMPLETE,
messages,
narrow,
anchor,
numBefore,
numAfter,
});
export const fetchMessages = (
narrow: Narrow,
anchor: number,
numBefore: number,
numAfter: number,
useFirstUnread: boolean = false,
): FetchMessagesAction => async (dispatch: Dispatch, getState: GetState) => {
dispatch(messageFetchStart(narrow, numBefore, numAfter));
const messages = await getMessages(
getAuth(getState()),
narrow,
anchor,
numBefore,
numAfter,
useFirstUnread,
);
dispatch(messageFetchComplete(messages, narrow, anchor, numBefore, numAfter));
};
export const fetchMessagesAroundAnchor = (narrow: Narrow, anchor: number): FetchMessagesAction =>
fetchMessages(
narrow,
anchor,
config.messagesPerRequest / 2,
config.messagesPerRequest / 2,
false,
);
export const fetchMessagesAtFirstUnread = (narrow: Narrow): FetchMessagesAction =>
fetchMessages(narrow, 0, config.messagesPerRequest / 2, config.messagesPerRequest / 2, true);
export const markMessagesRead = (messageIds: number[]): MarkMessagesReadAction => ({
type: MARK_MESSAGES_READ,
messageIds,
});
export const fetchOlder = (narrow: Narrow): FetchMessagesAction => (
dispatch: Dispatch,
getState: GetState,
) => {
const state = getState();
const firstMessageId = getFirstMessageId(narrow)(state);
const caughtUp = getCaughtUpForActiveNarrow(narrow)(state);
const fetching = getFetchingForActiveNarrow(narrow)(state);
const { needsInitialFetch } = getSession(state);
if (!needsInitialFetch && !fetching.older && !caughtUp.older && firstMessageId) {
dispatch(fetchMessages(narrow, firstMessageId, config.messagesPerRequest, 0));
}
};
export const fetchNewer = (narrow: Narrow): FetchMessagesAction => (
dispatch: Dispatch,
getState: GetState,
) => {
const state = getState();
const lastMessageId = getLastMessageId(narrow)(state);
const caughtUp = getCaughtUpForActiveNarrow(narrow)(state);
const fetching = getFetchingForActiveNarrow(narrow)(state);
const { needsInitialFetch } = getSession(state);
if (!needsInitialFetch && !fetching.newer && !caughtUp.newer && lastMessageId) {
dispatch(fetchMessages(narrow, lastMessageId, 0, config.messagesPerRequest));
}
};
export const initialFetchStart = (): InitialFetchStartAction => ({
type: INITIAL_FETCH_START,
});
export const initialFetchComplete = (): InitialFetchCompleteAction => ({
type: INITIAL_FETCH_COMPLETE,
});
export const fetchEssentialInitialData = () => async (dispatch: Dispatch, getState: GetState) => {
dispatch(initialFetchStart());
const auth = getAuth(getState());
const halfCount = Math.trunc(config.messagesPerRequest / 2);
timing.start('Essential server data');
// only fetch messages if chat screen is at the top of stack
// get narrow of top most chat screen in the stack
const narrow = getTopMostNarrow(getState());
if (narrow) {
dispatch(messageFetchStart(narrow, halfCount, halfCount));
}
const [initData, messages] = await Promise.all([
await tryUntilSuccessful(() =>
registerForEvents(auth, config.trackServerEvents, config.serverDataOnStartup),
),
narrow
&& (await tryUntilSuccessful(() => getMessages(auth, narrow, 0, halfCount, halfCount, true))),
]);
timing.end('Essential server data');
dispatch(realmInit(initData));
if (narrow && messages) {
dispatch(messageFetchComplete(messages, narrow, 0, halfCount, halfCount));
}
dispatch(initialFetchComplete());
dispatch(startEventPolling(initData.queue_id, initData.last_event_id));
};
export const fetchRestOfInitialData = () => async (dispatch: Dispatch, getState: GetState) => {
const auth = getAuth(getState());
const pushToken = getPushToken(getState());
timing.start('Rest of server data');
const [messages, streams] = await Promise.all([
await tryUntilSuccessful(() =>
getMessages(auth, ALL_PRIVATE_NARROW, LAST_MESSAGE_ANCHOR, 100, 0),
),
await tryUntilSuccessful(() => getStreams(auth)),
]);
timing.end('Rest of server data');
dispatch(messageFetchComplete(messages, ALL_PRIVATE_NARROW, LAST_MESSAGE_ANCHOR, 100, 0));
dispatch(initStreams(streams));
if (auth.apiKey !== '' && (pushToken === '' || pushToken === undefined)) {
refreshNotificationToken();
}
dispatch(trySendMessages());
};
export const doInitialFetch = () => async (dispatch: Dispatch, getState: GetState) => {
dispatch(fetchEssentialInitialData());
dispatch(fetchRestOfInitialData());
dispatch(initNotifications());
dispatch(sendFocusPing());
setInterval(() => sendFocusPing(), 60 * 1000);
};
export const uploadImage = (narrow: Narrow, uri: string, name: string) => async (
dispatch: Dispatch,
getState: GetState,
) => {
const auth = getAuth(getState());
const serverUri = await uploadFile(auth, uri, name);
const messageToSend = `[${name}](${serverUri})`;
dispatch(addToOutbox(narrow, messageToSend));
};