mirror of https://github.com/zulip/zulip.git
Cache the results of Recipient queries in our memcached cache.
(imported from commit a6ba25a9c62d2738e4738b076370e4b2bc0ffaba)
This commit is contained in:
parent
3c143db01a
commit
a23f6be28d
|
@ -4,7 +4,7 @@ from zephyr.lib.context_managers import lockfile
|
||||||
from zephyr.models import Realm, Stream, UserProfile, UserActivity, \
|
from zephyr.models import Realm, Stream, UserProfile, UserActivity, \
|
||||||
Subscription, Recipient, Message, UserMessage, \
|
Subscription, Recipient, Message, UserMessage, \
|
||||||
DefaultStream, StreamColor, UserPresence, \
|
DefaultStream, StreamColor, UserPresence, \
|
||||||
MAX_MESSAGE_LENGTH, get_client, get_stream
|
MAX_MESSAGE_LENGTH, get_client, get_stream, get_recipient
|
||||||
from django.db import transaction, IntegrityError
|
from django.db import transaction, IntegrityError
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
from zephyr.lib.initial_password import initial_password
|
from zephyr.lib.initial_password import initial_password
|
||||||
|
@ -216,7 +216,7 @@ def internal_send_message(sender_email, recipient_type, recipient,
|
||||||
else:
|
else:
|
||||||
type_id = UserProfile.objects.get(user__email__iexact=recipient).id
|
type_id = UserProfile.objects.get(user__email__iexact=recipient).id
|
||||||
|
|
||||||
message.recipient = Recipient.objects.get(type_id=type_id, type=recipient_type)
|
message.recipient = get_recipient(recipient_type, type_id)
|
||||||
|
|
||||||
message.subject = subject
|
message.subject = subject
|
||||||
message.content = content
|
message.content = content
|
||||||
|
@ -248,7 +248,7 @@ def pick_color(user_profile):
|
||||||
|
|
||||||
def get_subscription(stream_name, user_profile):
|
def get_subscription(stream_name, user_profile):
|
||||||
stream = get_stream(stream_name, user_profile.realm)
|
stream = get_stream(stream_name, user_profile.realm)
|
||||||
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
return Subscription.objects.filter(user_profile=user_profile,
|
return Subscription.objects.filter(user_profile=user_profile,
|
||||||
recipient=recipient, active=True)
|
recipient=recipient, active=True)
|
||||||
|
|
||||||
|
@ -262,8 +262,7 @@ def set_stream_color(user_profile, stream_name, color=None):
|
||||||
stream_color.save()
|
stream_color.save()
|
||||||
|
|
||||||
def do_add_subscription(user_profile, stream, no_log=False):
|
def do_add_subscription(user_profile, stream, no_log=False):
|
||||||
recipient = Recipient.objects.get(type_id=stream.id,
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
type=Recipient.STREAM)
|
|
||||||
(subscription, created) = Subscription.objects.get_or_create(
|
(subscription, created) = Subscription.objects.get_or_create(
|
||||||
user_profile=user_profile, recipient=recipient,
|
user_profile=user_profile, recipient=recipient,
|
||||||
defaults={'active': True})
|
defaults={'active': True})
|
||||||
|
@ -281,8 +280,7 @@ def do_add_subscription(user_profile, stream, no_log=False):
|
||||||
return did_subscribe
|
return did_subscribe
|
||||||
|
|
||||||
def do_remove_subscription(user_profile, stream, no_log=False):
|
def do_remove_subscription(user_profile, stream, no_log=False):
|
||||||
recipient = Recipient.objects.get(type_id=stream.id,
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
type=Recipient.STREAM)
|
|
||||||
maybe_sub = Subscription.objects.filter(user_profile=user_profile,
|
maybe_sub = Subscription.objects.filter(user_profile=user_profile,
|
||||||
recipient=recipient)
|
recipient=recipient)
|
||||||
if len(maybe_sub) == 0:
|
if len(maybe_sub) == 0:
|
||||||
|
|
|
@ -192,6 +192,10 @@ def get_stream(stream_name, realm):
|
||||||
except Stream.DoesNotExist:
|
except Stream.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@cache_with_key(lambda type, type_id: "get_recipient:%s:%s" % (type, type_id,))
|
||||||
|
def get_recipient(type, type_id):
|
||||||
|
return Recipient.objects.get(type_id=type_id, type=type)
|
||||||
|
|
||||||
# NB: This function is currently unused, but may come in handy.
|
# NB: This function is currently unused, but may come in handy.
|
||||||
def linebreak(string):
|
def linebreak(string):
|
||||||
return string.replace('\n\n', '<p/>').replace('\n', '<br/>')
|
return string.replace('\n\n', '<p/>').replace('\n', '<br/>')
|
||||||
|
|
|
@ -15,7 +15,8 @@ from django.core.mail import send_mail, mail_admins
|
||||||
from zephyr.models import Message, UserProfile, Stream, Subscription, \
|
from zephyr.models import Message, UserProfile, Stream, Subscription, \
|
||||||
Recipient, get_huddle, Realm, UserMessage, \
|
Recipient, get_huddle, Realm, UserMessage, \
|
||||||
PreregistrationUser, get_client, MitUser, User, UserActivity, \
|
PreregistrationUser, get_client, MitUser, User, UserActivity, \
|
||||||
MAX_SUBJECT_LENGTH, MAX_MESSAGE_LENGTH, get_stream, UserPresence
|
MAX_SUBJECT_LENGTH, MAX_MESSAGE_LENGTH, get_stream, UserPresence, \
|
||||||
|
get_recipient
|
||||||
from zephyr.lib.actions import do_add_subscription, do_remove_subscription, \
|
from zephyr.lib.actions import do_add_subscription, do_remove_subscription, \
|
||||||
do_change_password, create_mit_user_if_needed, do_change_full_name, \
|
do_change_password, create_mit_user_if_needed, do_change_full_name, \
|
||||||
do_change_enable_desktop_notifications, do_change_enter_sends, \
|
do_change_enable_desktop_notifications, do_change_enter_sends, \
|
||||||
|
@ -520,7 +521,7 @@ class NarrowBuilder(object):
|
||||||
stream = get_stream(operand, self.user_profile.realm)
|
stream = get_stream(operand, self.user_profile.realm)
|
||||||
if stream is None:
|
if stream is None:
|
||||||
raise BadNarrowOperator('unknown stream ' + operand)
|
raise BadNarrowOperator('unknown stream ' + operand)
|
||||||
recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
|
recipient = get_recipient(Recipient.STREAM, type_id=stream.id)
|
||||||
return Q(message__recipient=recipient)
|
return Q(message__recipient=recipient)
|
||||||
|
|
||||||
def by_subject(self, operand):
|
def by_subject(self, operand):
|
||||||
|
@ -541,8 +542,7 @@ class NarrowBuilder(object):
|
||||||
return Q(message__recipient=recipient)
|
return Q(message__recipient=recipient)
|
||||||
else:
|
else:
|
||||||
# Personal message
|
# Personal message
|
||||||
self_recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
self_recipient = get_recipient(Recipient.PERSONAL, type_id=self.user_profile.id)
|
||||||
type_id=self.user_profile.id)
|
|
||||||
if operand == self.user_profile.user.email:
|
if operand == self.user_profile.user.email:
|
||||||
# Personals with self
|
# Personals with self
|
||||||
return Q(message__recipient__type=Recipient.PERSONAL,
|
return Q(message__recipient__type=Recipient.PERSONAL,
|
||||||
|
@ -554,8 +554,7 @@ class NarrowBuilder(object):
|
||||||
except UserProfile.DoesNotExist:
|
except UserProfile.DoesNotExist:
|
||||||
raise BadNarrowOperator('unknown user ' + operand)
|
raise BadNarrowOperator('unknown user ' + operand)
|
||||||
|
|
||||||
narrow_recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
narrow_recipient = get_recipient(Recipient.PERSONAL, narrow_profile.id)
|
||||||
type_id=narrow_profile.id)
|
|
||||||
return ((Q(message__sender=narrow_profile) & Q(message__recipient=self_recipient)) |
|
return ((Q(message__sender=narrow_profile) & Q(message__recipient=self_recipient)) |
|
||||||
(Q(message__sender=self.user_profile) & Q(message__recipient=narrow_recipient)))
|
(Q(message__sender=self.user_profile) & Q(message__recipient=narrow_recipient)))
|
||||||
|
|
||||||
|
@ -604,7 +603,7 @@ def get_old_messages_backend(request, anchor = POST(converter=int),
|
||||||
user_profile=None, apply_markdown=True):
|
user_profile=None, apply_markdown=True):
|
||||||
if stream is not None:
|
if stream is not None:
|
||||||
stream = get_public_stream(request, stream, user_profile.realm)
|
stream = get_public_stream(request, stream, user_profile.realm)
|
||||||
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
query = UserMessage.objects.select_related('message').filter(message__recipient=recipient,
|
query = UserMessage.objects.select_related('message').filter(message__recipient=recipient,
|
||||||
user_profile=user_profile) \
|
user_profile=user_profile) \
|
||||||
.order_by('id')
|
.order_by('id')
|
||||||
|
@ -779,10 +778,9 @@ def recipient_for_emails(emails, not_forged_zephyr_mirror, user_profile, sender)
|
||||||
# Make sure the sender is included in huddle messages
|
# Make sure the sender is included in huddle messages
|
||||||
recipient_profile_ids.add(sender.id)
|
recipient_profile_ids.add(sender.id)
|
||||||
huddle = get_huddle(list(recipient_profile_ids))
|
huddle = get_huddle(list(recipient_profile_ids))
|
||||||
return Recipient.objects.get(type_id=huddle.id, type=Recipient.HUDDLE)
|
return get_recipient(Recipient.HUDDLE, huddle.id)
|
||||||
else:
|
else:
|
||||||
return Recipient.objects.get(type_id=list(recipient_profile_ids)[0],
|
return get_recipient(Recipient.PERSONAL, list(recipient_profile_ids)[0])
|
||||||
type=Recipient.PERSONAL)
|
|
||||||
|
|
||||||
@authenticated_json_post_view
|
@authenticated_json_post_view
|
||||||
@has_request_variables
|
@has_request_variables
|
||||||
|
@ -890,7 +888,7 @@ def send_message_backend(request, user_profile, client,
|
||||||
stream = get_stream(stream_name, user_profile.realm)
|
stream = get_stream(stream_name, user_profile.realm)
|
||||||
if stream is None:
|
if stream is None:
|
||||||
return json_error("Stream does not exist")
|
return json_error("Stream does not exist")
|
||||||
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
elif message_type_name == 'private':
|
elif message_type_name == 'private':
|
||||||
not_forged_zephyr_mirror = client and client.name == "zephyr_mirror" and not forged
|
not_forged_zephyr_mirror = client and client.name == "zephyr_mirror" and not forged
|
||||||
try:
|
try:
|
||||||
|
@ -1127,7 +1125,7 @@ def json_stream_exists(request, user_profile, stream=POST):
|
||||||
stream = get_stream(stream, user_profile.realm)
|
stream = get_stream(stream, user_profile.realm)
|
||||||
result = {"exists": bool(stream)}
|
result = {"exists": bool(stream)}
|
||||||
if stream is not None:
|
if stream is not None:
|
||||||
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
result["subscribed"] = Subscription.objects.filter(user_profile=user_profile,
|
result["subscribed"] = Subscription.objects.filter(user_profile=user_profile,
|
||||||
recipient=recipient,
|
recipient=recipient,
|
||||||
active=True).exists()
|
active=True).exists()
|
||||||
|
@ -1137,7 +1135,7 @@ def get_subscription_or_die(stream_name, user_profile):
|
||||||
stream = get_stream(stream_name, user_profile.realm)
|
stream = get_stream(stream_name, user_profile.realm)
|
||||||
if not stream:
|
if not stream:
|
||||||
raise JsonableError("Invalid stream %s" % (stream.name,))
|
raise JsonableError("Invalid stream %s" % (stream.name,))
|
||||||
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
||||||
subscription = Subscription.objects.filter(user_profile=user_profile,
|
subscription = Subscription.objects.filter(user_profile=user_profile,
|
||||||
recipient=recipient, active=True)
|
recipient=recipient, active=True)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue