mirror of
https://github.com/zulip/zulip.git
synced 2025-11-12 18:06:44 +00:00
Add admin status to bot_data
(imported from commit 47b84b3ef1e97e355dee84f0595e94a4612bf4df)
This commit is contained in:
@@ -4,25 +4,50 @@ var bot_data = (function () {
|
|||||||
var bots = {};
|
var bots = {};
|
||||||
var bot_fields = ['api_key', 'avatar_url', 'default_all_public_streams',
|
var bot_fields = ['api_key', 'avatar_url', 'default_all_public_streams',
|
||||||
'default_events_register_stream',
|
'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) {
|
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) {
|
exports.remove = function bot_data__remove(email) {
|
||||||
delete bots[email];
|
delete bots[email];
|
||||||
|
send_change_event();
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.update = function bot_data__update(email, bot_update) {
|
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() {
|
exports.get_all = function bot_data__get_all() {
|
||||||
return bots;
|
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];
|
return bots[email];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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');
|
var bot_data = require('js/bot_data.js');
|
||||||
|
|
||||||
@@ -45,4 +61,41 @@ var bot_data = require('js/bot_data.js');
|
|||||||
assert.equal(undefined, bot);
|
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);
|
||||||
|
}());
|
||||||
|
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user