Have smarter PM autocomplete: sort based on who you Humbug the most.

(imported from commit 8b0223c97bc2cb8a1470ca482831ca615aaf10ec)
This commit is contained in:
Jessica McKellar 2012-11-19 12:36:53 -05:00
parent dbb0241568
commit cb50d4e7ef
3 changed files with 64 additions and 34 deletions

View File

@ -20,13 +20,33 @@ function render_pm_object(person) {
return person.full_name + " <" + person.email + ">";
}
exports.update_typeahead = function() {
private_message_mapped = {};
private_message_typeahead_list = [];
$.each(people_list, function (i, obj) {
var label = render_pm_object(obj);
private_message_mapped[label] = obj;
private_message_typeahead_list.push(label);
function add_to_known_recipients(recipient_data, count_towards_autocomplete_preference) {
var name_string = render_pm_object(recipient_data);
if (private_message_mapped[name_string] === undefined) {
private_message_mapped[name_string] = recipient_data;
private_message_mapped[name_string].count = 0;
private_message_typeahead_list.push(name_string);
}
if (count_towards_autocomplete_preference) {
private_message_mapped[name_string].count += 1;
}
}
exports.known_to_typeahead = function (recipient_data) {
return private_message_mapped[render_pm_object(recipient_data)] !== undefined;
};
exports.update_all_recipients = function (recipients) {
$.each(recipients, function (idx, recipient_data) {
add_to_known_recipients(recipient_data, false);
});
};
exports.update_your_recipients = function (recipients) {
$.each(recipients, function (idx, recipient_data) {
if (recipient_data.email !== email) {
add_to_known_recipients(recipient_data, true);
}
});
};
@ -147,9 +167,7 @@ exports.initialize = function () {
});
$( "#private_message_recipient" ).typeahead({
source: function (query, process) {
return private_message_typeahead_list;
},
source: private_message_typeahead_list,
items: 4,
highlighter: composebox_typeahead_highlighter,
matcher: function (item) {
@ -159,18 +177,33 @@ exports.initialize = function () {
if (! current_recipient.match(/\S/)) {
return false;
}
// Case-insensitive (from Bootstrap's default matcher).
// Case-insensitive.
return (item.toLowerCase().indexOf(current_recipient.toLowerCase()) !== -1);
},
sorter: function (matches) {
matches.sort(function (x, y) {
var x_count = private_message_mapped[x].count;
var y_count = private_message_mapped[y].count;
if (x_count > y_count) {
return -1;
} else if (x_count < y_count) {
return 1;
}
return 0;
});
return matches;
},
updater: function (item) {
var obj = private_message_mapped[item];
var previous_recipients = get_cleaned_pm_recipients(this.query);
previous_recipients.pop();
previous_recipients = previous_recipients.join(", ");
if (previous_recipients.length !== 0) {
previous_recipients += ", ";
}
return previous_recipients + obj.email + ", ";
return previous_recipients + private_message_mapped[item].email + ", ";
},
stopAdvance: true // Do not advance to the next field on a tab or enter
});

View File

@ -13,13 +13,6 @@ exports.autocomplete_needs_update = function (needs_update) {
exports.update_autocomplete = function () {
stream_list.sort();
people_list.sort(function (x, y) {
if (x.email === y.email) return 0;
if (x.email < y.email) return -1;
return 1;
});
composebox_typeahead.update_typeahead();
search.update_typeahead();
autocomplete_needs_update = false;
};

View File

@ -1,7 +1,6 @@
var message_array = [];
var message_dict = {};
var subject_dict = {};
var people_set = {};
var viewport = $(window);
@ -16,9 +15,7 @@ var get_updates_params = {
};
$(function () {
$.each(people_list, function (idx, person) {
people_set[person.email] = true;
});
composebox_typeahead.update_all_recipients(people_list);
});
// The "message groups", i.e. blocks of messages collapsed by recipient.
@ -422,23 +419,31 @@ function add_message_metadata(dummy, message) {
message.display_reply_to = get_huddle_recipient(message, 'full_name');
involved_people = message.display_recipient;
if (message.sender_email === email) {
composebox_typeahead.update_your_recipients(involved_people);
} else {
composebox_typeahead.update_all_recipients(involved_people);
}
break;
case 'personal':
message.is_personal = true;
if (message.sender_email === email) { // that is, we sent the original message
message.reply_to = message.display_recipient.email;
message.display_reply_to = message.display_recipient.full_name;
} else {
message.reply_to = message.sender_email;
message.display_reply_to = message.sender_full_name;
}
involved_people = [message.display_recipient,
{'email': message.sender_email,
'full_name': message.sender_full_name}];
if (message.sender_email === email) { // that is, we sent the original message
message.reply_to = message.display_recipient.email;
message.display_reply_to = message.display_recipient.full_name;
composebox_typeahead.update_your_recipients(involved_people);
} else {
message.reply_to = message.sender_email;
message.display_reply_to = message.sender_full_name;
composebox_typeahead.update_all_recipients(involved_people);
}
break;
}
@ -446,8 +451,7 @@ function add_message_metadata(dummy, message) {
$.each(involved_people, function (idx, person) {
// Do the hasOwnProperty() call via the prototype to avoid problems
// with keys like "hasOwnProperty"
if (! Object.prototype.hasOwnProperty.call(people_set, person.email)) {
people_set[person.email] = true;
if (! composebox_typeahead.known_to_typeahead(person)) {
people_list.push(person);
typeahead_helper.autocomplete_needs_update(true);
}