refactor: Extract get_user_info_for_message_updates.

We'll want to expand this to get users that were mentioned in
the prior message, but this commit is just a refactoring.
This commit is contained in:
Steve Howell
2017-09-27 06:06:03 -07:00
committed by Tim Abbott
parent 5abf52de71
commit 646abb57b7
3 changed files with 43 additions and 13 deletions

View File

@@ -3001,6 +3001,28 @@ def truncate_topic(topic):
# type: (Text) -> Text # type: (Text) -> Text
return truncate_content(topic, MAX_SUBJECT_LENGTH, "...") return truncate_content(topic, MAX_SUBJECT_LENGTH, "...")
MessageUpdateUserInfoResult = TypedDict('MessageUpdateUserInfoResult', {
'message_user_ids': Set[int],
})
def get_user_info_for_message_updates(message_id):
# type: (int) -> MessageUpdateUserInfoResult
# We exclude UserMessage.flags.historical rows since those
# users did not receive the message originally, and thus
# probably are not relevant for reprocessed alert_words,
# mentions and similar rendering features. This may be a
# decision we change in the future.
query = UserMessage.objects.filter(
message=message_id,
flags=~UserMessage.flags.historical
)
message_user_ids = set(query.values_list('user_profile_id', flat=True))
return dict(
message_user_ids=message_user_ids,
)
def update_user_message_flags(message, ums): def update_user_message_flags(message, ums):
# type: (Message, Iterable[UserMessage]) -> None # type: (Message, Iterable[UserMessage]) -> None

View File

@@ -16,6 +16,7 @@ from zerver.lib.addressee import Addressee
from zerver.lib.actions import ( from zerver.lib.actions import (
do_send_messages, do_send_messages,
get_userids_for_missed_messages, get_userids_for_missed_messages,
get_user_info_for_message_updates,
internal_send_private_message, internal_send_private_message,
) )
@@ -1499,6 +1500,23 @@ class EditMessageTest(ZulipTestCase):
('<p>content before edit, line 1</p>\n' ('<p>content before edit, line 1</p>\n'
'<p>content before edit, line 3</p>')) '<p>content before edit, line 3</p>'))
def test_user_info_for_updates(self):
# type: () -> None
hamlet = self.example_user('hamlet')
cordelia = self.example_user('cordelia')
self.login(hamlet.email)
self.subscribe(hamlet, 'Scotland')
self.subscribe(cordelia, 'Scotland')
msg_id = self.send_message(hamlet.email, 'Scotland', Recipient.STREAM,
subject='subject 1', content='content 1')
user_info = get_user_info_for_message_updates(msg_id)
message_user_ids = user_info['message_user_ids']
self.assertIn(hamlet.id, message_user_ids)
self.assertIn(cordelia.id, message_user_ids)
def test_edit_cases(self): def test_edit_cases(self):
# type: () -> None # type: () -> None
"""This test verifies the accuracy of construction of Zulip's edit """This test verifies the accuracy of construction of Zulip's edit

View File

@@ -19,7 +19,7 @@ from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \
compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \ compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \
create_mirror_user_if_needed, check_send_message, do_update_message, \ create_mirror_user_if_needed, check_send_message, do_update_message, \
extract_recipients, truncate_body, render_incoming_message, do_delete_message, \ extract_recipients, truncate_body, render_incoming_message, do_delete_message, \
do_mark_all_as_read, do_mark_stream_messages_as_read do_mark_all_as_read, do_mark_stream_messages_as_read, get_user_info_for_message_updates
from zerver.lib.queue import queue_json_publish from zerver.lib.queue import queue_json_publish
from zerver.lib.cache import ( from zerver.lib.cache import (
generic_bulk_cached_fetch, generic_bulk_cached_fetch,
@@ -1094,17 +1094,7 @@ def update_message_backend(request, user_profile,
content = "(deleted)" content = "(deleted)"
content = truncate_body(content) content = truncate_body(content)
# We exclude UserMessage.flags.historical rows since those user_info = get_user_info_for_message_updates(message.id)
# users did not receive the message originally, and thus
# probably are not relevant for reprocessed alert_words,
# mentions and similar rendering features. This may be a
# decision we change in the future.
query = UserMessage.objects.filter(
message=message.id,
flags=~UserMessage.flags.historical
)
message_user_ids = set(query.values_list('user_profile_id', flat=True))
# We render the message using the current user's realm; since # We render the message using the current user's realm; since
# the cross-realm bots never edit messages, this should be # the cross-realm bots never edit messages, this should be
@@ -1112,7 +1102,7 @@ def update_message_backend(request, user_profile,
# Note: If rendering fails, the called code will raise a JsonableError. # Note: If rendering fails, the called code will raise a JsonableError.
rendered_content = render_incoming_message(message, rendered_content = render_incoming_message(message,
content, content,
message_user_ids, user_info['message_user_ids'],
user_profile.realm) user_profile.realm)
links_for_embed |= message.links_for_preview links_for_embed |= message.links_for_preview