Skip to content

Commit

Permalink
Fetch system bots using new get_system_bot function.
Browse files Browse the repository at this point in the history
This eliminate a bunch of uninteresting calls to
get_user_profile_by_email.
  • Loading branch information
gukoff authored and timabbott committed May 23, 2017
1 parent f3369b2 commit dd76222
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ os.environ['DJANGO_SETTINGS_MODULE'] = "zproject.settings"

django.setup()

from zerver.models import get_user_profile_by_email
from zerver.models import get_system_bot
from zerver.tornado.websocket_client import WebsocketClient
from django.conf import settings

Expand Down Expand Up @@ -134,11 +134,11 @@ def send_message_via_websocket(websocket_client, recepient_email, content):

if "staging" in options.site and settings.NAGIOS_STAGING_SEND_BOT is not None and \
settings.NAGIOS_STAGING_RECEIVE_BOT is not None:
sender = get_user_profile_by_email(settings.NAGIOS_STAGING_SEND_BOT)
recipient = get_user_profile_by_email(settings.NAGIOS_STAGING_RECEIVE_BOT)
sender = get_system_bot(settings.NAGIOS_STAGING_SEND_BOT)
recipient = get_system_bot(settings.NAGIOS_STAGING_RECEIVE_BOT)
else:
sender = get_user_profile_by_email(settings.NAGIOS_SEND_BOT)
recipient = get_user_profile_by_email(settings.NAGIOS_RECEIVE_BOT)
sender = get_system_bot(settings.NAGIOS_SEND_BOT)
recipient = get_system_bot(settings.NAGIOS_RECEIVE_BOT)

zulip_sender = zulip.Client(
email=sender.email,
Expand Down
22 changes: 13 additions & 9 deletions zerver/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
render_markdown,
)
from zerver.lib.realm_icon import realm_icon_url
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, RealmDomain, \
Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, UserHotspot, \
Client, DefaultStream, UserPresence, Referral, PushDeviceToken, MAX_SUBJECT_LENGTH, \
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \
RealmDomain, \
Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \
UserHotspot, \
Client, DefaultStream, UserPresence, Referral, PushDeviceToken, \
MAX_SUBJECT_LENGTH, \
MAX_MESSAGE_LENGTH, get_client, get_stream, get_recipient, get_huddle, \
get_user_profile_by_id, PreregistrationUser, get_display_recipient, \
get_realm, bulk_get_recipients, \
Expand All @@ -40,8 +43,9 @@
realm_filters_for_realm, RealmFilter, receives_offline_notifications, \
ScheduledJob, get_owned_bot_dicts, \
get_old_unclaimed_attachments, get_cross_realm_emails, \
Reaction, EmailChangeStatus, CustomProfileField, custom_profile_fields_for_realm, \
CustomProfileFieldValue, validate_attachment_request
Reaction, EmailChangeStatus, CustomProfileField, \
custom_profile_fields_for_realm, \
CustomProfileFieldValue, validate_attachment_request, get_system_bot

from zerver.lib.alert_words import alert_words_in_realm
from zerver.lib.avatar import avatar_url
Expand Down Expand Up @@ -1009,7 +1013,7 @@ def stream_welcome_message(stream):
def prep_stream_welcome_message(stream):
# type: (Stream) -> Optional[Dict[str, Any]]
realm = stream.realm
sender = get_user_profile_by_email(settings.WELCOME_BOT)
sender = get_system_bot(settings.WELCOME_BOT)
topic = _('hello')
content = stream_welcome_message(stream)

Expand Down Expand Up @@ -1212,7 +1216,7 @@ def send_pm_if_empty_stream(sender, stream, stream_name, realm):
(sender.full_name, stream_name, error_msg))
message = internal_prep_private_message(
realm=realm,
sender=get_user_profile_by_email(settings.NOTIFICATION_BOT),
sender=get_system_bot(settings.NOTIFICATION_BOT),
recipient_email=sender.bot_owner.email,
content=content)

