From dfeb89610786b1ba63cffe842f02da6f3b7e7867 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Thu, 18 Apr 2024 13:22:37 +0530 Subject: [PATCH] mention: Use NamedUserGroup objects in mention code. --- zerver/lib/mention.py | 8 ++++---- zerver/lib/message.py | 4 ++-- zerver/lib/notification_data.py | 4 ++-- ...514_update_usergroup_foreign_keys_to_namedusergroup.py | 7 +++++++ zerver/models/scheduled_jobs.py | 4 ++-- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py index f15cb52d8a..3ad4ce3685 100644 --- a/zerver/lib/mention.py +++ b/zerver/lib/mention.py @@ -7,7 +7,7 @@ from django.conf import settings from django.db.models import Q from zerver.lib.users import get_inaccessible_user_ids -from zerver.models import UserGroup, UserProfile +from zerver.models import NamedUserGroup, UserProfile from zerver.models.streams import get_linkable_streams BEFORE_MENTION_ALLOWED_REGEX = r"(? None: - self.user_group_name_info: Dict[str, UserGroup] = {} + self.user_group_name_info: Dict[str, NamedUserGroup] = {} self.user_group_members: Dict[int, List[int]] = {} user_group_names = possible_user_group_mentions(content) if user_group_names: - for group in UserGroup.objects.filter( + for group in NamedUserGroup.objects.filter( realm_id=realm_id, name__in=user_group_names, is_system_group=False ).prefetch_related("direct_members"): self.user_group_name_info[group.name.lower()] = group @@ -293,7 +293,7 @@ class MentionData: """ return set(self.user_id_info.keys()) - def get_user_group(self, name: str) -> Optional[UserGroup]: + def get_user_group(self, name: str) -> Optional[NamedUserGroup]: return self.user_group_name_info.get(name.lower(), None) def get_group_members(self, user_group_id: int) -> List[int]: diff --git a/zerver/lib/message.py b/zerver/lib/message.py index 1a190a2a55..e294dc0a90 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -47,11 +47,11 @@ from zerver.lib.user_topics import build_get_topic_visibility_policy, get_topic_ from zerver.lib.users import get_inaccessible_user_ids from zerver.models import ( Message, + NamedUserGroup, Realm, Recipient, Stream, Subscription, - UserGroup, UserMessage, UserProfile, UserTopic, @@ -1213,7 +1213,7 @@ def stream_wildcard_mention_allowed(sender: UserProfile, stream: Stream, realm: def check_user_group_mention_allowed(sender: UserProfile, user_group_ids: List[int]) -> None: - user_groups = UserGroup.objects.filter(id__in=user_group_ids).select_related( + user_groups = NamedUserGroup.objects.filter(id__in=user_group_ids).select_related( "can_mention_group" ) sender_is_system_bot = is_cross_realm_bot_email(sender.delivery_email) diff --git a/zerver/lib/notification_data.py b/zerver/lib/notification_data.py index 95f669ba36..2628df5d97 100644 --- a/zerver/lib/notification_data.py +++ b/zerver/lib/notification_data.py @@ -4,7 +4,7 @@ from typing import Any, Collection, Dict, List, Optional, Set from zerver.lib.mention import MentionData from zerver.lib.user_groups import get_user_group_member_ids -from zerver.models import UserGroup, UserProfile, UserTopic +from zerver.models import NamedUserGroup, UserProfile, UserTopic from zerver.models.scheduled_jobs import NotificationTriggers @@ -352,7 +352,7 @@ def get_mentioned_user_group( # all these messages. smallest_user_group_size = math.inf for user_group_id in mentioned_user_group_ids: - current_user_group = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm) + current_user_group = NamedUserGroup.objects.get(id=user_group_id, realm=user_profile.realm) current_mentioned_user_group = MentionedUserGroup( id=current_user_group.id, name=current_user_group.name, diff --git a/zerver/migrations/0514_update_usergroup_foreign_keys_to_namedusergroup.py b/zerver/migrations/0514_update_usergroup_foreign_keys_to_namedusergroup.py index 5d4ed4f4f6..086999a07a 100644 --- a/zerver/migrations/0514_update_usergroup_foreign_keys_to_namedusergroup.py +++ b/zerver/migrations/0514_update_usergroup_foreign_keys_to_namedusergroup.py @@ -17,4 +17,11 @@ class Migration(migrations.Migration): null=True, on_delete=django.db.models.deletion.CASCADE, to="zerver.namedusergroup" ), ), + migrations.AlterField( + model_name="scheduledmessagenotificationemail", + name="mentioned_user_group", + field=models.ForeignKey( + null=True, on_delete=django.db.models.deletion.CASCADE, to="zerver.namedusergroup" + ), + ), ] diff --git a/zerver/models/scheduled_jobs.py b/zerver/models/scheduled_jobs.py index 18d84a4668..deea50f7f3 100644 --- a/zerver/models/scheduled_jobs.py +++ b/zerver/models/scheduled_jobs.py @@ -13,7 +13,7 @@ from zerver.lib.display_recipient import get_recipient_ids from zerver.lib.timestamp import datetime_to_timestamp from zerver.models.clients import Client from zerver.models.constants import MAX_TOPIC_NAME_LENGTH -from zerver.models.groups import UserGroup +from zerver.models.groups import NamedUserGroup from zerver.models.messages import Message from zerver.models.realms import Realm from zerver.models.recipients import Recipient @@ -113,7 +113,7 @@ class ScheduledMessageNotificationEmail(models.Model): ] trigger = models.TextField(choices=EMAIL_NOTIFICATION_TRIGGER_CHOICES) - mentioned_user_group = models.ForeignKey(UserGroup, null=True, on_delete=CASCADE) + mentioned_user_group = models.ForeignKey(NamedUserGroup, null=True, on_delete=CASCADE) # Timestamp for when the notification should be processed and sent. # Calculated from the time the event was received and the batching period.