Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

models: Add visibility policy indexes on UserTopic.

Abhijeet Prasad Bodasbc60109867fc
diff --git a/zerver/migrations/0351_user_topic_visibility_indexes.py b/zerver/migrations/0351_user_topic_visibility_indexes.py
new file mode 100644
index 00000000000..1c513fbf83a
--- /dev/null
+++ b/zerver/migrations/0351_user_topic_visibility_indexes.py
@@ -0,0 +1,27 @@
+# Generated by Django 3.2.6 on 2021-09-17 18:34
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("zerver", "0350_usertopic_visibility_policy"),
+ ]
+
+ operations = [
+ migrations.AddIndex(
+ model_name="usertopic",
+ index=models.Index(
+ fields=["stream", "topic_name", "visibility_policy", "user_profile"],
+ name="zerver_usertopic_stream_topic_user_visibility_idx",
+ ),
+ ),
+ migrations.AddIndex(
+ model_name="usertopic",
+ index=models.Index(
+ fields=["user_profile", "visibility_policy", "stream", "topic_name"],
+ name="zerver_usertopic_user_visibility_idx",
+ ),
+ ),
+ ]
diff --git a/zerver/models.py b/zerver/models.py
index 7c9fd456527..ec2ca708650 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -2205,6 +2205,9 @@ class UserTopic(models.Model):
default=datetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
)
+ # Implicitly, if a UserTopic does not exist, the (user, topic)
+ # pair should have normal behavior for that (user, stream) pair.
+
# A normal muted topic. No notifications and unreads hidden.
MUTED = 1
@@ -2228,6 +2231,22 @@ class UserTopic(models.Model):
class Meta:
unique_together = ("user_profile", "stream", "topic_name")
+ indexes = [
+ # This index is designed to optimize queries fetching the
+ # set of users who have special policy for a stream,
+ # e.g. for the send-message code paths.
+ models.Index(
+ fields=("stream", "topic_name", "visibility_policy", "user_profile"),
+ name="zerver_usertopic_stream_topic_user_visibility_idx",
+ ),
+ # This index is useful for handling API requests fetching the
+ # muted topics for a given user or user/stream pair.
+ models.Index(
+ fields=("user_profile", "visibility_policy", "stream", "topic_name"),
+ name="zerver_usertopic_user_visibility_idx",
+ ),
+ ]
+
def __str__(self) -> str:
return f"<UserTopic: ({self.user_profile.email}, {self.stream.name}, {self.topic_name}, {self.last_updated})>"