mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 14:03:30 +00:00 
			
		
		
		
	compose: Display error for non-admin trying to post to announce_only stream.
Partially fixes #4708. Implements a first version (v1) for the feature. The next step would be to allow admins to toggle `is_announcement_only` in the UI.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							9ad292cf2a
						
					
				
				
					commit
					10a65a62db
				
			@@ -296,6 +296,33 @@ run_test('validate_stream_message', () => {
 | 
			
		||||
    assert($("#compose-all-everyone").visible());
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
run_test('test_validate_stream_message_announcement_only', () => {
 | 
			
		||||
    // This test is in continuation with test_validate but it has been seperated out
 | 
			
		||||
    // for better readabilty. Their relative position of execution should not be changed.
 | 
			
		||||
    // Although the position with respect to test_validate_stream_message does not matter
 | 
			
		||||
    // as `get_announcement_only` is reset at the end.
 | 
			
		||||
    global.page_params.is_admin = false;
 | 
			
		||||
    var sub = {
 | 
			
		||||
        stream_id: 102,
 | 
			
		||||
        name: 'stream102',
 | 
			
		||||
        subscribed: true,
 | 
			
		||||
        announcement_only: true,
 | 
			
		||||
    };
 | 
			
		||||
    stream_data.get_announcement_only = function () {
 | 
			
		||||
        return true;
 | 
			
		||||
    };
 | 
			
		||||
    compose_state.subject('subject102');
 | 
			
		||||
    stream_data.add_sub('stream102', sub);
 | 
			
		||||
    assert(!compose.validate());
 | 
			
		||||
    assert.equal($('#compose-error-msg').html(), i18n.t("Only organization admins are allowed to post to this stream."));
 | 
			
		||||
 | 
			
		||||
    // reset `get_announcement_only` so that any tests occurung after this
 | 
			
		||||
    // do not reproduce this error.
 | 
			
		||||
    stream_data.get_announcement_only = function () {
 | 
			
		||||
        return false;
 | 
			
		||||
    };
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
run_test('markdown_shortcuts', () => {
 | 
			
		||||
    var queryCommandEnabled = true;
 | 
			
		||||
    var event = {
 | 
			
		||||
 
 | 
			
		||||
@@ -32,6 +32,7 @@ run_test('basics', () => {
 | 
			
		||||
        stream_id: 2,
 | 
			
		||||
        in_home_view: true,
 | 
			
		||||
        invite_only: true,
 | 
			
		||||
        is_announcement_only: true,
 | 
			
		||||
    };
 | 
			
		||||
    var test = {
 | 
			
		||||
        subscribed: true,
 | 
			
		||||
@@ -61,6 +62,9 @@ run_test('basics', () => {
 | 
			
		||||
 | 
			
		||||
    assert(stream_data.get_invite_only('social'));
 | 
			
		||||
    assert(!stream_data.get_invite_only('unknown'));
 | 
			
		||||
    assert(stream_data.get_announcement_only('social'));
 | 
			
		||||
    assert(!stream_data.get_announcement_only('unknown'));
 | 
			
		||||
 | 
			
		||||
    assert.equal(stream_data.get_color('social'), 'red');
 | 
			
		||||
    assert.equal(stream_data.get_color('unknown'), global.stream_color.default_color);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -511,6 +511,16 @@ function validate_stream_message_announce(stream_name) {
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function validate_stream_message_announcement_only(stream_name) {
 | 
			
		||||
    // Only allow realm admins to post to announcement_only streams.
 | 
			
		||||
    var is_announcement_only = stream_data.get_announcement_only(stream_name);
 | 
			
		||||
    if (is_announcement_only && !page_params.is_admin) {
 | 
			
		||||
        compose_error(i18n.t("Only organization admins are allowed to post to this stream."));
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
exports.validation_error = function (error_type, stream_name) {
 | 
			
		||||
    var response;
 | 
			
		||||
 | 
			
		||||
@@ -557,6 +567,10 @@ function validate_stream_message() {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!validate_stream_message_announcement_only(stream_name)) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // If both `@all` is mentioned and it's in `#announce`, just validate
 | 
			
		||||
    // for `@all`. Users shouldn't have to hit "yes" more than once.
 | 
			
		||||
    if (util.is_all_or_everyone_mentioned(compose_state.message_content()) &&
 | 
			
		||||
 
 | 
			
		||||
@@ -276,6 +276,14 @@ exports.get_invite_only = function (stream_name) {
 | 
			
		||||
    return sub.invite_only;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.get_announcement_only = function (stream_name) {
 | 
			
		||||
    var sub = exports.get_sub(stream_name);
 | 
			
		||||
    if (sub === undefined) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    return sub.is_announcement_only;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
var default_stream_ids = new Dict();
 | 
			
		||||
 | 
			
		||||
exports.set_realm_default_streams = function (realm_default_streams) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user