diff --git a/static/js/bot_data.js b/static/js/bot_data.js index dd88c66808..99b5e53c50 100644 --- a/static/js/bot_data.js +++ b/static/js/bot_data.js @@ -4,25 +4,50 @@ var bot_data = (function () { var bots = {}; var bot_fields = ['api_key', 'avatar_url', 'default_all_public_streams', 'default_events_register_stream', - 'default_sending_stream', 'email', 'full_name']; + 'default_sending_stream', 'email', 'full_name', 'owner']; + + var send_change_event = _.debounce(function () { + $(document).trigger('zulip.bot_data_changed'); + }, 50); + + var set_can_admin = function bot_data__set_can_admin(bot) { + if (page_params.is_admin) { + bot.can_admin = true; + } else if (page_params.email === bot.owner) { + bot.can_admin = true; + } else { + bot.can_admin = false; + } + }; exports.add = function bot_data__add(bot) { - bots[bot.email] = _.pick(bot, bot_fields); + var clean_bot = _.pick(bot, bot_fields); + bots[bot.email] = clean_bot; + set_can_admin(clean_bot); + send_change_event(); }; exports.remove = function bot_data__remove(email) { delete bots[email]; + send_change_event(); }; exports.update = function bot_data__update(email, bot_update) { - _.extend(bots[email], _.pick(bot_update, bot_fields)); + var bot = bots[email]; + _.extend(bot, _.pick(bot_update, bot_fields)); + set_can_admin(bot); + send_change_event(); }; - exports.get_all = function bots_data__get_all() { - return bots; + exports.get_all = function bot_data__get_all() { + return _.values(bots); }; - exports.get = function bots_data__get(email) { + exports.get_editable = function bots_data__get_editable() { + return _.filter(bots, function (bot) { return bot.can_admin; }); + }; + + exports.get = function bot_data__get(email) { return bots[email]; }; diff --git a/zerver/tests/frontend/node/bot_data.js b/zerver/tests/frontend/node/bot_data.js index 7a9199a7ff..2791a59a7d 100644 --- a/zerver/tests/frontend/node/bot_data.js +++ b/zerver/tests/frontend/node/bot_data.js @@ -1,4 +1,20 @@ -set_global('$', function () {}); +var _ = global._; + +set_global('$', function () { + return {trigger: function () {}}; +}); +set_global('document', null); + +var page_params = { + is_admin: false, + email: 'owner@zulip.com' +}; +set_global('page_params', page_params); + +var patched_underscore = _.clone(_); +patched_underscore.debounce = function (f) { return(f); }; +global.patch_builtin('_', patched_underscore); + var bot_data = require('js/bot_data.js'); @@ -45,4 +61,41 @@ var bot_data = require('js/bot_data.js'); assert.equal(undefined, bot); }()); + (function test_owner_can_admin() { + var bot; + + bot_data.add(_.extend({owner: 'owner@zulip.com'}, test_bot)); + + bot = bot_data.get('bot1@zulip.com'); + assert(bot.can_admin); + + bot_data.add(_.extend({owner: 'notowner@zulip.com'}, test_bot)); + + bot = bot_data.get('bot1@zulip.com'); + assert.equal(false, bot.can_admin); + }()); + + (function test_admin_can_admin() { + var bot; + page_params.is_admin = true; + + bot_data.add(test_bot); + + bot = bot_data.get('bot1@zulip.com'); + assert(bot.can_admin); + + page_params.is_admin = false; + }()); + + (function test_get_editable() { + var can_admin; + + bot_data.add(_.extend({}, test_bot, {owner: 'owner@zulip.com'})); + bot_data.add(_.extend({}, test_bot, {email: 'bot2@zulip.com'})); + + can_admin = _.pluck(bot_data.get_editable(), 'email'); + assert.deepEqual(['bot1@zulip.com'], can_admin); + }()); + + }());