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;
if (!subs.have(stream_name)) {
if (!subs.is_subscribed(stream_name)) {
switch(check_stream_for_send(stream_name)) {
case "does-not-exist":
response = "<p>The stream <b>" +

View File

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

View File

@@ -321,24 +321,12 @@ exports.parse = function (str) {
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) {
if (message.type === "private") {
return true;
}
return exports.stream_in_home(message.stream);
return subs.in_home_view(message.stream);
};
exports.stream = function () {
@@ -580,7 +568,7 @@ function pick_empty_narrow_banner() {
// You have no private messages.
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.
return $("#nonsubbed_stream_narrow_message");
} else if (first_operator === "search") {

View File

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

View File

@@ -553,7 +553,7 @@ exports.setup_page = function () {
all_streams.sort();
all_streams.forEach(function (stream) {
if (exports.have(stream)) {
if (exports.is_subscribed(stream)) {
return;
}
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);
};
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);
if (sub !== undefined && sub.subscribed) {
return sub;
}
return false;
return sub !== undefined && sub.in_home_view;
};
exports.is_subscribed = function (stream_name) {
var sub = get_sub(stream_name);
return sub !== undefined && sub.subscribed;
};
function ajaxSubscribe(stream) {

View File

@@ -20,7 +20,7 @@ function make_tab_data() {
// Root breadcrumb item: Either Home or All Messages
var operator = ops[0][0];
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')) {
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
} else {

View File

@@ -1287,7 +1287,7 @@ $(function () {
var ypos = $(e.target).offset().top - viewport.scrollTop();
$(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",
fixed: true
});
@@ -1311,7 +1311,7 @@ $(function () {
}
$(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) {
colorpicker.spectrum(subs.sidebar_popover_colorpicker_options);
});

View File

@@ -142,14 +142,14 @@ exports.get_counts = function () {
}
$.each(unread_counts.stream, function(stream, msgs) {
if (! subs.have(stream)) {
if (! subs.is_subscribed(stream)) {
return true;
}
var count = Object.keys(msgs).length;
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;
}