Use an associative array for faster subscription checks.

This also helps us manage checking, case-insensitive, for
subscriptions while preserving the casing used by the class creator
for display.

It also fixes a bug where the class_list would become out of sync with
your true subscriptions, allowing you to appear to send messages to
classes to which you had unsubscribed.

(imported from commit 5e8d017bcfb27a71c52f7517733eda7b926d721b)
This commit is contained in:
Jessica McKellar 2012-10-09 15:01:41 -04:00
parent 26906777c1
commit fb62e808d7
5 changed files with 46 additions and 13 deletions

View File

@ -45,7 +45,6 @@
<script type="text/javascript"> <script type="text/javascript">
var initial_pointer = {{ user_profile.pointer }}; var initial_pointer = {{ user_profile.pointer }};
var email = "{{ user_profile.user.email }}"; var email = "{{ user_profile.user.email }}";
// class_list only contains lower-case names.
var class_list = {{ classes }}; var class_list = {{ classes }};
var people_list = {{ people }}; var people_list = {{ people }};
var have_initial_messages = {{ have_initial_messages }}; var have_initial_messages = {{ have_initial_messages }};

View File

@ -28,7 +28,8 @@ var globals =
+ ' loading_spinner templates' + ' loading_spinner templates'
// subscribe.js // subscribe.js
+ ' fetch_subs sub_from_home' + ' fetch_subs sub_from_home subscribed_to class_list_hash'
+ ' add_to_class_list'
// ui.js // ui.js
+ ' register_onclick hide_email show_email' + ' register_onclick hide_email show_email'

View File

@ -124,7 +124,7 @@ function validate_class_message() {
if (!check_class_for_send(class_name)) if (!check_class_for_send(class_name))
return false; return false;
if (class_list.indexOf(class_name.toLowerCase()) === -1) { if (!subscribed_to(class_name)) {
// You're not subbed to the class // You're not subbed to the class
$('#send-status').removeClass(status_classes).show(); $('#send-status').removeClass(status_classes).show();
$('#class-nosub-name').text(class_name); $('#class-nosub-name').text(class_name);

View File

@ -26,7 +26,8 @@ function sub_from_home(zephyr_class, prompt_button) {
data: {new_subscription: zephyr_class}, data: {new_subscription: zephyr_class},
dataType: 'json', dataType: 'json',
timeout: 10*60*1000, // 10 minutes in ms timeout: 10*60*1000, // 10 minutes in ms
success: function (data) { success: function (response) {
add_to_class_list(response.data);
$("#zephyr_compose form").ajaxSubmit(); $("#zephyr_compose form").ajaxSubmit();
prompt_button.stop(true).fadeOut(500); prompt_button.stop(true).fadeOut(500);
}, },
@ -36,6 +37,39 @@ function sub_from_home(zephyr_class, prompt_button) {
}); });
} }
class_list_hash = [];
function subscribed_to(class_name) {
return (class_list_hash[class_name.toLowerCase()] === true);
}
function case_insensitive_subscription_index(class_name) {
var i;
var name = class_name.toLowerCase();
for (i = 1; i < class_list.length; i++) {
if (name === class_list[i].toLowerCase()) {
return i;
}
}
return -1;
}
function add_to_class_list(class_name) {
if (!subscribed_to(class_name)) {
class_list.push(class_name);
class_list_hash[class_name.toLowerCase()] = true;
}
}
function remove_from_class_list(class_name) {
delete class_list_hash[class_name.toLowerCase()];
var removal_index = case_insensitive_subscription_index(class_name);
if (removal_index !== -1) {
class_list.splice(removal_index, 1);
}
}
// FIXME: It would be nice to move the UI setup into ui.js. // FIXME: It would be nice to move the UI setup into ui.js.
$(function () { $(function () {
$("#current_subscriptions").ajaxForm({ $("#current_subscriptions").ajaxForm({
@ -43,10 +77,7 @@ $(function () {
success: function (resp, statusText, xhr, form) { success: function (resp, statusText, xhr, form) {
var name = $.parseJSON(xhr.responseText).data; var name = $.parseJSON(xhr.responseText).data;
$('#subscriptions_table').find('button[value="' + name + '"]').parents('tr').remove(); $('#subscriptions_table').find('button[value="' + name + '"]').parents('tr').remove();
var removal_index = class_list.indexOf(name.toLowerCase()); remove_from_class_list(name);
if (removal_index !== -1) {
class_list.splice(removal_index, 1);
}
update_autocomplete(); update_autocomplete();
report_success("Successfully removed subscription to " + name, report_success("Successfully removed subscription to " + name,
$("#subscriptions-status")); $("#subscriptions-status"));
@ -62,7 +93,7 @@ $(function () {
$("#new_subscription").val(""); $("#new_subscription").val("");
var name = $.parseJSON(xhr.responseText).data; var name = $.parseJSON(xhr.responseText).data;
$('#subscriptions_table').prepend(templates.subscription({subscription: name})); $('#subscriptions_table').prepend(templates.subscription({subscription: name}));
class_list.push(name.toLowerCase()); add_to_class_list(name);
report_success("Successfully added subscription to " + name, report_success("Successfully added subscription to " + name,
$("#subscriptions-status")); $("#subscriptions-status"));
$("#new_subscription").focus(); $("#new_subscription").focus();

View File

@ -3,6 +3,7 @@ var zephyr_dict = {};
var instance_list = []; var instance_list = [];
$(function () { $(function () {
var i;
var send_status = $('#send-status'); var send_status = $('#send-status');
var buttons = $('#zephyr_compose').find('input[type="submit"]'); var buttons = $('#zephyr_compose').find('input[type="submit"]');
@ -36,6 +37,11 @@ $(function () {
send_status.hide(); send_status.hide();
$("#zephyr_compose form").ajaxForm(options); $("#zephyr_compose form").ajaxForm(options);
// Populate class_list_hash with data handed over to client-side template.
for (i = 0; i < class_list.length; i++) {
class_list_hash[class_list[i].toLowerCase()] = true;
}
}); });
var selected_zephyr_id = -1; /* to be filled in on document.ready */ var selected_zephyr_id = -1; /* to be filled in on document.ready */
@ -360,10 +366,6 @@ function add_zephyr_metadata(dummy, zephyr) {
switch (zephyr.type) { switch (zephyr.type) {
case 'class': case 'class':
zephyr.is_class = true; zephyr.is_class = true;
if ($.inArray(zephyr.display_recipient.toLowerCase(), class_list) === -1) {
class_list.push(zephyr.display_recipient.toLowerCase());
autocomplete_needs_update = true;
}
if ($.inArray(zephyr.instance, instance_list) === -1) { if ($.inArray(zephyr.instance, instance_list) === -1) {
instance_list.push(zephyr.instance); instance_list.push(zephyr.instance);
autocomplete_needs_update = true; autocomplete_needs_update = true;