mirror of
https://github.com/zulip/zulip.git
synced 2025-11-14 02:48:00 +00:00
Use topic_data.js for topic typeaheads.
This replaces some old code with calls to topic_data.js. Now our topic typeahead uses the same data as our sidebar, stream suggestions, and the "n" key, so any future improvements to that data will benefit all features the same. This is an important piece of #9857.
This commit is contained in:
@@ -17,6 +17,9 @@ set_global('md5', function (s) {
|
|||||||
return 'md5-' + s;
|
return 'md5-' + s;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
set_global('topic_data', {
|
||||||
|
});
|
||||||
|
|
||||||
var ct = composebox_typeahead;
|
var ct = composebox_typeahead;
|
||||||
var noop = function () {};
|
var noop = function () {};
|
||||||
|
|
||||||
@@ -93,6 +96,9 @@ var netherland_stream = {
|
|||||||
stream_id: 3,
|
stream_id: 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
stream_data.add_sub('Sweden', sweden_stream);
|
||||||
|
stream_data.add_sub('Denmark', denmark_stream);
|
||||||
|
|
||||||
set_global('$', global.make_zjquery());
|
set_global('$', global.make_zjquery());
|
||||||
|
|
||||||
set_global('page_params', {});
|
set_global('page_params', {});
|
||||||
@@ -174,20 +180,17 @@ user_pill.get_user_ids = function () {
|
|||||||
return [];
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
run_test('add_topic', () => {
|
run_test('topics_seen_for', () => {
|
||||||
ct.add_topic('Denmark', 'civil fears');
|
topic_data.get_recent_names = (stream_id) => {
|
||||||
ct.add_topic('devel', 'fading');
|
assert.equal(stream_id, denmark_stream.stream_id);
|
||||||
ct.add_topic('denmark', 'acceptance');
|
return ['With Twisted Metal', 'acceptance', 'civil fears'];
|
||||||
ct.add_topic('denmark', 'Acceptance');
|
};
|
||||||
ct.add_topic('Denmark', 'With Twisted Metal');
|
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
ct.topics_seen_for('Denmark'),
|
ct.topics_seen_for('Denmark'),
|
||||||
['With Twisted Metal', 'acceptance', 'civil fears']
|
['With Twisted Metal', 'acceptance', 'civil fears']
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
run_test('topics_seen_for', () => {
|
|
||||||
// Test when the stream doesn't exist (there are no topics)
|
// Test when the stream doesn't exist (there are no topics)
|
||||||
assert.deepEqual(ct.topics_seen_for('non-existing-stream'), []);
|
assert.deepEqual(ct.topics_seen_for('non-existing-stream'), []);
|
||||||
});
|
});
|
||||||
@@ -400,14 +403,12 @@ run_test('initialize', () => {
|
|||||||
|
|
||||||
var subject_typeahead_called = false;
|
var subject_typeahead_called = false;
|
||||||
$('#subject').typeahead = function (options) {
|
$('#subject').typeahead = function (options) {
|
||||||
// options.source()
|
|
||||||
ct.add_topic('Sweden', 'furniture');
|
|
||||||
ct.add_topic('Sweden', 'kronor');
|
|
||||||
ct.add_topic('Sweden', 'ice');
|
|
||||||
ct.add_topic('Sweden', 'more ice');
|
|
||||||
ct.add_topic('Sweden', 'even more ice');
|
|
||||||
ct.add_topic('Sweden', '<&>');
|
|
||||||
var topics = ['<&>', 'even more ice', 'furniture', 'ice', 'kronor', 'more ice'];
|
var topics = ['<&>', 'even more ice', 'furniture', 'ice', 'kronor', 'more ice'];
|
||||||
|
topic_data.get_recent_names = (stream_id) => {
|
||||||
|
assert.equal(stream_id, sweden_stream.stream_id);
|
||||||
|
return topics;
|
||||||
|
};
|
||||||
|
|
||||||
$('#stream').val('Sweden');
|
$('#stream').val('Sweden');
|
||||||
var actual_value = options.source();
|
var actual_value = options.source();
|
||||||
// Topics should be sorted alphabetically, not by addition order.
|
// Topics should be sorted alphabetically, not by addition order.
|
||||||
@@ -435,7 +436,7 @@ run_test('initialize', () => {
|
|||||||
//
|
//
|
||||||
// Notice that alphabetical sorting isn't managed by this sorter,
|
// Notice that alphabetical sorting isn't managed by this sorter,
|
||||||
// it is a result of the topics already being sorted after adding
|
// it is a result of the topics already being sorted after adding
|
||||||
// them with ct.add_topic().
|
// them with add_topic().
|
||||||
options.query = 'furniture';
|
options.query = 'furniture';
|
||||||
actual_value = options.sorter(['furniture']);
|
actual_value = options.sorter(['furniture']);
|
||||||
expected_value = ['furniture'];
|
expected_value = ['furniture'];
|
||||||
@@ -910,6 +911,8 @@ run_test('initialize', () => {
|
|||||||
assert(stream_one_called);
|
assert(stream_one_called);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
stream_data.clear_subscriptions();
|
||||||
|
|
||||||
run_test('begins_typeahead', () => {
|
run_test('begins_typeahead', () => {
|
||||||
|
|
||||||
var begin_typehead_this = {options: {completions: {
|
var begin_typehead_this = {options: {completions: {
|
||||||
|
|||||||
@@ -89,10 +89,8 @@ const messages = {
|
|||||||
const noop = () => undefined;
|
const noop = () => undefined;
|
||||||
|
|
||||||
set_global('alert_words', {});
|
set_global('alert_words', {});
|
||||||
set_global('composebox_typeahead', {});
|
|
||||||
|
|
||||||
alert_words.process_message = noop;
|
alert_words.process_message = noop;
|
||||||
composebox_typeahead.add_topic = noop;
|
|
||||||
|
|
||||||
// We can also bring in real code:
|
// We can also bring in real code:
|
||||||
zrequire('recent_senders');
|
zrequire('recent_senders');
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ zrequire('people');
|
|||||||
zrequire('message_store');
|
zrequire('message_store');
|
||||||
|
|
||||||
var noop = function () {};
|
var noop = function () {};
|
||||||
var with_overrides = global.with_overrides;
|
|
||||||
var people = global.people;
|
var people = global.people;
|
||||||
|
|
||||||
set_global('$', global.make_zjquery());
|
set_global('$', global.make_zjquery());
|
||||||
@@ -101,33 +100,18 @@ run_test('add_message_metadata', () => {
|
|||||||
sender_email: 'me@example.com',
|
sender_email: 'me@example.com',
|
||||||
sender_id: me.user_id,
|
sender_id: me.user_id,
|
||||||
type: 'stream',
|
type: 'stream',
|
||||||
display_recipient: [me, cindy],
|
display_recipient: 'Zoolippy',
|
||||||
stream: 'Zoolippy',
|
|
||||||
topic: 'cool thing',
|
topic: 'cool thing',
|
||||||
subject: 'the_subject',
|
subject: 'the_subject',
|
||||||
id: 2068,
|
id: 2068,
|
||||||
};
|
};
|
||||||
|
|
||||||
// test stream properties
|
|
||||||
with_overrides(function (override) {
|
|
||||||
override('compose.empty_topic_placeholder', function () {
|
|
||||||
return 'the_subject';
|
|
||||||
});
|
|
||||||
global.with_stub(function (stub) {
|
|
||||||
set_global('composebox_typeahead', {add_topic: stub.f});
|
|
||||||
message_store.set_message_booleans(message);
|
message_store.set_message_booleans(message);
|
||||||
message_store.add_message_metadata(message);
|
message_store.add_message_metadata(message);
|
||||||
var typeahead_added = stub.get_args('stream', 'subject');
|
assert.deepEqual(message.stream, message.display_recipient);
|
||||||
assert.deepEqual(typeahead_added.stream, [me, cindy]);
|
|
||||||
assert.equal(message.subject, typeahead_added.subject);
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(message.stream, [me, cindy]);
|
|
||||||
assert.equal(message.reply_to, 'me@example.com');
|
assert.equal(message.reply_to, 'me@example.com');
|
||||||
assert.deepEqual(message.flags, undefined);
|
assert.deepEqual(message.flags, undefined);
|
||||||
assert.equal(message.alerted, false);
|
assert.equal(message.alerted, false);
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test('errors', () => {
|
run_test('errors', () => {
|
||||||
|
|||||||
@@ -13,30 +13,13 @@ var composebox_typeahead = (function () {
|
|||||||
|
|
||||||
var exports = {};
|
var exports = {};
|
||||||
|
|
||||||
var seen_topics = new Dict();
|
exports.topics_seen_for = function (stream_name) {
|
||||||
|
var stream_id = stream_data.get_stream_id(stream_name);
|
||||||
exports.add_topic = function (uc_stream, uc_topic) {
|
if (!stream_id) {
|
||||||
// For Denmark/FooBar, we set
|
|
||||||
// seen_topics['denmark']['foobar'] to 'FooBar',
|
|
||||||
// where seen_topics is a Dict of Dicts
|
|
||||||
var stream = uc_stream.toLowerCase();
|
|
||||||
var topic = uc_topic.toLowerCase();
|
|
||||||
|
|
||||||
if (!seen_topics.has(stream)) {
|
|
||||||
seen_topics.set(stream, new Dict());
|
|
||||||
}
|
|
||||||
var topic_dict = seen_topics.get(stream);
|
|
||||||
if (!topic_dict.has(topic)) {
|
|
||||||
topic_dict.set(topic, uc_topic);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.topics_seen_for = function (stream) {
|
|
||||||
stream = stream.toLowerCase();
|
|
||||||
if (seen_topics.has(stream)) {
|
|
||||||
return seen_topics.get(stream).values().sort();
|
|
||||||
}
|
|
||||||
return [];
|
return [];
|
||||||
|
}
|
||||||
|
var topic_names = topic_data.get_recent_names(stream_id);
|
||||||
|
return topic_names;
|
||||||
};
|
};
|
||||||
|
|
||||||
function query_matches_language(query, lang) {
|
function query_matches_language(query, lang) {
|
||||||
@@ -623,7 +606,7 @@ exports.initialize = function () {
|
|||||||
|
|
||||||
$("#subject").typeahead({
|
$("#subject").typeahead({
|
||||||
source: function () {
|
source: function () {
|
||||||
var stream_name = $("#stream").val();
|
var stream_name = compose_state.stream_name();
|
||||||
return exports.topics_seen_for(stream_name);
|
return exports.topics_seen_for(stream_name);
|
||||||
},
|
},
|
||||||
items: 3,
|
items: 3,
|
||||||
|
|||||||
@@ -132,7 +132,6 @@ exports.add_message_metadata = function (message) {
|
|||||||
case 'stream':
|
case 'stream':
|
||||||
message.is_stream = true;
|
message.is_stream = true;
|
||||||
message.stream = message.display_recipient;
|
message.stream = message.display_recipient;
|
||||||
composebox_typeahead.add_topic(message.stream, message.subject);
|
|
||||||
message.reply_to = message.sender_email;
|
message.reply_to = message.sender_email;
|
||||||
|
|
||||||
topic_data.add_message({
|
topic_data.add_message({
|
||||||
|
|||||||
Reference in New Issue
Block a user