Files
zulip/static/js/bot_data.js
Steve Howell 155f6da8ba bots: Add owner_id to bot-related payloads.
For the below payloads we want `owner_id` instead
of `owner`, which we should deprecate.  (The
`owner` field is actually an email, which is
not a stable key.)

    page_params.realm_bots

    realm_bot/add

    realm_bot/update

IMPORTANT NOTE: Some of the data served in
these payloads is cached with the key
`bot_dicts_in_realm_cache_key`.

For page_params, we get the new field
via `get_owned_bot_dicts`.

For realm_bot/add, we modified
`created_bot_event`.

For realm_bot/update, we modified
`do_change_bot_owner`.

On the JS side, we no longer
look up the bot's owner directly in
`server_events_dispatch` when we get
a realm_bot/update event. Instead, we
delegate that job to `bot_data.js`.
I modified the tests accordingly.
2020-05-11 16:16:58 -07:00

99 lines
2.4 KiB
JavaScript

const bots = new Map();
const bot_fields = [
'api_key',
'avatar_url',
'bot_type',
'default_all_public_streams',
'default_events_register_stream',
'default_sending_stream',
'email',
'full_name',
'is_active',
'owner', // TODO: eliminate
'owner_id',
'user_id',
];
const services = new Map();
const services_fields = ['base_url', 'interface',
'config_data', 'service_name', 'token'];
const send_change_event = _.debounce(function () {
settings_bots.render_bots();
}, 50);
exports.add = function (bot) {
const clean_bot = _.pick(bot, bot_fields);
bots.set(bot.user_id, clean_bot);
const clean_services = bot.services.map(service => _.pick(service, services_fields));
services.set(bot.user_id, clean_services);
send_change_event();
};
exports.deactivate = function (bot_id) {
bots.get(bot_id).is_active = false;
send_change_event();
};
exports.del = function (bot_id) {
bots.delete(bot_id);
services.delete(bot_id);
send_change_event();
};
exports.update = function (bot_id, bot_update) {
const bot = bots.get(bot_id);
Object.assign(bot, _.pick(bot_update, bot_fields));
// We currently only support one service per bot.
const service = services.get(bot_id)[0];
if (typeof bot_update.services !== 'undefined' && bot_update.services.length > 0) {
Object.assign(service, _.pick(bot_update.services[0], services_fields));
}
// TODO: eliminate `owner` (which is an email)
if (bot.owner_id) {
const bot_owner = people.get_by_user_id(bot.owner_id);
bot.owner = bot_owner.email;
}
send_change_event();
};
exports.get_all_bots_for_current_user = function () {
const ret = [];
for (const bot of bots.values()) {
if (people.is_current_user(bot.owner)) {
ret.push(bot);
}
}
return ret;
};
exports.get_editable = function () {
const ret = [];
for (const bot of bots.values()) {
if (bot.is_active && people.is_current_user(bot.owner)) {
ret.push(bot);
}
}
return ret;
};
exports.get = function (bot_id) {
return bots.get(bot_id);
};
exports.get_services = function (bot_id) {
return services.get(bot_id);
};
exports.initialize = function (params) {
for (const bot of params.realm_bots) {
exports.add(bot);
}
};
window.bot_data = exports;