Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom realm filters #544

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions frontend_tests/casper_tests/11-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ casper.waitWhileSelector('.emoji_row', function () {
casper.test.assertDoesntExist('.emoji_row');
});

// Test custom realm filters
casper.waitForSelector('.admin-filter-form', function () {
casper.fill('form.admin-filter-form', {
'pattern': '#(?P<id>[0-9]+)',
'url_format_string': 'https://trac.zulip.net/ticket/%(id)s'
});
casper.click('form.admin-filter-form input.btn');
});

casper.waitUntilVisible('div#admin-filter-status', function () {
casper.test.assertSelectorHasText('div#admin-filter-status', 'Custom filter added!');
});

casper.waitForSelector('.filter_row', function () {
casper.test.assertSelectorHasText('.filter_row span.filter_pattern', '#(?P<id>[0-9]+)');
casper.test.assertSelectorHasText('.filter_row span.filter_url_format_string', 'https://trac.zulip.net/ticket/%(id)s');
casper.click('.filter_row button');
});

casper.waitWhileSelector('.filter_row', function () {
casper.test.assertDoesntExist('.filter_row');
});

casper.waitForSelector('.admin-filter-form', function () {
casper.fill('form.admin-filter-form', {
'pattern': 'a',
'url_format_string': 'https://trac.zulip.net/ticket/%(id)s'
});
casper.click('form.admin-filter-form input.btn');
});

casper.waitUntilVisible('div#admin-filter-pattern-status', function () {
casper.test.assertSelectorHasText('div#admin-filter-pattern-status', 'Failed: Filter pattern cannot start with `a`, use one of the valid prefixes: #');
});

// TODO: Test stream deletion

common.then_log_out();
Expand Down
23 changes: 23 additions & 0 deletions frontend_tests/node_tests/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,29 @@ function render(template_name, args) {
assert.equal(emoji_url.attr('src'), 'http://emojipedia-us.s3.amazonaws.com/cache/46/7f/467fe69069c408e07517621f263ea9b5.png');
}());

(function admin_filter_list() {
global.use_template('admin_filter_list');
var args = {
filter: {
"pattern": "#(?P<id>[0-9]+)",
"url_format_string": "https://trac.humbughq.com/ticket/%(id)s"
}
};

var html = '';
html += '<tbody id="admin_filters_table">';
html += render('admin_filter_list', args);
html += '</tbody>';

global.write_test_output('admin_filter_list.handlebars', html);

var filter_pattern = $(html).find('tr.filter_row:first span.filter_pattern');
var filter_format = $(html).find('tr.filter_row:first span.filter_url_format_string');

assert.equal(filter_pattern.text(), '#(?P<id>[0-9]+)');
assert.equal(filter_format.text(), 'https://trac.humbughq.com/ticket/%(id)s');
}());

// By the end of this test, we should have compiled all our templates. Ideally,
// we will also have exercised them to some degree, but that's a little trickier
// to enforce.
Expand Down
86 changes: 86 additions & 0 deletions static/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ exports.populate_emoji = function (emoji_data) {
loading.destroy_indicator($('#admin_page_emoji_loading_indicator'));
};

exports.populate_filters = function (filters_data) {
var filters_table = $("#admin_filters_table").expectOne();
filters_table.find("tr.filter_row").remove();
_.each(filters_data, function (filter) {
filters_table.append(
templates.render(
"admin_filter_list", {
filter: {
pattern: filter[0],
url_format_string: filter[1],
id: filter[2]
}
}
)
);
});
loading.destroy_indicator($('#admin_page_filters_loading_indicator'));
};

