mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
message: Extract build_message_send_dict function.
We extract the loop for building message dict in do_send_messages in a separate function named build_message_send_dict. This is a prep commit for moving the code for building of message dict in check_message.
This commit is contained in:
@@ -1386,6 +1386,87 @@ def do_schedule_messages(messages: Sequence[Mapping[str, Any]]) -> List[int]:
|
||||
return [scheduled_message.id for scheduled_message in scheduled_messages]
|
||||
|
||||
|
||||
def build_message_send_dict(message_dict: MutableMapping[str, Any],
|
||||
email_gateway: bool=False) -> MutableMapping[str, Any]:
|
||||
message_dict['stream'] = message_dict.get('stream', None)
|
||||
message_dict['local_id'] = message_dict.get('local_id', None)
|
||||
message_dict['sender_queue_id'] = message_dict.get('sender_queue_id', None)
|
||||
message_dict['realm'] = message_dict.get('realm', message_dict['message'].sender.realm)
|
||||
|
||||
mention_data = MentionData(
|
||||
realm_id=message_dict['realm'].id,
|
||||
content=message_dict['message'].content,
|
||||
)
|
||||
message_dict['mention_data'] = mention_data
|
||||
|
||||
if message_dict['message'].is_stream_message():
|
||||
stream_id = message_dict['message'].recipient.type_id
|
||||
stream_topic: Optional[StreamTopicTarget] = StreamTopicTarget(
|
||||
stream_id=stream_id,
|
||||
topic_name=message_dict['message'].topic_name(),
|
||||
)
|
||||
else:
|
||||
stream_topic = None
|
||||
|
||||
info = get_recipient_info(
|
||||
recipient=message_dict['message'].recipient,
|
||||
sender_id=message_dict['message'].sender_id,
|
||||
stream_topic=stream_topic,
|
||||
possibly_mentioned_user_ids=mention_data.get_user_ids(),
|
||||
possible_wildcard_mention=mention_data.message_has_wildcards(),
|
||||
)
|
||||
|
||||
message_dict['active_user_ids'] = info['active_user_ids']
|
||||
message_dict['push_notify_user_ids'] = info['push_notify_user_ids']
|
||||
message_dict['stream_push_user_ids'] = info['stream_push_user_ids']
|
||||
message_dict['stream_email_user_ids'] = info['stream_email_user_ids']
|
||||
message_dict['um_eligible_user_ids'] = info['um_eligible_user_ids']
|
||||
message_dict['long_term_idle_user_ids'] = info['long_term_idle_user_ids']
|
||||
message_dict['default_bot_user_ids'] = info['default_bot_user_ids']
|
||||
message_dict['service_bot_tuples'] = info['service_bot_tuples']
|
||||
|
||||
# Render our message_dicts.
|
||||
assert message_dict['message'].rendered_content is None
|
||||
|
||||
rendered_content = render_incoming_message(
|
||||
message_dict['message'],
|
||||
message_dict['message'].content,
|
||||
message_dict['active_user_ids'],
|
||||
message_dict['realm'],
|
||||
mention_data=message_dict['mention_data'],
|
||||
email_gateway=email_gateway,
|
||||
)
|
||||
message_dict['message'].rendered_content = rendered_content
|
||||
message_dict['message'].rendered_content_version = markdown_version
|
||||
message_dict['links_for_embed'] = message_dict['message'].links_for_preview
|
||||
|
||||
# Add members of the mentioned user groups into `mentions_user_ids`.
|
||||
for group_id in message_dict['message'].mentions_user_group_ids:
|
||||
members = message_dict['mention_data'].get_group_members(group_id)
|
||||
message_dict['message'].mentions_user_ids.update(members)
|
||||
|
||||
# Only send data to Tornado about wildcard mentions if message
|
||||
# rendering determined the message had an actual wildcard
|
||||
# mention in it (and not e.g. wildcard mention syntax inside a
|
||||
# code block).
|
||||
if message_dict['message'].mentions_wildcard:
|
||||
message_dict['wildcard_mention_user_ids'] = info['wildcard_mention_user_ids']
|
||||
else:
|
||||
message_dict['wildcard_mention_user_ids'] = []
|
||||
|
||||
'''
|
||||
Once we have the actual list of mentioned ids from message
|
||||
rendering, we can patch in "default bots" (aka normal bots)
|
||||
who were directly mentioned in this message as eligible to
|
||||
get UserMessage rows.
|
||||
'''
|
||||
mentioned_user_ids = message_dict['message'].mentions_user_ids
|
||||
default_bot_user_ids = message_dict['default_bot_user_ids']
|
||||
mentioned_bot_user_ids = default_bot_user_ids & mentioned_user_ids
|
||||
message_dict['um_eligible_user_ids'] |= mentioned_bot_user_ids
|
||||
|
||||
return message_dict
|
||||
|
||||
def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str, Any]]],
|
||||
email_gateway: bool=False,
|
||||
mark_as_read: Sequence[int]=[]) -> List[int]:
|
||||
@@ -1408,82 +1489,7 @@ def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str,
|
||||
messages = new_messages
|
||||
|
||||
for message_dict in messages:
|
||||
message_dict['stream'] = message_dict.get('stream', None)
|
||||
message_dict['local_id'] = message_dict.get('local_id', None)
|
||||
message_dict['sender_queue_id'] = message_dict.get('sender_queue_id', None)
|
||||
message_dict['realm'] = message_dict.get('realm', message_dict['message'].sender.realm)
|
||||
|
||||
mention_data = MentionData(
|
||||
realm_id=message_dict['realm'].id,
|
||||
content=message_dict['message'].content,
|
||||
)
|
||||
message_dict['mention_data'] = mention_data
|
||||
|
||||
if message_dict['message'].is_stream_message():
|
||||
stream_id = message_dict['message'].recipient.type_id
|
||||
stream_topic: Optional[StreamTopicTarget] = StreamTopicTarget(
|
||||
stream_id=stream_id,
|
||||
topic_name=message_dict['message'].topic_name(),
|
||||
)
|
||||
else:
|
||||
stream_topic = None
|
||||
|
||||
info = get_recipient_info(
|
||||
recipient=message_dict['message'].recipient,
|
||||
sender_id=message_dict['message'].sender_id,
|
||||
stream_topic=stream_topic,
|
||||
possibly_mentioned_user_ids=mention_data.get_user_ids(),
|
||||
possible_wildcard_mention=mention_data.message_has_wildcards(),
|
||||
)
|
||||
|
||||
message_dict['active_user_ids'] = info['active_user_ids']
|
||||
message_dict['push_notify_user_ids'] = info['push_notify_user_ids']
|
||||
message_dict['stream_push_user_ids'] = info['stream_push_user_ids']
|
||||
message_dict['stream_email_user_ids'] = info['stream_email_user_ids']
|
||||
message_dict['um_eligible_user_ids'] = info['um_eligible_user_ids']
|
||||
message_dict['long_term_idle_user_ids'] = info['long_term_idle_user_ids']
|
||||
message_dict['default_bot_user_ids'] = info['default_bot_user_ids']
|
||||
message_dict['service_bot_tuples'] = info['service_bot_tuples']
|
||||
|
||||
# Render our message_dicts.
|
||||
assert message_dict['message'].rendered_content is None
|
||||
|
||||
rendered_content = render_incoming_message(
|
||||
message_dict['message'],
|
||||
message_dict['message'].content,
|
||||
message_dict['active_user_ids'],
|
||||
message_dict['realm'],
|
||||
mention_data=message_dict['mention_data'],
|
||||
email_gateway=email_gateway,
|
||||
)
|
||||
message_dict['message'].rendered_content = rendered_content
|
||||
message_dict['message'].rendered_content_version = markdown_version
|
||||
message_dict['links_for_embed'] = message_dict['message'].links_for_preview
|
||||
|
||||
# Add members of the mentioned user groups into `mentions_user_ids`.
|
||||
for group_id in message_dict['message'].mentions_user_group_ids:
|
||||
members = message_dict['mention_data'].get_group_members(group_id)
|
||||
message_dict['message'].mentions_user_ids.update(members)
|
||||
|
||||
# Only send data to Tornado about wildcard mentions if message
|
||||
# rendering determined the message had an actual wildcard
|
||||
# mention in it (and not e.g. wildcard mention syntax inside a
|
||||
# code block).
|
||||
if message_dict['message'].mentions_wildcard:
|
||||
message_dict['wildcard_mention_user_ids'] = info['wildcard_mention_user_ids']
|
||||
else:
|
||||
message_dict['wildcard_mention_user_ids'] = []
|
||||
|
||||
'''
|
||||
Once we have the actual list of mentioned ids from message
|
||||
rendering, we can patch in "default bots" (aka normal bots)
|
||||
who were directly mentioned in this message as eligible to
|
||||
get UserMessage rows.
|
||||
'''
|
||||
mentioned_user_ids = message_dict['message'].mentions_user_ids
|
||||
default_bot_user_ids = message_dict['default_bot_user_ids']
|
||||
mentioned_bot_user_ids = default_bot_user_ids & mentioned_user_ids
|
||||
message_dict['um_eligible_user_ids'] |= mentioned_bot_user_ids
|
||||
message_dict = build_message_send_dict(message_dict, email_gateway)
|
||||
|
||||
# Save the message receipts in the database
|
||||
user_message_flags: Dict[int, Dict[int, List[str]]] = defaultdict(dict)
|
||||
|
||||
Reference in New Issue
Block a user