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.
This commit is contained in:
Abhijeet Prasad Bodas
2021-03-27 16:53:32 +05:30
committed by Tim Abbott
parent 89f6139505
commit 3bfcaa3968
21 changed files with 522 additions and 12 deletions

43
zerver/lib/user_mutes.py Normal file
View File

@@ -0,0 +1,43 @@
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()