2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-08-07 04:58:22 +02:00
|
|
|
def get_user_info_dict(realm_id: int) -> Dict[str, Dict[str, Any]]:
|
2021-02-12 08:19:30 +01:00
|
|
|
rows = (
|
|
|
|
UserStatus.objects.filter(
|
|
|
|
user_profile__realm_id=realm_id,
|
|
|
|
user_profile__is_active=True,
|
|
|
|
)
|
|
|
|
.exclude(
|
2021-02-12 08:20:45 +01:00
|
|
|
Q(status=UserStatus.NORMAL) & Q(status_text=""),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
.values(
|
2021-02-12 08:20:45 +01:00
|
|
|
"user_profile_id",
|
|
|
|
"status",
|
|
|
|
"status_text",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-01-21 19:06:03 +01:00
|
|
|
)
|
|
|
|
|
2020-09-02 08:14:51 +02:00
|
|
|
user_dict: Dict[str, Dict[str, Any]] = {}
|
2019-01-21 19:06:03 +01:00
|
|
|
for row in rows:
|
2021-02-12 08:20:45 +01:00
|
|
|
away = row["status"] == UserStatus.AWAY
|
|
|
|
status_text = row["status_text"]
|
|
|
|
user_id = row["user_profile_id"]
|
2019-01-21 19:06:03 +01:00
|
|
|
|
2020-09-02 08:14:51 +02:00
|
|
|
dct = {}
|
2019-01-21 19:06:03 +01:00
|
|
|
if away:
|
2021-02-12 08:20:45 +01:00
|
|
|
dct["away"] = away
|
2019-01-21 19:06:03 +01:00
|
|
|
if status_text:
|
2021-02-12 08:20:45 +01:00
|
|
|
dct["status_text"] = status_text
|
2018-12-17 16:19:18 +01:00
|
|
|
|
2020-08-07 04:58:22 +02:00
|
|
|
user_dict[str(user_id)] = dct
|
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(
|
|
|
|
user_profile_id: int, status: Optional[int], status_text: Optional[str], client_id: int
|
|
|
|
) -> 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 is not None:
|
2021-02-12 08:20:45 +01:00
|
|
|
defaults["status"] = status
|
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
|
|
|
|
|
|
|
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,
|
|
|
|
)
|