soft_deactivation: Stop creating UserMessage rows when soft deactivated.

This commit is contained in:
Aditya Bansal 2017-07-18 19:33:47 +05:30 committed by Tim Abbott
parent ce670a7892
commit 658a14d0bb
2 changed files with 152 additions and 1 deletions

View File

@ -727,6 +727,7 @@ def get_recipient_user_profiles(recipient, sender_id):
'user_profile__is_active', 'user_profile__is_active',
'user_profile__is_bot', 'user_profile__is_bot',
'user_profile__bot_type', 'user_profile__bot_type',
'user_profile__long_term_idle',
] ]
query = Subscription.objects.select_related("user_profile").only(*fields).filter( query = Subscription.objects.select_related("user_profile").only(*fields).filter(
recipient=recipient, active=True) recipient=recipient, active=True)
@ -818,7 +819,16 @@ def do_send_messages(messages_maybe_none):
um.flags |= UserMessage.flags.is_me_message um.flags |= UserMessage.flags.is_me_message
user_message_flags[message['message'].id][um.user_profile_id] = um.flags_list() user_message_flags[message['message'].id][um.user_profile_id] = um.flags_list()
ums.extend(ums_to_create)
user_messages = []
for um in ums_to_create:
if (um.user_profile.long_term_idle and
um.message.recipient.type == Recipient.STREAM and
int(um.flags) == 0):
continue
user_messages.append(um)
ums.extend(user_messages)
# Prepare to collect service queue events triggered by the message. # Prepare to collect service queue events triggered by the message.
message['message'].service_queue_events = defaultdict(list) message['message'].service_queue_events = defaultdict(list)

View File

@ -9,6 +9,7 @@ from zerver.lib import bugdown
from zerver.decorator import JsonableError from zerver.decorator import JsonableError
from zerver.lib.test_runner import slow from zerver.lib.test_runner import slow
from zerver.lib.cache import get_stream_cache_key, cache_delete from zerver.lib.cache import get_stream_cache_key, cache_delete
from zerver.lib.str_utils import force_text
from zilencer.models import Deployment from zilencer.models import Deployment
from zerver.lib.message import ( from zerver.lib.message import (
@ -47,6 +48,7 @@ from zerver.lib.actions import (
extract_recipients, extract_recipients,
do_create_user, do_create_user,
get_client, get_client,
do_add_alert_words,
) )
from zerver.lib.upload import create_attachment from zerver.lib.upload import create_attachment
@ -2276,3 +2278,142 @@ class SoftDeactivationMessageTest(ZulipTestCase):
self.assertEqual(len(idle_user_msg_list), idle_user_msg_count + 2) self.assertEqual(len(idle_user_msg_list), idle_user_msg_count + 2)
for sent_message in sent_message_list: for sent_message in sent_message_list:
self.assertEqual(idle_user_msg_list.pop(), sent_message) self.assertEqual(idle_user_msg_list.pop(), sent_message)
def test_user_message_filter(self):
# type: () -> None
# In this test we are basically testing out the logic used out in
# do_send_messages() in action.py for filtering the messages for which
# UserMessage rows should be created for a soft-deactivated user.
recipient_list = [
self.example_email("hamlet"),
self.example_email("iago"),
self.example_email('cordelia')
]
for email in recipient_list:
self.subscribe_to_stream(email, "Denmark")
cordelia = self.example_user('cordelia')
sender = self.example_email('iago')
stream_name = 'Denmark'
subject = 'foo'
def send_stream_message(content):
# type: (str) -> None
self.send_message(sender, stream_name, Recipient.STREAM,
content, subject)
def send_personal_message(content):
# type: (str) -> None
self.send_message(sender, self.example_email("hamlet"),
Recipient.PERSONAL, content)
long_term_idle_user = self.example_user('hamlet')
do_soft_deactivate_users([long_term_idle_user])
def assert_um_count(user, count):
# type: (UserProfile, int) -> None
user_messages = get_user_messages(user)
self.assertEqual(len(user_messages), count)
def assert_last_um_content(user, content, negate=False):
# type: (UserProfile, Text, bool) -> None
user_messages = get_user_messages(user)
if negate:
self.assertNotEqual(user_messages[-1].content, content)
else:
self.assertEqual(user_messages[-1].content, content)
# Test that sending a message to a stream with soft deactivated user
# doesn't end up creating UserMessage row for deactivated user.
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Test Message 1'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message), negate=True)
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
# Test sending a private message to soft deactivated user creates
# UserMessage row.
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Test PM'
send_personal_message(message)
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_last_um_content(long_term_idle_user, force_text(message))
# Test UserMessage row is created while user is deactivated if
# user itself is mentioned.
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Test @**King Hamlet** mention'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message))
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
# Test UserMessage row is not created while user is deactivated if
# anyone is mentioned but the user.
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Test @**Cordelia Lear** mention'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message), negate=True)
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
# Test UserMessage row is created while user is deactivated if
# there is a wildcard mention such as @all or @everyone
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Test @**all** mention'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message))
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Test @**everyone** mention'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message))
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
# Test UserMessage row is created while user is deactivated if there
# is a alert word in message.
do_add_alert_words(long_term_idle_user, ['test_alert_word'])
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = 'Testing test_alert_word'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message))
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
# Test UserMessage row is created while user is deactivated if
# message is a me message.
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = '/me says test'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message))
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))
# Test UserMessage row is created while user is deactivated if
# user itself is mentioned and message is a me message.
general_user_msg_count = len(get_user_messages(cordelia))
soft_deactivated_user_msg_count = len(get_user_messages(long_term_idle_user))
message = '/me says @**King Hamlet** mention'
send_stream_message(message)
assert_last_um_content(long_term_idle_user, force_text(message))
assert_um_count(long_term_idle_user, soft_deactivated_user_msg_count + 1)
assert_um_count(cordelia, general_user_msg_count + 1)
assert_last_um_content(cordelia, force_text(message))