Files
upstream-zulip/zerver/models/user_topics.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

88 lines
3.7 KiB
Python

from datetime import datetime, timezone
from django.db import models
from django.db.models import CASCADE
from django.db.models.functions import Lower, Upper
from typing_extensions import override
from zerver.models.constants import MAX_TOPIC_NAME_LENGTH
from zerver.models.recipients import Recipient
from zerver.models.streams import Stream
from zerver.models.users import UserProfile
class UserTopic(models.Model):
user_profile = models.ForeignKey(UserProfile, on_delete=CASCADE)
stream = models.ForeignKey(Stream, on_delete=CASCADE)
recipient = models.ForeignKey(Recipient, on_delete=CASCADE)
topic_name = models.CharField(max_length=MAX_TOPIC_NAME_LENGTH)
# The default value for last_updated is a few weeks before tracking
# of when topics were muted was first introduced. It's designed
# to be obviously incorrect so that one can tell it's backfilled data.
last_updated = models.DateTimeField(default=datetime(2020, 1, 1, 0, 0, tzinfo=timezone.utc))
class VisibilityPolicy(models.IntegerChoices):
# A normal muted topic. No notifications and unreads hidden.
MUTED = 1, "Muted topic"
# This topic will behave like an unmuted topic in an unmuted stream even if it
# belongs to a muted stream.
UNMUTED = 2, "Unmuted topic in muted stream"
# This topic will behave like `UNMUTED`, plus some additional
# display and/or notifications priority that is TBD and likely to
# be configurable; see #6027. Not yet implemented.
FOLLOWED = 3, "Followed topic"
# Implicitly, if a UserTopic does not exist, the (user, topic)
# pair should have normal behavior for that (user, stream) pair.
# We use this in our code to represent the condition in the comment above.
INHERIT = 0, "User's default policy for the stream."
visibility_policy = models.SmallIntegerField(
choices=VisibilityPolicy.choices, default=VisibilityPolicy.MUTED
)
class Meta:
constraints = [
models.UniqueConstraint(
"user_profile",
"stream",
Lower("topic_name"),
name="usertopic_case_insensitive_topic_uniq",
),
]
indexes = [
models.Index("stream", Upper("topic_name"), name="zerver_mutedtopic_stream_topic"),
# 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",
),
# Supports the per-message visibility policy lookup in
# matching_user_topic_exists_q, which has the recipient
# rather than the channel and matches topic names
# case-insensitively, so the index above cannot serve it.
models.Index(
"user_profile",
"visibility_policy",
"recipient",
Upper("topic_name"),
name="zerver_usertopic_user_visibility_recipient_idx",
),
]
@override
def __str__(self) -> str:
return f"({self.user_profile.email}, {self.stream.name}, {self.topic_name}, {self.last_updated})"