mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 16:14:02 +00:00
model__id syntax implies needing a JOIN on the model table to fetch the id. That's usually redundant, because the first table in the query simply has a 'model_id' column, so the id can be fetched directly. Django is actually smart enough to not do those redundant joins, but we should still avoid this misguided syntax. The exceptions are ManytoMany fields and queries doing a backward relationship lookup. If "streams" is a many-to-many relationship, then streams_id is invalid - streams__id syntax is needed. If "y" is a foreign fields from X to Y: class X: y = models.ForeignKey(Y) then object x of class X has the field x.y_id, but y of class Y doesn't have y.x_id. Thus Y queries need to be done like Y.objects.filter(x__id__in=some_list)
53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import datetime
|
|
from typing import Dict, List, Optional, Set
|
|
|
|
from zerver.lib.cache import cache_with_key, get_muting_users_cache_key
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
|
from zerver.models import MutedUser, UserProfile
|
|
|
|
|
|
def get_user_mutes(user_profile: UserProfile) -> List[Dict[str, int]]:
|
|
rows = MutedUser.objects.filter(user_profile=user_profile).values(
|
|
"muted_user_id",
|
|
"date_muted",
|
|
)
|
|
return [
|
|
{
|
|
"id": row["muted_user_id"],
|
|
"timestamp": datetime_to_timestamp(row["date_muted"]),
|
|
}
|
|
for row in rows
|
|
]
|
|
|
|
|
|
def add_user_mute(
|
|
user_profile: UserProfile, muted_user: UserProfile, date_muted: datetime.datetime
|
|
) -> None:
|
|
MutedUser.objects.create(
|
|
user_profile=user_profile,
|
|
muted_user=muted_user,
|
|
date_muted=date_muted,
|
|
)
|
|
|
|
|
|
def get_mute_object(user_profile: UserProfile, muted_user: UserProfile) -> Optional[MutedUser]:
|
|
try:
|
|
return MutedUser.objects.get(user_profile=user_profile, muted_user=muted_user)
|
|
except MutedUser.DoesNotExist:
|
|
return None
|
|
|
|
|
|
@cache_with_key(get_muting_users_cache_key, timeout=3600 * 24 * 7)
|
|
def get_muting_users(muted_user: UserProfile) -> Set[int]:
|
|
"""
|
|
This is kind of the inverse of `get_user_mutes` above.
|
|
While `get_user_mutes` is mainly used for event system work,
|
|
this is used in the message send codepath, to get a list
|
|
of IDs of users who have muted a particular user.
|
|
The result will also include deactivated users.
|
|
"""
|
|
rows = MutedUser.objects.filter(
|
|
muted_user=muted_user,
|
|
).values("user_profile_id")
|
|
return {row["user_profile_id"] for row in rows}
|