Skip to content

Commit

Permalink
stream settings: Stream_post_policy field for restricting reactions.
Browse files Browse the repository at this point in the history
Option to stream_post_policy is added for restricting posting and
reacting to admins only.

Fixes zulip#12835
  • Loading branch information
sahil839 committed Feb 16, 2020
1 parent 6a36ede commit 2372aa0
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 28 deletions.
37 changes: 35 additions & 2 deletions frontend_tests/node_tests/stream_data.js
Expand Up @@ -477,8 +477,8 @@ run_test('stream_settings', () => {
assert.equal(sub_rows[2].invite_only, false);

assert.equal(sub_rows[0].history_public_to_subscribers, true);
assert.equal(sub_rows[0].stream_post_policy ===
stream_data.stream_post_policy_values.admins.code, true);
assert.equal(sub_rows[0].stream_post_policy,
stream_data.stream_post_policy_values.admins.code);

const sub = stream_data.get_sub('a');
stream_data.update_stream_privacy(sub, {
Expand Down Expand Up @@ -991,3 +991,36 @@ run_test('all_topics_in_cache', () => {
sub.first_message_id = 2;
assert.equal(stream_data.all_topics_in_cache(sub), true);
});

run_test('get_restrict_emoji_reaction', () => {
const general = {
name: 'general',
stream_id: 1,
stream_post_policy: stream_data.stream_post_policy_values.everyone.code,
};

const test = {
name: 'test',
stream_id: 1,
stream_post_policy: stream_data.stream_post_policy_values.admins_can_post_and_react.code,
};

stream_data.add_sub(general);
stream_data.add_sub(test);

page_params.is_admin = false;

let restrict_emoji_reaction = stream_data.get_restrict_emoji_reaction('general');
assert.equal(restrict_emoji_reaction, false);

restrict_emoji_reaction = stream_data.get_restrict_emoji_reaction('test');
assert.equal(restrict_emoji_reaction, true);

page_params.is_admin = true;

restrict_emoji_reaction = stream_data.get_restrict_emoji_reaction('general');
assert.equal(restrict_emoji_reaction, false);

restrict_emoji_reaction = stream_data.get_restrict_emoji_reaction('test');
assert.equal(restrict_emoji_reaction, false);
});
2 changes: 2 additions & 0 deletions frontend_tests/node_tests/stream_events.js
Expand Up @@ -3,6 +3,7 @@ const return_true = function () { return true; };
set_global('$', global.make_zjquery());
set_global('document', 'document-stub');
set_global('i18n', global.stub_i18n);
set_global('current_msg_list', {rerender: noop});

set_global('colorspace', {
sRGB_to_linear: noop,
Expand All @@ -14,6 +15,7 @@ set_global('colorspace', {
zrequire('people');
zrequire('stream_data');
zrequire('stream_events');

const with_overrides = global.with_overrides;

const george = {
Expand Down
12 changes: 12 additions & 0 deletions static/generated/pygments_data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions static/js/click_handlers.js
Expand Up @@ -191,7 +191,10 @@ exports.initialize = function () {
e.stopPropagation();
const local_id = $(this).attr('data-reaction-id');
const message_id = rows.get_message_id(this);
reactions.process_reaction_click(message_id, local_id);
const message = current_msg_list.get(message_id);
if (!message.is_stream || !stream_data.get_restrict_emoji_reaction(message.stream)) {
reactions.process_reaction_click(message_id, local_id);
}
$(".tooltip").remove();
});

Expand Down Expand Up @@ -230,7 +233,10 @@ exports.initialize = function () {
const local_id = elem.attr('data-reaction-id');
const message_id = rows.get_message_id(e.currentTarget);
const title = reactions.get_reaction_title_data(message_id, local_id);

const message = current_msg_list.get(message_id);
if (message.is_stream && stream_data.get_restrict_emoji_reaction(message.stream)) {
$(this).closest(".message_reaction").find(".disable-reaction-button").show();
}
elem.tooltip({
title: title,
trigger: 'hover',
Expand All @@ -246,6 +252,7 @@ exports.initialize = function () {
$('#main_div').on('mouseleave', '.message_reaction', function (e) {
e.stopPropagation();
$(e.currentTarget).tooltip('destroy');
$(this).closest(".message_reaction").find(".disable-reaction-button").hide();
});

// DESTROY PERSISTING TOOLTIPS ON HOVER
Expand Down
3 changes: 2 additions & 1 deletion static/js/compose.js
Expand Up @@ -493,7 +493,8 @@ function validate_stream_message_post_policy(stream_name) {
const stream_post_permission_type = stream_data.stream_post_policy_values;
const stream_post_policy = stream_data.get_stream_post_policy(stream_name);

if (stream_post_policy === stream_post_permission_type.admins.code) {
if (stream_post_policy === stream_post_permission_type.admins.code ||
stream_post_policy === stream_post_permission_type.admins_can_post_and_react.code) {
compose_error(i18n.t("Only organization admins are allowed to post to this stream."));
return false;
}
Expand Down
12 changes: 10 additions & 2 deletions static/js/emoji_picker.js
Expand Up @@ -684,14 +684,21 @@ exports.register_click_handlers = function () {

$("#main_div").on("click", ".reaction_button", function (e) {
e.stopPropagation();

const message_id = rows.get_message_id(this);
exports.toggle_emoji_popover(this, message_id);
const message = current_msg_list.get(message_id);
if (!message.is_stream || !stream_data.get_restrict_emoji_reaction(message.stream)) {
exports.toggle_emoji_popover(this, message_id);
}
});

$("#main_div").on("mouseenter", ".reaction_button", function (e) {
e.stopPropagation();

const message_id = rows.get_message_id(this);
const message = current_msg_list.get(message_id);
if (message.is_stream && stream_data.get_restrict_emoji_reaction(message.stream)) {
$(this).find(".disable-emoji-icon").show();
}
const elem = $(e.currentTarget);
const title = i18n.t("Add emoji reaction");
elem.tooltip({
Expand All @@ -706,6 +713,7 @@ exports.register_click_handlers = function () {

$('#main_div').on('mouseleave', '.reaction_button', function (e) {
e.stopPropagation();
$(this).find(".disable-emoji-icon").hide();
$(e.currentTarget).tooltip('hide');
});

Expand Down
2 changes: 2 additions & 0 deletions static/js/message_list_view.js
Expand Up @@ -352,6 +352,8 @@ MessageListView.prototype = {
if (message_container.msg.stream) {
message_container.background_color =
stream_data.get_color(message_container.msg.stream);
message_container.restrict_emoji_reaction =
stream_data.get_restrict_emoji_reaction(message_container.msg.stream);
}

message_container.contains_mention = message_container.msg.mentioned;
Expand Down
12 changes: 11 additions & 1 deletion static/js/popovers.js
Expand Up @@ -101,6 +101,16 @@ function calculate_info_popover_placement(size, elt) {
}
}

function show_add_reaction_option(message) {
if (!message.sent_by_me) {
return false;
}
if (message.stream && stream_data.get_restrict_emoji_reaction(message.stream)) {
return false;
}
return true;
}

function get_custom_profile_field_data(user, field, field_types, dateFormat) {
const field_value = people.get_custom_profile_data(user.user_id, field.id);
const field_type = field.type;
Expand Down Expand Up @@ -478,7 +488,7 @@ exports.toggle_actions_popover = function (element, id) {
can_unmute_topic: can_unmute_topic,
should_display_collapse: should_display_collapse,
should_display_uncollapse: should_display_uncollapse,
should_display_add_reaction_option: message.sent_by_me,
should_display_add_reaction_option: show_add_reaction_option(message),
should_display_edit_history_option: should_display_edit_history_option,
conversation_time_uri: conversation_time_uri,
narrowed: narrow_state.active(),
Expand Down
13 changes: 13 additions & 0 deletions static/js/stream_data.js
Expand Up @@ -103,6 +103,10 @@ exports.stream_post_policy_values = {
code: 3,
description: i18n.t("Only organization full members can post"),
},
admins_can_post_and_react: {
code: 4,
description: i18n.t("Only organization administrators can post and react"),
},
};

exports.clear_subscriptions = function () {
Expand Down Expand Up @@ -519,6 +523,15 @@ exports.get_stream_post_policy = function (stream_name) {
return sub.stream_post_policy;
};

exports.get_restrict_emoji_reaction = function (stream_name) {
const stream_post_policy = exports.get_stream_post_policy(stream_name);
if (stream_post_policy === exports.stream_post_policy_values.admins_can_post_and_react.code
&& !page_params.is_admin) {
return true;
}
return false;
};

exports.all_topics_in_cache = function (sub) {
// Checks whether this browser's cache of contiguous messages
// (used to locally render narrows) in message_list.all has all
Expand Down
1 change: 1 addition & 0 deletions static/js/stream_events.js
Expand Up @@ -55,6 +55,7 @@ exports.update_property = function (stream_id, property, value, other_values) {
break;
case 'stream_post_policy':
subs.update_stream_post_policy(sub, value);
current_msg_list.rerender();
break;
default:
blueslip.warn("Unexpected subscription property type", {property: property,
Expand Down
9 changes: 8 additions & 1 deletion static/js/stream_ui_updates.js
Expand Up @@ -143,7 +143,14 @@ exports.update_stream_privacy_type_icon = function (sub) {

exports.update_stream_subscription_type_text = function (sub) {
const stream_settings = stream_edit.settings_for_sub(sub);
const html = render_subscription_type(sub);
const template_data = {
invite_only: sub.invite_only,
history_public_to_subscribers: sub.history_public_to_subscribers,
is_web_public: sub.is_web_public,
stream_post_policy: sub.stream_post_policy,
stream_post_policy_values: stream_data.stream_post_policy_values,
};
const html = render_subscription_type(template_data);
if (stream_edit.is_sub_settings_active(sub)) {
stream_settings.find('.subscription-type-text').expectOne().html(html);
}
Expand Down

0 comments on commit 2372aa0

Please sign in to comment.