2021-03-27 12:23:32 +01:00
|
|
|
import datetime
|
2021-03-27 13:31:26 +01:00
|
|
|
from typing import Dict, List, Optional, Set
|
2021-03-27 12:23:32 +01:00
|
|
|
|
2021-03-27 13:31:26 +01:00
|
|
|
from zerver.lib.cache import cache_with_key, get_muting_users_cache_key
|
2021-03-27 12:23:32 +01:00
|
|
|
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(
|
2021-04-22 16:23:09 +02:00
|
|
|
"muted_user_id",
|
2021-03-27 12:23:32 +01:00
|
|
|
"date_muted",
|
|
|
|
)
|
|
|
|
return [
|
|
|
|
{
|
2021-04-22 16:23:09 +02:00
|
|
|
"id": row["muted_user_id"],
|
2021-03-27 12:23:32 +01:00
|
|
|
"timestamp": datetime_to_timestamp(row["date_muted"]),
|
|
|
|
}
|
|
|
|
for row in rows
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def add_user_mute(
|
2021-04-07 19:33:10 +02:00
|
|
|
user_profile: UserProfile, muted_user: UserProfile, date_muted: datetime.datetime
|
2021-03-27 12:23:32 +01:00
|
|
|
) -> None:
|
|
|
|
MutedUser.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
muted_user=muted_user,
|
|
|
|
date_muted=date_muted,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-04-08 06:20:43 +02:00
|
|
|
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
|
2021-03-27 13:31:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
@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,
|
2021-04-22 16:23:09 +02:00
|
|
|
).values("user_profile_id")
|
|
|
|
return {row["user_profile_id"] for row in rows}
|