mirror of
https://github.com/zulip/zulip.git
synced 2026-06-15 21:01:31 +08:00
frontend: Internally refer to bots by ID.
This is done by using a bot's ID instead of email in the handler methods for bot_data.bots and bot_data.services, and updating all code paths involved.
This commit is contained in:
parent
bd6fa385a5
commit
4cc8c74aaa
@ -14,8 +14,8 @@ set_global('$', function (f) {
|
||||
set_global('document', null);
|
||||
|
||||
var page_params = {
|
||||
realm_bots: [{email: 'bot0@zulip.com', full_name: 'Bot 0'},
|
||||
{email: 'outgoingwebhook@zulip.com', full_name: "Outgoing webhook",
|
||||
realm_bots: [{email: 'bot0@zulip.com', user_id: 42, full_name: 'Bot 0'},
|
||||
{email: 'outgoingwebhook@zulip.com', user_id: 314, full_name: "Outgoing webhook",
|
||||
services: [{base_url: "http://foo.com", interface: 1}]}],
|
||||
is_admin: false,
|
||||
};
|
||||
@ -31,12 +31,13 @@ global.people.initialize_current_user(42);
|
||||
|
||||
bot_data.initialize();
|
||||
// Our startup logic should have added Bot 0 from page_params.
|
||||
assert.equal(bot_data.get('bot0@zulip.com').full_name, 'Bot 0');
|
||||
assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webhook');
|
||||
assert.equal(bot_data.get(42).full_name, 'Bot 0');
|
||||
assert.equal(bot_data.get(314).full_name, 'Outgoing webhook');
|
||||
|
||||
(function () {
|
||||
var test_bot = {
|
||||
email: 'bot1@zulip.com',
|
||||
user_id: 43,
|
||||
avatar_url: '',
|
||||
full_name: 'Bot 1',
|
||||
services: [{base_url: "http://bar.com", interface: 1}],
|
||||
@ -46,8 +47,8 @@ assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webh
|
||||
(function test_add() {
|
||||
bot_data.add(test_bot);
|
||||
|
||||
var bot = bot_data.get('bot1@zulip.com');
|
||||
var services = bot_data.get_services('bot1@zulip.com');
|
||||
var bot = bot_data.get(43);
|
||||
var services = bot_data.get_services(43);
|
||||
assert.equal('Bot 1', bot.full_name);
|
||||
assert.equal('http://bar.com', services[0].base_url);
|
||||
assert.equal(1, services[0].interface);
|
||||
@ -60,13 +61,13 @@ assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webh
|
||||
|
||||
bot_data.add(test_bot);
|
||||
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
bot = bot_data.get(43);
|
||||
assert.equal('Bot 1', bot.full_name);
|
||||
bot_data.update('bot1@zulip.com', {full_name: 'New Bot 1',
|
||||
bot_data.update(43, {full_name: 'New Bot 1',
|
||||
services: [{interface: 2,
|
||||
base_url: 'http://baz.com'}]});
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
services = bot_data.get_services('bot1@zulip.com');
|
||||
bot = bot_data.get(43);
|
||||
services = bot_data.get_services(43);
|
||||
assert.equal('New Bot 1', bot.full_name);
|
||||
assert.equal(2, services[0].interface);
|
||||
assert.equal('http://baz.com', services[0].base_url);
|
||||
@ -77,11 +78,11 @@ assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webh
|
||||
|
||||
bot_data.add(_.extend({}, test_bot, {is_active: true}));
|
||||
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
bot = bot_data.get(43);
|
||||
assert.equal('Bot 1', bot.full_name);
|
||||
assert(bot.is_active);
|
||||
bot_data.deactivate('bot1@zulip.com');
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
bot_data.deactivate(43);
|
||||
bot = bot_data.get(43);
|
||||
assert.equal(bot.is_active, false);
|
||||
}());
|
||||
|
||||
@ -90,12 +91,12 @@ assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webh
|
||||
|
||||
bot_data.add(_.extend({owner: 'owner@zulip.com'}, test_bot));
|
||||
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
bot = bot_data.get(43);
|
||||
assert(bot.can_admin);
|
||||
|
||||
bot_data.add(_.extend({owner: 'notowner@zulip.com'}, test_bot));
|
||||
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
bot = bot_data.get(43);
|
||||
assert.equal(false, bot.can_admin);
|
||||
}());
|
||||
|
||||
@ -105,7 +106,7 @@ assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webh
|
||||
|
||||
bot_data.add(test_bot);
|
||||
|
||||
bot = bot_data.get('bot1@zulip.com');
|
||||
bot = bot_data.get(43);
|
||||
assert(bot.can_admin);
|
||||
|
||||
page_params.is_admin = false;
|
||||
@ -114,9 +115,9 @@ assert.equal(bot_data.get('outgoingwebhook@zulip.com').full_name, 'Outgoing webh
|
||||
(function test_get_editable() {
|
||||
var can_admin;
|
||||
|
||||
bot_data.add(_.extend({}, test_bot, {owner: 'owner@zulip.com', is_active: true}));
|
||||
bot_data.add(_.extend({}, test_bot, {email: 'bot2@zulip.com', owner: 'owner@zulip.com', is_active: true}));
|
||||
bot_data.add(_.extend({}, test_bot, {email: 'bot3@zulip.com', owner: 'not_owner@zulip.com', is_active: true}));
|
||||
bot_data.add(_.extend({}, test_bot, {user_id: 44, owner: 'owner@zulip.com', is_active: true}));
|
||||
bot_data.add(_.extend({}, test_bot, {user_id: 45, email: 'bot2@zulip.com', owner: 'owner@zulip.com', is_active: true}));
|
||||
bot_data.add(_.extend({}, test_bot, {user_id: 46, email: 'bot3@zulip.com', owner: 'not_owner@zulip.com', is_active: true}));
|
||||
|
||||
can_admin = _.pluck(bot_data.get_editable(), 'email');
|
||||
assert.deepEqual(['bot1@zulip.com', 'bot2@zulip.com'], can_admin);
|
||||
|
||||
@ -214,6 +214,7 @@ var event_fixtures = {
|
||||
op: 'remove',
|
||||
bot: {
|
||||
email: 'the-bot@example.com',
|
||||
user_id: '42',
|
||||
full_name: 'The Bot',
|
||||
},
|
||||
},
|
||||
@ -585,8 +586,8 @@ with_overrides(function (override) {
|
||||
override('bot_data.deactivate', bot_stub.f);
|
||||
override('settings_users.update_user_data', admin_stub.f);
|
||||
dispatch(event);
|
||||
var args = bot_stub.get_args('email');
|
||||
assert_same(args.email, event.bot.email);
|
||||
var args = bot_stub.get_args('user_id');
|
||||
assert_same(args.user_id, event.bot.user_id);
|
||||
|
||||
admin_stub.get_args('update_user_id', 'update_bot_data');
|
||||
});
|
||||
@ -600,8 +601,8 @@ with_overrides(function (override) {
|
||||
|
||||
dispatch(event);
|
||||
|
||||
var args = bot_stub.get_args('email', 'bot');
|
||||
assert_same(args.email, event.bot.email);
|
||||
var args = bot_stub.get_args('user_id', 'bot');
|
||||
assert_same(args.user_id, event.bot.user_id);
|
||||
assert_same(args.bot, event.bot);
|
||||
|
||||
args = admin_stub.get_args('update_user_id', 'update_bot_data');
|
||||
|
||||
@ -4,7 +4,7 @@ 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', 'is_active', 'owner', 'bot_type'];
|
||||
'email', 'full_name', 'is_active', 'owner', 'bot_type', 'user_id'];
|
||||
var services = {};
|
||||
var services_fields = ['base_url', 'interface'];
|
||||
|
||||
@ -24,28 +24,28 @@ var bot_data = (function () {
|
||||
|
||||
exports.add = function bot_data__add(bot) {
|
||||
var clean_bot = _.pick(bot, bot_fields);
|
||||
bots[bot.email] = clean_bot;
|
||||
bots[bot.user_id] = clean_bot;
|
||||
set_can_admin(clean_bot);
|
||||
var clean_services = _.map(bot.services, function (service) {
|
||||
return _.pick(service, services_fields);
|
||||
});
|
||||
services[bot.email] = clean_services;
|
||||
services[bot.user_id] = clean_services;
|
||||
|
||||
send_change_event();
|
||||
};
|
||||
|
||||
exports.deactivate = function bot_data__deactivate(email) {
|
||||
bots[email].is_active = false;
|
||||
exports.deactivate = function bot_data__deactivate(bot_id) {
|
||||
bots[bot_id].is_active = false;
|
||||
send_change_event();
|
||||
};
|
||||
|
||||
exports.update = function bot_data__update(email, bot_update) {
|
||||
var bot = bots[email];
|
||||
exports.update = function bot_data__update(bot_id, bot_update) {
|
||||
var bot = bots[bot_id];
|
||||
_.extend(bot, _.pick(bot_update, bot_fields));
|
||||
set_can_admin(bot);
|
||||
|
||||
// We currently only support one service per bot.
|
||||
var service = services[email][0];
|
||||
var service = services[bot_id][0];
|
||||
if (typeof bot_update.services !== 'undefined' && bot_update.services.length > 0) {
|
||||
_.extend(service, _.pick(bot_update.services[0], services_fields));
|
||||
}
|
||||
@ -64,12 +64,12 @@ var bot_data = (function () {
|
||||
});
|
||||
};
|
||||
|
||||
exports.get = function bot_data__get(email) {
|
||||
return bots[email];
|
||||
exports.get = function bot_data__get(bot_id) {
|
||||
return bots[bot_id];
|
||||
};
|
||||
|
||||
exports.get_services = function bot_data__get_services(email) {
|
||||
return services[email];
|
||||
exports.get_services = function bot_data__get_services(bot_id) {
|
||||
return services[bot_id];
|
||||
};
|
||||
|
||||
exports.initialize = function () {
|
||||
|
||||
@ -112,14 +112,14 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) {
|
||||
bot_data.add(event.bot);
|
||||
settings_users.update_user_data(event.bot.user_id, event.bot);
|
||||
} else if (event.op === 'remove') {
|
||||
bot_data.deactivate(event.bot.email);
|
||||
bot_data.deactivate(event.bot.user_id);
|
||||
event.bot.is_active = false;
|
||||
settings_users.update_user_data(event.bot.user_id, event.bot);
|
||||
} else if (event.op === 'update') {
|
||||
if (_.has(event.bot, 'owner_id')) {
|
||||
event.bot.owner = people.get_person_from_user_id(event.bot.owner_id).email;
|
||||
}
|
||||
bot_data.update(event.bot.email, event.bot);
|
||||
bot_data.update(event.bot.user_id, event.bot);
|
||||
settings_users.update_user_data(event.bot.user_id, event.bot);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -32,6 +32,7 @@ function render_bots() {
|
||||
add_bot_row({
|
||||
name: elem.full_name,
|
||||
email: elem.email,
|
||||
user_id: elem.user_id,
|
||||
type: exports.type_id_to_string(elem.bot_type),
|
||||
avatar_url: elem.avatar_url,
|
||||
api_key: elem.api_key,
|
||||
@ -106,14 +107,9 @@ exports.set_up = function () {
|
||||
$('#download_flaskbotrc').click(function () {
|
||||
var OUTGOING_WEBHOOK_BOT_TYPE_INT = 3;
|
||||
var content = "";
|
||||
$("#active_bots_list .bot-information-box").each(function () {
|
||||
var bot_info = $(this);
|
||||
var email = bot_info.find(".email .value").text();
|
||||
var api_key = bot_info.find(".api_key .api-key-value-and-button .value").text();
|
||||
var bot = bot_data.get(email);
|
||||
|
||||
if (bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) {
|
||||
content += exports.generate_flaskbotrc_content(email, api_key);
|
||||
_.each(bot_data.get_all_bots_for_current_user(), function (bot) {
|
||||
if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) {
|
||||
content += exports.generate_flaskbotrc_content(bot.email, bot.api_key);
|
||||
}
|
||||
});
|
||||
$(this).attr("href", "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content));
|
||||
@ -289,21 +285,22 @@ exports.set_up = function () {
|
||||
var form = $('#settings_page .edit_bot_form');
|
||||
var image = li.find(".image");
|
||||
var bot_info = li;
|
||||
var bot_id = bot_info.find('.bot_info').attr('data-user_id').valueOf();
|
||||
var reset_edit_bot = li.find(".reset_edit_bot");
|
||||
var owner_select = $(templates.render("bot_owner_select", {users_list:users_list}));
|
||||
var old_full_name = bot_info.find(".name").text();
|
||||
var old_owner = bot_data.get(bot_info.find(".email .value").text()).owner;
|
||||
var bot_email = bot_info.find(".email .value").text();
|
||||
var bot_type = bot_info.find(".type .value").text();
|
||||
|
||||
var old_owner = bot_data.get(bot_id).owner;
|
||||
var bot_email = bot_data.get(bot_id).email;
|
||||
var bot_type = bot_data.get(bot_id).bot_type;
|
||||
$("#settings_page .edit_bot .edit_bot_name").val(old_full_name);
|
||||
$("#settings_page .edit_bot .select-form").text("").append(owner_select);
|
||||
$("#settings_page .edit_bot .edit-bot-owner select").val(old_owner);
|
||||
$("#settings_page .edit_bot_form").attr("data-email", bot_email);
|
||||
$("#settings_page .edit_bot_form").attr("data-type", bot_type);
|
||||
$(".edit_bot_email").text(bot_email);
|
||||
|
||||
if (bot_type === "Outgoing webhook") {
|
||||
var services = bot_data.get_services(bot_email);
|
||||
if (bot_type.toString() === OUTGOING_WEBHOOK_BOT_TYPE) {
|
||||
var services = bot_data.get_services(bot_id);
|
||||
$("#settings_page .edit_bot #service_data").show();
|
||||
// Currently, we only support one service per bot.
|
||||
$("#settings_page .edit_bot #edit_service_base_url").val(services[0].base_url);
|
||||
@ -336,17 +333,17 @@ exports.set_up = function () {
|
||||
},
|
||||
submitHandler: function () {
|
||||
var email = form.attr('data-email');
|
||||
var type = form.attr('data-type');
|
||||
var full_name = form.find('.edit_bot_name').val();
|
||||
var bot_owner = form.find('.edit-bot-owner select').val();
|
||||
var file_input = $(".edit_bot").find('.edit_bot_avatar_file_input');
|
||||
var spinner = form.find('.edit_bot_spinner');
|
||||
var edit_button = form.find('.edit_bot_button');
|
||||
var formData = new FormData();
|
||||
|
||||
formData.append('csrfmiddlewaretoken', csrf_token);
|
||||
formData.append('full_name', full_name);
|
||||
formData.append('bot_owner', bot_owner);
|
||||
if (bot_type === "Outgoing webhook") {
|
||||
if (type === OUTGOING_WEBHOOK_BOT_TYPE) {
|
||||
var service_payload_url = $("#settings_page .edit_bot #edit_service_base_url").val();
|
||||
var service_interface = $("#settings_page .edit_bot #edit_service_interface :selected").val();
|
||||
formData.append('service_payload_url', JSON.stringify(service_payload_url));
|
||||
|
||||
@ -403,7 +403,7 @@ exports.on_load_success = function (realm_people_data) {
|
||||
} else if (person.is_bot) {
|
||||
// Dynamically add the owner select control in order to
|
||||
// avoid performance issues in case of large number of users.
|
||||
owner_select.val(bot_data.get(person.email).owner || "");
|
||||
owner_select.val(bot_data.get(user_id).owner || "");
|
||||
form_row.find(".edit_bot_owner_container").append(owner_select);
|
||||
}
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
{{/if}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="bot_info">
|
||||
<div class="bot_info" data-user_id="{{user_id}}">
|
||||
<div class="email">
|
||||
<div class="field">{{t "Username" }}</div>
|
||||
<div class="value">{{email}}</div>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user