mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	stream_data.js: Replace user_email with user_id in func is_user_subscribed.
This commit is contained in:
		@@ -26,11 +26,11 @@ var bob = {
 | 
			
		||||
    full_name: 'Bob',
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
people.add(me);
 | 
			
		||||
people.add_in_realm(me);
 | 
			
		||||
people.initialize_current_user(me.user_id);
 | 
			
		||||
 | 
			
		||||
people.add(alice);
 | 
			
		||||
people.add(bob);
 | 
			
		||||
people.add_in_realm(alice);
 | 
			
		||||
people.add_in_realm(bob);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
(function test_set_focused_recipient() {
 | 
			
		||||
@@ -65,6 +65,7 @@ people.add(bob);
 | 
			
		||||
    assert(compose_fade.would_receive_message('me@example.com'));
 | 
			
		||||
    assert(compose_fade.would_receive_message('alice@example.com'));
 | 
			
		||||
    assert(!compose_fade.would_receive_message('bob@example.com'));
 | 
			
		||||
    assert.equal(compose_fade.would_receive_message('nonrealmuser@example.com'), undefined);
 | 
			
		||||
 | 
			
		||||
    var good_msg = {
 | 
			
		||||
        type: 'stream',
 | 
			
		||||
 
 | 
			
		||||
@@ -169,26 +169,24 @@ zrequire('marked', 'third/marked/lib/marked');
 | 
			
		||||
 | 
			
		||||
    stream_data.set_subscribers(sub, [fred.user_id, george.user_id]);
 | 
			
		||||
    stream_data.update_calculated_fields(sub);
 | 
			
		||||
    assert(stream_data.user_is_subscribed('Rome', 'FRED@zulip.com'));
 | 
			
		||||
    assert(stream_data.user_is_subscribed('Rome', 'fred@zulip.com'));
 | 
			
		||||
    assert(stream_data.user_is_subscribed('Rome', 'george@zulip.com'));
 | 
			
		||||
    assert(!stream_data.user_is_subscribed('Rome', 'not_fred@zulip.com'));
 | 
			
		||||
    assert(stream_data.is_user_subscribed('Rome', fred.user_id));
 | 
			
		||||
    assert(stream_data.is_user_subscribed('Rome', george.user_id));
 | 
			
		||||
    assert(!stream_data.is_user_subscribed('Rome', not_fred.user_id));
 | 
			
		||||
 | 
			
		||||
    stream_data.set_subscribers(sub, []);
 | 
			
		||||
 | 
			
		||||
    var email = 'brutus@zulip.com';
 | 
			
		||||
    var brutus = {
 | 
			
		||||
        email: email,
 | 
			
		||||
        email: 'brutus@zulip.com',
 | 
			
		||||
        full_name: 'Brutus',
 | 
			
		||||
        user_id: 104,
 | 
			
		||||
    };
 | 
			
		||||
    people.add(brutus);
 | 
			
		||||
    assert(!stream_data.user_is_subscribed('Rome', email));
 | 
			
		||||
    assert(!stream_data.is_user_subscribed('Rome', brutus.user_id));
 | 
			
		||||
 | 
			
		||||
    // add
 | 
			
		||||
    var ok = stream_data.add_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert(ok);
 | 
			
		||||
    assert(stream_data.user_is_subscribed('Rome', email));
 | 
			
		||||
    assert(stream_data.is_user_subscribed('Rome', brutus.user_id));
 | 
			
		||||
    sub = stream_data.get_sub('Rome');
 | 
			
		||||
    stream_data.update_subscribers_count(sub);
 | 
			
		||||
    assert.equal(sub.subscriber_count, 1);
 | 
			
		||||
@@ -198,7 +196,7 @@ zrequire('marked', 'third/marked/lib/marked');
 | 
			
		||||
 | 
			
		||||
    // verify that adding an already-added subscriber is a noop
 | 
			
		||||
    stream_data.add_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert(stream_data.user_is_subscribed('Rome', email));
 | 
			
		||||
    assert(stream_data.is_user_subscribed('Rome', brutus.user_id));
 | 
			
		||||
    sub = stream_data.get_sub('Rome');
 | 
			
		||||
    stream_data.update_subscribers_count(sub);
 | 
			
		||||
    assert.equal(sub.subscriber_count, 1);
 | 
			
		||||
@@ -206,20 +204,16 @@ zrequire('marked', 'third/marked/lib/marked');
 | 
			
		||||
    // remove
 | 
			
		||||
    ok = stream_data.remove_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert(ok);
 | 
			
		||||
    assert(!stream_data.user_is_subscribed('Rome', email));
 | 
			
		||||
    assert(!stream_data.is_user_subscribed('Rome', brutus.user_id));
 | 
			
		||||
    sub = stream_data.get_sub('Rome');
 | 
			
		||||
    stream_data.update_subscribers_count(sub);
 | 
			
		||||
    assert.equal(sub.subscriber_count, 0);
 | 
			
		||||
 | 
			
		||||
    // verify that checking subscription with bad email is a noop
 | 
			
		||||
    var bad_email = 'notbrutus@zulip.org';
 | 
			
		||||
    global.blueslip.error = function (msg) {
 | 
			
		||||
        assert.equal(msg, "Unknown email for get_user_id: " + bad_email);
 | 
			
		||||
    };
 | 
			
		||||
    // verify that checking subscription with undefined user id
 | 
			
		||||
    global.blueslip.warn = function (msg) {
 | 
			
		||||
        assert.equal(msg, "Bad email passed to user_is_subscribed: " + bad_email);
 | 
			
		||||
        assert.equal(msg, "Undefined user_id passed to function is_user_subscribed");
 | 
			
		||||
    };
 | 
			
		||||
    assert(!stream_data.user_is_subscribed('Rome', bad_email));
 | 
			
		||||
    assert.equal(stream_data.is_user_subscribed('Rome', undefined), undefined);
 | 
			
		||||
 | 
			
		||||
    // Verify noop for bad stream when removing subscriber
 | 
			
		||||
    var bad_stream = 'UNKNOWN';
 | 
			
		||||
@@ -236,7 +230,7 @@ zrequire('marked', 'third/marked/lib/marked');
 | 
			
		||||
    // verify that removing an already-removed subscriber is a noop
 | 
			
		||||
    ok = stream_data.remove_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert(!ok);
 | 
			
		||||
    assert(!stream_data.user_is_subscribed('Rome', email));
 | 
			
		||||
    assert(!stream_data.is_user_subscribed('Rome', brutus.user_id));
 | 
			
		||||
    sub = stream_data.get_sub('Rome');
 | 
			
		||||
    stream_data.update_subscribers_count(sub);
 | 
			
		||||
    assert.equal(sub.subscriber_count, 0);
 | 
			
		||||
@@ -247,24 +241,24 @@ zrequire('marked', 'third/marked/lib/marked');
 | 
			
		||||
    stream_data.add_sub('Rome', sub);
 | 
			
		||||
    stream_data.add_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    sub.subscribed = true;
 | 
			
		||||
    assert(stream_data.user_is_subscribed('Rome', email));
 | 
			
		||||
    assert(stream_data.is_user_subscribed('Rome', brutus.user_id));
 | 
			
		||||
 | 
			
		||||
    // Verify that we noop and don't crash when unsubscribed.
 | 
			
		||||
    sub.subscribed = false;
 | 
			
		||||
    stream_data.update_calculated_fields(sub);
 | 
			
		||||
    ok = stream_data.add_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert(ok);
 | 
			
		||||
    assert.equal(stream_data.user_is_subscribed('Rome', email), true);
 | 
			
		||||
    assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), true);
 | 
			
		||||
    stream_data.remove_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert.equal(stream_data.user_is_subscribed('Rome', email), false);
 | 
			
		||||
    assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), false);
 | 
			
		||||
    stream_data.add_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert.equal(stream_data.user_is_subscribed('Rome', email), true);
 | 
			
		||||
    assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), true);
 | 
			
		||||
 | 
			
		||||
    sub.invite_only = true;
 | 
			
		||||
    stream_data.update_calculated_fields(sub);
 | 
			
		||||
    assert.equal(stream_data.user_is_subscribed('Rome', email), undefined);
 | 
			
		||||
    assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), undefined);
 | 
			
		||||
    stream_data.remove_subscriber('Rome', brutus.user_id);
 | 
			
		||||
    assert.equal(stream_data.user_is_subscribed('Rome', email), undefined);
 | 
			
		||||
    assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), undefined);
 | 
			
		||||
 | 
			
		||||
    // Verify that we don't crash and return false for a bad stream.
 | 
			
		||||
    ok = stream_data.add_subscriber('UNKNOWN', brutus.user_id);
 | 
			
		||||
 
 | 
			
		||||
