From d8d703af452fb0aae1662001bfc55685c3ff4bf8 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 19 Dec 2018 16:11:17 +0000 Subject: [PATCH] frontend: Add basic user_status module. So far this processes page_params, but it's otherwise an unused internal API. --- .eslintrc.json | 1 + frontend_tests/node_tests/user_status.js | 39 +++++++++++++++++++ static/js/bundles/app.js | 1 + static/js/ui_init.js | 1 + static/js/user_status.js | 49 ++++++++++++++++++++++++ tools/test-js-with-node | 1 + 6 files changed, 92 insertions(+) create mode 100644 frontend_tests/node_tests/user_status.js create mode 100644 static/js/user_status.js diff --git a/.eslintrc.json b/.eslintrc.json index 6f9f13e849..b4bfd71412 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -197,6 +197,7 @@ "user_groups": false, "user_pill": false, "user_search": false, + "user_status": false, "util": false, "poll_widget": false, "widgetize": false, diff --git a/frontend_tests/node_tests/user_status.js b/frontend_tests/node_tests/user_status.js new file mode 100644 index 0000000000..33a2f989ff --- /dev/null +++ b/frontend_tests/node_tests/user_status.js @@ -0,0 +1,39 @@ +set_global('channel', {}); +set_global('page_params', {}); +zrequire('user_status'); + +function initialize() { + page_params.away_user_ids = [1, 2, 3]; + user_status.initialize(); +} + +run_test('basics', () => { + initialize(); + assert(user_status.is_away(2)); + assert(!user_status.is_away(99)); + + assert(!user_status.is_away(4)); + user_status.set_away(4); + assert(user_status.is_away(4)); + user_status.revoke_away(4); + assert(!user_status.is_away(4)); +}); + +run_test('server', () => { + initialize(); + + var away_arg; + + channel.post = (opts) => { + away_arg = opts.data.away; + assert.equal(opts.url, '/json/users/me/status'); + }; + + assert.equal(away_arg, undefined); + + user_status.server_set_away(); + assert.equal(away_arg, true); + + user_status.server_revoke_away(); + assert.equal(away_arg, false); +}); diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 5bce15910f..07dc082404 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -152,6 +152,7 @@ import "js/server_events_dispatch.js"; import "js/zulip.js"; import "js/presence.js"; import "js/user_search.js"; +import "js/user_status.js"; import "js/buddy_data.js"; import "js/padded_widget.js"; import "js/buddy_list.js"; diff --git a/static/js/ui_init.js b/static/js/ui_init.js index bd941624f9..4ea97379b7 100644 --- a/static/js/ui_init.js +++ b/static/js/ui_init.js @@ -297,6 +297,7 @@ $(function () { } server_events.initialize(); people.initialize(); + user_status.initialize(); compose_pm_pill.initialize(); search_pill_widget.initialize(); reload.initialize(); diff --git a/static/js/user_status.js b/static/js/user_status.js new file mode 100644 index 0000000000..54e701e43e --- /dev/null +++ b/static/js/user_status.js @@ -0,0 +1,49 @@ +var user_status = (function () { + +var exports = {}; + +var away_user_ids = new Dict(); + +exports.server_set_away = function () { + channel.post({ + url: '/json/users/me/status', + data: {away: true}, + idempotent: true, + }); +}; + +exports.server_revoke_away = function () { + channel.post({ + url: '/json/users/me/status', + data: {away: false}, + idempotent: true, + }); +}; + +exports.set_away = function (user_id) { + away_user_ids.set(user_id, true); +}; + +exports.revoke_away = function (user_id) { + away_user_ids.del(user_id); +}; + +exports.is_away = function (user_id) { + return away_user_ids.has(user_id); +}; + +exports.initialize = function () { + _.each(page_params.away_user_ids, function (user_id) { + away_user_ids.set(user_id, true); + }); + + delete page_params.away_user_ids; +}; + +return exports; + +}()); +if (typeof module !== 'undefined') { + module.exports = user_status; +} +window.user_status = user_status; diff --git a/tools/test-js-with-node b/tools/test-js-with-node index f68f71e7f9..1f2cb85620 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -83,6 +83,7 @@ enforce_fully_covered = { 'static/js/user_groups.js', 'static/js/user_pill.js', 'static/js/user_search.js', + 'static/js/user_status.js', 'static/js/util.js', 'static/js/widgetize.js', 'static/js/poll_widget.js',