Attempt to render messages before accepting them from the user.

This fixes trac #407.

(imported from commit e33647eec32266790f864d14ad377d51956d2a6f)
This commit is contained in:
Tim Abbott 2013-03-08 14:40:39 -05:00
parent 2f022ebb34
commit 254bc9f361
3 changed files with 13 additions and 7 deletions

View File

@ -127,7 +127,7 @@ def get_user_profile_by_id(uid):
return user_hash[uid]
return UserProfile.objects.select_related().get(id=uid)
def do_send_message(message, no_log=False):
def do_send_message(message, rendered_content=None, no_log=False):
# Log the message to our message log for populate_db to refill
if not no_log:
log_message(message)
@ -161,7 +161,7 @@ def do_send_message(message, no_log=False):
# Render Markdown etc. here and store (automatically) in
# memcached, so that the single-threaded Tornado server
# doesn't have to.
message.to_dict(apply_markdown=True)
message.to_dict(apply_markdown=True, rendered_content=rendered_content)
message.to_dict(apply_markdown=False)
data = dict(
secret = settings.SHARED_SECRET,

View File

@ -191,8 +191,8 @@ class Message(models.Model):
def __str__(self):
return self.__repr__()
@cache_with_key(lambda self, apply_markdown: 'message_dict:%d:%d' % (self.id, apply_markdown))
def to_dict(self, apply_markdown):
@cache_with_key(lambda self, apply_markdown, rendered_content=None: 'message_dict:%d:%d' % (self.id, apply_markdown))
def to_dict(self, apply_markdown, rendered_content=None):
display_recipient = get_display_recipient(self.recipient)
if self.recipient.type == Recipient.STREAM:
display_type = "stream"
@ -225,9 +225,10 @@ class Message(models.Model):
client = self.sending_client.name)
if apply_markdown:
rendered_content = bugdown.convert_safe(self.content)
if rendered_content is None:
rendered_content = '<p>[Humbug note: Sorry, we could not understand the formatting of your message]</p>'
rendered_content = bugdown.convert(self.content)
if rendered_content is None:
rendered_content = '<p>[Humbug note: Sorry, we could not understand the formatting of your message]</p>'
obj['content'] = rendered_content
obj['content_type'] = 'text/html'
else:

View File

@ -48,6 +48,7 @@ import requests
import os
import base64
from collections import defaultdict
from zephyr.lib import bugdown
SERVER_GENERATION = int(time.time())
@ -871,6 +872,10 @@ def send_message_backend(request, user_profile, client,
else:
return json_error("Invalid message type")
rendered_content = bugdown.convert(message_content)
if rendered_content is None:
return json_error("We were unable to render your message")
message = Message()
message.sender = sender
message.content = message_content
@ -887,7 +892,7 @@ def send_message_backend(request, user_profile, client,
if client.name == "zephyr_mirror" and already_sent_mirrored_message(message):
return json_success()
do_send_message(message)
do_send_message(message, rendered_content=rendered_content)
return json_success()