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)

View File

@@ -20,7 +20,7 @@ from zerver.lib import bugdown
from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \
compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \
create_mirror_user_if_needed, check_send_message, do_update_message, \
extract_recipients, truncate_body
extract_recipients, truncate_body, render_incoming_message
from zerver.lib.cache import generic_bulk_cached_fetch
from zerver.lib.response import json_success, json_error
from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection
@@ -910,9 +910,9 @@ def update_message_backend(request, user_profile,
if content == "":
raise JsonableError(_("Content can't be empty"))
content = truncate_body(content)
rendered_content = message.render_markdown(content)
if not rendered_content:
raise JsonableError(_("We were unable to render your updated message"))
# If rendering fails, the called code will raise a JsonableError.
rendered_content = render_incoming_message(message, content)
do_update_message(user_profile, message, subject, propagate_mode, content, rendered_content)
return json_success()