exports.setup_page = function () {
var options = {
realm_name: page_params.realm_name,
Expand All @@ -102,13 +121,18 @@ exports.setup_page = function () {
$("#admin-emoji-status").expectOne().hide();
$("#admin-emoji-name-status").expectOne().hide();
$("#admin-emoji-url-status").expectOne().hide();
$("#admin-filter-status").expectOne().hide();
$("#admin-filter-pattern-status").expectOne().hide();
$("#admin-filter-format-status").expectOne().hide();


// create loading indicators
loading.make_indicator($('#admin_page_users_loading_indicator'));
loading.make_indicator($('#admin_page_bots_loading_indicator'));
loading.make_indicator($('#admin_page_streams_loading_indicator'));
loading.make_indicator($('#admin_page_deactivated_users_loading_indicator'));
loading.make_indicator($('#admin_page_emoji_loading_indicator'));
loading.make_indicator($('#admin_page_filters_loading_indicator'));

// Populate users and bots tables
channel.get({
Expand All @@ -131,6 +155,9 @@ exports.setup_page = function () {
// Populate emoji table
exports.populate_emoji(page_params.realm_emoji);

// Populate filters table
exports.populate_filters(page_params.realm_filters);

// Setup click handlers
$(".admin_user_table").on("click", ".deactivate", function (e) {
e.preventDefault();
Expand Down Expand Up @@ -489,6 +516,65 @@ exports.setup_page = function () {
});
});

$('.admin_filters_table').on('click', '.delete', function (e) {
e.preventDefault();
e.stopPropagation();
var btn = $(this);

channel.del({
url: '/json/realm/filters/' + encodeURIComponent(btn.attr('data-filter-id')),
error: function (xhr, error_type) {
if (xhr.status.toString().charAt(0) === "4") {
btn.closest("td").html(
$("<p>").addClass("text-error").text($.parseJSON(xhr.responseText).msg)
);
} else {
btn.text("Failed!");
}
},
success: function () {
var row = btn.parents('tr');
row.remove();
}
});
});

$(".administration").on("submit", "form.admin-filter-form", function (e) {
e.preventDefault();
e.stopPropagation();
var filter_status = $('#admin-filter-status');
var pattern_status = $('#admin-filter-pattern-status');
var format_status = $('#admin-filter-format-status');
filter_status.hide();
pattern_status.hide();
format_status.hide();
var filter = {};
$(this).serializeArray().map(function (x){filter[x.name] = x.value;});

channel.put({
url: "/json/realm/filters",
data: $(this).serialize(),
success: function (data) {
filter.id = data.id;
ui.report_success("Custom filter added!", filter_status);
},
error: function (xhr, error) {
var errors = $.parseJSON(xhr.responseText).msg;
if (errors.pattern !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.pattern});
ui.report_error("Failed", xhr, pattern_status);
}
if (errors.url_format_string !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.url_format_string});
ui.report_error("Failed", xhr, format_status);
}
if (errors.__all__ !== undefined) {
xhr.responseText = JSON.stringify({msg: errors.__all__});
ui.report_error("Failed", xhr, filter_status);
}
}
});
});
};

