2014-02-27 00:01:18 +01:00
|
|
|
var bot_data = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var bots = {};
|
|
|
|
var bot_fields = ['api_key', 'avatar_url', 'default_all_public_streams',
|
|
|
|
'default_events_register_stream',
|
2014-02-27 18:27:31 +01:00
|
|
|
'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;
|
2016-06-08 05:54:07 +02:00
|
|
|
} else if (bot.owner !== undefined && util.is_current_user(bot.owner)) {
|
2014-02-27 18:27:31 +01:00
|
|
|
bot.can_admin = true;
|
|
|
|
} else {
|
|
|
|
bot.can_admin = false;
|
|
|
|
}
|
|
|
|
};
|
2014-02-27 00:01:18 +01:00
|
|
|
|
|
|
|
exports.add = function bot_data__add(bot) {
|
2014-02-27 18:27:31 +01:00
|
|
|
var clean_bot = _.pick(bot, bot_fields);
|
|
|
|
bots[bot.email] = clean_bot;
|
|
|
|
set_can_admin(clean_bot);
|
|
|
|
send_change_event();
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.remove = function bot_data__remove(email) {
|
|
|
|
delete bots[email];
|
2014-02-27 18:27:31 +01:00
|
|
|
send_change_event();
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.update = function bot_data__update(email, bot_update) {
|
2014-02-27 18:27:31 +01:00
|
|
|
var bot = bots[email];
|
|
|
|
_.extend(bot, _.pick(bot_update, bot_fields));
|
|
|
|
set_can_admin(bot);
|
|
|
|
send_change_event();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_editable = function bots_data__get_editable() {
|
2016-12-13 22:33:22 +01:00
|
|
|
return _.filter(bots, function (bot) {
|
|
|
|
return util.is_current_user(bot.owner);
|
|
|
|
});
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
exports.get = function bot_data__get(email) {
|
2014-02-27 00:01:18 +01:00
|
|
|
return bots[email];
|
|
|
|
};
|
|
|
|
|
|
|
|
$(function init() {
|
|
|
|
_.each(page_params.bot_list, function (bot) {
|
|
|
|
exports.add(bot);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = bot_data;
|
|
|
|
}
|