2016-04-20 22:37:26 +02:00
|
|
|
var message_list = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2016-04-25 23:45:25 +02:00
|
|
|
exports.narrowed = undefined;
|
|
|
|
|
2016-04-20 22:37:26 +02:00
|
|
|
exports.MessageList = function (table_name, filter, opts) {
|
2013-07-25 22:08:16 +02:00
|
|
|
_.extend(this, {
|
|
|
|
collapse_messages: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
muting_enabled: true,
|
2013-07-25 22:08:16 +02:00
|
|
|
}, opts);
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view = new MessageListView(this, table_name, this.collapse_messages);
|
2013-07-25 22:08:16 +02:00
|
|
|
|
2018-03-09 22:23:23 +01:00
|
|
|
this.fetch_status = FetchStatus();
|
|
|
|
|
2013-09-19 00:43:59 +02:00
|
|
|
if (this.muting_enabled) {
|
|
|
|
this._all_items = [];
|
|
|
|
}
|
2012-12-05 23:54:49 +01:00
|
|
|
this._items = [];
|
|
|
|
this._hash = {};
|
2014-03-11 19:59:44 +01:00
|
|
|
this._local_only = {};
|
2012-12-05 23:54:49 +01:00
|
|
|
this.table_name = table_name;
|
2013-04-25 21:13:06 +02:00
|
|
|
this.filter = filter;
|
2013-02-20 18:26:50 +01:00
|
|
|
this._selected_id = -1;
|
2013-02-27 20:47:04 +01:00
|
|
|
|
2013-04-25 21:13:06 +02:00
|
|
|
if (this.filter === undefined) {
|
2013-08-10 01:31:31 +02:00
|
|
|
this.filter = new Filter();
|
2013-04-25 21:13:06 +02:00
|
|
|
}
|
2013-11-06 17:35:24 +01:00
|
|
|
|
|
|
|
this.narrowed = this.table_name === "zfilt";
|
2013-08-14 23:48:28 +02:00
|
|
|
|
|
|
|
this.num_appends = 0;
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
return this;
|
2016-04-20 22:37:26 +02:00
|
|
|
};
|
2013-02-26 23:30:13 +01:00
|
|
|
|
2016-04-20 22:37:26 +02:00
|
|
|
exports.MessageList.prototype = {
|
2014-01-22 22:20:36 +01:00
|
|
|
add_messages: function MessageList_add_messages(messages, opts) {
|
2013-09-22 16:41:33 +02:00
|
|
|
var self = this;
|
|
|
|
var predicate = self.filter.predicate();
|
|
|
|
var top_messages = [];
|
|
|
|
var bottom_messages = [];
|
|
|
|
var interior_messages = [];
|
|
|
|
|
|
|
|
// If we're initially populating the list, save the messages in
|
|
|
|
// bottom_messages regardless
|
|
|
|
if (self.selected_id() === -1 && self.empty()) {
|
2013-09-28 21:31:11 +02:00
|
|
|
var narrow_messages = _.filter(messages, predicate);
|
|
|
|
bottom_messages = _.reject(narrow_messages, function (msg) {
|
|
|
|
return self.get(msg.id);
|
|
|
|
});
|
2013-09-22 16:41:33 +02:00
|
|
|
} else {
|
|
|
|
_.each(messages, function (msg) {
|
|
|
|
// Filter out duplicates that are already in self, and all messages
|
|
|
|
// that fail our filter predicate
|
|
|
|
if (! (self.get(msg.id) === undefined && predicate(msg))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:49:25 +02:00
|
|
|
// Put messages in correct order on either side of the
|
|
|
|
// message list. This code path assumes that messages
|
|
|
|
// is a (1) sorted, and (2) consecutive block of
|
|
|
|
// messages that belong in this message list; those
|
|
|
|
// facts should be ensured by the caller.
|
2013-09-22 16:41:33 +02:00
|
|
|
if (self.empty() || msg.id > self.last().id) {
|
|
|
|
bottom_messages.push(msg);
|
|
|
|
} else if (msg.id < self.first().id) {
|
|
|
|
top_messages.push(msg);
|
|
|
|
} else {
|
|
|
|
interior_messages.push(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interior_messages.length > 0) {
|
|
|
|
self.add_and_rerender(top_messages.concat(interior_messages).concat(bottom_messages));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (top_messages.length > 0) {
|
|
|
|
self.prepend(top_messages);
|
|
|
|
}
|
|
|
|
if (bottom_messages.length > 0) {
|
2014-01-22 22:20:36 +01:00
|
|
|
self.append(bottom_messages, opts);
|
2013-09-22 16:41:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:45:25 +02:00
|
|
|
if ((self === exports.narrowed) && !self.empty()) {
|
2013-09-22 16:41:33 +02:00
|
|
|
// If adding some new messages to the message tables caused
|
|
|
|
// our current narrow to no longer be empty, hide the empty
|
|
|
|
// feed placeholder text.
|
|
|
|
narrow.hide_empty_narrow_message();
|
2014-03-04 22:39:34 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:45:25 +02:00
|
|
|
if ((self === exports.narrowed) && !self.empty() &&
|
2014-03-04 22:39:34 +01:00
|
|
|
(self.selected_id() === -1) && !opts.delay_render) {
|
2013-09-22 16:41:33 +02:00
|
|
|
// And also select the newly arrived message.
|
|
|
|
self.select_id(self.selected_id(), {then_scroll: true, use_closest: true});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-30 19:00:15 +01:00
|
|
|
get: function MessageList_get(id) {
|
2013-12-18 20:09:09 +01:00
|
|
|
id = parseFloat(id);
|
2012-12-05 23:54:49 +01:00
|
|
|
if (isNaN(id)) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
|
|
|
return this._hash[id];
|
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
num_items: function MessageList_num_items() {
|
|
|
|
return this._items.length;
|
|
|
|
},
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
empty: function MessageList_empty() {
|
|
|
|
return this._items.length === 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
first: function MessageList_first() {
|
|
|
|
return this._items[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
last: function MessageList_last() {
|
|
|
|
return this._items[this._items.length - 1];
|
|
|
|
},
|
|
|
|
|
2013-08-14 23:04:24 +02:00
|
|
|
nth_most_recent_id: function MessageList_nth_most_recent_id(n) {
|
|
|
|
var i = this._items.length - n;
|
|
|
|
if (i < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return this._items[i].id;
|
2013-08-14 23:04:24 +02:00
|
|
|
},
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
clear: function MessageList_clear(opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.extend({clear_selected_id: true}, opts);
|
2013-02-22 20:48:31 +01:00
|
|
|
|
2013-09-28 21:04:34 +02:00
|
|
|
if (this.muting_enabled) {
|
|
|
|
this._all_items = [];
|
|
|
|
}
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
this._items = [];
|
|
|
|
this._hash = {};
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.clear_rendering_state(true);
|
2013-02-22 20:48:31 +01:00
|
|
|
|
|
|
|
if (opts.clear_selected_id) {
|
|
|
|
this._selected_id = -1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-02-20 18:26:50 +01:00
|
|
|
selected_id: function MessageList_selected_id() {
|
|
|
|
return this._selected_id;
|
|
|
|
},
|
|
|
|
|
2013-02-20 18:33:04 +01:00
|
|
|
select_id: function MessageList_select_id(id, opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.extend({
|
2013-07-03 22:14:32 +02:00
|
|
|
then_scroll: false,
|
2014-02-04 22:02:26 +01:00
|
|
|
target_scroll_offset: undefined,
|
2013-07-03 22:14:32 +02:00
|
|
|
use_closest: false,
|
2013-12-18 19:06:55 +01:00
|
|
|
empty_ok: false,
|
2014-01-22 22:20:36 +01:00
|
|
|
mark_read: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
force_rerender: false,
|
2013-07-03 22:14:32 +02:00
|
|
|
}, opts, {
|
|
|
|
id: id,
|
|
|
|
msg_list: this,
|
2017-01-12 00:17:43 +01:00
|
|
|
previously_selected: this._selected_id,
|
2013-07-03 22:14:32 +02:00
|
|
|
});
|
2013-07-03 19:15:07 +02:00
|
|
|
|
2017-03-27 19:26:30 +02:00
|
|
|
function convert_id(str_id) {
|
|
|
|
var id = parseFloat(str_id);
|
|
|
|
if (isNaN(id)) {
|
|
|
|
blueslip.fatal("Bad message id " + str_id);
|
|
|
|
}
|
|
|
|
return id;
|
2013-02-20 18:33:04 +01:00
|
|
|
}
|
2013-08-07 20:28:50 +02:00
|
|
|
|
2017-03-27 19:26:30 +02:00
|
|
|
id = convert_id(id);
|
|
|
|
|
2013-09-27 21:18:54 +02:00
|
|
|
var closest_id = this.closest_id(id);
|
|
|
|
|
2017-06-15 23:46:41 +02:00
|
|
|
var error_data;
|
|
|
|
|
2013-09-27 21:18:54 +02:00
|
|
|
// The name "use_closest" option is a bit legacy. We
|
|
|
|
// are always gonna move to the closest visible id; the flag
|
|
|
|
// just says whether we call blueslip.error or not. The caller
|
|
|
|
// sets use_closest to true when it expects us to move the
|
|
|
|
// pointer as needed, so only generate an error if the flag is
|
|
|
|
// false.
|
|
|
|
if (!opts.use_closest && closest_id !== id) {
|
2017-06-15 23:46:41 +02:00
|
|
|
error_data = {
|
2017-06-15 22:33:38 +02:00
|
|
|
table_name: this.table_name,
|
|
|
|
id: id,
|
|
|
|
closest_id: closest_id,
|
|
|
|
};
|
2013-09-27 21:18:54 +02:00
|
|
|
blueslip.error("Selected message id not in MessageList",
|
2017-06-15 22:33:38 +02:00
|
|
|
error_data);
|
2013-02-20 18:33:04 +01:00
|
|
|
}
|
2013-03-13 18:48:02 +01:00
|
|
|
|
2013-12-18 19:06:55 +01:00
|
|
|
if (closest_id === -1 && !opts.empty_ok) {
|
2017-06-15 23:46:41 +02:00
|
|
|
error_data = {
|
2013-12-02 21:40:49 +01:00
|
|
|
table_name: this.table_name,
|
|
|
|
id: id,
|
2017-01-12 00:17:43 +01:00
|
|
|
items_length: this._items.length,
|
2013-12-02 21:40:49 +01:00
|
|
|
};
|
|
|
|
blueslip.fatal("Cannot select id -1", error_data);
|
2013-10-30 18:38:16 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 21:18:54 +02:00
|
|
|
id = closest_id;
|
|
|
|
opts.id = id;
|
2013-02-20 18:33:04 +01:00
|
|
|
this._selected_id = id;
|
2013-09-27 21:18:54 +02:00
|
|
|
|
2014-01-22 22:20:36 +01:00
|
|
|
if (opts.force_rerender) {
|
|
|
|
this.rerender();
|
|
|
|
} else if (!opts.from_rendering) {
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.maybe_rerender();
|
2013-07-03 19:53:27 +02:00
|
|
|
}
|
2013-03-04 20:22:09 +01:00
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('message_selected.zulip', opts));
|
2013-02-20 18:33:04 +01:00
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
reselect_selected_id: function MessageList_select_closest_id() {
|
|
|
|
this.select_id(this._selected_id, {from_rendering: true});
|
|
|
|
},
|
|
|
|
|
2013-02-14 23:48:37 +01:00
|
|
|
selected_message: function MessageList_selected_message() {
|
2013-02-20 18:26:50 +01:00
|
|
|
return this.get(this._selected_id);
|
2013-02-14 23:48:37 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
selected_row: function MessageList_selected_row() {
|
2013-08-14 22:00:32 +02:00
|
|
|
return this.get_row(this._selected_id);
|
2013-02-14 23:48:37 +01:00
|
|
|
},
|
|
|
|
|
2014-03-11 20:17:14 +01:00
|
|
|
// Returns the index where you could insert the desired ID
|
|
|
|
// into the message list, without disrupting the sort order
|
|
|
|
// This takes into account the potentially-unsorted
|
|
|
|
// nature of local message IDs in the message list
|
|
|
|
_lower_bound: function MessageList__lower_bound(id) {
|
|
|
|
var self = this;
|
2016-12-05 07:02:18 +01:00
|
|
|
function less_func(msg, ref_id, a_idx) {
|
2014-03-11 20:17:14 +01:00
|
|
|
if (self._is_localonly_id(msg.id)) {
|
|
|
|
// First non-local message before this one
|
2016-12-02 15:16:33 +01:00
|
|
|
var effective = self._next_nonlocal_message(self._items, a_idx,
|
|
|
|
function (idx) { return idx - 1; });
|
2014-03-11 20:17:14 +01:00
|
|
|
if (effective) {
|
|
|
|
// Turn the 10.02 in [11, 10.02, 12] into 11.02
|
|
|
|
var decimal = parseFloat((msg.id % 1).toFixed(0.02));
|
|
|
|
var effective_id = effective.id + decimal;
|
|
|
|
return effective_id < ref_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg.id < ref_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.lower_bound(self._items, id, less_func);
|
|
|
|
},
|
|
|
|
|
2013-02-20 00:49:21 +01:00
|
|
|
closest_id: function MessageList_closest_id(id) {
|
2014-03-11 20:17:14 +01:00
|
|
|
// We directly keep track of local-only messages,
|
|
|
|
// so if we're asked for one that we know we have,
|
|
|
|
// just return it directly
|
|
|
|
if (this._local_only.hasOwnProperty(id)) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2013-07-25 22:08:16 +02:00
|
|
|
var items = this._items;
|
|
|
|
|
|
|
|
if (items.length === 0) {
|
2013-02-20 00:49:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
|
2014-03-11 20:17:14 +01:00
|
|
|
var closest = this._lower_bound(id);
|
|
|
|
|
2014-03-12 20:39:04 +01:00
|
|
|
if (closest < items.length && id === items[closest].id) {
|
2014-03-11 20:17:14 +01:00
|
|
|
return items[closest].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
var potential_closest_matches = [];
|
|
|
|
if (closest > 0 && this._is_localonly_id(items[closest - 1].id)) {
|
|
|
|
// Since we treated all blocks of local ids as their left-most-non-local message
|
|
|
|
// for lower_bound purposes, find the real leftmost index (first non-local id)
|
|
|
|
do {
|
|
|
|
potential_closest_matches.push(closest);
|
2016-11-30 19:05:04 +01:00
|
|
|
closest -= 1;
|
2016-12-01 20:02:56 +01:00
|
|
|
} while (closest > 0 && this._is_localonly_id(items[closest - 1].id));
|
2014-03-11 20:17:14 +01:00
|
|
|
}
|
|
|
|
potential_closest_matches.push(closest);
|
|
|
|
|
|
|
|
if (closest === items.length) {
|
2013-02-20 00:49:21 +01:00
|
|
|
closest = closest - 1;
|
2014-03-11 20:17:14 +01:00
|
|
|
} else {
|
2016-12-02 15:16:33 +01:00
|
|
|
// Any of the ids that we skipped over (due to them being local-only) might be the
|
|
|
|
// closest ID to the desired one, in case there is no exact match.
|
2014-03-11 20:17:14 +01:00
|
|
|
potential_closest_matches.unshift(_.last(potential_closest_matches) - 1);
|
|
|
|
var best_match = items[closest].id;
|
|
|
|
|
|
|
|
_.each(potential_closest_matches, function (potential_idx) {
|
|
|
|
if (potential_idx < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-16 00:30:41 +02:00
|
|
|
var item = items[potential_idx];
|
|
|
|
|
|
|
|
if (item === undefined) {
|
|
|
|
blueslip.warn('Invalid potential_idx: ' + potential_idx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var potential_match = item.id;
|
2014-03-11 20:17:14 +01:00
|
|
|
// If the potential id is the closest to the requested, save that one
|
|
|
|
if (Math.abs(id - potential_match) < Math.abs(best_match - id)) {
|
|
|
|
best_match = potential_match;
|
|
|
|
closest = potential_idx;
|
|
|
|
}
|
|
|
|
});
|
2013-02-20 00:49:21 +01:00
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
return items[closest].id;
|
2013-02-20 00:49:21 +01:00
|
|
|
},
|
|
|
|
|
2013-07-24 22:33:06 +02:00
|
|
|
advance_past_messages: function MessageList_advance_past_messages(msg_ids) {
|
|
|
|
// Start with the current pointer, but then keep advancing the
|
|
|
|
// pointer while the next message's id is in msg_ids. See trac #1555
|
|
|
|
// for more context, but basically we are skipping over contiguous
|
|
|
|
// messages that we have recently visited.
|
|
|
|
var next_msg_id = 0;
|
|
|
|
|
|
|
|
var id_set = {};
|
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(msg_ids, function (msg_id) {
|
2013-07-24 22:33:06 +02:00
|
|
|
id_set[msg_id] = true;
|
|
|
|
});
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
var idx = this.selected_idx() + 1;
|
2013-07-24 22:33:06 +02:00
|
|
|
while (idx < this._items.length) {
|
|
|
|
var msg_id = this._items[idx].id;
|
|
|
|
if (!id_set[msg_id]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next_msg_id = msg_id;
|
2016-11-30 19:05:04 +01:00
|
|
|
idx += 1;
|
2013-07-24 22:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (next_msg_id > 0) {
|
|
|
|
this._selected_id = next_msg_id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
_add_to_hash: function MessageList__add_to_hash(messages) {
|
|
|
|
var self = this;
|
|
|
|
messages.forEach(function (elem) {
|
2013-12-18 20:09:09 +01:00
|
|
|
var id = parseFloat(elem.id);
|
2012-12-05 23:54:49 +01:00
|
|
|
if (isNaN(id)) {
|
2013-03-11 17:32:28 +01:00
|
|
|
blueslip.fatal("Bad message id");
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
2014-03-11 19:59:44 +01:00
|
|
|
if (self._is_localonly_id(id)) {
|
|
|
|
self._local_only[id] = elem;
|
|
|
|
}
|
2012-12-05 23:54:49 +01:00
|
|
|
if (self._hash[id] !== undefined) {
|
2013-03-11 17:32:28 +01:00
|
|
|
blueslip.error("Duplicate message added to MessageList");
|
|
|
|
return;
|
2012-12-05 23:54:49 +01:00
|
|
|
}
|
|
|
|
self._hash[id] = elem;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
selected_idx: function MessageList_selected_idx() {
|
2014-03-11 20:17:14 +01:00
|
|
|
return this._lower_bound(this._selected_id);
|
2013-04-10 18:30:36 +02:00
|
|
|
},
|
|
|
|
|
2014-02-05 16:55:24 +01:00
|
|
|
subscribed_bookend_content: function (stream_name) {
|
2017-06-06 01:54:56 +02:00
|
|
|
return i18n.t("You subscribed to stream __stream__",
|
|
|
|
{stream: stream_name});
|
2014-02-05 16:55:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
unsubscribed_bookend_content: function (stream_name) {
|
2017-06-06 01:54:56 +02:00
|
|
|
return i18n.t("You unsubscribed from stream __stream__",
|
|
|
|
{stream: stream_name});
|
2014-02-05 16:55:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
not_subscribed_bookend_content: function (stream_name) {
|
2017-06-06 01:54:56 +02:00
|
|
|
return i18n.t("You are not subscribed to stream __stream__",
|
|
|
|
{stream: stream_name});
|
2014-02-05 16:55:24 +01:00
|
|
|
},
|
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
// Maintains a trailing bookend element explaining any changes in
|
|
|
|
// your subscribed/unsubscribed status at the bottom of the
|
|
|
|
// message list.
|
|
|
|
update_trailing_bookend: function MessageList_update_trailing_bookend() {
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.clear_trailing_bookend();
|
2013-04-10 23:38:30 +02:00
|
|
|
if (!this.narrowed) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-15 10:55:40 +02:00
|
|
|
var stream_name = narrow_state.stream();
|
|
|
|
if (stream_name === undefined) {
|
2013-04-10 23:38:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-02 17:09:31 +01:00
|
|
|
var trailing_bookend_content;
|
2017-06-21 02:20:33 +02:00
|
|
|
var show_button = true;
|
2017-09-15 10:55:40 +02:00
|
|
|
var subscribed = stream_data.is_subscribed(stream_name);
|
2013-04-10 23:38:30 +02:00
|
|
|
if (subscribed) {
|
2017-09-15 10:55:40 +02:00
|
|
|
trailing_bookend_content = this.subscribed_bookend_content(stream_name);
|
2013-04-10 23:38:30 +02:00
|
|
|
} else {
|
|
|
|
if (!this.last_message_historical) {
|
2017-09-15 10:55:40 +02:00
|
|
|
trailing_bookend_content = this.unsubscribed_bookend_content(stream_name);
|
2017-06-21 02:20:33 +02:00
|
|
|
|
2017-09-15 10:56:34 +02:00
|
|
|
// For invite only streams or streams that no longer
|
|
|
|
// exist, hide the resubscribe button
|
|
|
|
var sub = stream_data.get_sub(stream_name);
|
|
|
|
if (sub !== undefined) {
|
|
|
|
show_button = !sub.invite_only;
|
|
|
|
} else {
|
|
|
|
show_button = false;
|
|
|
|
}
|
2013-04-10 23:38:30 +02:00
|
|
|
} else {
|
2017-09-15 10:55:40 +02:00
|
|
|
trailing_bookend_content = this.not_subscribed_bookend_content(stream_name);
|
2013-04-10 23:38:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (trailing_bookend_content !== undefined) {
|
2017-06-21 02:20:33 +02:00
|
|
|
this.view.render_trailing_bookend(trailing_bookend_content, subscribed, show_button);
|
2013-04-10 23:38:30 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-19 00:43:59 +02:00
|
|
|
unmuted_messages: function MessageList_unmuted_messages(messages) {
|
|
|
|
return _.reject(messages, function (message) {
|
2013-12-09 23:12:27 +01:00
|
|
|
return muting.is_topic_muted(message.stream, message.subject) &&
|
|
|
|
!message.mentioned;
|
2013-09-19 00:43:59 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-01-22 22:20:36 +01:00
|
|
|
append: function MessageList_append(messages, opts) {
|
|
|
|
opts = _.extend({delay_render: false, messages_are_new: false}, opts);
|
|
|
|
|
2013-09-19 00:43:59 +02:00
|
|
|
var viewable_messages;
|
|
|
|
if (this.muting_enabled) {
|
|
|
|
this._all_items = this._all_items.concat(messages);
|
|
|
|
viewable_messages = this.unmuted_messages(messages);
|
|
|
|
} else {
|
|
|
|
viewable_messages = messages;
|
|
|
|
}
|
|
|
|
this._items = this._items.concat(viewable_messages);
|
2013-08-14 23:48:28 +02:00
|
|
|
|
|
|
|
this.num_appends += 1;
|
|
|
|
|
2012-12-05 23:54:49 +01:00
|
|
|
this._add_to_hash(messages);
|
2013-02-28 22:38:08 +01:00
|
|
|
|
2014-01-22 22:20:36 +01:00
|
|
|
if (!opts.delay_render) {
|
|
|
|
this.view.append(viewable_messages, opts.messages_are_new);
|
|
|
|
}
|
2012-12-05 23:54:49 +01:00
|
|
|
},
|
|
|
|
|
2013-02-28 23:00:03 +01:00
|
|
|
prepend: function MessageList_prepend(messages) {
|
2013-09-19 00:43:59 +02:00
|
|
|
var viewable_messages;
|
|
|
|
if (this.muting_enabled) {
|
|
|
|
this._all_items = messages.concat(this._all_items);
|
|
|
|
viewable_messages = this.unmuted_messages(messages);
|
|
|
|
} else {
|
|
|
|
viewable_messages = messages;
|
|
|
|
}
|
|
|
|
this._items = viewable_messages.concat(this._items);
|
2012-12-05 23:54:49 +01:00
|
|
|
this._add_to_hash(messages);
|
2013-09-19 00:43:59 +02:00
|
|
|
this.view.prepend(viewable_messages);
|
2012-12-05 23:54:49 +01:00
|
|
|
},
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
add_and_rerender: function MessageList_add_and_rerender(messages) {
|
2013-04-10 18:30:36 +02:00
|
|
|
// To add messages that might be in the interior of our
|
|
|
|
// existing messages list, we just add the new messages and
|
|
|
|
// then rerender the whole thing.
|
2013-09-19 00:43:59 +02:00
|
|
|
|
|
|
|
var viewable_messages;
|
|
|
|
if (this.muting_enabled) {
|
|
|
|
this._all_items = messages.concat(this._all_items);
|
|
|
|
this._all_items.sort(function (a, b) {return a.id - b.id;});
|
|
|
|
|
|
|
|
viewable_messages = this.unmuted_messages(messages);
|
|
|
|
this._items = viewable_messages.concat(this._items);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this._items = messages.concat(this._items);
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
this._items.sort(function (a, b) {return a.id - b.id;});
|
2013-04-10 18:30:36 +02:00
|
|
|
this._add_to_hash(messages);
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.rerender_the_whole_thing();
|
2013-04-10 18:30:36 +02:00
|
|
|
},
|
|
|
|
|
2013-12-17 20:50:11 +01:00
|
|
|
remove_and_rerender: function MessageList_remove_and_rerender(messages) {
|
|
|
|
var self = this;
|
|
|
|
_.each(messages, function (message) {
|
|
|
|
var stored_message = self._hash[message.id];
|
|
|
|
if (stored_message !== undefined) {
|
|
|
|
delete self._hash[stored_message];
|
|
|
|
}
|
2014-03-13 20:07:33 +01:00
|
|
|
delete self._local_only[message.id];
|
2013-12-17 20:50:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var msg_ids_to_remove = {};
|
|
|
|
_.each(messages, function (message) {
|
|
|
|
msg_ids_to_remove[message.id] = true;
|
|
|
|
});
|
|
|
|
this._items = _.filter(this._items, function (message) {
|
|
|
|
return !msg_ids_to_remove.hasOwnProperty(message.id);
|
|
|
|
});
|
|
|
|
if (this.muting_enabled) {
|
|
|
|
this._all_items = _.filter(this._all_items, function (message) {
|
|
|
|
return !msg_ids_to_remove.hasOwnProperty(message.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-25 22:45:11 +01:00
|
|
|
this.rerender();
|
2013-12-17 20:50:11 +01:00
|
|
|
},
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
show_edit_message: function MessageList_show_edit_message(row, edit_obj) {
|
|
|
|
row.find(".message_edit_form").empty().append(edit_obj.form);
|
2017-04-20 23:31:37 +02:00
|
|
|
row.find(".message_content, .status-message").hide();
|
2017-09-11 01:35:14 +02:00
|
|
|
row.find(".message_edit").css("display", "block");
|
2013-07-16 23:36:31 +02:00
|
|
|
row.find(".message_edit_content").autosize();
|
2013-05-15 00:22:16 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
hide_edit_message: function MessageList_hide_edit_message(row) {
|
2017-04-20 23:17:42 +02:00
|
|
|
row.find(".message_content, .status-message").show();
|
2013-05-15 00:22:16 +02:00
|
|
|
row.find(".message_edit").hide();
|
2017-06-15 18:15:09 +02:00
|
|
|
row.trigger("mouseleave");
|
2013-05-15 00:22:16 +02:00
|
|
|
},
|
|
|
|
|
2013-08-16 23:45:13 +02:00
|
|
|
show_edit_topic: function MessageList_show_edit_topic(recipient_row, form) {
|
|
|
|
recipient_row.find(".topic_edit_form").empty().append(form);
|
2017-08-28 06:54:31 +02:00
|
|
|
recipient_row.find('.icon-vector-pencil').hide();
|
2013-08-16 23:45:13 +02:00
|
|
|
recipient_row.find(".stream_topic").hide();
|
|
|
|
recipient_row.find(".topic_edit").show();
|
|
|
|
},
|
|
|
|
|
|
|
|
hide_edit_topic: function MessageList_hide_edit_topic(recipient_row) {
|
|
|
|
recipient_row.find(".stream_topic").show();
|
2017-08-28 06:54:31 +02:00
|
|
|
recipient_row.find('.icon-vector-pencil').show();
|
2013-08-16 23:45:13 +02:00
|
|
|
recipient_row.find(".topic_edit").hide();
|
|
|
|
},
|
|
|
|
|
2013-07-26 17:19:13 +02:00
|
|
|
show_message_as_read: function (message, options) {
|
2013-08-14 22:00:32 +02:00
|
|
|
var row = this.get_row(message.id);
|
2014-01-23 18:23:31 +01:00
|
|
|
if ((options.from === 'pointer' && feature_flags.mark_read_at_bottom) ||
|
|
|
|
options.from === "server") {
|
2013-07-26 17:19:13 +02:00
|
|
|
row.find('.unread_marker').addClass('fast_fade');
|
|
|
|
} else {
|
|
|
|
row.find('.unread_marker').addClass('slow_fade');
|
|
|
|
}
|
|
|
|
row.removeClass('unread');
|
2013-07-01 23:28:27 +02:00
|
|
|
},
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
rerender: function MessageList_rerender() {
|
|
|
|
// We need to clear the rendering state, rather than just
|
2013-08-16 17:10:22 +02:00
|
|
|
// doing clear_table, since we want to potentially recollapse
|
2013-05-14 21:18:11 +02:00
|
|
|
// things.
|
2013-09-30 21:23:57 +02:00
|
|
|
this._selected_id = this.closest_id(this._selected_id);
|
2013-08-16 17:10:22 +02:00
|
|
|
this.view.clear_rendering_state(false);
|
|
|
|
this.view.update_render_window(this.selected_idx(), false);
|
|
|
|
this.view.rerender_preserving_scrolltop();
|
2013-06-04 22:07:44 +02:00
|
|
|
if (this._selected_id !== -1) {
|
|
|
|
this.select_id(this._selected_id);
|
|
|
|
}
|
2013-05-14 21:18:11 +02:00
|
|
|
},
|
|
|
|
|
2013-09-19 00:43:59 +02:00
|
|
|
rerender_after_muting_changes: function MessageList_rerender_after_muting_changes() {
|
|
|
|
if (!this.muting_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._items = this.unmuted_messages(this._all_items);
|
|
|
|
this.rerender();
|
|
|
|
},
|
|
|
|
|
2016-04-23 00:56:44 +02:00
|
|
|
all_messages: function MessageList_all_messages() {
|
2012-12-05 23:54:49 +01:00
|
|
|
return this._items;
|
2013-08-14 22:00:32 +02:00
|
|
|
},
|
|
|
|
|
2017-08-02 22:37:13 +02:00
|
|
|
first_unread_message_id: function MessageList_first_unread_message_id() {
|
|
|
|
var first_unread = _.find(this._items, function (message) {
|
|
|
|
return unread.message_unread(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (first_unread) {
|
|
|
|
return first_unread.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no unread, return the bottom message
|
|
|
|
return this.last().id;
|
|
|
|
},
|
|
|
|
|
2014-01-31 22:06:07 +01:00
|
|
|
// Returns messages from the given message list in the specified range, inclusive
|
|
|
|
message_range: function MessageList_message_range(start, end) {
|
|
|
|
if (start === -1) {
|
|
|
|
blueslip.error("message_range given a start of -1");
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:17:14 +01:00
|
|
|
var start_idx = this._lower_bound(start);
|
|
|
|
var end_idx = this._lower_bound(end);
|
2014-01-31 22:06:07 +01:00
|
|
|
return this._items.slice(start_idx, end_idx + 1);
|
|
|
|
},
|
|
|
|
|
2013-08-14 22:00:32 +02:00
|
|
|
get_row: function (id) {
|
2013-08-16 17:10:22 +02:00
|
|
|
return this.view.get_row(id);
|
2013-08-22 19:57:48 +02:00
|
|
|
},
|
|
|
|
|
2014-03-11 19:59:44 +01:00
|
|
|
_is_localonly_id: function MessageList__is_localonly_id(id) {
|
|
|
|
return id % 1 !== 0;
|
|
|
|
},
|
|
|
|
|
2016-12-02 15:16:33 +01:00
|
|
|
_next_nonlocal_message: function MessageList__next_nonlocal_message(item_list,
|
|
|
|
start_index, op) {
|
2014-03-11 19:59:44 +01:00
|
|
|
var cur_idx = start_index;
|
|
|
|
do {
|
|
|
|
cur_idx = op(cur_idx);
|
2016-12-01 20:02:56 +01:00
|
|
|
} while (item_list[cur_idx] !== undefined && this._is_localonly_id(item_list[cur_idx].id));
|
2014-03-11 19:59:44 +01:00
|
|
|
return item_list[cur_idx];
|
|
|
|
},
|
|
|
|
|
2017-01-24 19:12:21 +01:00
|
|
|
update_user_full_name: function (user_id, full_name) {
|
|
|
|
_.each(this._items, function (item) {
|
|
|
|
if (item.sender_id && (item.sender_id === user_id)) {
|
|
|
|
item.sender_full_name = full_name;
|
|
|
|
}
|
|
|
|
});
|
2018-04-03 03:24:36 +02:00
|
|
|
if (this.table_name !== undefined) {
|
|
|
|
this.view.rerender_preserving_scrolltop();
|
|
|
|
}
|
2017-01-24 19:12:21 +01:00
|
|
|
},
|
|
|
|
|
2017-02-23 00:31:34 +01:00
|
|
|
update_user_avatar: function (user_id, avatar_url) {
|
|
|
|
// TODO:
|
|
|
|
// We may want to de-dup some logic with update_user_full_name,
|
|
|
|
// especially if we want to optimize this with some kind of
|
|
|
|
// hash that maps sender_id -> messages.
|
|
|
|
_.each(this._items, function (item) {
|
|
|
|
if (item.sender_id && (item.sender_id === user_id)) {
|
|
|
|
item.small_avatar_url = avatar_url;
|
|
|
|
}
|
|
|
|
});
|
2018-04-03 03:24:36 +02:00
|
|
|
if (this.table_name !== undefined) {
|
|
|
|
this.view.rerender_preserving_scrolltop();
|
|
|
|
}
|
2017-02-23 00:31:34 +01:00
|
|
|
},
|
|
|
|
|
2017-01-05 17:34:27 +01:00
|
|
|
update_stream_name: function MessageList_update_stream_name(stream_id,
|
|
|
|
new_stream_name) {
|
2013-08-22 19:57:48 +02:00
|
|
|
_.each(this._items, function (item) {
|
2017-01-05 17:34:27 +01:00
|
|
|
if (item.stream_id && (item.stream_id === stream_id)) {
|
|
|
|
item.display_recipient = new_stream_name;
|
|
|
|
item.stream = new_stream_name;
|
2013-08-22 19:57:48 +02:00
|
|
|
}
|
|
|
|
});
|
2018-04-03 03:24:36 +02:00
|
|
|
if (this.table_name !== undefined) {
|
|
|
|
this.view.rerender_preserving_scrolltop();
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
change_message_id: function MessageList_change_message_id(old_id, new_id) {
|
|
|
|
// Update our local cache that uses the old id to the new id
|
|
|
|
function message_sort_func(a, b) {return a.id - b.id;}
|
|
|
|
|
|
|
|
if (this._hash.hasOwnProperty(old_id)) {
|
2014-03-07 22:08:57 +01:00
|
|
|
var msg = this._hash[old_id];
|
2013-12-19 17:03:08 +01:00
|
|
|
delete this._hash[old_id];
|
2014-03-07 22:08:57 +01:00
|
|
|
this._hash[new_id] = msg;
|
2014-01-24 17:15:51 +01:00
|
|
|
} else {
|
|
|
|
return;
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
|
|
|
|
2014-03-11 19:59:44 +01:00
|
|
|
if (this._local_only.hasOwnProperty(old_id)) {
|
|
|
|
if (this._is_localonly_id(new_id)) {
|
|
|
|
this._local_only[new_id] = this._local_only[old_id];
|
|
|
|
}
|
|
|
|
delete this._local_only[old_id];
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
if (this._selected_id === old_id) {
|
|
|
|
this._selected_id = new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this message is now out of order, re-order and re-render
|
|
|
|
var self = this;
|
|
|
|
setTimeout(function () {
|
|
|
|
var current_message = self._hash[new_id];
|
|
|
|
var index = self._items.indexOf(current_message);
|
|
|
|
|
|
|
|
if (index === -1) {
|
2017-10-06 21:36:39 +02:00
|
|
|
if (!self.muting_enabled && current_msg_list === self) {
|
2013-12-19 17:03:08 +01:00
|
|
|
blueslip.error("Trying to re-order message but can't find message with new_id in _items!");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-02 15:16:33 +01:00
|
|
|
var next = self._next_nonlocal_message(self._items, index,
|
|
|
|
function (idx) { return idx + 1; });
|
|
|
|
var prev = self._next_nonlocal_message(self._items, index,
|
|
|
|
function (idx) { return idx - 1; });
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
|
|
if ((next !== undefined && current_message.id > next.id) ||
|
|
|
|
(prev !== undefined && current_message.id < prev.id)) {
|
|
|
|
blueslip.debug("Changed message ID from server caused out-of-order list, reordering");
|
|
|
|
self._items.sort(message_sort_func);
|
|
|
|
if (self.muting_enabled) {
|
|
|
|
self._all_items.sort(message_sort_func);
|
|
|
|
}
|
2014-02-25 21:50:31 +01:00
|
|
|
self.view.rerender_preserving_scrolltop();
|
2014-03-12 22:54:28 +01:00
|
|
|
|
|
|
|
if (self._selected_id !== -1) {
|
|
|
|
self.select_id(self._selected_id);
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
|
|
|
}, 0);
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2016-11-23 05:06:34 +01:00
|
|
|
|
2017-03-27 18:00:31 +02:00
|
|
|
get_last_message_sent_by_me: function () {
|
2016-11-23 05:06:34 +01:00
|
|
|
var msg_index = _.findLastIndex(this._items, {sender_id: page_params.user_id});
|
|
|
|
if (msg_index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var msg = this._items[msg_index];
|
2017-03-27 18:00:31 +02:00
|
|
|
return msg;
|
2016-11-23 05:06:34 +01:00
|
|
|
},
|
2012-12-05 23:54:49 +01:00
|
|
|
};
|
2013-04-09 19:59:15 +02:00
|
|
|
|
2016-04-21 22:49:23 +02:00
|
|
|
exports.all = new exports.MessageList(
|
|
|
|
undefined, undefined,
|
|
|
|
{muting_enabled: false}
|
|
|
|
);
|
|
|
|
|
2013-05-30 16:11:06 +02:00
|
|
|
// We stop autoscrolling when the user is clearly in the middle of
|
|
|
|
// doing something. Be careful, though, if you try to capture
|
|
|
|
// mousemove, then you will have to contend with the autoscroll
|
|
|
|
// itself generating mousemove events.
|
2017-07-03 01:53:36 +02:00
|
|
|
$(document).on('message_selected.zulip zuliphashchange.zulip wheel', function () {
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.stop_auto_scrolling();
|
2013-04-09 19:59:15 +02:00
|
|
|
});
|
2016-04-20 22:37:26 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
2013-02-26 23:30:13 +01:00
|
|
|
}());
|
2013-08-06 21:30:31 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
2016-04-20 22:37:26 +02:00
|
|
|
module.exports = message_list;
|
2013-08-06 21:30:31 +02:00
|
|
|
}
|