refactor: Extract people.is_valid_email_for_compose().

It's easier to unit test logic inside of people.js than compose.js.

We allow users to compose emails to any of our cross-realm bots.
Someday we may tighten up which cross-realm bots are valid targets,
since it's not necessarily the case that those bots do anything
useful when you send them messages.
This commit is contained in:
Steve Howell 2017-10-26 09:45:08 -07:00
parent 8cf5e05827
commit 2699a6aac9
3 changed files with 17 additions and 10 deletions

View File

@ -67,6 +67,7 @@ initialize();
var active_user_ids = people.get_active_user_ids();
assert.deepEqual(active_user_ids, [isaac.user_id]);
assert.equal(people.realm_user_is_active_human_or_bot(isaac.user_id), true);
assert(people.is_valid_email_for_compose(isaac.email));
// Now deactivate isaac
people.deactivate(isaac);
@ -74,6 +75,7 @@ initialize();
assert(!person);
assert.equal(people.get_realm_count(), 0);
assert.equal(people.realm_user_is_active_human_or_bot(isaac.user_id), false);
assert.equal(people.is_valid_email_for_compose(isaac.email), false);
var bot_botson = {
email: 'botson-bot@example.com',
@ -654,6 +656,8 @@ initialize();
assert.equal(people.realm_get('alice@example.com').full_name, 'Alice');
assert(people.is_cross_realm_email('bot@example.com'));
assert(people.is_valid_email_for_compose('bot@example.com'));
assert(people.is_valid_email_for_compose('alice@example.com'));
assert(people.is_my_user_id(42));
assert.equal(global.page_params.realm_users, undefined);

View File

@ -333,16 +333,7 @@ exports.update_email = function (user_id, new_email) {
exports.get_invalid_recipient_emails = function () {
var private_recipients = util.extract_pm_recipients(compose_state.recipient());
var invalid_recipients = [];
_.each(private_recipients, function (email) {
if (people.realm_get(email) !== undefined) {
return;
}
if (people.is_cross_realm_email(email)) {
return;
}
invalid_recipients.push(email);
});
var invalid_recipients = _.reject(private_recipients, people.is_valid_email_for_compose);
return invalid_recipients;
};

View File

@ -525,6 +525,18 @@ exports.small_avatar_url = function (message) {
return url;
};
exports.is_valid_email_for_compose = function (email) {
if (people.is_cross_realm_email(email)) {
return true;
}
var person = people.get_by_email(email);
if (!person) {
return false;
}
return active_user_dict.has(person.user_id);
};
exports.realm_get = function realm_get(email) {
var person = people.get_by_email(email);
if (!person) {