2018-05-06 21:43:17 +02:00
|
|
|
/* eslint indent: "off" */
|
|
|
|
|
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',
|
2017-02-06 21:02:53 +01:00
|
|
|
'default_events_register_stream', 'default_sending_stream',
|
2018-01-22 18:36:53 +01:00
|
|
|
'email', 'full_name', 'is_active', 'owner', 'bot_type', 'user_id'];
|
2018-01-16 20:40:03 +01:00
|
|
|
var services = {};
|
2018-01-30 19:24:18 +01:00
|
|
|
var services_fields = ['base_url', 'interface',
|
2018-05-30 11:09:35 +02:00
|
|
|
'config_data', 'service_name', 'token'];
|
2014-02-27 18:27:31 +01:00
|
|
|
|
|
|
|
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;
|
2017-01-19 20:18:03 +01:00
|
|
|
} else if (bot.owner !== undefined && people.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);
|
2018-01-22 18:36:53 +01:00
|
|
|
bots[bot.user_id] = clean_bot;
|
2014-02-27 18:27:31 +01:00
|
|
|
set_can_admin(clean_bot);
|
2018-01-16 20:40:03 +01:00
|
|
|
var clean_services = _.map(bot.services, function (service) {
|
|
|
|
return _.pick(service, services_fields);
|
|
|
|
});
|
2018-01-22 18:36:53 +01:00
|
|
|
services[bot.user_id] = clean_services;
|
2018-01-16 20:40:03 +01:00
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
send_change_event();
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
exports.deactivate = function bot_data__deactivate(bot_id) {
|
|
|
|
bots[bot_id].is_active = false;
|
2014-02-27 18:27:31 +01:00
|
|
|
send_change_event();
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
2018-03-08 16:28:37 +01:00
|
|
|
exports.delete = function bot_data__delete(bot_id) {
|
|
|
|
delete bots[bot_id];
|
|
|
|
delete services[bot_id];
|
|
|
|
send_change_event();
|
|
|
|
};
|
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
exports.update = function bot_data__update(bot_id, bot_update) {
|
|
|
|
var bot = bots[bot_id];
|
2014-02-27 18:27:31 +01:00
|
|
|
_.extend(bot, _.pick(bot_update, bot_fields));
|
|
|
|
set_can_admin(bot);
|
2018-01-16 20:40:03 +01:00
|
|
|
|
|
|
|
// We currently only support one service per bot.
|
2018-01-22 18:36:53 +01:00
|
|
|
var service = services[bot_id][0];
|
2018-01-16 20:40:03 +01:00
|
|
|
if (typeof bot_update.services !== 'undefined' && bot_update.services.length > 0) {
|
|
|
|
_.extend(service, _.pick(bot_update.services[0], services_fields));
|
|
|
|
}
|
2014-02-27 18:27:31 +01:00
|
|
|
send_change_event();
|
|
|
|
};
|
|
|
|
|
2017-02-25 16:00:01 +01:00
|
|
|
exports.get_all_bots_for_current_user = function bots_data__get_editable() {
|
|
|
|
return _.filter(bots, function (bot) {
|
|
|
|
return people.is_current_user(bot.owner);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
exports.get_editable = function bots_data__get_editable() {
|
2016-12-13 22:33:22 +01:00
|
|
|
return _.filter(bots, function (bot) {
|
2017-02-07 19:48:17 +01:00
|
|
|
return bot.is_active && people.is_current_user(bot.owner);
|
2016-12-13 22:33:22 +01:00
|
|
|
});
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
exports.get = function bot_data__get(bot_id) {
|
|
|
|
return bots[bot_id];
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
exports.get_services = function bot_data__get_services(bot_id) {
|
|
|
|
return services[bot_id];
|
2018-01-16 20:40:03 +01:00
|
|
|
};
|
|
|
|
|
2017-05-25 02:50:50 +02:00
|
|
|
exports.initialize = function () {
|
2017-04-21 08:24:30 +02:00
|
|
|
_.each(page_params.realm_bots, function (bot) {
|
2014-02-27 00:01:18 +01:00
|
|
|
exports.add(bot);
|
|
|
|
});
|
2018-01-30 10:09:05 +01:00
|
|
|
delete page_params.realm_bots;
|
2017-05-25 02:50:50 +02:00
|
|
|
};
|
2014-02-27 00:01:18 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = bot_data;
|
|
|
|
}
|