@@ -118,8 +118,8 @@ exports.would_receive_message = function (email) {
 | 
			
		||||
    if (focused_recipient.type === 'stream') {
 | 
			
		||||
        var user = people.get_active_user_for_email(email);
 | 
			
		||||
        var sub = stream_data.get_sub(focused_recipient.stream);
 | 
			
		||||
        if (!sub) {
 | 
			
		||||
            // If the stream isn't valid, there is no risk of a mix
 | 
			
		||||
        if (!sub || !user) {
 | 
			
		||||
            // If the stream or user isn't valid, there is no risk of a mix
 | 
			
		||||
            // yet, so don't fade.
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -129,7 +129,7 @@ exports.would_receive_message = function (email) {
 | 
			
		||||
            // not subscribed.
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        return stream_data.user_is_subscribed(focused_recipient.stream, email);
 | 
			
		||||
        return stream_data.is_user_subscribed(focused_recipient.stream, user.user_id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // PM, so check if the given email is in the recipients list.
 | 
			
		||||
 
 | 
			
		||||
@@ -365,18 +365,17 @@ exports.remove_subscriber = function (stream_name, user_id) {
 | 
			
		||||
    return true;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.user_is_subscribed = function (stream_name, user_email) {
 | 
			
		||||
exports.is_user_subscribed = function (stream_name, user_id) {
 | 
			
		||||
    var sub = exports.get_sub(stream_name);
 | 
			
		||||
    if (typeof sub === 'undefined' || !sub.can_access_subscribers) {
 | 
			
		||||
        // If we don't know about the stream, or we ourselves cannot access subscriber list,
 | 
			
		||||
        // so we return undefined (treated as falsy if not explicitly handled).
 | 
			
		||||
        blueslip.warn("We got a user_is_subscribed call for a non-existent or inaccessible stream.");
 | 
			
		||||
        blueslip.warn("We got a is_user_subscribed call for a non-existent or inaccessible stream.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    var user_id = people.get_user_id(user_email);
 | 
			
		||||
    if (!user_id) {
 | 
			
		||||
        blueslip.warn("Bad email passed to user_is_subscribed: " + user_email);
 | 
			
		||||
        return false;
 | 
			
		||||
    if (typeof user_id === "undefined") {
 | 
			
		||||
        blueslip.warn("Undefined user_id passed to function is_user_subscribed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return sub.subscribers.has(user_id);
 | 
			
		||||
 
 | 
			
		||||
@@ -213,7 +213,7 @@ function show_subscription_settings(sub_row) {
 | 
			
		||||
            // Case-insensitive.
 | 
			
		||||
            var item_matches = (item.email.toLowerCase().indexOf(query) !== -1) ||
 | 
			
		||||
                               (item.full_name.toLowerCase().indexOf(query) !== -1);
 | 
			
		||||
            var is_subscribed = stream_data.user_is_subscribed(sub.name, item.email);
 | 
			
		||||
            var is_subscribed = stream_data.is_user_subscribed(sub.name, item.user_id);
 | 
			
		||||
            return item_matches && !is_subscribed;
 | 
			
		||||
        },
 | 
			
		||||
        sorter: function (matches) {
 | 
			
		||||
 
 | 
			
		||||
@@ -186,8 +186,8 @@ function compare_for_at_mentioning(person_a, person_b, tertiary_compare, current
 | 
			
		||||
 | 
			
		||||
    // give preference to subscribed users first
 | 
			
		||||
    if (current_stream !== undefined) {
 | 
			
		||||
        var a_is_sub = stream_data.user_is_subscribed(current_stream, person_a.email);
 | 
			
		||||
        var b_is_sub = stream_data.user_is_subscribed(current_stream, person_b.email);
 | 
			
		||||
        var a_is_sub = stream_data.is_user_subscribed(current_stream, person_a.user_id);
 | 
			
		||||
        var b_is_sub = stream_data.is_user_subscribed(current_stream, person_b.user_id);
 | 
			
		||||
 | 
			
		||||
        if (a_is_sub && !b_is_sub) {
 | 
			
		||||
            return -1;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user