typeahead: Migrate from _@ to @_ for silent mentions.

We also clean up the code a little to make it easier to
read and combine the matching code for both mentions and
silent_mentions.
This commit is contained in:
Rohitt Vashishtha 2019-02-20 22:49:05 +05:30 committed by Tim Abbott
parent 5c2e64d6a2
commit 8119258c4d
3 changed files with 41 additions and 46 deletions

View File

@ -347,22 +347,22 @@ run_test('content_typeahead_selected', () => {
document_stub_trigger3_called = true;
};
fake_this.query = '_@kin';
fake_this.query = '@_kin';
fake_this.token = 'kin';
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
expected_value = '_@**King Hamlet** ';
expected_value = '@_**King Hamlet** ';
assert.equal(actual_value, expected_value);
fake_this.query = '_@*kin';
fake_this.query = '@_*kin';
fake_this.token = 'kin';
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
expected_value = '_@**King Hamlet** ';
expected_value = '@_**King Hamlet** ';
assert.equal(actual_value, expected_value);
fake_this.query = '_@**kin';
fake_this.query = '@_**kin';
fake_this.token = 'kin';
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
expected_value = '_@**King Hamlet** ';
expected_value = '@_**King Hamlet** ';
assert.equal(actual_value, expected_value);
// user group mention
@ -1128,39 +1128,39 @@ run_test('begins_typeahead', () => {
assert_typeahead_equals("#foo\n~~~py", lang_list);
assert_typeahead_equals("@", false);
assert_typeahead_equals("_@", false);
assert_typeahead_equals("@_", false);
assert_typeahead_equals(" @", false);
assert_typeahead_equals(" _@", false);
assert_typeahead_equals(" @_", false);
assert_typeahead_equals("test @**o", all_mentions);
assert_typeahead_equals("test _@**o", all_mentions);
assert_typeahead_equals("test @_**o", all_mentions);
assert_typeahead_equals("test @*o", all_mentions);
assert_typeahead_equals("test _@*k", all_mentions);
assert_typeahead_equals("test @_*k", all_mentions);
assert_typeahead_equals("test @*h", all_mentions);
assert_typeahead_equals("test _@*h", all_mentions);
assert_typeahead_equals("test @_*h", all_mentions);
assert_typeahead_equals("test @", false);
assert_typeahead_equals("test _@", false);
assert_typeahead_equals("test @_", false);
assert_typeahead_equals("test no@o", false);
assert_typeahead_equals("test no_@k", false);
assert_typeahead_equals("test no@_k", false);
assert_typeahead_equals("@ ", false);
assert_typeahead_equals("_@ ", false);
assert_typeahead_equals("@_ ", false);
assert_typeahead_equals("@* ", false);
assert_typeahead_equals("_@* ", false);
assert_typeahead_equals("@_* ", false);
assert_typeahead_equals("@** ", false);
assert_typeahead_equals("_@** ", false);
assert_typeahead_equals("@_** ", false);
assert_typeahead_equals("test\n@i", all_mentions);
assert_typeahead_equals("test\n_@i", all_mentions);
assert_typeahead_equals("test\n@_i", all_mentions);
assert_typeahead_equals("test\n @l", all_mentions);
assert_typeahead_equals("test\n _@l", all_mentions);
assert_typeahead_equals("test\n @_l", all_mentions);
assert_typeahead_equals("@zuli", all_mentions);
assert_typeahead_equals("_@zuli", all_mentions);
assert_typeahead_equals("@_zuli", all_mentions);
assert_typeahead_equals("@ zuli", false);
assert_typeahead_equals("_@ zuli", false);
assert_typeahead_equals("@_ zuli", false);
assert_typeahead_equals(" @zuli", all_mentions);
assert_typeahead_equals(" _@zuli", all_mentions);
assert_typeahead_equals(" @_zuli", all_mentions);
assert_typeahead_equals("test @o", all_mentions);
assert_typeahead_equals("test _@k", all_mentions);
assert_typeahead_equals("test @_o", all_mentions);
assert_typeahead_equals("test @z", all_mentions);
assert_typeahead_equals("test _@z", all_mentions);
assert_typeahead_equals("test @_z", all_mentions);
assert_typeahead_equals(":", false);
assert_typeahead_equals(": ", false);

View File

@ -440,23 +440,17 @@ exports.compose_content_begins_typeahead = function (query) {
if (this.options.completions.mention && current_token[0] === '@') {
current_token = current_token.substring(1);
current_token = filter_mention_name(current_token);
if (!current_token) {
return false;
}
this.completing = 'mention';
this.token = current_token;
return get_mention_candidates_data();
// Silent mentions
if (current_token.startsWith('_')) {
this.completing = 'silent_mention';
current_token = current_token.substring(1);
}
if (this.options.completions.silent_mention && current_token.startsWith('_@')) {
// Ideally, we'd figure out a way to deduplicate this with the main mentions block.
current_token = current_token.substring(2);
current_token = filter_mention_name(current_token);
if (!current_token) {
this.completing = null;
return false;
}
this.completing = 'silent_mention';
this.token = current_token;
return get_mention_candidates_data();
}
@ -512,18 +506,13 @@ exports.content_typeahead_selected = function (item) {
beginning = beginning.substring(0, beginning.length - this.token.length - 1) + " :" + item.emoji_name + ": ";
}
} else if (this.completing === 'mention' || this.completing === 'silent_mention') {
beginning = beginning.substring(0, beginning.length - this.token.length - 1);
if (beginning.endsWith('@*')) {
beginning = beginning.substring(0, beginning.length - 2);
} else if (beginning.endsWith('@')) {
beginning = beginning.substring(0, beginning.length - 1);
}
var is_silent = this.completing === 'silent_mention';
if (user_groups.is_user_group(item)) {
beginning += '@*' + item.name + '* ';
beginning = '@*' + item.name + '* ';
$(document).trigger('usermention_completed.zulip', {user_group: item});
} else {
var mention_text = people.get_mention_syntax(item.full_name, item.user_id);
beginning += mention_text + ' ';
var mention_text = people.get_mention_syntax(item.full_name, item.user_id, is_silent);
beginning = mention_text + ' ';
$(document).trigger('usermention_completed.zulip', {mentioned: item});
}
} else if (this.completing === 'stream') {

View File

@ -850,8 +850,14 @@ exports.is_duplicate_full_name = function (full_name) {
return false;
};
exports.get_mention_syntax = function (full_name, user_id) {
var mention = '@**' + full_name;
exports.get_mention_syntax = function (full_name, user_id, silent) {
var mention = '';
if (silent) {
mention += '@_**';
} else {
mention += '@**';
}
mention += full_name;
if (!user_id) {
blueslip.warn('get_mention_syntax called without user_id.');
}