Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message_list_data: Convert MessageListData to an ES6 class.

Anders Kaseorg8e5574fbe660
diff --git a/static/js/message_list_data.js b/static/js/message_list_data.js
index 80d9d8ce041..edf9e22a824 100644
--- a/static/js/message_list_data.js
+++ b/static/js/message_list_data.js
@@ -2,45 +2,45 @@ const _ = require("lodash");
const util = require("./util");
-function MessageListData(opts) {
- this.muting_enabled = opts.muting_enabled;
- if (this.muting_enabled) {
- this._all_items = [];
- }
- this._items = [];
- this._hash = new Map();
- this._local_only = new Set();
- this._selected_id = -1;
+class MessageListData {
+ constructor(opts) {
+ this.muting_enabled = opts.muting_enabled;
+ if (this.muting_enabled) {
+ this._all_items = [];
+ }
+ this._items = [];
+ this._hash = new Map();
+ this._local_only = new Set();
+ this._selected_id = -1;
- let filter = opts.filter;
- if (filter === undefined) {
- filter = new Filter();
- }
+ let filter = opts.filter;
+ if (filter === undefined) {
+ filter = new Filter();
+ }
- this.filter = filter;
- this.fetch_status = FetchStatus();
-}
+ this.filter = filter;
+ this.fetch_status = FetchStatus();
+ }
-MessageListData.prototype = {
all_messages() {
return this._items;
- },
+ }
num_items() {
return this._items.length;
- },
+ }
empty() {
return this._items.length === 0;
- },
+ }
first() {
return this._items[0];
- },
+ }
last() {
return this._items[this._items.length - 1];
- },
+ }
select_idx() {
if (this._selected_id === -1) {
@@ -53,7 +53,7 @@ MessageListData.prototype = {
return;
}
return i;
- },
+ }
prev() {
const i = this.select_idx();
@@ -67,7 +67,7 @@ MessageListData.prototype = {
}
return this._items[i - 1].id;
- },
+ }
next() {
const i = this.select_idx();
@@ -81,7 +81,7 @@ MessageListData.prototype = {
}
return this._items[i + 1].id;
- },
+ }
is_at_end() {
if (this._selected_id === -1) {
@@ -97,7 +97,7 @@ MessageListData.prototype = {
const last_msg = this._items[n - 1];
return last_msg.id === this._selected_id;
- },
+ }
nth_most_recent_id(n) {
const i = this._items.length - n;
@@ -105,7 +105,7 @@ MessageListData.prototype = {
return -1;
}
return this._items[i].id;
- },
+ }
clear() {
if (this.muting_enabled) {
@@ -114,7 +114,7 @@ MessageListData.prototype = {
this._items = [];
this._hash.clear();
- },
+ }
get(id) {
id = parseFloat(id);
@@ -122,66 +122,65 @@ MessageListData.prototype = {
return;
}
return this._hash.get(id);
- },
+ }
clear_selected_id() {
this._selected_id = -1;
- },
+ }
selected_id() {
return this._selected_id;
- },
+ }
set_selected_id(id) {
this._selected_id = id;
- },
+ }
selected_idx() {
return this._lower_bound(this._selected_id);
- },
+ }
reset_select_to_closest() {
this._selected_id = this.closest_id(this._selected_id);
- },
+ }
is_search() {
return this.filter.is_search();
- },
+ }
can_mark_messages_read() {
return this.filter.can_mark_messages_read();
- },
+ }
_get_predicate() {
// We cache this.
if (!this.predicate) {
this.predicate = this.filter.predicate();
}
return this.predicate;
- },
+ }
valid_non_duplicated_messages(messages) {
const predicate = this._get_predicate();
- const self = this;
- return messages.filter((msg) => self.get(msg.id) === undefined && predicate(msg));
- },
+ return messages.filter((msg) => this.get(msg.id) === undefined && predicate(msg));
+ }
filter_incoming(messages) {
const predicate = this._get_predicate();
return messages.filter(predicate);
- },
+ }
unmuted_messages(messages) {
return messages.filter(
(message) =>
!muting.is_topic_muted(message.stream_id, message.topic) || message.mentioned,
);
- },
+ }
update_items_for_muting() {
if (!this.muting_enabled) {
return;
}
this._items = this.unmuted_messages(this._all_items);
- },
+ }
first_unread_message_id() {
const first_unread = this._items.find((message) => unread.message_unread(message));
@@ -192,7 +191,7 @@ MessageListData.prototype = {
// if no unread, return the bottom message
return this.last().id;
- },
+ }
update_user_full_name(user_id, full_name) {
for (const item of this._items) {
@@ -200,7 +199,7 @@ MessageListData.prototype = {
item.sender_full_name = full_name;
}
}
- },
+ }
update_user_avatar(user_id, avatar_url) {
// TODO:
@@ -212,7 +211,7 @@ MessageListData.prototype = {
item.small_avatar_url = avatar_url;
}
}
- },
+ }
update_stream_name(stream_id, new_stream_name) {
for (const item of this._items) {
@@ -221,23 +220,22 @@ MessageListData.prototype = {
item.stream = new_stream_name;
}
}
- },
+ }
add_messages(messages) {
- const self = this;
let top_messages = [];
let bottom_messages = [];
let interior_messages = [];
// If we're initially populating the list, save the messages in
// bottom_messages regardless
- if (self.selected_id() === -1 && self.empty()) {
- const narrow_messages = self.filter_incoming(messages);
- bottom_messages = narrow_messages.filter((msg) => !self.get(msg.id));
+ if (this.selected_id() === -1 && this.empty()) {
+ const narrow_messages = this.filter_incoming(messages);
+ bottom_messages = narrow_messages.filter((msg) => !this.get(msg.id));
} else {
// Filter out duplicates that are already in self, and all messages
// that fail our filter predicate
- messages = self.valid_non_duplicated_messages(messages);
+ messages = this.valid_non_duplicated_messages(messages);
for (const msg of messages) {
// Put messages in correct order on either side of the
@@ -245,9 +243,9 @@ MessageListData.prototype = {
// is a (1) sorted, and (2) consecutive block of
// messages that belong in this message list; those
// facts should be ensured by the caller.
- if (self.empty() || msg.id > self.last().id) {
+ if (this.empty() || msg.id > this.last().id) {
bottom_messages.push(msg);
- } else if (msg.id < self.first().id) {
+ } else if (msg.id < this.first().id) {
top_messages.push(msg);
} else {
interior_messages.push(msg);
@@ -256,15 +254,15 @@ MessageListData.prototype = {
}
if (interior_messages.length > 0) {
- interior_messages = self.add_anywhere(interior_messages);
+ interior_messages = this.add_anywhere(interior_messages);
}
if (top_messages.length > 0) {
- top_messages = self.prepend(top_messages);
+ top_messages = this.prepend(top_messages);
}
if (bottom_messages.length > 0) {
- bottom_messages = self.append(bottom_messages);
+ bottom_messages = this.append(bottom_messages);
}
const info = {
@@ -274,7 +272,7 @@ MessageListData.prototype = {
};
return info;
- },
+ }
add_anywhere(messages) {
// Caller should have already filtered messages.
@@ -296,7 +294,7 @@ MessageListData.prototype = {
this._items.sort((a, b) => a.id - b.id);
this._add_to_hash(messages);
return viewable_messages;
- },
+ }
append(messages) {
// Caller should have already filtered
@@ -310,7 +308,7 @@ MessageListData.prototype = {
this._items = this._items.concat(viewable_messages);
this._add_to_hash(messages);
return viewable_messages;
- },
+ }
prepend(messages) {
// Caller should have already filtered
@@ -324,17 +322,15 @@ MessageListData.prototype = {
this._items = viewable_messages.concat(this._items);
this._add_to_hash(messages);
return viewable_messages;
- },
+ }
remove(messages) {
- const self = this;
-
for (const message of messages) {
- const stored_message = self._hash.get(message.id);
+ const stored_message = this._hash.get(message.id);
if (stored_message !== undefined) {
- self._hash.delete(stored_message);
+ this._hash.delete(stored_message);
}
- self._local_only.delete(message.id);
+ this._local_only.delete(message.id);
}
const msg_ids_to_remove = new Set();
@@ -349,7 +345,7 @@ MessageListData.prototype = {
(message) => !msg_ids_to_remove.has(message.id),
);
}
- },
+ }
// Returns messages from the given message list in the specified range, inclusive
message_range(start, end) {
@@ -360,18 +356,17 @@ MessageListData.prototype = {
const start_idx = this._lower_bound(start);
const end_idx = this._lower_bound(end);
return this._items.slice(start_idx, end_idx + 1);
- },
+ }
// 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(id) {
- const self = this;
- function less_func(msg, ref_id, a_idx) {
- if (self._is_localonly_id(msg.id)) {
+ const less_func = (msg, ref_id, a_idx) => {
+ if (this._is_localonly_id(msg.id)) {
// First non-local message before this one
- const effective = self._next_nonlocal_message(self._items, a_idx, (idx) => idx - 1);
+ const effective = this._next_nonlocal_message(this._items, a_idx, (idx) => idx - 1);
if (effective) {
// Turn the 10.02 in [11, 10.02, 12] into 11.02
const decimal = parseFloat((msg.id % 1).toFixed(0.02));
@@ -380,10 +375,10 @@ MessageListData.prototype = {
}
}
return msg.id < ref_id;
- }
+ };
- return util.lower_bound(self._items, id, less_func);
- },
+ return util.lower_bound(this._items, id, less_func);
+ }
closest_id(id) {
// We directly keep track of local-only messages,
@@ -444,7 +439,7 @@ MessageListData.prototype = {
}
}
return items[closest].id;
- },
+ }
advance_past_messages(msg_ids) {
// Start with the current pointer, but then keep advancing the
@@ -468,29 +463,28 @@ MessageListData.prototype = {
if (next_msg_id > 0) {
this.set_selected_id(next_msg_id);
}
- },
+ }
_add_to_hash(messages) {
- const self = this;
messages.forEach((elem) => {
const id = parseFloat(elem.id);
if (isNaN(id)) {
blueslip.fatal("Bad message id");
}
- if (self._is_localonly_id(id)) {
- self._local_only.add(id);
+ if (this._is_localonly_id(id)) {
+ this._local_only.add(id);
}
- if (self._hash.has(id)) {
+ if (this._hash.has(id)) {
blueslip.error("Duplicate message added to MessageListData");
return;
}
- self._hash.set(id, elem);
+ this._hash.set(id, elem);
});
- },
+ }
_is_localonly_id(id) {
return id % 1 !== 0;
- },
+ }
_next_nonlocal_message(item_list, start_index, op) {
let cur_idx = start_index;
@@ -498,7 +492,7 @@ MessageListData.prototype = {
cur_idx = op(cur_idx);
} while (item_list[cur_idx] !== undefined && this._is_localonly_id(item_list[cur_idx].id));
return item_list[cur_idx];
- },
+ }
change_message_id(old_id, new_id, opts) {
// Update our local cache that uses the old id to the new id
@@ -522,19 +516,16 @@ MessageListData.prototype = {
}
this.reorder_messages(new_id, opts);
- },
+ }
reorder_messages(new_id, opts) {
- function message_sort_func(a, b) {
- return a.id - b.id;
- }
+ const message_sort_func = (a, b) => a.id - b.id;
// If this message is now out of order, re-order and re-render
- const self = this;
- const current_message = self._hash.get(new_id);
- const index = self._items.indexOf(current_message);
+ const current_message = this._hash.get(new_id);
+ const index = this._items.indexOf(current_message);
if (index === -1) {
- if (!self.muting_enabled && opts.is_current_list()) {
+ if (!this.muting_enabled && opts.is_current_list()) {
blueslip.error(
"Trying to re-order message but can't find message with new_id in _items!",
);
@@ -542,17 +533,17 @@ MessageListData.prototype = {
return;
}
- const next = self._next_nonlocal_message(self._items, index, (idx) => idx + 1);
- const prev = self._next_nonlocal_message(self._items, index, (idx) => idx - 1);
+ const next = this._next_nonlocal_message(this._items, index, (idx) => idx + 1);
+ const prev = this._next_nonlocal_message(this._items, index, (idx) => idx - 1);
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);
+ this._items.sort(message_sort_func);
+ if (this.muting_enabled) {
+ this._all_items.sort(message_sort_func);
}
// The data updates above need to happen synchronously,
@@ -562,7 +553,7 @@ MessageListData.prototype = {
// don't have records for why we added it then.
setTimeout(opts.re_render, 0);
}
- },
+ }
get_last_message_sent_by_me() {
const msg_index = _.findLastIndex(this._items, {sender_id: page_params.user_id});
@@ -571,8 +562,8 @@ MessageListData.prototype = {
}
const msg = this._items[msg_index];
return msg;
- },
-};
+ }
+}
module.exports = MessageListData;