2019-11-02 00:06:25 +01:00
|
|
|
const Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let people_dict;
|
|
|
|
let people_by_name_dict;
|
|
|
|
let people_by_user_id_dict;
|
|
|
|
let active_user_dict;
|
|
|
|
let cross_realm_dict;
|
|
|
|
let pm_recipient_count_dict;
|
|
|
|
let duplicate_full_name_data;
|
|
|
|
let my_user_id;
|
2016-12-15 22:18:59 +01:00
|
|
|
|
|
|
|
// We have an init() function so that our automated tests
|
|
|
|
// can easily clear data.
|
|
|
|
exports.init = function () {
|
|
|
|
// The following three Dicts point to the same objects
|
2017-01-31 20:44:51 +01:00
|
|
|
// (all people we've seen), but people_dict can have duplicate
|
|
|
|
// keys related to email changes. We want to deprecate
|
|
|
|
// people_dict over time and always do lookups by user_id.
|
2016-12-15 22:18:59 +01:00
|
|
|
people_dict = new Dict({fold_case: true});
|
|
|
|
people_by_name_dict = new Dict({fold_case: true});
|
|
|
|
people_by_user_id_dict = new Dict();
|
2017-01-31 20:44:51 +01:00
|
|
|
|
2017-10-26 18:26:28 +02:00
|
|
|
// The next dictionary includes all active users (human/user)
|
|
|
|
// in our realm, but it excludes non-active users and
|
|
|
|
// cross-realm bots.
|
|
|
|
active_user_dict = new Dict();
|
2017-01-31 21:35:45 +01:00
|
|
|
cross_realm_dict = new Dict(); // keyed by user_id
|
2017-01-31 21:02:15 +01:00
|
|
|
pm_recipient_count_dict = new Dict();
|
2018-08-08 21:56:07 +02:00
|
|
|
|
|
|
|
// The next Dict maintains a set of ids of people with same full names.
|
|
|
|
duplicate_full_name_data = new Dict({fold_case: true});
|
2016-12-15 22:18:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// WE INITIALIZE DATA STRUCTURES HERE!
|
|
|
|
exports.init();
|
2014-01-30 22:42:19 +01:00
|
|
|
|
2016-10-31 15:56:57 +01:00
|
|
|
exports.get_person_from_user_id = function (user_id) {
|
2017-01-24 23:10:01 +01:00
|
|
|
if (!people_by_user_id_dict.has(user_id)) {
|
|
|
|
blueslip.error('Unknown user_id in get_person_from_user_id: ' + user_id);
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-01-24 23:10:01 +01:00
|
|
|
}
|
2016-10-31 15:56:57 +01:00
|
|
|
return people_by_user_id_dict.get(user_id);
|
|
|
|
};
|
|
|
|
|
2017-01-31 20:44:51 +01:00
|
|
|
exports.get_by_email = function (email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = people_dict.get(email);
|
2017-01-31 20:44:51 +01:00
|
|
|
|
|
|
|
if (!person) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-01-31 20:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (person.email.toLowerCase() !== email.toLowerCase()) {
|
|
|
|
blueslip.warn(
|
|
|
|
'Obsolete email passed to get_by_email: ' + email +
|
|
|
|
' new email = ' + person.email
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return person;
|
|
|
|
};
|
|
|
|
|
2017-03-30 18:47:38 +02:00
|
|
|
exports.get_realm_count = function () {
|
|
|
|
// This returns the number of active people in our realm. It should
|
|
|
|
// exclude bots and deactivated users.
|
2017-10-26 18:26:28 +02:00
|
|
|
return active_user_dict.num_items();
|
2017-03-30 18:47:38 +02:00
|
|
|
};
|
|
|
|
|
2017-02-07 17:59:11 +01:00
|
|
|
exports.id_matches_email_operand = function (user_id, email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_by_email(email);
|
2017-02-07 17:59:11 +01:00
|
|
|
|
|
|
|
if (!person) {
|
|
|
|
// The user may type bad data into the search bar, so
|
|
|
|
// we don't complain too loud here.
|
|
|
|
blueslip.debug('User email operand unknown: ' + email);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 18:19:09 +02:00
|
|
|
return person.user_id === user_id;
|
2017-02-07 17:59:11 +01:00
|
|
|
};
|
|
|
|
|
2017-01-31 20:44:51 +01:00
|
|
|
exports.update_email = function (user_id, new_email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = people_by_user_id_dict.get(user_id);
|
2017-01-31 20:44:51 +01:00
|
|
|
person.email = new_email;
|
|
|
|
people_dict.set(new_email, person);
|
|
|
|
|
|
|
|
// For legacy reasons we don't delete the old email
|
|
|
|
// keys in our dictionaries, so that reverse lookups
|
|
|
|
// still work correctly.
|
2014-01-30 22:42:19 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 15:22:24 +01:00
|
|
|
exports.get_user_id = function (email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_by_email(email);
|
2016-10-30 15:22:24 +01:00
|
|
|
if (person === undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const error_msg = 'Unknown email for get_user_id: ' + email;
|
2017-03-04 01:36:02 +01:00
|
|
|
blueslip.error(error_msg);
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2016-10-30 15:22:24 +01:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = person.user_id;
|
2016-10-30 15:22:24 +01:00
|
|
|
if (!user_id) {
|
2017-10-07 17:57:16 +02:00
|
|
|
blueslip.error('No user_id found for ' + email);
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2016-10-30 15:22:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return user_id;
|
|
|
|
};
|
|
|
|
|
2017-03-26 19:32:54 +02:00
|
|
|
exports.is_known_user_id = function (user_id) {
|
|
|
|
/*
|
|
|
|
For certain low-stakes operations, such as emoji reactions,
|
|
|
|
we may get a user_id that we don't know about, because the
|
|
|
|
user may have been deactivated. (We eventually want to track
|
|
|
|
deactivated users on the client, but until then, this is an
|
|
|
|
expedient thing we can check.)
|
|
|
|
*/
|
|
|
|
return people_by_user_id_dict.has(user_id);
|
|
|
|
};
|
|
|
|
|
2017-08-01 15:54:47 +02:00
|
|
|
function sort_numerically(user_ids) {
|
|
|
|
user_ids = _.map(user_ids, function (user_id) {
|
|
|
|
return parseInt(user_id, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
user_ids.sort(function (a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
|
|
|
|
return user_ids;
|
|
|
|
}
|
|
|
|
|
2017-02-05 00:22:16 +01:00
|
|
|
exports.huddle_string = function (message) {
|
|
|
|
if (message.type !== 'private') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = _.map(message.display_recipient, function (recip) {
|
2017-02-05 00:22:16 +01:00
|
|
|
return recip.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
function is_huddle_recip(user_id) {
|
|
|
|
return user_id &&
|
|
|
|
people_by_user_id_dict.has(user_id) &&
|
2018-06-06 18:19:09 +02:00
|
|
|
!exports.is_my_user_id(user_id);
|
2017-02-05 00:22:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
user_ids = _.filter(user_ids, is_huddle_recip);
|
|
|
|
|
|
|
|
if (user_ids.length <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-01 15:54:47 +02:00
|
|
|
|
|
|
|
user_ids = sort_numerically(user_ids);
|
2017-02-05 00:22:16 +01:00
|
|
|
|
|
|
|
return user_ids.join(',');
|
|
|
|
};
|
|
|
|
|
2016-11-15 22:55:37 +01:00
|
|
|
exports.user_ids_string_to_emails_string = function (user_ids_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = user_ids_string.split(',');
|
|
|
|
let emails = _.map(user_ids, function (user_id) {
|
|
|
|
const person = people_by_user_id_dict.get(user_id);
|
2016-11-15 22:55:37 +01:00
|
|
|
if (person) {
|
|
|
|
return person.email;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.all(emails)) {
|
2018-10-27 15:40:36 +02:00
|
|
|
blueslip.warn('Unknown user ids: ' + user_ids_string);
|
2016-11-15 22:55:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-19 00:33:32 +01:00
|
|
|
|
|
|
|
emails = _.map(emails, function (email) {
|
|
|
|
return email.toLowerCase();
|
|
|
|
});
|
|
|
|
|
|
|
|
emails.sort();
|
|
|
|
|
2016-11-15 22:55:37 +01:00
|
|
|
return emails.join(',');
|
|
|
|
};
|
|
|
|
|
2019-06-06 21:49:01 +02:00
|
|
|
exports.user_ids_string_to_ids_array = function (user_ids_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = user_ids_string.split(',');
|
|
|
|
const ids = _.map(user_ids, function (id) {
|
2019-06-06 21:49:01 +02:00
|
|
|
return Number(id);
|
|
|
|
});
|
|
|
|
return ids;
|
|
|
|
};
|
|
|
|
|
2019-07-11 18:54:28 +02:00
|
|
|
exports.emails_strings_to_user_ids_array = function (emails_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = exports.emails_strings_to_user_ids_string(emails_string);
|
2019-07-11 18:54:28 +02:00
|
|
|
if (user_ids_string === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_array = exports.user_ids_string_to_ids_array(user_ids_string);
|
2019-07-11 18:54:28 +02:00
|
|
|
return user_ids_array;
|
|
|
|
};
|
|
|
|
|
2017-02-25 00:30:51 +01:00
|
|
|
exports.reply_to_to_user_ids_string = function (emails_string) {
|
|
|
|
// This is basically emails_strings_to_user_ids_string
|
|
|
|
// without blueslip warnings, since it can be called with
|
|
|
|
// invalid data.
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = emails_string.split(',');
|
2017-02-25 00:30:51 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = _.map(emails, function (email) {
|
|
|
|
const person = exports.get_by_email(email);
|
2017-02-25 00:30:51 +01:00
|
|
|
if (person) {
|
|
|
|
return person.user_id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.all(user_ids)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:54:47 +02:00
|
|
|
user_ids = sort_numerically(user_ids);
|
2017-02-25 00:30:51 +01:00
|
|
|
|
|
|
|
return user_ids.join(',');
|
|
|
|
};
|
|
|
|
|
2017-06-02 05:46:09 +02:00
|
|
|
exports.get_user_time_preferences = function (user_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_timezone = exports.get_person_from_user_id(user_id).timezone;
|
2017-04-02 21:01:58 +02:00
|
|
|
if (user_timezone) {
|
|
|
|
if (page_params.twenty_four_hour_time) {
|
2017-06-02 05:46:09 +02:00
|
|
|
return {
|
|
|
|
timezone: user_timezone,
|
2018-10-07 22:52:49 +02:00
|
|
|
format: "H:mm",
|
2017-06-02 05:46:09 +02:00
|
|
|
};
|
2017-04-02 21:01:58 +02:00
|
|
|
}
|
2017-06-02 05:46:09 +02:00
|
|
|
return {
|
|
|
|
timezone: user_timezone,
|
2018-10-07 22:52:49 +02:00
|
|
|
format: "h:mm A",
|
2017-06-02 05:46:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_user_time = function (user_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_pref = exports.get_user_time_preferences(user_id);
|
2017-06-02 05:46:09 +02:00
|
|
|
if (user_pref) {
|
|
|
|
return moment().tz(user_pref.timezone).format(user_pref.format);
|
2017-04-02 21:01:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-02 20:08:24 +01:00
|
|
|
exports.get_user_type = function (user_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_profile = exports.get_person_from_user_id(user_id);
|
2018-11-02 20:08:24 +01:00
|
|
|
|
|
|
|
if (user_profile.is_admin) {
|
|
|
|
return i18n.t("Administrator");
|
|
|
|
} else if (user_profile.is_guest) {
|
|
|
|
return i18n.t("Guest");
|
|
|
|
} else if (user_profile.is_bot) {
|
|
|
|
return i18n.t("Bot");
|
|
|
|
}
|
|
|
|
return i18n.t("Member");
|
|
|
|
};
|
|
|
|
|
2016-11-15 22:55:37 +01:00
|
|
|
exports.emails_strings_to_user_ids_string = function (emails_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = emails_string.split(',');
|
2017-02-24 16:58:33 +01:00
|
|
|
return exports.email_list_to_user_ids_string(emails);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.email_list_to_user_ids_string = function (emails) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = _.map(emails, function (email) {
|
|
|
|
const person = exports.get_by_email(email);
|
2016-11-15 22:55:37 +01:00
|
|
|
if (person) {
|
|
|
|
return person.user_id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.all(user_ids)) {
|
2017-02-24 16:58:33 +01:00
|
|
|
blueslip.warn('Unknown emails: ' + emails);
|
2016-11-15 22:55:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-19 00:33:32 +01:00
|
|
|
|
2017-08-01 15:54:47 +02:00
|
|
|
user_ids = sort_numerically(user_ids);
|
2016-11-19 00:33:32 +01:00
|
|
|
|
2016-11-15 22:55:37 +01:00
|
|
|
return user_ids.join(',');
|
|
|
|
};
|
|
|
|
|
2018-02-23 16:16:55 +01:00
|
|
|
exports.safe_full_names = function (user_ids) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let names = _.map(user_ids, function (user_id) {
|
|
|
|
const person = people_by_user_id_dict.get(user_id);
|
2018-02-23 16:16:55 +01:00
|
|
|
if (person) {
|
|
|
|
return person.full_name;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
names = _.filter(names);
|
|
|
|
|
|
|
|
return names.join(', ');
|
|
|
|
};
|
|
|
|
|
2017-01-25 16:23:22 +01:00
|
|
|
exports.get_full_name = function (user_id) {
|
|
|
|
return people_by_user_id_dict.get(user_id).full_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_recipients = function (user_ids_string) {
|
|
|
|
// See message_store.get_pm_full_names() for a similar function.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = user_ids_string.split(',');
|
|
|
|
const other_ids = _.reject(user_ids, exports.is_my_user_id);
|
2017-01-25 16:23:22 +01:00
|
|
|
|
|
|
|
if (other_ids.length === 0) {
|
|
|
|
// private message with oneself
|
|
|
|
return exports.my_full_name();
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const names = _.map(other_ids, exports.get_full_name).sort();
|
2017-01-25 16:23:22 +01:00
|
|
|
return names.join(', ');
|
|
|
|
};
|
|
|
|
|
2017-02-14 01:15:26 +01:00
|
|
|
exports.pm_reply_user_string = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = exports.pm_with_user_ids(message);
|
2017-02-14 01:15:26 +01:00
|
|
|
|
|
|
|
if (!user_ids) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_ids.join(',');
|
|
|
|
};
|
|
|
|
|
2017-02-09 03:18:40 +01:00
|
|
|
exports.pm_reply_to = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = exports.pm_with_user_ids(message);
|
2017-02-09 03:18:40 +01:00
|
|
|
|
2017-02-14 00:46:51 +01:00
|
|
|
if (!user_ids) {
|
2017-02-09 03:18:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = _.map(user_ids, function (user_id) {
|
|
|
|
const person = people_by_user_id_dict.get(user_id);
|
2017-02-09 03:18:40 +01:00
|
|
|
if (!person) {
|
|
|
|
blueslip.error('Unknown user id in message: ' + user_id);
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
return person.email;
|
|
|
|
});
|
|
|
|
|
|
|
|
emails.sort();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const reply_to = emails.join(',');
|
2017-02-09 03:18:40 +01:00
|
|
|
|
|
|
|
return reply_to;
|
|
|
|
};
|
|
|
|
|
2017-08-01 15:35:07 +02:00
|
|
|
function sorted_other_user_ids(user_ids) {
|
|
|
|
// This excludes your own user id unless you're the only user
|
|
|
|
// (i.e. you sent a message to yourself).
|
2017-02-06 20:48:01 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const other_user_ids = _.filter(user_ids, function (user_id) {
|
2019-10-25 09:45:13 +02:00
|
|
|
return !exports.is_my_user_id(user_id);
|
2017-02-06 20:48:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (other_user_ids.length >= 1) {
|
|
|
|
user_ids = other_user_ids;
|
|
|
|
} else {
|
|
|
|
user_ids = [my_user_id];
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:54:47 +02:00
|
|
|
user_ids = sort_numerically(user_ids);
|
2017-02-06 20:48:01 +01:00
|
|
|
|
|
|
|
return user_ids;
|
2017-08-01 15:35:07 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:51:56 +02:00
|
|
|
exports.pm_lookup_key = function (user_ids_string) {
|
|
|
|
/*
|
|
|
|
The server will sometimes include our own user id
|
|
|
|
in keys for PMs, but we only want our user id if
|
|
|
|
we sent a message to ourself.
|
|
|
|
*/
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = user_ids_string.split(',');
|
2017-08-01 15:51:56 +02:00
|
|
|
user_ids = sorted_other_user_ids(user_ids);
|
|
|
|
return user_ids.join(',');
|
|
|
|
};
|
|
|
|
|
2018-10-18 22:05:28 +02:00
|
|
|
exports.all_user_ids_in_pm = function (message) {
|
|
|
|
if (message.type !== 'private') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.display_recipient.length === 0) {
|
|
|
|
blueslip.error('Empty recipient list in message');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = _.map(message.display_recipient, function (elem) {
|
2018-10-18 22:05:28 +02:00
|
|
|
return elem.user_id || elem.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
user_ids = sort_numerically(user_ids);
|
|
|
|
return user_ids;
|
|
|
|
};
|
|
|
|
|
2017-08-01 15:35:07 +02:00
|
|
|
exports.pm_with_user_ids = function (message) {
|
|
|
|
if (message.type !== 'private') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.display_recipient.length === 0) {
|
|
|
|
blueslip.error('Empty recipient list in message');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = _.map(message.display_recipient, function (elem) {
|
2017-08-01 15:35:07 +02:00
|
|
|
return elem.user_id || elem.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return sorted_other_user_ids(user_ids);
|
2017-02-06 20:48:01 +01:00
|
|
|
};
|
|
|
|
|
2017-06-04 01:51:53 +02:00
|
|
|
exports.group_pm_with_user_ids = function (message) {
|
|
|
|
if (message.type !== 'private') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.display_recipient.length === 0) {
|
|
|
|
blueslip.error('Empty recipient list in message');
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = _.map(message.display_recipient, function (elem) {
|
2017-06-04 01:51:53 +02:00
|
|
|
return elem.user_id || elem.id;
|
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
const is_user_present = _.some(user_ids, function (user_id) {
|
2019-10-25 09:45:13 +02:00
|
|
|
return exports.is_my_user_id(user_id);
|
2017-06-04 01:51:53 +02:00
|
|
|
});
|
|
|
|
if (is_user_present) {
|
|
|
|
user_ids.sort();
|
|
|
|
if (user_ids.length > 2) {
|
|
|
|
return user_ids;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-10-18 22:05:28 +02:00
|
|
|
exports.pm_perma_link = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = exports.all_user_ids_in_pm(message);
|
2018-10-18 22:05:28 +02:00
|
|
|
|
|
|
|
if (!user_ids) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let suffix;
|
2018-10-18 22:05:28 +02:00
|
|
|
|
|
|
|
if (user_ids.length >= 3) {
|
|
|
|
suffix = 'group';
|
|
|
|
} else {
|
|
|
|
suffix = 'pm';
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const slug = user_ids.join(',') + '-' + suffix;
|
|
|
|
const uri = "#narrow/pm-with/" + slug;
|
2018-10-18 22:05:28 +02:00
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
2017-02-06 20:48:01 +01:00
|
|
|
exports.pm_with_url = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = exports.pm_with_user_ids(message);
|
2017-02-06 20:48:01 +01:00
|
|
|
|
|
|
|
if (!user_ids) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let suffix;
|
2017-02-06 20:48:01 +01:00
|
|
|
|
|
|
|
if (user_ids.length > 1) {
|
|
|
|
suffix = 'group';
|
|
|
|
} else {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_person_from_user_id(user_ids[0]);
|
2017-02-06 20:48:01 +01:00
|
|
|
if (person && person.email) {
|
|
|
|
suffix = person.email.split('@')[0].toLowerCase();
|
|
|
|
} else {
|
|
|
|
blueslip.error('Unknown people in message');
|
|
|
|
suffix = 'unk';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const slug = user_ids.join(',') + '-' + suffix;
|
|
|
|
const uri = "#narrow/pm-with/" + slug;
|
2017-02-06 20:48:01 +01:00
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
2017-02-10 03:18:57 +01:00
|
|
|
exports.update_email_in_reply_to = function (reply_to, user_id, new_email) {
|
|
|
|
// We try to replace an old email with a new email in a reply_to,
|
|
|
|
// but we try to avoid changing the reply_to if we don't have to,
|
|
|
|
// and we don't warn on any errors.
|
2019-11-02 00:06:25 +01:00
|
|
|
let emails = reply_to.split(',');
|
2017-02-10 03:18:57 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const persons = _.map(emails, function (email) {
|
2017-02-10 03:18:57 +01:00
|
|
|
return people_dict.get(email.trim());
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.all(persons)) {
|
|
|
|
return reply_to;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const needs_patch = _.any(persons, function (person) {
|
2017-02-10 03:18:57 +01:00
|
|
|
return person.user_id === user_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!needs_patch) {
|
|
|
|
return reply_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
emails = _.map(persons, function (person) {
|
|
|
|
if (person.user_id === user_id) {
|
|
|
|
return new_email;
|
|
|
|
}
|
|
|
|
return person.email;
|
|
|
|
});
|
|
|
|
|
|
|
|
return emails.join(',');
|
|
|
|
};
|
2017-02-08 22:10:06 +01:00
|
|
|
|
|
|
|
exports.pm_with_operand_ids = function (operand) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let emails = operand.split(',');
|
2017-02-08 22:10:06 +01:00
|
|
|
emails = _.map(emails, function (email) { return email.trim(); });
|
2019-11-02 00:06:25 +01:00
|
|
|
let persons = _.map(emails, function (email) {
|
2017-02-08 22:10:06 +01:00
|
|
|
return people_dict.get(email);
|
|
|
|
});
|
|
|
|
|
2017-06-19 09:32:56 +02:00
|
|
|
// If your email is included in a PM group with other people, just ignore it
|
|
|
|
if (persons.length > 1) {
|
|
|
|
persons = _.without(persons, people_by_user_id_dict.get(my_user_id));
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:10:06 +01:00
|
|
|
if (!_.all(persons)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = _.map(persons, function (person) {
|
2017-02-08 22:10:06 +01:00
|
|
|
return person.user_id;
|
|
|
|
});
|
|
|
|
|
2017-08-01 15:54:47 +02:00
|
|
|
user_ids = sort_numerically(user_ids);
|
2017-02-08 22:10:06 +01:00
|
|
|
|
|
|
|
return user_ids;
|
|
|
|
};
|
|
|
|
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
exports.emails_to_slug = function (emails_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let slug = exports.reply_to_to_user_ids_string(emails_string);
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
|
|
|
if (!slug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
slug += '-';
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = emails_string.split(',');
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
|
|
|
|
if (emails.length === 1) {
|
|
|
|
slug += emails[0].split('@')[0].toLowerCase();
|
|
|
|
} else {
|
|
|
|
slug += 'group';
|
|
|
|
}
|
|
|
|
|
|
|
|
return slug;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.slug_to_emails = function (slug) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const m = /^([\d,]+)-/.exec(slug);
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
if (m) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids_string = m[1];
|
2018-10-18 19:41:44 +02:00
|
|
|
user_ids_string = exports.exclude_me_from_string(user_ids_string);
|
|
|
|
return exports.user_ids_string_to_emails_string(user_ids_string);
|
Make nicer slugs for "pm-with" narrows.
The slugs for PM-with narrows now have user ids in them, so they
are more resilient to email changes, and they have less escaping
characters and are generally prettier.
Examples:
narrow/pm-with/3-cordelia
narrow/pm-with/3,5-group
The part of the URL that is actionable is the comma-delimited
list of one or more userids.
When we decode the slugs, we only use the part before the dash; the
stuff after the dash is just for humans. If we don't see a number
before the dash, we fall back to the old decoding (which should only
matter during a transition period where folks may have old links).
For group PMS, we always say "group" after the dash. For single PMs,
we use the person's email userid, since it's usually fairly concise
and not noisy for a URL. We may tinker with this later.
Basically, the heart of this change is these two new methods:
people.emails_to_slug
people.slug_to_emails
And then we unify the encode codepath as follows:
narrow.pm_with_uri ->
hashchange.operators_to_hash ->
hashchange.encode_operand ->
people.emails_to_slug
The decode path didn't really require much modication in this commit,
other than to have hashchange.decode_operand call people.slug_to_emails
for the pm-with case.
2017-01-06 02:00:03 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-18 19:41:44 +02:00
|
|
|
exports.exclude_me_from_string = function (user_ids_string) {
|
|
|
|
// Exclude me from a user_ids_string UNLESS I'm the
|
|
|
|
// only one in it.
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_ids = user_ids_string.split(',');
|
2018-10-18 19:41:44 +02:00
|
|
|
|
|
|
|
if (user_ids.length <= 1) {
|
|
|
|
// We either have a message to ourself, an empty
|
|
|
|
// slug, or a message to somebody else where we weren't
|
|
|
|
// part of the slug.
|
|
|
|
return user_ids.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
user_ids = _.reject(user_ids, exports.is_my_user_id);
|
|
|
|
|
|
|
|
return user_ids.join(',');
|
|
|
|
};
|
|
|
|
|
2017-02-17 00:44:21 +01:00
|
|
|
exports.format_small_avatar_url = function (raw_url) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const url = raw_url + "&s=50";
|
2017-01-21 21:45:52 +01:00
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2017-01-11 16:45:06 +01:00
|
|
|
exports.sender_is_bot = function (message) {
|
|
|
|
if (message.sender_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_person_from_user_id(message.sender_id);
|
2017-01-11 16:45:06 +01:00
|
|
|
return person.is_bot;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-10-31 18:15:29 +01:00
|
|
|
exports.sender_is_guest = function (message) {
|
|
|
|
if (message.sender_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_person_from_user_id(message.sender_id);
|
2018-10-31 18:15:29 +01:00
|
|
|
return person.is_guest;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-04-05 21:30:15 +02:00
|
|
|
function gravatar_url_for_email(email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = md5(email.toLowerCase());
|
|
|
|
const avatar_url = 'https://secure.gravatar.com/avatar/' + hash + '?d=identicon';
|
|
|
|
const small_avatar_url = exports.format_small_avatar_url(avatar_url);
|
2018-04-05 21:30:15 +02:00
|
|
|
return small_avatar_url;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:36:32 +02:00
|
|
|
exports.small_avatar_url_for_person = function (person) {
|
|
|
|
if (person.avatar_url) {
|
|
|
|
return exports.format_small_avatar_url(person.avatar_url);
|
|
|
|
}
|
|
|
|
return gravatar_url_for_email(person.email);
|
|
|
|
};
|
|
|
|
|
2017-01-21 20:29:39 +01:00
|
|
|
exports.small_avatar_url = function (message) {
|
|
|
|
// Try to call this function in all places where we need 25px
|
|
|
|
// avatar images, so that the browser can help
|
|
|
|
// us avoid unnecessary network trips. (For user-uploaded avatars,
|
|
|
|
// the s=25 parameter is essentially ignored, but it's harmless.)
|
|
|
|
//
|
|
|
|
// We actually request these at s=50, so that we look better
|
|
|
|
// on retina displays.
|
2017-01-21 21:34:27 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let person;
|
2017-01-21 21:34:27 +01:00
|
|
|
if (message.sender_id) {
|
|
|
|
// We should always have message.sender_id, except for in the
|
|
|
|
// tutorial, where it's ok to fall back to the url in the fake
|
|
|
|
// messages.
|
|
|
|
person = exports.get_person_from_user_id(message.sender_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first time we encounter a sender in a message, we may
|
|
|
|
// not have person.avatar_url set, but if we do, then use that.
|
2018-04-05 21:36:32 +02:00
|
|
|
if (person && person.avatar_url) {
|
|
|
|
return exports.small_avatar_url_for_person(person);
|
2017-11-03 17:25:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get info from the message if we didn't have a `person` object
|
|
|
|
// or if the avatar was missing. We do this verbosely to avoid false
|
|
|
|
// positives on line coverage (we don't do branch checking).
|
2018-04-05 21:40:05 +02:00
|
|
|
if (message.avatar_url) {
|
2018-04-05 21:32:12 +02:00
|
|
|
return exports.format_small_avatar_url(message.avatar_url);
|
2017-01-21 21:34:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 21:39:24 +02:00
|
|
|
// For computing the user's email, we first trust the person
|
|
|
|
// object since that is updated via our real-time sync system, but
|
|
|
|
// if unavailable, we use the sender email.
|
2019-11-02 00:06:25 +01:00
|
|
|
let email;
|
2018-04-05 21:39:24 +02:00
|
|
|
if (person) {
|
|
|
|
email = person.email;
|
|
|
|
} else {
|
2017-11-03 17:25:47 +01:00
|
|
|
email = message.sender_email;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:40:05 +02:00
|
|
|
return gravatar_url_for_email(email);
|
2017-01-21 20:29:39 +01:00
|
|
|
};
|
|
|
|
|
2017-10-26 18:45:08 +02:00
|
|
|
exports.is_valid_email_for_compose = function (email) {
|
2019-10-25 09:45:13 +02:00
|
|
|
if (exports.is_cross_realm_email(email)) {
|
2017-10-26 18:45:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_by_email(email);
|
2017-10-26 18:45:08 +02:00
|
|
|
if (!person) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return active_user_dict.has(person.user_id);
|
|
|
|
};
|
|
|
|
|
2018-05-28 14:10:33 +02:00
|
|
|
exports.is_valid_bulk_emails_for_compose = function (emails) {
|
|
|
|
// Returns false if at least one of the emails is invalid.
|
|
|
|
return _.every(emails, function (email) {
|
2019-10-25 09:45:13 +02:00
|
|
|
if (!exports.is_valid_email_for_compose(email)) {
|
2018-05-28 14:10:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-26 18:52:42 +02:00
|
|
|
exports.get_active_user_for_email = function (email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_by_email(email);
|
2017-01-31 23:45:10 +01:00
|
|
|
if (!person) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-01-31 23:45:10 +01:00
|
|
|
}
|
2017-10-26 18:26:28 +02:00
|
|
|
return active_user_dict.get(person.user_id);
|
2014-01-30 22:42:19 +01:00
|
|
|
};
|
|
|
|
|
2017-10-26 21:00:30 +02:00
|
|
|
exports.is_active_user_for_popover = function (user_id) {
|
|
|
|
// For popover menus, we include cross-realm bots as active
|
|
|
|
// users.
|
|
|
|
|
|
|
|
if (cross_realm_dict.get(user_id)) {
|
2017-10-25 00:59:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-26 21:00:30 +02:00
|
|
|
if (active_user_dict.has(user_id)) {
|
|
|
|
return true;
|
2017-10-25 00:59:51 +02:00
|
|
|
}
|
2017-10-26 21:00:30 +02:00
|
|
|
|
|
|
|
// TODO: We can report errors here once we start loading
|
|
|
|
// deactivated users at page-load time. For now just warn.
|
|
|
|
if (!people_by_user_id_dict.has(user_id)) {
|
|
|
|
blueslip.warn("Unexpectedly invalid user_id in user popover query: " + user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-10-14 00:33:36 +02:00
|
|
|
};
|
|
|
|
|
2016-11-01 19:36:50 +01:00
|
|
|
exports.get_all_persons = function () {
|
2017-01-31 20:44:51 +01:00
|
|
|
return people_by_user_id_dict.values();
|
2016-11-01 19:36:50 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 19:45:53 +01:00
|
|
|
exports.get_realm_persons = function () {
|
2017-10-26 18:26:28 +02:00
|
|
|
return active_user_dict.values();
|
2016-11-01 19:45:53 +01:00
|
|
|
};
|
|
|
|
|
2018-07-28 20:13:20 +02:00
|
|
|
exports.get_active_human_persons = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const human_persons = exports.get_realm_persons().filter(function (person) {
|
2018-07-28 20:13:20 +02:00
|
|
|
return !person.is_bot;
|
|
|
|
});
|
|
|
|
return human_persons;
|
|
|
|
};
|
|
|
|
|
2017-10-26 18:17:43 +02:00
|
|
|
exports.get_active_user_ids = function () {
|
|
|
|
// This includes active users and active bots.
|
2017-10-26 18:26:28 +02:00
|
|
|
return active_user_dict.keys();
|
2017-10-07 20:10:10 +02:00
|
|
|
};
|
|
|
|
|
2016-11-02 23:48:47 +01:00
|
|
|
exports.is_cross_realm_email = function (email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_by_email(email);
|
2017-01-31 21:35:45 +01:00
|
|
|
if (!person) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-01-31 21:35:45 +01:00
|
|
|
}
|
|
|
|
return cross_realm_dict.has(person.user_id);
|
2016-11-02 23:48:47 +01:00
|
|
|
};
|
|
|
|
|
2016-11-03 21:59:18 +01:00
|
|
|
exports.get_recipient_count = function (person) {
|
|
|
|
// We can have fake person objects like the "all"
|
|
|
|
// pseudo-person in at-mentions. They will have
|
|
|
|
// the pm_recipient_count on the object itself.
|
|
|
|
if (person.pm_recipient_count) {
|
|
|
|
return person.pm_recipient_count;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = person.user_id || person.id;
|
|
|
|
const count = pm_recipient_count_dict.get(user_id);
|
2016-11-03 21:59:18 +01:00
|
|
|
|
|
|
|
return count || 0;
|
|
|
|
};
|
|
|
|
|
2017-01-31 21:02:15 +01:00
|
|
|
exports.incr_recipient_count = function (user_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const old_count = pm_recipient_count_dict.get(user_id) || 0;
|
2017-01-31 21:02:15 +01:00
|
|
|
pm_recipient_count_dict.set(user_id, old_count + 1);
|
2016-11-03 21:59:18 +01:00
|
|
|
};
|
|
|
|
|
2017-06-21 03:33:30 +02:00
|
|
|
// Diacritic removal from:
|
|
|
|
// https://stackoverflow.com/questions/18236208/perform-a-find-match-with-javascript-ignoring-special-language-characters-acce
|
2019-09-19 08:02:49 +02:00
|
|
|
const diacritic_regexes = {
|
|
|
|
letters_only: /^[a-z]+$/,
|
|
|
|
a: /[áàãâä]/g,
|
|
|
|
e: /[éèëê]/g,
|
|
|
|
i: /[íìïî]/g,
|
|
|
|
o: /[óòöôõ]/g,
|
|
|
|
u: /[úùüû]/g,
|
|
|
|
c: /[ç]/g,
|
|
|
|
n: /[ñ]/g,
|
|
|
|
};
|
|
|
|
|
2019-01-22 22:03:50 +01:00
|
|
|
exports.remove_diacritics = function (s) {
|
2019-09-19 08:02:49 +02:00
|
|
|
if (diacritic_regexes.letters_only.test(s)) {
|
2017-06-21 03:33:30 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-09-19 08:02:49 +02:00
|
|
|
return s.replace(diacritic_regexes.a, "a")
|
|
|
|
.replace(diacritic_regexes.e, "e")
|
|
|
|
.replace(diacritic_regexes.i, "i")
|
|
|
|
.replace(diacritic_regexes.o, "o")
|
|
|
|
.replace(diacritic_regexes.u, "u")
|
|
|
|
.replace(diacritic_regexes.c, "c")
|
|
|
|
.replace(diacritic_regexes.n, "n");
|
2019-01-22 22:03:50 +01:00
|
|
|
};
|
2017-06-21 03:33:30 +02:00
|
|
|
|
2017-06-23 08:33:48 +02:00
|
|
|
exports.person_matches_query = function (user, query) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const email = user.email.toLowerCase();
|
|
|
|
const names = user.full_name.toLowerCase().split(' ');
|
2016-10-18 19:21:38 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let termlets = query.toLowerCase().split(/\s+/);
|
2017-06-23 08:33:48 +02:00
|
|
|
termlets = _.map(termlets, function (termlet) {
|
|
|
|
return termlet.trim();
|
|
|
|
});
|
2016-11-14 22:34:46 +01:00
|
|
|
|
2017-06-23 08:33:48 +02:00
|
|
|
if (email.indexOf(query.trim()) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return _.all(termlets, function (termlet) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const is_ascii = /^[a-z]+$/.test(termlet);
|
2017-06-23 08:33:48 +02:00
|
|
|
return _.any(names, function (name) {
|
|
|
|
if (is_ascii) {
|
|
|
|
// Only ignore diacritics if the query is plain ascii
|
2019-01-22 22:03:50 +01:00
|
|
|
name = exports.remove_diacritics(name);
|
2017-06-23 08:33:48 +02:00
|
|
|
}
|
|
|
|
if (name.indexOf(termlet) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-14 22:22:02 +01:00
|
|
|
});
|
2017-06-23 08:33:48 +02:00
|
|
|
});
|
|
|
|
};
|
2016-11-14 22:22:02 +01:00
|
|
|
|
2017-06-23 08:33:48 +02:00
|
|
|
exports.filter_people_by_search_terms = function (users, search_terms) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const filtered_users = new Dict();
|
2018-04-06 20:43:34 +02:00
|
|
|
|
|
|
|
// Loop through users and populate filtered_users only
|
|
|
|
// if they include search_terms
|
|
|
|
_.each(users, function (user) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = exports.get_by_email(user.email);
|
2018-04-06 20:43:34 +02:00
|
|
|
// Get person object (and ignore errors)
|
|
|
|
if (!person || !person.full_name) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-14 21:37:36 +01:00
|
|
|
|
2018-04-06 20:43:34 +02:00
|
|
|
// Return user emails that include search terms
|
2019-11-02 00:06:25 +01:00
|
|
|
const match = _.any(search_terms, function (search_term) {
|
2018-04-06 20:43:34 +02:00
|
|
|
return exports.person_matches_query(user, search_term);
|
2016-10-18 19:21:38 +02:00
|
|
|
});
|
2018-04-06 20:43:34 +02:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
filtered_users.set(person.user_id, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return filtered_users;
|
2016-10-18 19:21:38 +02:00
|
|
|
};
|
|
|
|
|
2017-10-26 18:52:42 +02:00
|
|
|
exports.get_by_name = function (name) {
|
2014-01-30 22:42:19 +01:00
|
|
|
return people_by_name_dict.get(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
function people_cmp(person1, person2) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const name_cmp = util.strcmp(person1.full_name, person2.full_name);
|
2014-01-30 22:42:19 +01:00
|
|
|
if (name_cmp < 0) {
|
|
|
|
return -1;
|
|
|
|
} else if (name_cmp > 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return util.strcmp(person1.email, person2.email);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.get_rest_of_realm = function get_rest_of_realm() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const people_minus_you = [];
|
2017-10-26 18:26:28 +02:00
|
|
|
active_user_dict.each(function (person) {
|
2017-01-19 20:18:03 +01:00
|
|
|
if (!exports.is_current_user(person.email)) {
|
2016-12-03 03:08:47 +01:00
|
|
|
people_minus_you.push({email: person.email,
|
2017-01-07 07:58:08 +01:00
|
|
|
user_id: person.user_id,
|
2016-12-03 03:08:47 +01:00
|
|
|
full_name: person.full_name});
|
2014-01-30 22:42:19 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return people_minus_you.sort(people_cmp);
|
|
|
|
};
|
|
|
|
|
2018-08-08 21:56:07 +02:00
|
|
|
exports.track_duplicate_full_name = function (full_name, user_id, to_remove) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let ids = new Dict();
|
2018-08-08 21:56:07 +02:00
|
|
|
if (duplicate_full_name_data.has(full_name)) {
|
|
|
|
ids = duplicate_full_name_data.get(full_name);
|
|
|
|
}
|
|
|
|
if (!to_remove && user_id) {
|
|
|
|
ids.set(user_id);
|
|
|
|
}
|
|
|
|
if (to_remove && user_id && ids.has(user_id)) {
|
|
|
|
ids.del(user_id);
|
|
|
|
}
|
2018-12-07 21:21:39 +01:00
|
|
|
duplicate_full_name_data.set(full_name, ids);
|
2018-08-08 21:56:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_duplicate_full_name = function (full_name) {
|
|
|
|
if (duplicate_full_name_data.has(full_name)) {
|
|
|
|
return duplicate_full_name_data.get(full_name).keys().length > 1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-02-20 18:19:05 +01:00
|
|
|
exports.get_mention_syntax = function (full_name, user_id, silent) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let mention = '';
|
2019-02-20 18:19:05 +01:00
|
|
|
if (silent) {
|
|
|
|
mention += '@_**';
|
|
|
|
} else {
|
|
|
|
mention += '@**';
|
|
|
|
}
|
|
|
|
mention += full_name;
|
2018-10-13 02:25:38 +02:00
|
|
|
if (!user_id) {
|
|
|
|
blueslip.warn('get_mention_syntax called without user_id.');
|
|
|
|
}
|
2019-10-25 09:45:13 +02:00
|
|
|
if (exports.is_duplicate_full_name(full_name) && user_id) {
|
2018-10-13 02:25:38 +02:00
|
|
|
mention += '|' + user_id;
|
|
|
|
}
|
|
|
|
mention += '**';
|
|
|
|
return mention;
|
|
|
|
};
|
|
|
|
|
2014-02-01 15:17:17 +01:00
|
|
|
exports.add = function add(person) {
|
2016-10-31 15:56:57 +01:00
|
|
|
if (person.user_id) {
|
|
|
|
people_by_user_id_dict.set(person.user_id, person);
|
|
|
|
} else {
|
|
|
|
// We eventually want to lock this down completely
|
|
|
|
// and report an error and not update other the data
|
|
|
|
// structures here, but we have a lot of edge cases
|
|
|
|
// with cross-realm bots, zephyr users, etc., deactivated
|
|
|
|
// users, where we are probably fine for now not to
|
|
|
|
// find them via user_id lookups.
|
|
|
|
blueslip.warn('No user_id provided for ' + person.email);
|
|
|
|
}
|
|
|
|
|
2018-08-08 21:56:07 +02:00
|
|
|
exports.track_duplicate_full_name(person.full_name, person.user_id);
|
2014-01-30 22:42:19 +01:00
|
|
|
people_dict.set(person.email, person);
|
|
|
|
people_by_name_dict.set(person.full_name, person);
|
|
|
|
};
|
|
|
|
|
2017-01-31 23:45:10 +01:00
|
|
|
exports.add_in_realm = function (person) {
|
2017-10-26 18:26:28 +02:00
|
|
|
active_user_dict.set(person.user_id, person);
|
2014-01-30 22:42:19 +01:00
|
|
|
exports.add(person);
|
|
|
|
};
|
|
|
|
|
2016-12-15 22:44:42 +01:00
|
|
|
exports.deactivate = function (person) {
|
|
|
|
// We don't fully remove a person from all of our data
|
|
|
|
// structures, because deactivated users can be part
|
|
|
|
// of somebody's PM list.
|
2017-10-26 18:26:28 +02:00
|
|
|
active_user_dict.del(person.user_id);
|
2014-01-30 22:42:19 +01:00
|
|
|
};
|
|
|
|
|
2017-11-06 17:03:01 +01:00
|
|
|
exports.report_late_add = function (user_id, email) {
|
|
|
|
// This function is extracted to make unit testing easier,
|
|
|
|
// plus we may fine-tune our reporting here for different
|
|
|
|
// types of realms.
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg = 'Added user late: user_id=' + user_id + ' email=' + email;
|
2017-11-06 17:03:01 +01:00
|
|
|
|
2018-08-04 15:40:25 +02:00
|
|
|
if (reload_state.is_in_progress()) {
|
2018-04-23 21:13:21 +02:00
|
|
|
blueslip.log(msg);
|
|
|
|
} else {
|
|
|
|
blueslip.error(msg);
|
|
|
|
}
|
2017-11-06 17:03:01 +01:00
|
|
|
};
|
|
|
|
|
2016-12-15 23:33:36 +01:00
|
|
|
exports.extract_people_from_message = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let involved_people;
|
2016-12-15 23:33:36 +01:00
|
|
|
|
|
|
|
switch (message.type) {
|
|
|
|
case 'stream':
|
|
|
|
involved_people = [{full_name: message.sender_full_name,
|
|
|
|
user_id: message.sender_id,
|
|
|
|
email: message.sender_email}];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'private':
|
|
|
|
involved_people = message.display_recipient;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new people involved in this message to the people list
|
|
|
|
_.each(involved_people, function (person) {
|
2017-11-06 16:47:19 +01:00
|
|
|
if (person.unknown_local_echo_user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = person.user_id || person.id;
|
2017-11-06 16:47:19 +01:00
|
|
|
|
|
|
|
if (people_by_user_id_dict.has(user_id)) {
|
|
|
|
return;
|
2017-11-06 15:48:44 +01:00
|
|
|
}
|
2017-11-06 16:47:19 +01:00
|
|
|
|
2017-11-06 17:03:01 +01:00
|
|
|
exports.report_late_add(user_id, person.email);
|
|
|
|
|
2017-11-06 16:47:19 +01:00
|
|
|
exports.add({
|
|
|
|
email: person.email,
|
|
|
|
user_id: user_id,
|
|
|
|
full_name: person.full_name,
|
|
|
|
is_admin: person.is_realm_admin || false,
|
|
|
|
is_bot: person.is_bot || false,
|
|
|
|
});
|
2017-11-06 15:48:44 +01:00
|
|
|
});
|
|
|
|
};
|
2016-12-15 23:33:36 +01:00
|
|
|
|
2017-11-06 15:48:44 +01:00
|
|
|
exports.maybe_incr_recipient_count = function (message) {
|
|
|
|
if (message.type !== 'private') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.sent_by_me) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the number of PMs we've sent to this person to improve autocomplete
|
|
|
|
_.each(message.display_recipient, function (person) {
|
|
|
|
|
|
|
|
if (person.unknown_local_echo_user) {
|
|
|
|
return;
|
2016-12-15 23:33:36 +01:00
|
|
|
}
|
2017-11-06 15:48:44 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = person.user_id || person.id;
|
2017-11-06 15:48:44 +01:00
|
|
|
exports.incr_recipient_count(user_id);
|
2016-12-15 23:33:36 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-21 00:02:28 +01:00
|
|
|
exports.set_full_name = function (person_obj, new_full_name) {
|
|
|
|
if (people_by_name_dict.has(person_obj.full_name)) {
|
|
|
|
people_by_name_dict.del(person_obj.full_name);
|
|
|
|
}
|
2018-08-08 21:56:07 +02:00
|
|
|
// Remove previous and add new full name to the duplicate full name tracker.
|
|
|
|
exports.track_duplicate_full_name(person_obj.full_name, person_obj.user_id, true);
|
|
|
|
exports.track_duplicate_full_name(new_full_name, person_obj.user_id);
|
2017-01-21 00:02:28 +01:00
|
|
|
people_by_name_dict.set(new_full_name, person_obj);
|
|
|
|
person_obj.full_name = new_full_name;
|
|
|
|
};
|
|
|
|
|
2018-03-10 14:41:44 +01:00
|
|
|
exports.set_custom_profile_field_data = function (user_id, field) {
|
|
|
|
if (field.id === undefined) {
|
|
|
|
blueslip.error("Unknown field id " + field.id);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 06:41:06 +01:00
|
|
|
people_by_user_id_dict.get(user_id).profile_data[field.id] = {
|
|
|
|
value: field.value,
|
2018-12-31 07:45:33 +01:00
|
|
|
rendered_value: field.rendered_value,
|
2018-12-31 06:41:06 +01:00
|
|
|
};
|
2018-03-10 14:41:44 +01:00
|
|
|
};
|
|
|
|
|
2017-01-19 20:18:03 +01:00
|
|
|
exports.is_current_user = function (email) {
|
|
|
|
if (email === null || email === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-19 23:04:52 +01:00
|
|
|
|
|
|
|
return email.toLowerCase() === exports.my_current_email().toLowerCase();
|
2017-01-19 20:18:03 +01:00
|
|
|
};
|
|
|
|
|
2017-01-20 23:16:28 +01:00
|
|
|
exports.initialize_current_user = function (user_id) {
|
|
|
|
my_user_id = user_id;
|
2017-01-19 23:04:52 +01:00
|
|
|
};
|
|
|
|
|
2017-01-20 23:49:20 +01:00
|
|
|
exports.my_full_name = function () {
|
|
|
|
return people_by_user_id_dict.get(my_user_id).full_name;
|
|
|
|
};
|
|
|
|
|
2017-01-19 23:04:52 +01:00
|
|
|
exports.my_current_email = function () {
|
|
|
|
return people_by_user_id_dict.get(my_user_id).email;
|
|
|
|
};
|
2017-01-19 20:18:03 +01:00
|
|
|
|
2017-01-19 23:52:09 +01:00
|
|
|
exports.my_current_user_id = function () {
|
|
|
|
return my_user_id;
|
|
|
|
};
|
|
|
|
|
2018-02-26 20:09:07 +01:00
|
|
|
exports.my_custom_profile_data = function (field_id) {
|
|
|
|
if (field_id === undefined) {
|
|
|
|
blueslip.error("Undefined field id");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 20:41:35 +02:00
|
|
|
return exports.get_custom_profile_data(my_user_id, field_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_custom_profile_data = function (user_id, field_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const profile_data = people_by_user_id_dict.get(user_id).profile_data;
|
2019-04-07 17:29:53 +02:00
|
|
|
if (profile_data === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return profile_data[field_id];
|
2018-02-26 20:09:07 +01:00
|
|
|
};
|
|
|
|
|
2017-01-20 17:43:45 +01:00
|
|
|
exports.is_my_user_id = function (user_id) {
|
|
|
|
if (!user_id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return user_id.toString() === my_user_id.toString();
|
|
|
|
};
|
|
|
|
|
2017-05-24 04:15:52 +02:00
|
|
|
exports.initialize = function () {
|
2017-04-24 21:59:07 +02:00
|
|
|
_.each(page_params.realm_users, function (person) {
|
2016-11-01 21:01:42 +01:00
|
|
|
exports.add_in_realm(person);
|
2014-01-30 22:42:19 +01:00
|
|
|
});
|
|
|
|
|
2017-11-06 14:56:06 +01:00
|
|
|
_.each(page_params.realm_non_active_users, function (person) {
|
|
|
|
exports.add(person);
|
|
|
|
});
|
|
|
|
|
2016-11-02 23:48:47 +01:00
|
|
|
_.each(page_params.cross_realm_bots, function (person) {
|
|
|
|
if (!people_dict.has(person.email)) {
|
|
|
|
exports.add(person);
|
|
|
|
}
|
2017-01-31 21:35:45 +01:00
|
|
|
cross_realm_dict.set(person.user_id, person);
|
2016-11-02 23:48:47 +01:00
|
|
|
});
|
2016-11-01 20:55:18 +01:00
|
|
|
|
2017-01-20 23:16:28 +01:00
|
|
|
exports.initialize_current_user(page_params.user_id);
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2017-04-24 21:59:07 +02:00
|
|
|
delete page_params.realm_users; // We are the only consumer of this.
|
2017-11-06 14:56:06 +01:00
|
|
|
delete page_params.realm_non_active_users;
|
2016-11-02 23:48:47 +01:00
|
|
|
delete page_params.cross_realm_bots;
|
2017-05-24 04:15:52 +02:00
|
|
|
};
|
2014-01-30 22:42:19 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.people = exports;
|