Truncate long messages instead of rejecting them.

(imported from commit 41e0ce1e91ab7a441f2b21ad445344a86ae18fc7)
This commit is contained in:
Jessica McKellar 2013-11-22 12:33:22 -05:00
parent 9f3ab73fa4
commit baef4b9b00
1 changed files with 13 additions and 4 deletions

View File

@ -492,10 +492,9 @@ def check_message(sender, client, message_type_name, message_to,
stream = None stream = None
if len(message_to) == 0: if len(message_to) == 0:
raise JsonableError("Message must have recipients") raise JsonableError("Message must have recipients")
if len(message_content) > MAX_MESSAGE_LENGTH:
raise JsonableError("Message too long")
if len(message_content.strip()) == 0: if len(message_content.strip()) == 0:
raise JsonableError("Message must not be empty") raise JsonableError("Message must not be empty")
message_content = truncate_body(message_content)
if realm is None: if realm is None:
realm = sender.realm realm = sender.realm
@ -1409,6 +1408,17 @@ def subscribed_to_stream(user_profile, stream):
except Subscription.DoesNotExist: except Subscription.DoesNotExist:
return False return False
def truncate_content(content, max_length, truncation_message):
if len(content) > max_length:
content = content[:max_length - len(truncation_message)] + truncation_message
return content
def truncate_body(body):
return truncate_content(body, MAX_MESSAGE_LENGTH, "...")
def truncate_topic(topic):
return truncate_content(topic, MAX_SUBJECT_LENGTH, "...")
def do_update_message(user_profile, message_id, subject, propagate_mode, content): def do_update_message(user_profile, message_id, subject, propagate_mode, content):
try: try:
message = Message.objects.select_related().get(id=message_id) message = Message.objects.select_related().get(id=message_id)
@ -1448,8 +1458,7 @@ def do_update_message(user_profile, message_id, subject, propagate_mode, content
if content is not None: if content is not None:
if len(content.strip()) == 0: if len(content.strip()) == 0:
content = "(deleted)" content = "(deleted)"
if len(content) > MAX_MESSAGE_LENGTH: content = truncate_body(content)
raise JsonableError("Message too long")
rendered_content = message.render_markdown(content) rendered_content = message.render_markdown(content)
if not rendered_content: if not rendered_content:
raise JsonableError("We were unable to render your updated message") raise JsonableError("We were unable to render your updated message")