forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_fetch.js
354 lines (303 loc) · 10.9 KB
/
message_fetch.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var message_fetch = (function () {
var exports = {};
var consts = {
backfill_idle_time: 10 * 1000,
error_retry_time: 5000,
backfill_batch_size: 1000,
narrow_before: 50,
narrow_after: 50,
num_before_pointer: 200,
num_after_pointer: 200,
backward_batch_size: 100,
forward_batch_size: 100,
catch_up_batch_size: 1000,
};
function process_result(data, opts) {
var messages = data.messages;
if (!$('#connection-error').hasClass('get-events-error')) {
ui_report.hide_error($("#connection-error"));
}
if (messages.length === 0 && current_msg_list === message_list.narrowed &&
message_list.narrowed.empty()) {
// Even after trying to load more messages, we have no
// messages to display in this narrow.
narrow.show_empty_narrow_message();
}
_.each(messages, message_store.set_message_booleans);
messages = _.map(messages, message_store.add_message_metadata);
// In case any of the newly fetched messages are new, add them to
// our unread data structures. It's important that this run even
// when fetching in a narrow, since we might return unread
// messages that aren't in the home view data set (e.g. on a muted
// stream).
message_util.do_unread_count_updates(messages);
// If we're loading more messages into the home view, save them to
// the message_list.all as well, as the home_msg_list is reconstructed
// from message_list.all.
if (opts.msg_list === home_msg_list) {
message_util.add_messages(messages, message_list.all, {messages_are_new: false});
}
if (messages.length !== 0) {
message_util.add_messages(messages, opts.msg_list, {messages_are_new: false});
}
activity.process_loaded_messages(messages);
stream_list.update_streams_sidebar();
pm_list.update_private_messages();
stream_list.maybe_scroll_narrow_into_view();
if (opts.cont !== undefined) {
opts.cont(data);
}
}
function get_messages_success(data, opts) {
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
// We unnarrowed before receiving new messages so
// don't bother processing the newly arrived messages.
return;
}
if (!data) {
// The server occasionally returns no data during a
// restart. Ignore those responses and try again
setTimeout(function () {
exports.load_messages(opts);
}, 0);
return;
}
process_result(data, opts);
resize.resize_bottom_whitespace();
}
exports.load_messages = function (opts) {
var data = {anchor: opts.anchor,
num_before: opts.num_before,
num_after: opts.num_after};
if (opts.msg_list.narrowed && narrow_state.active()) {
var operators = narrow_state.public_operators();
if (page_params.narrow !== undefined) {
operators = operators.concat(page_params.narrow);
}
data.narrow = JSON.stringify(operators);
}
if (opts.msg_list === home_msg_list && page_params.narrow_stream !== undefined) {
data.narrow = JSON.stringify(page_params.narrow);
}
if (opts.use_first_unread_anchor) {
data.use_first_unread_anchor = true;
}
data.client_gravatar = true;
channel.get({
url: '/json/messages',
data: data,
idempotent: true,
success: function (data) {
get_messages_success(data, opts);
},
error: function (xhr) {
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
// We unnarrowed before getting an error so don't
// bother trying again or doing further processing.
return;
}
if (xhr.status === 400) {
// Bad request: We probably specified a narrow operator
// for a nonexistent stream or something. We shouldn't
// retry or display a connection error.
//
// FIXME: Warn the user when this has happened?
var data = {
messages: [],
};
process_result(data, opts);
return;
}
// We might want to be more clever here
$('#connection-error').addClass("show");
setTimeout(function () {
exports.load_messages(opts);
}, consts.error_retry_time);
},
});
};
exports.load_messages_for_narrow = function (opts) {
var msg_list = message_list.narrowed;
msg_list.fetch_status.start_initial_narrow();
message_fetch.load_messages({
anchor: opts.then_select_id.toFixed(),
num_before: consts.narrow_before,
num_after: consts.narrow_after,
msg_list: msg_list,
use_first_unread_anchor: opts.use_first_unread_anchor,
cont: function (data) {
msg_list.fetch_status.finish_initial_narrow({
found_oldest: data.found_oldest,
found_newest: data.found_newest,
});
message_scroll.hide_indicators();
opts.cont();
},
});
};
exports.get_backfill_anchor = function (msg_list) {
var oldest_message_id;
if (msg_list === home_msg_list) {
msg_list = message_list.all;
}
if (msg_list.first() === undefined) {
oldest_message_id = page_params.pointer;
} else {
oldest_message_id = msg_list.first().id;
}
return oldest_message_id;
};
exports.get_frontfill_anchor = function (msg_list) {
if (msg_list === home_msg_list) {
msg_list = message_list.all;
}
var last_msg = msg_list.last();
if (last_msg) {
return last_msg.id;
}
return page_params.pointer;
};
exports.maybe_load_older_messages = function (opts) {
// This function gets called when you scroll to the top
// of your window, and you want to get messages older
// than what the browers originally fetched.
var msg_list = opts.msg_list;
if (!msg_list.fetch_status.can_load_older_messages()) {
// We may already be loading old messages or already
// got the oldest one.
return;
}
opts.show_loading();
exports.do_backfill({
msg_list: msg_list,
num_before: consts.backward_batch_size,
cont: function () {
opts.hide_loading();
},
});
};
exports.do_backfill = function (opts) {
var msg_list = opts.msg_list;
msg_list.fetch_status.start_older_batch();
if (msg_list === home_msg_list) {
message_list.all.fetch_status.start_older_batch();
}
var anchor = exports.get_backfill_anchor(msg_list).toFixed();
exports.load_messages({
anchor: anchor,
num_before: opts.num_before,
num_after: 0,
msg_list: msg_list,
cont: function (data) {
msg_list.fetch_status.finish_older_batch({
found_oldest: data.found_oldest,
});
if (msg_list === home_msg_list) {
message_list.all.fetch_status.finish_older_batch({
found_oldest: data.found_oldest,
});
}
if (opts.cont) {
opts.cont();
}
},
});
};
exports.maybe_load_newer_messages = function (opts) {
// This function gets called when you scroll to the top
// of your window, and you want to get messages newer
// than what the browers originally fetched.
var msg_list = opts.msg_list;
if (!msg_list.fetch_status.can_load_newer_messages()) {
// We may already be loading new messages or already
// got the newest one.
return;
}
msg_list.fetch_status.start_newer_batch();
if (msg_list === home_msg_list) {
message_list.all.fetch_status.start_newer_batch();
}
var anchor = exports.get_frontfill_anchor(msg_list).toFixed();
exports.load_messages({
anchor: anchor,
num_before: 0,
num_after: consts.forward_batch_size,
msg_list: msg_list,
cont: function (data) {
msg_list.fetch_status.finish_newer_batch({
found_newest: data.found_newest,
});
if (msg_list === home_msg_list) {
message_list.all.fetch_status.finish_newer_batch({
found_newest: data.found_newest,
});
}
},
});
};
exports.start_backfilling_messages = function () {
// backfill more messages after the user is idle
$(document).idle({idle: consts.backfill_idle_time,
onIdle: function () {
exports.do_backfill({
num_before: consts.backfill_batch_size,
msg_list: home_msg_list,
});
}});
};
exports.initialize = function () {
// get the initial message list
function load_more(data) {
// If we received the initially selected message, select it on the client side,
// but not if the user has already selected another one during load.
//
// We fall back to the closest selected id, as the user may have removed
// a stream from the home before already
if (home_msg_list.selected_id() === -1 && !home_msg_list.empty()) {
home_msg_list.select_id(page_params.pointer,
{then_scroll: true, use_closest: true,
target_scroll_offset: page_params.initial_offset});
}
home_msg_list.fetch_status.finish_newer_batch({
found_newest: data.found_newest,
});
message_list.all.fetch_status.finish_newer_batch({
found_newest: data.found_newest,
});
if (data.found_newest) {
server_events.home_view_loaded();
exports.start_backfilling_messages();
return;
}
// If we fall through here, we need to keep fetching more data, and
// we'll call back to the function we're in.
var messages = data.messages;
var latest_id = messages[messages.length - 1].id;
exports.load_messages({
anchor: latest_id.toFixed(),
num_before: 0,
num_after: consts.catch_up_batch_size,
msg_list: home_msg_list,
cont: load_more,
});
}
if (page_params.have_initial_messages) {
home_msg_list.fetch_status.start_newer_batch();
message_list.all.fetch_status.start_newer_batch();
exports.load_messages({
anchor: page_params.pointer,
num_before: consts.num_before_pointer,
num_after: consts.num_after_pointer,
msg_list: home_msg_list,
cont: load_more,
});
} else {
server_events.home_view_loaded();
}
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = message_fetch;
}
window.message_fetch = message_fetch;