2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime
|
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
|
2021-07-25 16:31:12 +02:00
|
|
|
from zerver.lib.utils import assert_is_not_none
|
2021-03-27 12:23:32 +01:00
|
|
|
from zerver.models import MutedUser, UserProfile
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_user_mutes(user_profile: UserProfile) -> list[dict[str, int]]:
|
2021-03-27 12:23:32 +01:00
|
|
|
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-07-25 16:31:12 +02:00
|
|
|
"timestamp": datetime_to_timestamp(assert_is_not_none(row["date_muted"])),
|
2021-03-27 12:23:32 +01:00
|
|
|
}
|
|
|
|
for row in rows
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-11-19 19:45:19 +01:00
|
|
|
def add_user_mute(user_profile: UserProfile, muted_user: UserProfile, date_muted: datetime) -> None:
|
2021-03-27 12:23:32 +01:00
|
|
|
MutedUser.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
muted_user=muted_user,
|
|
|
|
date_muted=date_muted,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_mute_object(user_profile: UserProfile, muted_user: UserProfile) -> MutedUser | None:
|
2021-04-08 06:20:43 +02:00
|
|
|
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)
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_muting_users(muted_user_id: int) -> set[int]:
|
2021-03-27 13:31:26 +01:00
|
|
|
"""
|
|
|
|
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(
|
2021-06-06 04:47:36 +02:00
|
|
|
muted_user_id=muted_user_id,
|
2021-04-22 16:23:09 +02:00
|
|
|
).values("user_profile_id")
|
|
|
|
return {row["user_profile_id"] for row in rows}
|