Files
zulip/zerver/lib/user_mutes.py
Abhijeet Prasad Bodas 3bfcaa3968 mute user: Add backend infrastructure code.
Adds backend code for the mute users feature.
This is just infrastructure work (database
interactions, helpers, tests, events, API docs
etc) and does not involve any behavioral/semantic
aspects of muted users.

Adds POST and DELETE endpoints, to keep the
URL scheme mostly consistent in terms of `users/me`.

TODOs:
1. Add tests for exporting `zulip_muteduser` database table.
2. Add dedicated methods to python-zulip-api to be used
   in place of the current `client.call_endpoint` implementation.
2021-04-06 18:44:08 -07:00

44 lines
1.2 KiB
Python

import datetime
from typing import Dict, List, Optional
from django.utils.timezone import now as timezone_now
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: Optional[datetime.datetime] = None,
) -> None:
if date_muted is None:
date_muted = timezone_now()
MutedUser.objects.create(
user_profile=user_profile,
muted_user=muted_user,
date_muted=date_muted,
)
def remove_user_mute(user_profile: UserProfile, muted_user: UserProfile) -> None:
MutedUser.objects.get(user_profile=user_profile, muted_user=muted_user).delete()
def user_is_muted(user_profile: UserProfile, muted_user: UserProfile) -> bool:
return MutedUser.objects.filter(user_profile=user_profile, muted_user=muted_user).exists()