2022-07-21 15:19:40 +02:00
|
|
|
from typing import Dict, Optional, TypedDict
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
from django.db.models import Q
|
2018-12-17 16:19:18 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserStatus
|
2018-12-17 16:19:18 +01:00
|
|
|
|
|
|
|
|
2022-07-21 15:19:40 +02:00
|
|
|
class UserInfoDict(TypedDict, total=False):
|
|
|
|
status_text: str
|
|
|
|
emoji_name: str
|
|
|
|
emoji_code: str
|
|
|
|
reaction_type: str
|
|
|
|
away: bool
|
|
|
|
|
|
|
|
|
|
|
|
class RawUserInfoDict(TypedDict):
|
|
|
|
user_profile_id: int
|
2022-09-22 12:12:08 +02:00
|
|
|
user_profile__presence_enabled: bool
|
2022-07-21 15:19:40 +02:00
|
|
|
status_text: str
|
|
|
|
emoji_name: str
|
|
|
|
emoji_code: str
|
|
|
|
reaction_type: str
|
|
|
|
|
|
|
|
|
|
|
|
def format_user_status(row: RawUserInfoDict) -> UserInfoDict:
|
2022-09-22 12:12:08 +02:00
|
|
|
# Deprecated way for clients to access the user's `presence_enabled`
|
2022-09-22 12:21:00 +02:00
|
|
|
# setting, with away != presence_enabled. Can be removed when clients
|
|
|
|
# migrate "away" (also referred to as "unavailable") feature to directly
|
|
|
|
# use and update the user's presence_enabled setting.
|
2022-09-22 12:12:08 +02:00
|
|
|
presence_enabled = row["user_profile__presence_enabled"]
|
|
|
|
away = not presence_enabled
|
2021-09-03 01:46:24 +02:00
|
|
|
status_text = row["status_text"]
|
|
|
|
emoji_name = row["emoji_name"]
|
|
|
|
emoji_code = row["emoji_code"]
|
|
|
|
reaction_type = row["reaction_type"]
|
|
|
|
|
2022-07-21 15:19:40 +02:00
|
|
|
dct: UserInfoDict = {}
|
2021-09-03 01:46:24 +02:00
|
|
|
if away:
|
|
|
|
dct["away"] = away
|
|
|
|
if status_text:
|
|
|
|
dct["status_text"] = status_text
|
|
|
|
if emoji_name:
|
|
|
|
dct["emoji_name"] = emoji_name
|
|
|
|
dct["emoji_code"] = emoji_code
|
|
|
|
dct["reaction_type"] = reaction_type
|
|
|
|
|
|
|
|
return dct
|
|
|
|
|
|
|
|
|
2022-09-16 18:15:15 +02:00
|
|
|
def get_user_status_dict(realm_id: int) -> Dict[str, UserInfoDict]:
|
2021-02-12 08:19:30 +01:00
|
|
|
rows = (
|
|
|
|
UserStatus.objects.filter(
|
|
|
|
user_profile__realm_id=realm_id,
|
|
|
|
user_profile__is_active=True,
|
|
|
|
)
|
|
|
|
.exclude(
|
2022-09-22 12:12:08 +02:00
|
|
|
Q(user_profile__presence_enabled=True)
|
2021-06-22 18:42:31 +02:00
|
|
|
& Q(status_text="")
|
|
|
|
& Q(emoji_name="")
|
|
|
|
& Q(emoji_code="")
|
|
|
|
& Q(reaction_type=UserStatus.UNICODE_EMOJI),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
.values(
|
2021-02-12 08:20:45 +01:00
|
|
|
"user_profile_id",
|
2022-09-22 12:12:08 +02:00
|
|
|
"user_profile__presence_enabled",
|
2021-02-12 08:20:45 +01:00
|
|
|
"status_text",
|
2021-06-22 18:42:31 +02:00
|
|
|
"emoji_name",
|
|
|
|
"emoji_code",
|
|
|
|
"reaction_type",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2022-07-21 15:19:40 +02:00
|
|
|
user_dict: Dict[str, UserInfoDict] = {}
|
2019-01-21 19:06:03 +01:00
|
|
|
for row in rows:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_id = row["user_profile_id"]
|
2021-09-03 01:46:24 +02:00
|
|
|
user_dict[str(user_id)] = format_user_status(row)
|
2019-01-21 19:06:03 +01:00
|
|
|
|
|
|
|
return user_dict
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def update_user_status(
|
2021-06-22 18:42:31 +02:00
|
|
|
user_profile_id: int,
|
|
|
|
status_text: Optional[str],
|
|
|
|
client_id: int,
|
|
|
|
emoji_name: Optional[str],
|
|
|
|
emoji_code: Optional[str],
|
|
|
|
reaction_type: Optional[str],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2018-12-17 16:19:18 +01:00
|
|
|
timestamp = timezone_now()
|
|
|
|
|
2019-01-21 18:19:59 +01:00
|
|
|
defaults = dict(
|
|
|
|
client_id=client_id,
|
|
|
|
timestamp=timestamp,
|
2018-12-17 16:19:18 +01:00
|
|
|
)
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
if status_text is not None:
|
2021-02-12 08:20:45 +01:00
|
|
|
defaults["status_text"] = status_text
|
2019-01-21 18:19:59 +01:00
|
|
|
|
2021-06-22 18:42:31 +02:00
|
|
|
if emoji_name is not None:
|
|
|
|
defaults["emoji_name"] = emoji_name
|
|
|
|
|
|
|
|
if emoji_code is not None:
|
|
|
|
defaults["emoji_code"] = emoji_code
|
|
|
|
|
|
|
|
if reaction_type is not None:
|
|
|
|
defaults["reaction_type"] = reaction_type
|
|
|
|
|
2019-01-21 18:19:59 +01:00
|
|
|
UserStatus.objects.update_or_create(
|
2018-12-17 16:19:18 +01:00
|
|
|
user_profile_id=user_profile_id,
|
2019-01-21 18:19:59 +01:00
|
|
|
defaults=defaults,
|
|
|
|
)
|