Expand Down Expand Up @@ -2199,7 +2203,7 @@ def do_create_realm(string_id, name, restricted_to_domain=None,

# Include a welcome message in this notifications stream
stream_name = notifications_stream.name
sender = get_user_profile_by_email(settings.WELCOME_BOT)
sender = get_system_bot(settings.WELCOME_BOT)
topic = "welcome"
content = """Hello, and welcome to Zulip!
Expand All @@ -2224,7 +2228,7 @@ def do_create_realm(string_id, name, restricted_to_domain=None,
# Send a notification to the admin realm (if configured)
if settings.NEW_USER_BOT is not None:
signup_message = "Signups enabled"
admin_realm = get_user_profile_by_email(settings.NEW_USER_BOT).realm
admin_realm = get_system_bot(settings.NEW_USER_BOT).realm
internal_send_message(admin_realm, settings.NEW_USER_BOT, "stream",
"signups", string_id, signup_message)
return (realm, created)
Expand Down
6 changes: 3 additions & 3 deletions zerver/lib/bugdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from zerver.lib.cache import (
cache_with_key, cache_get_many, cache_set_many, NotFoundInCache)
from zerver.lib.url_preview import preview as link_preview
from zerver.models import Message, Realm, UserProfile, get_user_profile_by_email
from zerver.models import Message, Realm, UserProfile
import zerver.lib.alert_words as alert_words
import zerver.lib.mention as mention
from zerver.lib.str_utils import force_str, force_text
Expand Down Expand Up @@ -1510,7 +1510,7 @@ def do_convert(content, message=None, message_realm=None, possible_words=None, s
return timeout(5, _md_engine.convert, content)
except Exception:
from zerver.lib.actions import internal_send_message
from zerver.models import get_user_profile_by_email
from zerver.models import get_system_bot

cleaned = _sanitize_for_log(content)

Expand All @@ -1519,7 +1519,7 @@ def do_convert(content, message=None, message_realm=None, possible_words=None, s
% (traceback.format_exc(), cleaned))
subject = "Markdown parser failure on %s" % (platform.node(),)
if settings.ERROR_BOT is not None:
error_bot_realm = get_user_profile_by_email(settings.ERROR_BOT).realm
error_bot_realm = get_system_bot(settings.ERROR_BOT).realm
internal_send_message(error_bot_realm, settings.ERROR_BOT, "stream",
"errors", subject, "Markdown parser failed, email sent with details.")
mail.mail_admins(
Expand Down
4 changes: 4 additions & 0 deletions zerver/lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def user_profile_cache_key(email, realm):
# type: (Text, Realm) -> Text
return u"user_profile:%s:%s" % (make_safe_digest(email.strip()), realm.id,)

def bot_profile_cache_key(email):
# type: (Text) -> Text
return u"bot_profile:%s" % (make_safe_digest(email.strip()))

def user_profile_by_id_cache_key(user_profile_id):
# type: (int) -> Text
return u"user_profile_by_id:%s" % (user_profile_id,)
Expand Down
8 changes: 4 additions & 4 deletions zerver/lib/email_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from zerver.lib.upload import upload_message_image
from zerver.lib.utils import generate_random_token
from zerver.lib.str_utils import force_text
from zerver.models import Stream, Recipient, get_user_profile_by_email, \
from zerver.models import Stream, Recipient, \
get_user_profile_by_id, get_display_recipient, get_recipient, \
Message, Realm, UserProfile
Message, Realm, UserProfile, get_system_bot
from six import binary_type
import six
import talon
Expand All @@ -42,7 +42,7 @@ def report_to_zulip(error_message):
# type: (Text) -> None
if settings.ERROR_BOT is None:
return
error_bot = get_user_profile_by_email(settings.ERROR_BOT)
error_bot = get_system_bot(settings.ERROR_BOT)
error_stream = Stream.objects.get(name="errors", realm=error_bot.realm)
send_zulip(settings.ERROR_BOT, error_stream, u"email mirror error",
u"""~~~\n%s\n~~~""" % (error_message,))
Expand Down Expand Up @@ -247,7 +247,7 @@ def filter_footer(text):

def extract_and_upload_attachments(message, realm):
# type: (message.Message, Realm) -> Text
user_profile = get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT)
user_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT)
attachment_links = []

payload = message.get_payload()
Expand Down
6 changes: 3 additions & 3 deletions zerver/lib/error_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.utils.translation import ugettext as _
from typing import Any, Dict, Text

from zerver.models import get_user_profile_by_email
from zerver.models import get_system_bot
from zerver.lib.actions import internal_send_message
from zerver.lib.response import json_success, json_error

Expand Down Expand Up @@ -70,7 +70,7 @@ def zulip_browser_error(report):
body += ("Message: %(message)s\n"
% (report))

realm = get_user_profile_by_email(settings.ERROR_BOT).realm
realm = get_system_bot(settings.ERROR_BOT).realm
internal_send_message(realm, settings.ERROR_BOT,
"stream", "errors", format_subject(subject), body)

Expand All @@ -97,7 +97,7 @@ def zulip_server_error(report):
request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower()))
request_repr += "~~~~"

realm = get_user_profile_by_email(settings.ERROR_BOT).realm
realm = get_system_bot(settings.ERROR_BOT).realm
internal_send_message(realm, settings.ERROR_BOT,
"stream", "errors", format_subject(subject),
"Error generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s" % (
Expand Down
19 changes: 10 additions & 9 deletions zerver/lib/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
from zerver.models import UserProfile, Realm, Client, Huddle, Stream, \
UserMessage, Subscription, Message, RealmEmoji, RealmFilter, \
RealmDomain, Recipient, DefaultStream, get_user_profile_by_id, \
UserPresence, UserActivity, UserActivityInterval, get_user_profile_by_email, \
get_display_recipient, Attachment
UserPresence, UserActivity, UserActivityInterval, \
get_user_profile_by_email, \
get_display_recipient, Attachment, get_system_bot
from zerver.lib.parallel import run_parallel
from zerver.lib.utils import mkdir_p
from six.moves import range
Expand Down Expand Up @@ -587,9 +588,9 @@ def fetch_user_profile_cross_realm(response, config, context):
response['zerver_userprofile_crossrealm'] = []
else:
response['zerver_userprofile_crossrealm'] = [dict(email=x.email, id=x.id) for x in [
get_user_profile_by_email(settings.NOTIFICATION_BOT),
get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT),
get_user_profile_by_email(settings.WELCOME_BOT),
get_system_bot(settings.NOTIFICATION_BOT),
get_system_bot(settings.EMAIL_GATEWAY_BOT),
get_system_bot(settings.WELCOME_BOT),
]]

def fetch_attachment_data(response, realm_id, message_ids):
Expand Down Expand Up @@ -860,7 +861,7 @@ def export_files_from_s3(realm, bucket_name, output_dir, processing_avatars=Fals
bucket_list = bucket.list(prefix="%s/" % (realm.id,))

if settings.EMAIL_GATEWAY_BOT is not None:
email_gateway_bot = get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT)
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT)
else:
email_gateway_bot = None

Expand Down Expand Up @@ -956,9 +957,9 @@ def export_avatars_from_local(realm, local_dir, output_dir):

users = list(UserProfile.objects.filter(realm=realm))
users += [
get_user_profile_by_email(settings.NOTIFICATION_BOT),
get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT),
get_user_profile_by_email(settings.WELCOME_BOT),
get_system_bot(settings.NOTIFICATION_BOT),
get_system_bot(settings.EMAIL_GATEWAY_BOT),
get_system_bot(settings.WELCOME_BOT),
]
for user in users:
if user.avatar_source == UserProfile.AVATAR_FROM_GRAVATAR:
Expand Down
4 changes: 2 additions & 2 deletions zerver/lib/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from zerver.lib.actions import internal_send_message
from zerver.lib.redis_utils import get_redis_client
from zerver.models import get_realm, get_user_profile_by_email, \
from zerver.models import get_realm, get_system_bot, \
UserProfile, Realm

import time
Expand Down Expand Up @@ -63,7 +63,7 @@ def deliver_feedback_by_zulip(message):

content += message['content']

user_profile = get_user_profile_by_email(settings.FEEDBACK_BOT)
user_profile = get_system_bot(settings.FEEDBACK_BOT)
internal_send_message(user_profile.realm, settings.FEEDBACK_BOT,
"stream", settings.FEEDBACK_STREAM, subject, content)

Expand Down
4 changes: 2 additions & 2 deletions zerver/lib/html_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ def highlight_html_differences(s1, s2):

if not verify_html(retval):
from zerver.lib.actions import internal_send_message
from zerver.models import get_user_profile_by_email
from zerver.models import get_system_bot
# We probably want more information here
logging.getLogger('').error('HTML diff produced mal-formed HTML')

if settings.ERROR_BOT is not None:
subject = "HTML diff failure on %s" % (platform.node(),)
realm = get_user_profile_by_email(settings.ERROR_BOT).realm
realm = get_system_bot(settings.ERROR_BOT).realm
internal_send_message(realm, settings.ERROR_BOT, "stream",
"errors", subject, "HTML diff produced malformed HTML")
return s2
Expand Down
7 changes: 6 additions & 1 deletion zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
display_recipient_cache_key, cache_delete, \
get_stream_cache_key, active_user_dicts_in_realm_cache_key, \
bot_dicts_in_realm_cache_key, active_user_dict_fields, \
bot_dict_fields, flush_message
bot_dict_fields, flush_message, bot_profile_cache_key
from zerver.lib.utils import make_safe_digest, generate_random_token
from zerver.lib.str_utils import ModelReprMixin
from django.db import transaction
Expand Down Expand Up @@ -1368,6 +1368,11 @@ def get_user(email, realm):
# type: (Text, Realm) -> UserProfile
return UserProfile.objects.select_related().get(email__iexact=email.strip(), realm=realm)

@cache_with_key(bot_profile_cache_key, timeout=3600*24*7)
def get_system_bot(email):
# type: (Text) -> UserProfile
return UserProfile.objects.select_related().get(email__iexact=email.strip())

@cache_with_key(active_user_dicts_in_realm_cache_key, timeout=3600*24*7)
def get_active_user_dicts_in_realm(realm):
# type: (Realm) -> List[Dict[str, Any]]
Expand Down
7 changes: 4 additions & 3 deletions zerver/views/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
from zerver.lib.validator import check_string, check_int, check_list, check_dict, \
check_bool, check_variable_type
from zerver.models import UserProfile, Stream, Realm, Subscription, \
Recipient, get_recipient, get_stream, get_active_user_dicts_in_realm
Recipient, get_recipient, get_stream, get_active_user_dicts_in_realm, \
get_system_bot

from collections import defaultdict
import ujson
Expand Down Expand Up @@ -301,7 +302,7 @@ def add_subscriptions_backend(request, user_profile,
private_stream_names=private_stream_names
)

sender = get_user_profile_by_email(settings.NOTIFICATION_BOT)
sender = get_system_bot(settings.NOTIFICATION_BOT)
notifications.append(
internal_prep_private_message(
realm=user_profile.realm,
Expand All @@ -318,7 +319,7 @@ def add_subscriptions_backend(request, user_profile,
stream_msg = "a new stream #**%s**." % created_streams[0].name
msg = ("%s just created %s" % (user_profile.full_name, stream_msg))

sender = get_user_profile_by_email(settings.NOTIFICATION_BOT)
sender = get_system_bot(settings.NOTIFICATION_BOT)
stream_name = notifications_stream.name
topic = 'Streams'

Expand Down
6 changes: 3 additions & 3 deletions zerver/worker/queue_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.core.handlers.base import BaseHandler
from zerver.models import get_user_profile_by_email, \
from zerver.models import \
get_user_profile_by_id, get_prereg_user_by_email, get_client, \
UserMessage, Message, Realm
UserMessage, Message, Realm, get_system_bot
from zerver.lib.context_managers import lockfile
from zerver.lib.error_notify import do_report_error
from zerver.lib.feedback import handle_feedback
Expand Down Expand Up @@ -301,7 +301,7 @@ def process_one_batch(self):
for query in slow_queries:
content += " %s\n" % (query,)

error_bot_realm = get_user_profile_by_email(settings.ERROR_BOT).realm
error_bot_realm = get_system_bot(settings.ERROR_BOT).realm
internal_send_message(error_bot_realm, settings.ERROR_BOT,
"stream", "logs", topic, content)

Expand Down

0 comments on commit dd76222

Please sign in to comment.