Files
zulip/zerver/lib/user_mutes.py
Abhijeet Prasad Bodas 152508e346 mute user: Reduce two database fetches when unmuting to one.
Previously, when unmuting a user, we used to make
two database fetches - one to verify that the user
is has been muted before, and one while actually
unmuting the user.

This reduces that to one, by passing around the
`MutedUser` object fetched in the first round.

Since the new function returns `Optional[MutedUser]`,
we need to use a hack for events tests, because
mypy does not yet use the type inferred from
`assert foo is not None` in nested functions like lambdas.
See python/mypy@8780d45507.
2021-04-08 23:04:28 -07:00

37 lines
1.0 KiB
Python

import datetime
from typing import Dict, List, Optional
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