2023-12-15 03:18:20 +01:00
|
|
|
import hashlib
|
|
|
|
from collections import defaultdict
|
2024-07-12 02:30:17 +02:00
|
|
|
from typing import TYPE_CHECKING
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
from django.db import models, transaction
|
|
|
|
from django_stubs_ext import ValuesQuerySet
|
|
|
|
from typing_extensions import override
|
|
|
|
|
|
|
|
from zerver.lib.display_recipient import get_display_recipient
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from zerver.models import Subscription
|
|
|
|
|
|
|
|
|
|
|
|
class Recipient(models.Model):
|
|
|
|
"""Represents an audience that can potentially receive messages in Zulip.
|
|
|
|
|
|
|
|
This table essentially functions as a generic foreign key that
|
|
|
|
allows Message.recipient_id to be a simple ForeignKey representing
|
|
|
|
the audience for a message, while supporting the different types
|
|
|
|
of audiences Zulip supports for a message.
|
|
|
|
|
|
|
|
Recipient has just two attributes: The enum type, and a type_id,
|
2024-07-05 13:13:40 +02:00
|
|
|
which is the ID of the UserProfile/Stream/DirectMessageGroup object
|
|
|
|
containing all the metadata for the audience. There are 3 recipient
|
|
|
|
types:
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
1. 1:1 direct message: The type_id is the ID of the UserProfile
|
|
|
|
who will receive any message to this Recipient. The sender
|
|
|
|
of such a message is represented separately.
|
|
|
|
2. Stream message: The type_id is the ID of the associated Stream.
|
|
|
|
3. Group direct message: In Zulip, group direct messages are
|
2024-07-05 13:13:40 +02:00
|
|
|
represented by DirectMessageGroup objects, which encode the set of
|
|
|
|
users in the conversation. The type_id is the ID of the associated
|
|
|
|
DirectMessageGroup object; the set of users is usually retrieved
|
|
|
|
via the Subscription table. See the DirectMessageGroup model for
|
|
|
|
details.
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
See also the Subscription model, which stores which UserProfile
|
|
|
|
objects are subscribed to which Recipient objects.
|
|
|
|
"""
|
|
|
|
|
2024-05-29 21:41:40 +02:00
|
|
|
id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")
|
2023-12-15 03:18:20 +01:00
|
|
|
type_id = models.IntegerField(db_index=True)
|
|
|
|
type = models.PositiveSmallIntegerField(db_index=True)
|
2024-07-08 16:46:01 +02:00
|
|
|
# Valid types are {personal, stream, direct_message_group}
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
# The type for 1:1 direct messages.
|
|
|
|
PERSONAL = 1
|
|
|
|
# The type for stream messages.
|
|
|
|
STREAM = 2
|
|
|
|
# The type group direct messages.
|
2024-03-22 00:39:33 +01:00
|
|
|
DIRECT_MESSAGE_GROUP = 3
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("type", "type_id")
|
|
|
|
|
|
|
|
# N.B. If we used Django's choice=... we would get this for free (kinda)
|
2024-07-08 16:46:01 +02:00
|
|
|
_type_names = {
|
|
|
|
PERSONAL: "personal",
|
|
|
|
STREAM: "stream",
|
|
|
|
DIRECT_MESSAGE_GROUP: "direct_message_group",
|
|
|
|
}
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.label()} ({self.type_id}, {self.type})"
|
|
|
|
|
|
|
|
def label(self) -> str:
|
|
|
|
from zerver.models import Stream
|
|
|
|
|
|
|
|
if self.type == Recipient.STREAM:
|
|
|
|
return Stream.objects.get(id=self.type_id).name
|
|
|
|
else:
|
|
|
|
return str(get_display_recipient(self))
|
|
|
|
|
|
|
|
def type_name(self) -> str:
|
|
|
|
# Raises KeyError if invalid
|
|
|
|
return self._type_names[self.type]
|
|
|
|
|
|
|
|
|
2024-07-04 14:05:48 +02:00
|
|
|
def get_direct_message_group_user_ids(recipient: Recipient) -> ValuesQuerySet["Subscription", int]:
|
2023-12-15 03:18:20 +01:00
|
|
|
from zerver.models import Subscription
|
|
|
|
|
2024-03-22 00:39:33 +01:00
|
|
|
assert recipient.type == Recipient.DIRECT_MESSAGE_GROUP
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
Subscription.objects.filter(
|
|
|
|
recipient=recipient,
|
|
|
|
)
|
|
|
|
.order_by("user_profile_id")
|
|
|
|
.values_list("user_profile_id", flat=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def bulk_get_direct_message_group_user_ids(recipient_ids: list[int]) -> dict[int, set[int]]:
|
2023-12-15 03:18:20 +01:00
|
|
|
"""
|
2024-07-08 16:46:01 +02:00
|
|
|
Takes a list of direct_message_group type recipient_ids, returns
|
|
|
|
a dictmapping recipient id to list of user ids in the direct
|
|
|
|
message group.
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
We rely on our caller to pass us recipient_ids that correspond
|
2024-07-08 16:46:01 +02:00
|
|
|
to direct_message_group, but technically this function is valid
|
|
|
|
for any typeof subscription.
|
2023-12-15 03:18:20 +01:00
|
|
|
"""
|
|
|
|
from zerver.models import Subscription
|
|
|
|
|
|
|
|
if not recipient_ids:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
subscriptions = Subscription.objects.filter(
|
|
|
|
recipient_id__in=recipient_ids,
|
|
|
|
).only("user_profile_id", "recipient_id")
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
result_dict: dict[int, set[int]] = defaultdict(set)
|
2023-12-15 03:18:20 +01:00
|
|
|
for subscription in subscriptions:
|
|
|
|
result_dict[subscription.recipient_id].add(subscription.user_profile_id)
|
|
|
|
|
|
|
|
return result_dict
|
|
|
|
|
|
|
|
|
2024-07-05 13:13:40 +02:00
|
|
|
class DirectMessageGroup(models.Model):
|
2023-12-15 03:18:20 +01:00
|
|
|
"""
|
|
|
|
Represents a group of individuals who may have a
|
|
|
|
group direct message conversation together.
|
|
|
|
|
2024-07-05 13:13:40 +02:00
|
|
|
The membership of the DirectMessageGroup is stored in the Subscription
|
|
|
|
table just like with Streams - for each user in the DirectMessageGroup,
|
|
|
|
there is a Subscription object tied to the UserProfile and the
|
|
|
|
DirectMessageGroup's recipient object.
|
2023-12-15 03:18:20 +01:00
|
|
|
|
|
|
|
A hash of the list of user IDs is stored in the huddle_hash field
|
|
|
|
below, to support efficiently mapping from a set of users to the
|
2024-07-05 13:13:40 +02:00
|
|
|
corresponding DirectMessageGroup object.
|
2023-12-15 03:18:20 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: We should consider whether using
|
|
|
|
# CommaSeparatedIntegerField would be better.
|
|
|
|
huddle_hash = models.CharField(max_length=40, db_index=True, unique=True)
|
2024-07-05 13:13:40 +02:00
|
|
|
# Foreign key to the Recipient object for this DirectMessageGroup.
|
2023-12-15 03:18:20 +01:00
|
|
|
recipient = models.ForeignKey(Recipient, null=True, on_delete=models.SET_NULL)
|
|
|
|
|
2024-07-05 13:13:40 +02:00
|
|
|
# TODO: The model still uses the old "zerver_huddle" database table.
|
|
|
|
# As a part of the migration of "Huddle" to "DirectMessageGroup"
|
|
|
|
# it needs to be renamed to "zerver_directmessagegroup".
|
|
|
|
class Meta:
|
|
|
|
db_table = "zerver_huddle"
|
|
|
|
|
2023-12-15 03:18:20 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_direct_message_group_hash(id_list: list[int]) -> str:
|
2023-12-15 03:18:20 +01:00
|
|
|
id_list = sorted(set(id_list))
|
|
|
|
hash_key = ",".join(str(x) for x in id_list)
|
|
|
|
return hashlib.sha1(hash_key.encode()).hexdigest()
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_or_create_direct_message_group(id_list: list[int]) -> DirectMessageGroup:
|
2023-12-15 03:18:20 +01:00
|
|
|
"""
|
2024-07-05 13:13:40 +02:00
|
|
|
Takes a list of user IDs and returns the DirectMessageGroup
|
|
|
|
object for the group consisting of these users. If the
|
|
|
|
DirectMessageGroup object does not yet exist, it will be
|
|
|
|
transparently created.
|
2023-12-15 03:18:20 +01:00
|
|
|
"""
|
|
|
|
from zerver.models import Subscription, UserProfile
|
|
|
|
|
2024-07-04 14:05:48 +02:00
|
|
|
direct_message_group_hash = get_direct_message_group_hash(id_list)
|
2023-12-15 03:18:20 +01:00
|
|
|
with transaction.atomic():
|
2024-07-05 13:13:40 +02:00
|
|
|
(direct_message_group, created) = DirectMessageGroup.objects.get_or_create(
|
2024-07-04 14:05:48 +02:00
|
|
|
huddle_hash=direct_message_group_hash
|
|
|
|
)
|
2023-12-15 03:18:20 +01:00
|
|
|
if created:
|
2024-03-22 00:39:33 +01:00
|
|
|
recipient = Recipient.objects.create(
|
2024-07-04 14:05:48 +02:00
|
|
|
type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP
|
2024-03-22 00:39:33 +01:00
|
|
|
)
|
2024-07-04 14:05:48 +02:00
|
|
|
direct_message_group.recipient = recipient
|
|
|
|
direct_message_group.save(update_fields=["recipient"])
|
2023-12-15 03:18:20 +01:00
|
|
|
subs_to_create = [
|
|
|
|
Subscription(
|
|
|
|
recipient=recipient,
|
|
|
|
user_profile_id=user_profile_id,
|
|
|
|
is_user_active=is_active,
|
|
|
|
)
|
|
|
|
for user_profile_id, is_active in UserProfile.objects.filter(id__in=id_list)
|
|
|
|
.distinct("id")
|
|
|
|
.values_list("id", "is_active")
|
|
|
|
]
|
|
|
|
Subscription.objects.bulk_create(subs_to_create)
|
2024-07-04 14:05:48 +02:00
|
|
|
return direct_message_group
|