From 0359c083d13289a4879ce25216beb689ec17a772 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 30 Dec 2021 12:59:30 +0000 Subject: [PATCH] refactor: Extract get_linkable_streams. This is a one-liner with two purposes: * We want the comment to explain the business rule. * We want to just work in id space. --- zerver/lib/mention.py | 6 +++--- zerver/models.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py index e144b5d09e..39aa842d9c 100644 --- a/zerver/lib/mention.py +++ b/zerver/lib/mention.py @@ -5,7 +5,7 @@ from typing import Dict, List, Match, Optional, Set, Tuple from django.db.models import Q -from zerver.models import Realm, UserGroup, UserProfile, get_active_streams +from zerver.models import Realm, UserGroup, UserProfile, get_linkable_streams # Match multi-word string between @** ** or match any one-word # sequences after @ @@ -206,8 +206,8 @@ def get_stream_name_map(realm: Realm, stream_names: Set[str]) -> Dict[str, int]: q_list = {Q(name=name) for name in stream_names} rows = ( - get_active_streams( - realm=realm, + get_linkable_streams( + realm_id=realm.id, ) .filter( functools.reduce(lambda a, b: a | b, q_list), diff --git a/zerver/models.py b/zerver/models.py index 3173782d47..9a720aa3e8 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -2476,6 +2476,16 @@ def get_active_streams(realm: Optional[Realm]) -> QuerySet: return Stream.objects.filter(realm=realm, deactivated=False) +def get_linkable_streams(realm_id: int) -> QuerySet: + """ + This returns the streams that we are allowed to linkify using + something like "#frontend" in our markup. For now the business + rule is that you can link any stream in the realm that hasn't + been deactivated (similar to how get_active_streams works). + """ + return Stream.objects.filter(realm_id=realm_id, deactivated=False) + + def get_stream(stream_name: str, realm: Realm) -> Stream: """ Callers that don't have a Realm object already available should use