messages_for_topic: Use stream.recipient_id for more efficient query.

This commit is contained in:
Mateusz Mandera 2020-02-11 16:04:05 +01:00 committed by Tim Abbott
parent bddb370750
commit 2475adbf8a
3 changed files with 4 additions and 4 deletions

View File

@ -94,9 +94,9 @@ def filter_by_exact_message_topic(query: QuerySet, message: Message) -> QuerySet
def filter_by_topic_name_via_message(query: QuerySet, topic_name: str) -> QuerySet:
return query.filter(message__subject__iexact=topic_name)
def messages_for_topic(stream_id: int, topic_name: str) -> QuerySet:
def messages_for_topic(stream_recipient_id: int, topic_name: str) -> QuerySet:
return Message.objects.filter(
recipient__type_id=stream_id,
recipient_id=stream_recipient_id,
subject__iexact=topic_name,
)

View File

@ -43,7 +43,7 @@ def archive(request: HttpRequest,
all_messages = list(
messages_for_topic(
stream_id=stream_id,
stream_recipient_id=stream.recipient_id,
topic_name=topic_name,
).select_related('sender').order_by('date_sent')
)

View File

@ -508,7 +508,7 @@ def delete_in_topic(request: HttpRequest, user_profile: UserProfile,
topic_name: str=REQ("topic_name")) -> HttpResponse:
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
messages = messages_for_topic(stream.id, topic_name)
messages = messages_for_topic(stream.recipient_id, topic_name)
if not stream.is_history_public_to_subscribers():
# Don't allow the user to delete messages that they don't have access to.
deletable_message_ids = UserMessage.objects.filter(