Add and use util.is_current_user helper function.

Previously, we were checking if a particular user was the current user
in dozens of places in the codebase, and correct case-insensitive
checks were not used consistently, leading to bugs like #502.
This commit is contained in:
Tim Abbott 2016-06-07 20:54:07 -07:00
parent 0bf2d171ae
commit b25562ca1d
13 changed files with 31 additions and 19 deletions

View File

@ -1,3 +1,7 @@
add_dependencies({
util: 'js/util.js'
});
set_global('page_params', {
alert_words: ['alertone', 'alerttwo', 'alertthree', 'al*rt.*s', '.+'],
email: 'tester@zulip.com'

View File

@ -1,3 +1,7 @@
add_dependencies({
util: 'js/util.js'
});
var _ = global._;
set_global('$', function (f) {

View File

@ -5,7 +5,8 @@ add_dependencies({
var people = require("js/people.js");
set_global('page_params', {
people_list: []
people_list: [],
email: 'hamlet@example.com'
});
set_global('activity', {
set_user_statuses: function () {}

View File

@ -371,7 +371,7 @@ function focus_ping() {
// Ping returns the active peer list
_.each(data.presences, function (presence, this_email) {
if (page_params.email !== this_email) {
if (!util.is_current_user(this_email)) {
exports.presence_info[this_email] = status_from_timestamp(data.server_timestamp, presence);
}
});
@ -413,7 +413,7 @@ exports.set_user_statuses = function (users, server_time) {
var updated_users = {};
var status;
_.each(users, function (presence, email) {
if (email === page_params.email) {
if (util.is_current_user(email)) {
return;
}
status = status_from_timestamp(server_time, presence);

View File

@ -42,7 +42,7 @@ exports.process_message = function (message) {
};
exports.notifies = function (message) {
return ((message.sender_email !== page_params.email) && message.alerted);
return !util.is_current_user(message.sender_email) && message.alerted;
};
return exports;

View File

@ -13,7 +13,7 @@ var bot_data = (function () {
var set_can_admin = function bot_data__set_can_admin(bot) {
if (page_params.is_admin) {
bot.can_admin = true;
} else if (page_params.email === bot.owner) {
} else if (bot.owner !== undefined && util.is_current_user(bot.owner)) {
bot.can_admin = true;
} else {
bot.can_admin = false;

View File

@ -103,7 +103,7 @@ exports.would_receive_message = function (email) {
// helpful if we want to emphasize the '.unfaded' class later (applied
// to users who will definitely receive the message).
if (email.toLowerCase() === page_params.email.toLowerCase()) {
if (util.is_current_user(email)) {
// We never want to fade you yourself, so pretend it's true even if
// it's not.
return true;

View File

@ -332,7 +332,7 @@ MessageListView.prototype = {
if (row.hasClass('mention')) {
row.find('.user-mention').each(function () {
var email = $(this).attr('data-user-email');
if (email === '*' || email === page_params.email) {
if (email === '*' || util.is_current_user(email)) {
$(this).addClass('user-mention-me');
}
});
@ -558,8 +558,7 @@ MessageListView.prototype = {
var row_id = rows.id(elem);
// check for `row_id` NaN in case we're looking at a date row or bookend row
if (row_id > -1 &&
this.get_message(row_id).sender_email === page_params.email)
{
util.is_current_user(this.get_message(row_id).sender_email)) {
distance_to_last_message_sent_by_me += elem.height();
id_of_last_message_sent_by_us = rows.id(elem);
}

View File

@ -20,7 +20,7 @@ exports.get_private_message_recipient = function (message, attr, fallback_attr)
var recipient, i;
var other_recipients = _.filter(message.display_recipient,
function (element) {
return element.email !== page_params.email;
return !util.is_current_user(element.email);
});
if (other_recipients.length === 0) {
// private message with oneself
@ -131,7 +131,7 @@ function add_message_metadata(message) {
var involved_people;
message.sent_by_me = (message.sender_email === page_params.email);
message.sent_by_me = util.is_current_user(message.sender_email);
message.flags = message.flags || [];
message.historical = (message.flags !== undefined &&

View File

@ -488,7 +488,7 @@ function get_message_header(message) {
if (message.display_recipient.length > 2) {
return "group PM with " + message.display_reply_to;
}
if (message.reply_to === page_params.email) {
if (util.is_current_user(message.reply_to)) {
return "PM with yourself";
}
return "PM with " + message.display_reply_to;
@ -496,7 +496,7 @@ function get_message_header(message) {
exports.possibly_notify_new_messages_outside_viewport = function (messages) {
_.each(messages, function (message) {
if (message.sender_email !== page_params.email) {
if (!util.is_current_user(message.sender_email)) {
return;
}
// queue up offscreen because of narrowed, or (secondarily) offscreen
@ -533,7 +533,7 @@ exports.possibly_notify_new_messages_outside_viewport = function (messages) {
// the current_msg_list (!can_apply_locally; a.k.a. "a search").
exports.notify_messages_outside_current_search = function (messages) {
_.each(messages, function (message) {
if (message.sender_email !== page_params.email) {
if (!util.is_current_user(message.sender_email)) {
return;
}
exports.notify_above_composebox("Sent! Your recent message is outside the current search.",

View File

@ -44,7 +44,7 @@ function people_cmp(person1, person2) {
exports.get_rest_of_realm = function get_rest_of_realm() {
var people_minus_you = [];
realm_people_dict.each(function (person) {
if (person.email !== page_params.email) {
if (!util.is_current_user(person.email)) {
people_minus_you.push({"email": person.email,
"full_name": person.full_name});
}
@ -129,7 +129,7 @@ exports.update = function update(person) {
person_obj.full_name = person.full_name;
if (person.email === page_params.email) {
if (util.is_current_user(person.email)) {
page_params.fullname = person.full_name;
}
}
@ -137,7 +137,7 @@ exports.update = function update(person) {
if (_.has(person, 'is_admin')) {
person_obj.is_admin = person.is_admin;
if (person.email === page_params.email) {
if (util.is_current_user(person.email)) {
page_params.is_admin = person.is_admin;
admin.show_or_hide_menu_item();
}

View File

@ -887,7 +887,7 @@ $(function () {
if (data.subscribed.hasOwnProperty(principal)) {
error_elem.addClass("hide");
warning_elem.addClass("hide");
if (principal === page_params.email) {
if (util.is_current_user(principal)) {
// mark_subscribed adds the user to the member list
exports.mark_subscribed(stream);
} else {
@ -925,7 +925,7 @@ $(function () {
// Remove the user from the subscriber list.
list_entry.remove();
if (principal === page_params.email) {
if (util.is_current_user(principal)) {
// If you're unsubscribing yourself, mark whole
// stream entry as you being unsubscribed.
exports.mark_unsubscribed(stream_name);

View File

@ -61,6 +61,10 @@ exports.same_stream_and_subject = function util_same_stream_and_subject(a, b) {
(a.subject.toLowerCase() === b.subject.toLowerCase()));
};
exports.is_current_user = function (email) {
return email.toLowerCase() === page_params.email.toLowerCase();
};
exports.same_major_recipient = function (a, b) {
// Same behavior as same_recipient, except that it returns true for messages
// on different topics but the same stream.