Clean up subs.have by splitting it into logical pieces

(imported from commit 0fe55aa62ccc95cae5c9aba565b0e4c04692fb46)
This commit is contained in:
Leo Franchi
2013-06-12 15:13:19 -04:00
parent a9e4441bee
commit 969e0dd827
8 changed files with 26 additions and 33 deletions

View File

@@ -512,7 +512,7 @@ function validate_stream_message() {
var response; var response;
if (!subs.have(stream_name)) { if (!subs.is_subscribed(stream_name)) {
switch(check_stream_for_send(stream_name)) { switch(check_stream_for_send(stream_name)) {
case "does-not-exist": case "does-not-exist":
response = "<p>The stream <b>" + response = "<p>The stream <b>" +

View File

@@ -509,7 +509,7 @@ MessageList.prototype = {
if (stream === undefined) { if (stream === undefined) {
return; return;
} }
var trailing_bookend_content, subscribed = subs.have(stream); var trailing_bookend_content, subscribed = subs.is_subscribed(stream);
if (subscribed) { if (subscribed) {
if (this.last_message_historical) { if (this.last_message_historical) {
trailing_bookend_content = "--- Subscribed to stream " + stream + " ---"; trailing_bookend_content = "--- Subscribed to stream " + stream + " ---";

View File

@@ -321,24 +321,12 @@ exports.parse = function (str) {
return operators; return operators;
}; };
exports.stream_in_home = function (stream_name) {
// If we don't know about this stream for some reason,
// we might not have loaded the in_home_view information
// yet so show it
var stream = subs.have(stream_name);
if (stream) {
return stream.in_home_view;
} else {
return true;
}
};
exports.message_in_home = function (message) { exports.message_in_home = function (message) {
if (message.type === "private") { if (message.type === "private") {
return true; return true;
} }
return exports.stream_in_home(message.stream); return subs.in_home_view(message.stream);
}; };
exports.stream = function () { exports.stream = function () {
@@ -580,7 +568,7 @@ function pick_empty_narrow_banner() {
// You have no private messages. // You have no private messages.
return $("#empty_narrow_all_private_message"); return $("#empty_narrow_all_private_message");
} }
} else if ((first_operator === "stream") && !subs.have(first_operand)) { } else if ((first_operator === "stream") && !subs.is_subscribed(first_operand)) {
// You are narrowed to a stream to which you aren't subscribed. // You are narrowed to a stream to which you aren't subscribed.
return $("#nonsubbed_stream_narrow_message"); return $("#nonsubbed_stream_narrow_message");
} else if (first_operator === "search") { } else if (first_operator === "search") {

View File

@@ -39,8 +39,7 @@ exports.sort_narrow_list = function () {
var elems = []; var elems = [];
$.each(streams, function(i, stream) { $.each(streams, function(i, stream) {
// TODO: we should export the sub objects better var li = $(subs.get(stream).sidebar_li);
var li = $(subs.have(stream).sidebar_li);
if (sort_recent) { if (sort_recent) {
if (recent_subjects[stream] === undefined) { if (recent_subjects[stream] === undefined) {
li.addClass('inactive_stream'); li.addClass('inactive_stream');
@@ -93,8 +92,8 @@ function add_narrow_filter(name, type) {
var args = {name: name, var args = {name: name,
id: subs.stream_id(name), id: subs.stream_id(name),
uri: narrow.by_stream_uri(name), uri: narrow.by_stream_uri(name),
not_in_home_view: (subs.have(name).in_home_view === false), not_in_home_view: (subs.in_home_view(name) === false),
invite_only: subs.have(name).invite_only, invite_only: subs.get(name).invite_only,
color: subs.get_color(name)}; color: subs.get_color(name)};
var list_item = templates.render('stream_sidebar_row', args); var list_item = templates.render('stream_sidebar_row', args);
$("#" + type + "_filters").append(list_item); $("#" + type + "_filters").append(list_item);
@@ -185,7 +184,7 @@ exports.update_streams_sidebar = function () {
if (op_subject.length !== 0) { if (op_subject.length !== 0) {
subject = op_subject[0]; subject = op_subject[0];
} }
if (subs.have(op_stream[0])) { if (subs.is_subscribed(op_stream[0])) {
rebuild_recent_subjects(op_stream[0], subject); rebuild_recent_subjects(op_stream[0], subject);
} }
} }
@@ -268,7 +267,7 @@ $(function () {
} }
} }
var op_stream = event.filter.operands('stream'); var op_stream = event.filter.operands('stream');
if (op_stream.length !== 0 && subs.have(op_stream[0])) { if (op_stream.length !== 0 && subs.is_subscribed(op_stream[0])) {
var stream_li = get_filter_li('stream', op_stream[0]); var stream_li = get_filter_li('stream', op_stream[0]);
var op_subject = event.filter.operands('subject'); var op_subject = event.filter.operands('subject');
var subject; var subject;

View File

@@ -553,7 +553,7 @@ exports.setup_page = function () {
all_streams.sort(); all_streams.sort();
all_streams.forEach(function (stream) { all_streams.forEach(function (stream) {
if (exports.have(stream)) { if (exports.is_subscribed(stream)) {
return; return;
} }
var sub = create_sub(stream, {subscribed: false}); var sub = create_sub(stream, {subscribed: false});
@@ -599,12 +599,18 @@ exports.setup_page = function () {
$.when.apply(this, requests).then(populate_and_fill, failed_listing); $.when.apply(this, requests).then(populate_and_fill, failed_listing);
}; };
exports.have = function (stream_name) { exports.get = function (stream_name) {
return get_sub(stream_name);
};
exports.in_home_view = function (stream_name) {
var sub = get_sub(stream_name); var sub = get_sub(stream_name);
if (sub !== undefined && sub.subscribed) { return sub !== undefined && sub.in_home_view;
return sub; };
}
return false; exports.is_subscribed = function (stream_name) {
var sub = get_sub(stream_name);
return sub !== undefined && sub.subscribed;
}; };
function ajaxSubscribe(stream) { function ajaxSubscribe(stream) {

View File

@@ -20,7 +20,7 @@ function make_tab_data() {
// Root breadcrumb item: Either Home or All Messages // Root breadcrumb item: Either Home or All Messages
var operator = ops[0][0]; var operator = ops[0][0];
var operand = ops[0][1]; var operand = ops[0][1];
if ((operator === 'stream' && !narrow.stream_in_home(operand)) || if ((operator === 'stream' && !subs.in_home_view(operand)) ||
(operator === 'in' && operand === 'all')) { (operator === 'in' && operand === 'all')) {
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root")); tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
} else { } else {

View File

@@ -1287,7 +1287,7 @@ $(function () {
var ypos = $(e.target).offset().top - viewport.scrollTop(); var ypos = $(e.target).offset().top - viewport.scrollTop();
$(e.target).popover({ $(e.target).popover({
content: templates.render('stream_sidebar_actions', {'stream': subs.have(stream)}), content: templates.render('stream_sidebar_actions', {'stream': subs.get(stream)}),
trigger: "manual", trigger: "manual",
fixed: true fixed: true
}); });
@@ -1311,7 +1311,7 @@ $(function () {
} }
$(e.target).popover("show"); $(e.target).popover("show");
var popover = $('.streams_popover[data-id=' + subs.have(stream).id + ']'); var popover = $('.streams_popover[data-id=' + subs.get(stream).id + ']');
update_spectrum(popover, function(colorpicker) { update_spectrum(popover, function(colorpicker) {
colorpicker.spectrum(subs.sidebar_popover_colorpicker_options); colorpicker.spectrum(subs.sidebar_popover_colorpicker_options);
}); });

View File

@@ -142,14 +142,14 @@ exports.get_counts = function () {
} }
$.each(unread_counts.stream, function(stream, msgs) { $.each(unread_counts.stream, function(stream, msgs) {
if (! subs.have(stream)) { if (! subs.is_subscribed(stream)) {
return true; return true;
} }
var count = Object.keys(msgs).length; var count = Object.keys(msgs).length;
res.stream_count[stream] = count; res.stream_count[stream] = count;
if (narrow.stream_in_home(stream)) { if (subs.in_home_view(stream)) {
res.home_unread_messages += only_in_home_view(Object.keys(msgs)).length; res.home_unread_messages += only_in_home_view(Object.keys(msgs)).length;
} }