Extract user_events.js.

This moves people.update() to user_events.person().

We now also use user_id as the key for finding person
objects in people.js (instead of email).
This commit is contained in:
Steve Howell 2017-01-21 07:49:27 -08:00 committed by Tim Abbott
parent 601bad554e
commit 4eb1a8f07d
8 changed files with 128 additions and 83 deletions

View File

@ -90,6 +90,7 @@
"home_msg_list": false,
"pm_list": false,
"unread_ui": false,
"user_events": false,
"Plotly": false
},
"rules": {

View File

@ -588,7 +588,7 @@ run(function (override, capture, args) {
assert_same(args.person, event.person);
event = event_fixtures.realm_user__update;
override('people', 'update', capture(['person']));
override('user_events', 'update_person', capture(['person']));
dispatch(event);
assert_same(args.person, event.person);

View File

@ -6,17 +6,6 @@ global.stub_out_jquery();
var people = require("js/people.js");
set_global('page_params', {
people_list: [],
});
set_global('activity', {
redraw: function () {},
});
set_global('admin', {
update_user_full_name: function () {},
show_or_hide_menu_item: function () {},
});
var _ = global._;
var me = {
@ -42,7 +31,6 @@ initialize();
var realm_persons = people.get_realm_persons();
assert.equal(_.size(realm_persons), 0);
var full_name = 'Isaac Newton';
var email = 'isaac@example.com';
var isaac = {
@ -67,16 +55,6 @@ initialize();
assert.equal(_.size(realm_persons), 1);
assert.equal(realm_persons[0].full_name, 'Isaac Newton');
people.update({email: email, is_admin: true});
person = people.get_by_email(email);
assert.equal(person.full_name, full_name);
assert.equal(person.is_admin, true);
people.update({email: email, full_name: 'Sir Isaac'});
person = people.get_by_email(email);
assert.equal(person.full_name, 'Sir Isaac');
assert.equal(person.is_admin, true);
// Now deactivate isaac
people.deactivate(isaac);
person = people.realm_get(email);
@ -92,12 +70,6 @@ initialize();
}());
(function test_updates() {
people.update({email: me.email, is_admin: false});
assert(!global.page_params.is_admin);
people.update({email: me.email, full_name: 'Me V2'});
assert.equal(people.my_full_name(), 'Me V2');
var person = people.get_by_email('me@example.com');
people.set_full_name(person, 'Me the Third');
assert.equal(people.my_full_name(), 'Me the Third');
@ -105,7 +77,6 @@ initialize();
assert.equal(people.get_by_name('Me the Third').email, 'me@example.com');
}());
(function test_get_person_from_user_id() {
var person = {
email: 'mary@example.com',
@ -118,17 +89,7 @@ initialize();
person = people.get_person_from_user_id(42);
assert.equal(person.email, 'mary@example.com');
// The semantics for update() are going to eventually
// change to use user_id as a key, but now we use email
// as a key and change attributes. With the current
// behavior, we don't have to make update() do anything
// new.
person = {
email: 'mary@example.com',
user_id: 42,
full_name: 'Mary New',
};
people.update(person);
people.set_full_name(person, 'Mary New');
person = people.get_person_from_user_id(42);
assert.equal(person.full_name, 'Mary New');

View File

@ -0,0 +1,66 @@
global.stub_out_jquery();
add_dependencies({
people: 'js/people.js',
});
var people = global.people;
var user_events = require("js/user_events.js");
set_global('activity', {
redraw: function () {},
});
set_global('admin', {
update_user_full_name: function () {},
show_or_hide_menu_item: function () {},
});
set_global('page_params', {
is_admin: true,
});
var me = {
email: 'me@example.com',
user_id: 30,
full_name: 'Me Myself',
is_admin: true,
};
function initialize() {
people.init();
people.add(me);
people.initialize_current_user(me.user_id);
}
initialize();
(function test_updates() {
var person;
var isaac = {
email: 'isaac@example.com',
user_id: 32,
full_name: 'Isaac Newton',
};
people.add(isaac);
user_events.update_person({user_id: isaac.user_id, is_admin: true});
person = people.get_by_email(isaac.email);
assert.equal(person.full_name, 'Isaac Newton');
assert.equal(person.is_admin, true);
user_events.update_person({user_id: isaac.user_id, full_name: 'Sir Isaac'});
person = people.get_by_email(isaac.email);
assert.equal(person.full_name, 'Sir Isaac');
assert.equal(person.is_admin, true);
user_events.update_person({user_id: me.user_id, is_admin: false});
assert(!global.page_params.is_admin);
user_events.update_person({user_id: me.user_id, full_name: 'Me V2'});
assert.equal(people.my_full_name(), 'Me V2');
}());

View File

@ -307,47 +307,6 @@ exports.set_full_name = function (person_obj, new_full_name) {
person_obj.full_name = new_full_name;
};
exports.update = function update(person) {
if (! people_dict.has(person.email)) {
blueslip.error("Got update_person event for unexpected user",
{email: person.email});
return;
}
var person_obj = people_dict.get(person.email);
if (_.has(person, 'full_name')) {
exports.set_full_name(person_obj, person.full_name);
admin.update_user_full_name(person.email, person.full_name);
activity.redraw();
// TODO: update sender names on messages
}
if (_.has(person, 'is_admin')) {
person_obj.is_admin = person.is_admin;
if (exports.is_current_user(person.email)) {
page_params.is_admin = person.is_admin;
admin.show_or_hide_menu_item();
}
}
if (_.has(person, 'avatar_url')) {
var url = person.avatar_url + "&y=" + new Date().getTime();
person_obj.avatar_url = url;
if (exports.is_current_user(person.email)) {
page_params.avatar_url = url;
$("#user-settings-avatar").attr("src", url);
}
$(".inline_profile_picture.u-" + person.id).css({
"background-image": "url(" + url + ")",
});
}
};
exports.is_current_user = function (email) {
if (email === null || email === undefined) {
return false;

View File

@ -127,7 +127,7 @@ function dispatch_normal_event(event) {
} else if (event.op === 'remove') {
people.deactivate(event.person);
} else if (event.op === 'update') {
people.update(event.person);
user_events.update_person(event.person);
}
break;

57
static/js/user_events.js Normal file
View File

@ -0,0 +1,57 @@
var user_events = (function () {
var exports = {};
// This module is kind of small, but it will help us keep
// server_events.js simple while breaking some circular
// dependencies that existed when this code was in people.js.
// (We should do bot updates here too.)
exports.update_person = function update(person) {
var person_obj = people.get_person_from_user_id(person.user_id);
if (!person_obj) {
blueslip.error("Got update_person event for unexpected user",
{email: person.user_id});
return;
}
if (_.has(person, 'full_name')) {
people.set_full_name(person_obj, person.full_name);
admin.update_user_full_name(person.email, person.full_name);
activity.redraw();
// TODO: update sender names on messages
}
if (_.has(person, 'is_admin')) {
person_obj.is_admin = person.is_admin;
if (people.is_my_user_id(person.user_id)) {
page_params.is_admin = person.is_admin;
admin.show_or_hide_menu_item();
}
}
if (_.has(person, 'avatar_url')) {
var url = person.avatar_url + "&y=" + new Date().getTime();
person_obj.avatar_url = url;
if (people.is_my_user_id(person.user_id)) {
page_params.avatar_url = url;
$("#user-settings-avatar").attr("src", url);
}
$(".inline_profile_picture.u-" + person.id).css({
"background-image": "url(" + url + ")",
});
}
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = user_events;
}

View File

@ -834,6 +834,7 @@ JS_SPECS = {
'js/server_events.js',
'js/zulip.js',
'js/activity.js',
'js/user_events.js',
'js/colorspace.js',
'js/timerender.js',
'js/tutorial.js',