mirror of
https://github.com/zulip/zulip.git
synced 2025-11-15 11:22:04 +00:00
Add bot_data module that updated with events
(imported from commit b0bd714258132fc81db763d316a15f5a81b1f4ff)
This commit is contained in:
39
static/js/bot_data.js
Normal file
39
static/js/bot_data.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
var bot_data = (function () {
|
||||||
|
var exports = {};
|
||||||
|
|
||||||
|
var bots = {};
|
||||||
|
var bot_fields = ['api_key', 'avatar_url', 'default_all_public_streams',
|
||||||
|
'default_events_register_stream',
|
||||||
|
'default_sending_stream', 'email', 'full_name'];
|
||||||
|
|
||||||
|
exports.add = function bot_data__add(bot) {
|
||||||
|
bots[bot.email] = _.pick(bot, bot_fields);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.remove = function bot_data__remove(email) {
|
||||||
|
delete bots[email];
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.update = function bot_data__update(email, bot_update) {
|
||||||
|
_.extend(bots[email], _.pick(bot_update, bot_fields));
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.get_all = function bots_data__get_all() {
|
||||||
|
return bots;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.get = function bots_data__get(email) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -87,6 +87,15 @@ function get_events_success(events) {
|
|||||||
people.update(event.person);
|
people.update(event.person);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'realm_bot':
|
||||||
|
if (event.op === 'add') {
|
||||||
|
bot_data.add(event.bot);
|
||||||
|
} else if (event.op === 'remove') {
|
||||||
|
bot_data.remove(event.bot.email);
|
||||||
|
} else if (event.op === 'update') {
|
||||||
|
bot_data.update(event.bot.email, event.bot);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'stream':
|
case 'stream':
|
||||||
if (event.op === 'update') {
|
if (event.op === 'update') {
|
||||||
// Legacy: Stream properties are still managed by subs.js on the client side.
|
// Legacy: Stream properties are still managed by subs.js on the client side.
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ var globals =
|
|||||||
+ ' message_edit tab_bar emoji popovers navigate people settings alert_words_ui message_store'
|
+ ' message_edit tab_bar emoji popovers navigate people settings alert_words_ui message_store'
|
||||||
+ ' avatar feature_flags search_suggestion referral stream_color Dict'
|
+ ' avatar feature_flags search_suggestion referral stream_color Dict'
|
||||||
+ ' Filter summary admin stream_data muting WinChan muting_ui Socket channel'
|
+ ' Filter summary admin stream_data muting WinChan muting_ui Socket channel'
|
||||||
+ ' message_flags'
|
+ ' message_flags bot_data'
|
||||||
|
|
||||||
// colorspace.js
|
// colorspace.js
|
||||||
+ ' colorspace'
|
+ ' colorspace'
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ def notify_created_bot(user_profile):
|
|||||||
default_events_register_stream=default_events_register_stream_name,
|
default_events_register_stream=default_events_register_stream_name,
|
||||||
default_all_public_streams=user_profile.default_all_public_streams,
|
default_all_public_streams=user_profile.default_all_public_streams,
|
||||||
avatar_url=avatar_url(user_profile),
|
avatar_url=avatar_url(user_profile),
|
||||||
|
owner=user_profile.bot_owner.email,
|
||||||
))
|
))
|
||||||
send_event(event, bot_owner_userids(user_profile))
|
send_event(event, bot_owner_userids(user_profile))
|
||||||
|
|
||||||
@@ -2237,6 +2238,7 @@ def get_realm_bot_dicts(user_profile):
|
|||||||
'default_sending_stream': botdict['default_sending_stream__name'],
|
'default_sending_stream': botdict['default_sending_stream__name'],
|
||||||
'default_events_register_stream': botdict['default_events_register_stream__name'],
|
'default_events_register_stream': botdict['default_events_register_stream__name'],
|
||||||
'default_all_public_streams': botdict['default_all_public_streams'],
|
'default_all_public_streams': botdict['default_all_public_streams'],
|
||||||
|
'owner': botdict['bot_owner__email'],
|
||||||
'avatar_url': get_avatar_url(botdict['avatar_source'], botdict['email']),
|
'avatar_url': get_avatar_url(botdict['avatar_source'], botdict['email']),
|
||||||
}
|
}
|
||||||
for botdict in get_active_bot_dicts_in_realm(user_profile.realm)]
|
for botdict in get_active_bot_dicts_in_realm(user_profile.realm)]
|
||||||
|
|||||||
@@ -1042,9 +1042,10 @@ def get_active_bot_dicts_in_realm(realm):
|
|||||||
'email', 'default_sending_stream__name',
|
'email', 'default_sending_stream__name',
|
||||||
'default_events_register_stream__name',
|
'default_events_register_stream__name',
|
||||||
'default_all_public_streams', 'api_key',
|
'default_all_public_streams', 'api_key',
|
||||||
'avatar_source') \
|
'bot_owner__email', 'avatar_source') \
|
||||||
.select_related('default_sending_stream',
|
.select_related('default_sending_stream',
|
||||||
'default_events_register_stream')
|
'default_events_register_stream',
|
||||||
|
'bot_owner')
|
||||||
|
|
||||||
def get_prereg_user_by_email(email):
|
def get_prereg_user_by_email(email):
|
||||||
# A user can be invited many times, so only return the result of the latest
|
# A user can be invited many times, so only return the result of the latest
|
||||||
|
|||||||
@@ -378,6 +378,7 @@ class BotTest(AuthedTestCase):
|
|||||||
default_sending_stream=None,
|
default_sending_stream=None,
|
||||||
default_events_register_stream=None,
|
default_events_register_stream=None,
|
||||||
default_all_public_streams=False,
|
default_all_public_streams=False,
|
||||||
|
owner='hamlet@zulip.com',
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
event['event']
|
event['event']
|
||||||
@@ -432,6 +433,7 @@ class BotTest(AuthedTestCase):
|
|||||||
default_sending_stream='Denmark',
|
default_sending_stream='Denmark',
|
||||||
default_events_register_stream=None,
|
default_events_register_stream=None,
|
||||||
default_all_public_streams=False,
|
default_all_public_streams=False,
|
||||||
|
owner='hamlet@zulip.com',
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
event['event']
|
event['event']
|
||||||
@@ -492,6 +494,7 @@ class BotTest(AuthedTestCase):
|
|||||||
default_sending_stream=None,
|
default_sending_stream=None,
|
||||||
default_events_register_stream='Denmark',
|
default_events_register_stream='Denmark',
|
||||||
default_all_public_streams=False,
|
default_all_public_streams=False,
|
||||||
|
owner='hamlet@zulip.com',
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
event['event']
|
event['event']
|
||||||
|
|||||||
48
zerver/tests/frontend/node/bot_data.js
Normal file
48
zerver/tests/frontend/node/bot_data.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
set_global('$', function () {});
|
||||||
|
|
||||||
|
var bot_data = require('js/bot_data.js');
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
var test_bot = {
|
||||||
|
email: 'bot1@zulip.com',
|
||||||
|
avatar_url: '',
|
||||||
|
default_all_public_streams: '',
|
||||||
|
default_events_register_stream: '',
|
||||||
|
default_sending_stream: '',
|
||||||
|
full_name: 'Bot 1',
|
||||||
|
extra: 'Not in data'
|
||||||
|
};
|
||||||
|
|
||||||
|
(function test_add() {
|
||||||
|
bot_data.add(test_bot);
|
||||||
|
|
||||||
|
var bot = bot_data.get('bot1@zulip.com');
|
||||||
|
assert.equal('Bot 1', bot.full_name);
|
||||||
|
assert.equal(undefined, bot.extra);
|
||||||
|
}());
|
||||||
|
|
||||||
|
(function test_update() {
|
||||||
|
var bot;
|
||||||
|
|
||||||
|
bot_data.add(test_bot);
|
||||||
|
|
||||||
|
bot = bot_data.get('bot1@zulip.com');
|
||||||
|
assert.equal('Bot 1', bot.full_name);
|
||||||
|
bot_data.update('bot1@zulip.com', {full_name: 'New Bot 1'});
|
||||||
|
bot = bot_data.get('bot1@zulip.com');
|
||||||
|
assert.equal('New Bot 1', bot.full_name);
|
||||||
|
}());
|
||||||
|
|
||||||
|
(function test_remove() {
|
||||||
|
var bot;
|
||||||
|
|
||||||
|
bot_data.add(test_bot);
|
||||||
|
|
||||||
|
bot = bot_data.get('bot1@zulip.com');
|
||||||
|
assert.equal('Bot 1', bot.full_name);
|
||||||
|
bot_data.remove('bot1@zulip.com');
|
||||||
|
bot = bot_data.get('bot1@zulip.com');
|
||||||
|
assert.equal(undefined, bot);
|
||||||
|
}());
|
||||||
|
|
||||||
|
}());
|
||||||
@@ -571,6 +571,7 @@ JS_SPECS = {
|
|||||||
'js/emoji.js',
|
'js/emoji.js',
|
||||||
'js/referral.js',
|
'js/referral.js',
|
||||||
'js/custom_markdown.js',
|
'js/custom_markdown.js',
|
||||||
|
'js/bot_data.js',
|
||||||
],
|
],
|
||||||
'output_filename': 'min/app.js'
|
'output_filename': 'min/app.js'
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user