return exports;
Expand Down
1 change: 1 addition & 0 deletions static/js/server_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ function get_events_success(events) {
case 'realm_filters':
page_params.realm_filters = event.realm_filters;
echo.set_realm_filters(page_params.realm_filters);
admin.populate_filters(event.realm_filters);
break;
case 'update_global_notifications':
notifications.handle_global_notification_updates(event.notification_name,
Expand Down
38 changes: 31 additions & 7 deletions static/styles/zulip.css
Original file line number Diff line number Diff line change
Expand Up @@ -3221,7 +3221,8 @@ div.edit_bot {

.edit_bot_form .control-label,
#create_bot_form .control-label,
.admin-emoji-form .control-label {
.admin-emoji-form .control-label,
.admin-filter-form .control-label {
width: 10em;
text-align: right;
margin-right: 20px;
Expand Down Expand Up @@ -3382,12 +3383,14 @@ div.edit_bot {
#administration .settings-section .admin-realm-form,
#settings .settings-section .new-bot-form,
#emoji-settings .new-emoji-form,
#filter-settings .new-filter-form,
#settings .settings-section .edit-bot-form-box {
margin-top: 35px;
}

#settings .settings-section .new-bot-section-title,
#emoji-settings .new-emoji-section-title {
#emoji-settings .new-emoji-section-title,
#filter-settings .new-filter-section-title {
top: 20px;
left: 20px;
}
Expand All @@ -3401,13 +3404,15 @@ div.edit_bot {
#settings .settings-section .account-settings-form .control-label,
#settings .settings-section .new-bot-form .control-label,
#emoji-settings .new-emoji-form .control-label,
#filter-settings .new-filter-form .control-label,
#settings .settings-section .edit-bot-form-box .control-label {
width: 120px;
}

#settings .settings-section .account-settings-form .controls,
#settings .settings-section .new-bot-form .controls,
#emoji-settings .new-emoji-form .controls,
#filter-settings .new-filter-form .controls,
#settings .settings-section .edit-bot-form-box .controls {
margin-left: 140px;
}
Expand Down Expand Up @@ -3476,7 +3481,8 @@ div.edit_bot {

#settings .bot-information-box,
#settings .add-new-bot-box,
#emoji-settings .add-new-emoji-box {
#emoji-settings .add-new-emoji-box,
#filter-settings .add-new-filter-box {
background: #e3e3e3;
padding: 10px;
margin-left: 38px;
Expand All @@ -3488,7 +3494,8 @@ div.edit_bot {
}

#settings .add-new-bot-box,
#emoji-settings .add-new-emoji-box {
#emoji-settings .add-new-emoji-box,
#filter-settings .add-new-filter-box {
background: #cbe3cb;
}

Expand Down Expand Up @@ -3539,6 +3546,7 @@ div.edit_bot {

#settings .settings-section .new-bot-form .control-label,
#emoji-settings .new-emoji-form .control-label,
#filter-settings .new-filter-form .control-label,
#settings .settings-section .edit-bot-form-box .control-label {
float: left;
width: 120px;
Expand All @@ -3547,7 +3555,8 @@ div.edit_bot {
}

#settings .settings-section .new-bot-form .controls,
#emoji-settings .new-emoji-form .controls {
#emoji-settings .new-emoji-form .controls,
#filter-settings .new-filter-form .controls {
margin-left: 110px;
}

Expand All @@ -3563,6 +3572,7 @@ div.edit_bot {
#settings .settings-section .account-settings-form,
#settings .settings-section .new-bot-form,
#emoji-settings .new-emoji-form,
#filter-settings .new-filter-form,
#settings .settings-section .notification-settings-form,
#settings .settings-section .display-settings-form,
#settings .settings-section .edit-bot-form-box {
Expand All @@ -3573,6 +3583,7 @@ div.edit_bot {
#settings .settings-section .account-settings-form .control-label,
#settings .settings-section .new-bot-form .control-label,
#emoji-settings .new-emoji-form .control-label,
#filter-settings .new-filter-form .control-label,
#settings .settings-section .edit-bot-form-box .control-label {
display: block;
width: 120px;
Expand All @@ -3587,6 +3598,7 @@ div.edit_bot {
#settings .settings-section .account-settings-form .controls,
#settings .settings-section .new-bot-form .controls,
#emoji-settings .new-emoji-form .controls,
#filter-settings .new-filter-form .controls,
#settings .settings-section .edit-bot-form-box .controls {
margin: auto;
text-align: center;
Expand All @@ -3598,15 +3610,18 @@ div.edit_bot {
#settings .settings-section .account-settings-form .controls input,
#settings .settings-section .new-bot-form .controls button,
#emoji-settings .new-emoji-form .controls button,
#filter-settings .new-filter-form .controls button,
#settings .settings-section .edit-bot-form-box .controls button,
#settings .settings-section .new-bot-form .controls input,
#emoji-settings .new-emoji-form .controls input,
#filter-settings .new-filter-form .controls input,
#settings .settings-section .edit-bot-form-box .controls input {
margin: auto;
}

#settings .settings-section .new-bot-form,
#emoji-settings .new-emoji-form {
#emoji-settings .new-emoji-form,
#filter-settings .new-filter-form {
padding: 0px;
width: 100%;
text-align: center;
Expand Down Expand Up @@ -4233,7 +4248,8 @@ li.show-more-private-messages a {

}

.admin_emoji_table {
.admin_emoji_table,
.admin_filters_table {
margin: 20px auto;
}

Expand All @@ -4255,3 +4271,11 @@ li.show-more-private-messages a {
#emoji-settings .new-emoji-form #emoji_url {
width: 60%;
}

.admin_filters_table {
margin-top: 20px;
}

#admin-filter-pattern-status, #admin-filter-format-status {
margin: 20px 0 0 0;
}
15 changes: 15 additions & 0 deletions static/templates/admin_filter_list.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{#with filter}}
<tr class="filter_row">
<td>
<span class="filter_pattern">{{pattern}}</span>
</td>
<td>
<span class="filter_url_format_string">{{url_format_string}}</span>
</td>
<td>
<button class="btn delete btn-danger" data-filter-id="{{id}}">
Delete
</button>
</td>
</tr>
{{/with}}
Loading