unread: Add code to set unread counts from page_params data.

This won't run in production due to the feature flag.
This commit is contained in:
Steve Howell
2017-08-01 08:50:40 -04:00
committed by Tim Abbott
parent 540c452fc2
commit b215229d22
4 changed files with 132 additions and 0 deletions

View File

@@ -17,6 +17,10 @@ var stream_data = require('js/stream_data.js');
set_global('stream_data', stream_data);
set_global('blueslip', {});
set_global('page_params', {});
set_global('feature_flags', {
load_server_counts: true,
});
var Dict = global.Dict;
var muting = global.muting;
@@ -465,7 +469,64 @@ stream_data.get_stream_id = function () {
assert(!unread.message_unread({flags: ['read']}));
}());
(function test_server_counts() {
// note that user_id 30 is "me"
page_params.unread_msgs = {
pms: [
{
sender_id: 101,
unread_message_ids: [
31, 32, 60, 61, 62, 63,
],
},
],
huddles: [
{
user_ids_string: "4,6,30,101",
unread_message_ids: [
34, 50,
],
},
],
streams: [
{
stream_id: 1,
topic: "test",
unread_message_ids: [
33, 35, 36,
],
},
],
mentions: [31, 34, 40, 41],
};
unread.declare_bankruptcy();
unread.initialize();
assert.equal(unread.num_unread_for_person('101'), 6);
assert.equal(unread.num_unread_for_person('4,6,101'), 2);
assert.equal(unread.num_unread_for_person('30'), 0);
assert.equal(unread.num_unread_for_topic(0, 'bogus'), 0);
assert.equal(unread.num_unread_for_topic(1, 'bogus'), 0);
assert.equal(unread.num_unread_for_topic(1, 'test'), 3);
assert.equal(unread.unread_mentions_counter.count(), 4);
unread.mark_as_read(40);
assert.equal(unread.unread_mentions_counter.count(), 3);
unread.mark_as_read(35);
assert.equal(unread.num_unread_for_topic(1, 'test'), 2);
unread.mark_as_read(34);
assert.equal(unread.num_unread_for_person('4,6,101'), 1);
}());
(function test_errors() {
unread.declare_bankruptcy();
global.blueslip.warn = function () {};
// Test unknown message leads to zero count

View File

@@ -2,6 +2,8 @@ var feature_flags = (function () {
var exports = {};
exports.load_server_counts = false;
// Experimental modification to support much wider message views.
exports.full_width = false;

View File

@@ -249,6 +249,7 @@ $(function () {
reload.initialize();
server_events.initialize();
people.initialize();
unread.initialize();
bot_data.initialize(); // Must happen after people.initialize()
message_fetch.initialize();
emoji.initialize();

View File

@@ -28,6 +28,12 @@ function make_id_set() {
return ids.has(id);
};
self.add_many = function (id_list) {
_.each(id_list, function (id) {
ids.set(id, true);
});
};
self.del = function (id) {
ids.del(id);
};
@@ -105,6 +111,29 @@ exports.unread_pm_counter = (function () {
bucketer.clear();
};
self.set_pms = function (pms) {
_.each(pms, function (obj) {
var user_ids_string = obj.sender_id.toString();
self.set_message_ids(user_ids_string, obj.unread_message_ids);
});
};
self.set_huddles = function (huddles) {
_.each(huddles, function (obj) {
var user_ids_string = people.pm_lookup_key(obj.user_ids_string);
self.set_message_ids(user_ids_string, obj.unread_message_ids);
});
};
self.set_message_ids = function (user_ids_string, unread_message_ids) {
_.each(unread_message_ids, function (msg_id) {
bucketer.add({
bucket_key: user_ids_string,
item_id: msg_id,
});
});
};
self.add = function (message) {
var user_ids_string = people.pm_reply_user_string(message);
if (user_ids_string) {
@@ -168,6 +197,19 @@ exports.unread_topic_counter = (function () {
bucketer.clear();
};
self.set_streams = function (objs) {
_.each(objs, function (obj) {
var stream_id = obj.stream_id;
var topic = obj.topic;
var unread_message_ids = obj.unread_message_ids;
_.each(unread_message_ids, function (msg_id) {
self.add(stream_id, topic, msg_id);
});
});
};
self.add = function (stream_id, topic, msg_id) {
bucketer.add({
bucket_key: stream_id,
@@ -417,6 +459,32 @@ exports.set_read_flag = function (message) {
message.unread = false;
};
exports.load_server_counts = function () {
var unread_msgs = page_params.unread_msgs;
exports.unread_pm_counter.set_huddles(unread_msgs.huddles);
exports.unread_pm_counter.set_pms(unread_msgs.pms);
exports.unread_topic_counter.set_streams(unread_msgs.streams);
exports.unread_mentions_counter.add_many(unread_msgs.mentions);
_.each(unread_msgs.huddles, function (obj) {
unread_messages.add_many(obj.unread_message_ids);
});
_.each(unread_msgs.pms, function (obj) {
unread_messages.add_many(obj.unread_message_ids);
});
_.each(unread_msgs.streams, function (obj) {
unread_messages.add_many(obj.unread_message_ids);
});
unread_messages.add_many(unread_msgs.mentions);
};
exports.initialize = function () {
if (feature_flags.load_server_counts) {
exports.load_server_counts();
}
};
return exports;
}());
if (typeof module !== 'undefined') {