mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
My recent refactoring that split out MessageListData
introduced a nasty bug where we were putting muted
messages into the "All Messages" view even though
the underlying list was correctly filtering
them, so the symptoms were two-fold:
- muted messages cluttered up your feed
- replying to the message caused a traceback (since
it wasn't actually in the underlying data
structure)
This has to do with what MessageListData.add_messages()
was passing back to MessageList to orchestrate drawing
in MessageListView.
I think what happened here is I got this working kind
of sloppily but correctly for the non-muting case and
then got in the weeds of some other stuff. Not my
finest moment.
The actual correct code here is simple enough. We
triage top, interior, and bottom, and then the respective
methods that put the data into the data structure
return the filtered lists (i.e. not muted) and put them
into the info structure.
Fixes #9656
220 lines
5.4 KiB
JavaScript
220 lines
5.4 KiB
JavaScript
zrequire('unread');
|
|
zrequire('util');
|
|
|
|
zrequire('Filter', 'js/filter');
|
|
zrequire('MessageListData', 'js/message_list_data');
|
|
|
|
set_global('page_params', {});
|
|
set_global('blueslip', global.make_zblueslip());
|
|
set_global('muting', {});
|
|
|
|
global.patch_builtin('setTimeout', (f, delay) => {
|
|
assert.equal(delay, 0);
|
|
return f();
|
|
});
|
|
|
|
function make_msg(msg_id) {
|
|
return {
|
|
id: msg_id,
|
|
unread: true,
|
|
};
|
|
}
|
|
|
|
function make_msgs(msg_ids) {
|
|
return _.map(msg_ids, make_msg);
|
|
}
|
|
|
|
function assert_contents(mld, msg_ids) {
|
|
const msgs = mld.all_messages();
|
|
assert.deepEqual(msgs, make_msgs(msg_ids));
|
|
}
|
|
|
|
run_test('basics', () => {
|
|
const mld = new MessageListData({
|
|
muting_enabled: false,
|
|
filter: undefined,
|
|
});
|
|
|
|
assert.equal(mld.is_search(), false);
|
|
|
|
mld.add_anywhere(make_msgs([35, 25, 15, 45]));
|
|
|
|
assert_contents(mld, [15, 25, 35, 45]);
|
|
|
|
const new_msgs = make_msgs([10, 20, 30, 40, 50, 60, 70]);
|
|
const info = mld.add_messages(new_msgs);
|
|
|
|
assert.deepEqual(info, {
|
|
top_messages: make_msgs([10]),
|
|
interior_messages: make_msgs([20, 30, 40]),
|
|
bottom_messages: make_msgs([50, 60, 70]),
|
|
});
|
|
|
|
assert_contents(mld, [10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70]);
|
|
|
|
assert.equal(mld.selected_id(), -1);
|
|
assert.equal(mld.closest_id(8), 10);
|
|
assert.equal(mld.closest_id(27), 25);
|
|
assert.equal(mld.closest_id(72), 70);
|
|
|
|
mld.set_selected_id(50);
|
|
assert.equal(mld.selected_id(), 50);
|
|
assert.equal(mld.selected_idx(), 8);
|
|
|
|
mld.remove([mld.get(50)]);
|
|
assert_contents(mld, [10, 15, 20, 25, 30, 35, 40, 45, 60, 70]);
|
|
|
|
mld.update_items_for_muting();
|
|
assert_contents(mld, [10, 15, 20, 25, 30, 35, 40, 45, 60, 70]);
|
|
|
|
mld.reset_select_to_closest();
|
|
assert.equal(mld.selected_id(), 45);
|
|
assert.equal(mld.selected_idx(), 7);
|
|
|
|
assert.equal(mld.first_unread_message_id(), 10);
|
|
mld.get(10).unread = false;
|
|
assert.equal(mld.first_unread_message_id(), 15);
|
|
|
|
|
|
mld.clear();
|
|
assert_contents(mld, []);
|
|
assert.equal(mld.closest_id(99), -1);
|
|
assert.equal(mld.get_last_message_sent_by_me(), undefined);
|
|
|
|
mld.add_messages(make_msgs([120, 125.01, 130, 140]));
|
|
assert_contents(mld, [120, 125.01, 130, 140]);
|
|
mld.set_selected_id(125.01);
|
|
assert.equal(mld.selected_id(), 125.01);
|
|
|
|
mld.get(125.01).id = 145;
|
|
mld.change_message_id(125.01, 145, {
|
|
re_render: () => {},
|
|
});
|
|
assert_contents(mld, [120, 130, 140, 145]);
|
|
|
|
_.each(mld.all_messages(), (msg) => {
|
|
msg.unread = false;
|
|
});
|
|
|
|
assert.equal(mld.first_unread_message_id(), 145);
|
|
});
|
|
|
|
run_test('muting enabled', () => {
|
|
const mld = new MessageListData({
|
|
muting_enabled: true,
|
|
filter: undefined,
|
|
});
|
|
|
|
muting.is_topic_muted = function () { return true; };
|
|
mld.add_anywhere(make_msgs([35, 25, 15, 45]));
|
|
assert_contents(mld, []);
|
|
|
|
mld.get(35).mentioned = true;
|
|
mld.update_items_for_muting();
|
|
assert.deepEqual(mld._items, [mld.get(35)]);
|
|
|
|
mld.remove(make_msgs([35, 15]));
|
|
assert_contents(mld, []);
|
|
assert.deepEqual(mld._all_items, make_msgs([25, 45]));
|
|
|
|
const msgs = make_msgs([10, 20]);
|
|
msgs[0].mentioned = true;
|
|
mld.prepend(msgs);
|
|
assert.deepEqual(mld._items, [mld.get(10)]);
|
|
assert.deepEqual(mld._all_items, msgs.concat(make_msgs([25, 45])));
|
|
|
|
mld.clear();
|
|
assert.deepEqual(mld._all_items, []);
|
|
});
|
|
|
|
run_test('more muting', () => {
|
|
muting.is_topic_muted = function (stream, topic) {
|
|
return topic === 'muted';
|
|
};
|
|
|
|
const mld = new MessageListData({
|
|
muting_enabled: true,
|
|
filter: undefined,
|
|
});
|
|
|
|
const orig_messages = [
|
|
{id: 3, subject: 'muted'},
|
|
{id: 4},
|
|
{id: 7, subject: 'muted'},
|
|
{id: 8},
|
|
];
|
|
|
|
const orig_info = mld.add_messages(orig_messages);
|
|
|
|
assert.deepEqual(orig_info, {
|
|
top_messages: [],
|
|
interior_messages: [],
|
|
bottom_messages: [
|
|
{id: 4},
|
|
{id: 8},
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(
|
|
_.pluck(mld._all_items, 'id'),
|
|
[3, 4, 7, 8]
|
|
);
|
|
|
|
assert.deepEqual(
|
|
_.pluck(mld.all_messages(), 'id'),
|
|
[4, 8]
|
|
);
|
|
|
|
const more_messages = [
|
|
{id: 1, subject: 'muted'},
|
|
{id: 2},
|
|
{id: 3, subject: 'muted'}, // dup
|
|
{id: 5, subject: 'muted'},
|
|
{id: 6},
|
|
{id: 9, subject: 'muted'},
|
|
{id: 10},
|
|
];
|
|
|
|
const more_info = mld.add_messages(more_messages);
|
|
|
|
assert.deepEqual(
|
|
_.pluck(mld._all_items, 'id'),
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
);
|
|
|
|
assert.deepEqual(
|
|
_.pluck(mld.all_messages(), 'id'),
|
|
[2, 4, 6, 8, 10]
|
|
);
|
|
|
|
assert.deepEqual(more_info, {
|
|
top_messages: [
|
|
{id: 2},
|
|
],
|
|
interior_messages: [
|
|
{id: 6},
|
|
],
|
|
bottom_messages: [
|
|
{id: 10},
|
|
],
|
|
});
|
|
|
|
});
|
|
|
|
run_test('errors', () => {
|
|
const mld = new MessageListData({
|
|
muting_enabled: false,
|
|
filter: undefined,
|
|
});
|
|
assert.equal(mld.get('bogus-id'), undefined);
|
|
|
|
blueslip.set_test_data('fatal', 'Bad message id');
|
|
mld._add_to_hash(['asdf']);
|
|
assert.equal(blueslip.get_test_logs('fatal').length, 1);
|
|
|
|
blueslip.set_test_data('error', 'Duplicate message added to MessageListData');
|
|
mld._hash[1] = 'taken';
|
|
mld._add_to_hash(make_msgs([1]));
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
});
|