diff --git a/.eslintrc.json b/.eslintrc.json index 74fe76a93f..97437f1bea 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -90,6 +90,7 @@ "home_msg_list": false, "pm_list": false, "unread_ui": false, + "user_events": false, "Plotly": false }, "rules": { diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 5a60ae95c7..2ed9ea6826 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -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); diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js index 2cbfc7051d..cb652bf162 100644 --- a/frontend_tests/node_tests/people.js +++ b/frontend_tests/node_tests/people.js @@ -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'); diff --git a/frontend_tests/node_tests/user_events.js b/frontend_tests/node_tests/user_events.js new file mode 100644 index 0000000000..2555402d7d --- /dev/null +++ b/frontend_tests/node_tests/user_events.js @@ -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'); + +}()); + + + diff --git a/static/js/people.js b/static/js/people.js index d36cc3b4fa..73b80d9656 100644 --- a/static/js/people.js +++ b/static/js/people.js @@ -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; diff --git a/static/js/server_events.js b/static/js/server_events.js index a3b4b1016c..7b3b82e1f0 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -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; diff --git a/static/js/user_events.js b/static/js/user_events.js new file mode 100644 index 0000000000..6966f613ee --- /dev/null +++ b/static/js/user_events.js @@ -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; +} diff --git a/zproject/settings.py b/zproject/settings.py index 9d6bba01a3..90e6f6ab3d 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -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',