rendering: Consolidate code to render new messages.

We now use render_incoming_message() to render all incoming
new messages (sends/edits), so that they will get the same treatment.

This change also establishes do_send_messages() as the code
path to get new messages rendered.  It removes some
logic from check_message() that only happened on certain code paths
for sending messages, and which would only detect failures by
expensively rendering messages, so it wasn't much of a guard.

This change also helps to phase out maybe_render_content(), which
deepens the call stack without providing much clarity to the reader,
since it's behavior is so variable.

Finally, this sets up to fix a flaw in the way we compute which
users have alert words in their messages (in a subsequent commit).
This commit is contained in:
Steve Howell
2016-09-14 12:58:44 -07:00
committed by Tim Abbott
parent 798e6faa9e
commit cb0d75b23b
2 changed files with 24 additions and 9 deletions

View File

@@ -627,6 +627,14 @@ def do_send_message(message, rendered_content = None, no_log = False, stream = N
'stream': stream,
'local_id': local_id}])[0]
def render_incoming_message(message, content):
# type: (Message, text_type) -> text_type
try:
rendered_content = message.render_markdown(content)
except BugdownRenderingException:
raise JsonableError(_('Unable to render message'))
return rendered_content
def do_send_messages(messages):
# type: (Sequence[Optional[MutableMapping[str, Any]]]) -> List[int]
# Filter out messages which didn't pass internal_prep_message properly
@@ -685,7 +693,16 @@ def do_send_messages(messages):
# Only deliver the message to active user recipients
message['active_recipients'] = [user_profile for user_profile in message['recipients']
if user_profile.is_active]
message['message'].maybe_render_content(None)
# Render our messages.
for message in messages:
assert message['message'].rendered_content is None
rendered_content = render_incoming_message(
message['message'],
message['message'].content)
message['message'].set_rendered_content(rendered_content)
for message in messages:
message['message'].update_calculated_fields()
# Save the message receipts in the database
@@ -1065,10 +1082,8 @@ def check_message(sender, client, message_type_name, message_to,
message.pub_date = timezone.now()
message.sending_client = client
try:
message.maybe_render_content(realm.domain)
except BugdownRenderingException:
raise JsonableError(_("Unable to render message"))
# We render messages later in the process.
assert message.rendered_content is None
if client.name == "zephyr_mirror":
id = already_sent_mirrored_message_id(message)