From d0a49e3a80b47c6e0ef59075d5569dd4c0417dbd Mon Sep 17 00:00:00 2001 From: Jessica McKellar Date: Fri, 22 Nov 2013 12:37:21 -0500 Subject: [PATCH] Replace decentralized topic truncation with a single check when sending messages. (imported from commit 291c999147dd59a9c7e1d9de446293755c17e99d) --- zerver/lib/actions.py | 7 ++----- zerver/views/webhooks.py | 20 ++------------------ zilencer/error_notify.py | 10 ++-------- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index e26401ec3e..e83423795c 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -511,8 +511,7 @@ def check_message(sender, client, message_type_name, message_to, subject = subject_name.strip() if subject == "": raise JsonableError("Topic can't be empty") - if len(subject) > MAX_SUBJECT_LENGTH: - raise JsonableError("Topic too long") + subject = truncate_topic(subject) ## FIXME: Commented out temporarily while we figure out what we want # if not valid_stream_name(subject): # return json_error("Invalid subject name") @@ -1483,9 +1482,7 @@ def do_update_message(user_profile, message_id, subject, propagate_mode, content subject = subject.strip() if subject == "": raise JsonableError("Topic can't be empty") - - if len(subject) > MAX_SUBJECT_LENGTH: - raise JsonableError("Topic too long") + subject = truncate_topic(subject) event["orig_subject"] = orig_subject message.subject = subject event["subject"] = subject diff --git a/zerver/views/webhooks.py b/zerver/views/webhooks.py index 30798ff555..3b67129c50 100644 --- a/zerver/views/webhooks.py +++ b/zerver/views/webhooks.py @@ -4,8 +4,7 @@ from __future__ import absolute_import from django.conf import settings from django.views.decorators.csrf import csrf_exempt -from zerver.models import UserProfile, get_client, MAX_SUBJECT_LENGTH, \ - get_user_profile_by_email +from zerver.models import UserProfile, get_client, get_user_profile_by_email from zerver.lib.actions import check_send_message, convert_html_to_markdown from zerver.lib.response import json_success, json_error from zerver.decorator import authenticated_api_view, REQ, \ @@ -141,8 +140,6 @@ def api_github_landing(request, user_profile, event=REQ, # about them return json_success() - subject = elide_subject(subject) - # customer14.invalid has a stream per GitHub project and wants the topic to # always be 'GitHub'. if user_profile.realm.domain == "customer14.invalid": @@ -196,11 +193,6 @@ def build_message_from_gitlog(user_profile, name, ref, commits, before, after, u return (subject, content) -def elide_subject(subject): - if len(subject) > MAX_SUBJECT_LENGTH: - subject = subject[:MAX_SUBJECT_LENGTH - 3].rstrip() + '...' - return subject - def guess_zulip_user_from_jira(jira_username, realm): try: # Try to find a matching user in Zulip @@ -357,8 +349,6 @@ def api_jira_webhook(request, user_profile): logging.warning("Got JIRA event type we don't understand: %s" % (event,)) return json_error("Unknown JIRA event type") - subject = elide_subject(subject) - check_send_message(user_profile, get_client("JIRA webhook"), "stream", [stream], subject, content) return json_success() @@ -427,8 +417,6 @@ def api_pivotal_webhook(request, user_profile): except AttributeError: return json_error("Failed to extract data from Pivotal XML response") - subject = elide_subject(subject) - check_send_message(user_profile, get_client("Pivotal webhook"), "stream", [stream], subject, content) return json_success() @@ -477,8 +465,6 @@ def api_beanstalk_webhook(request, user_profile, subject = "svn r%s" % (revision,) content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg) - subject = elide_subject(subject) - check_send_message(user_profile, get_client("Beanstalk webhook"), "stream", ["commits"], subject, content) return json_success() @@ -522,7 +508,6 @@ def api_newrelic_webhook(request, user_profile, alert=REQ(converter=json_to_dict else: return json_error("Unknown webhook request") - subject = elide_subject(subject) check_send_message(user_profile, get_client("NewRelic webhook"), "stream", [stream], subject, content) return json_success() @@ -550,7 +535,6 @@ def api_bitbucket_webhook(request, user_profile, payload=REQ(converter=json_to_d content = build_commit_list_content(commits, branch, None, payload['user']) subject += '/%s' % (branch,) - subject = elide_subject(subject) check_send_message(user_profile, get_client("BitBucket webhook"), "stream", [stream], subject, content) return json_success() @@ -700,7 +684,7 @@ def api_freshdesk_webhook(request, user_profile, stream=REQ(default='')): ticket = TicketDict(ticket_data) - subject = elide_subject("#%s: %s" % (ticket.id, ticket.subject)) + subject = "#%s: %s" % (ticket.id, ticket.subject) try: event_info = parse_freshdesk_event(ticket.triggered_event) diff --git a/zilencer/error_notify.py b/zilencer/error_notify.py index 994677bf03..ef829ce6ef 100644 --- a/zilencer/error_notify.py +++ b/zilencer/error_notify.py @@ -8,15 +8,9 @@ from zerver.lib.actions import internal_send_message def format_subject(subject): """ - Escape CR and LF characters, and limit length to MAX_SUBJECT_LENGTH. + Escape CR and LF characters. """ - from zerver.models import MAX_SUBJECT_LENGTH - subject = subject.replace('\n', '\\n').replace('\r', '\\r') - - if len(subject) > MAX_SUBJECT_LENGTH: - subject = subject[:MAX_SUBJECT_LENGTH-3].rstrip() + "..." - - return subject + return subject.replace('\n', '\\n').replace('\r', '\\r') def user_info_str(report): if report['user_full_name'] and report['user_email']: