2016-07-26 23:26:39 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import time
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, Text
|
2016-07-26 23:26:39 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-11-06 02:49:37 +01:00
|
|
|
from django.utils import timezone
|
2016-07-26 23:26:39 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from zerver.decorator import authenticated_json_post_view
|
|
|
|
from zerver.lib.actions import get_status_dict, update_user_presence
|
|
|
|
from zerver.lib.request import has_request_variables, REQ, JsonableError
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.lib.validator import check_bool
|
2017-02-11 08:38:16 +01:00
|
|
|
from zerver.models import UserActivity, UserPresence, UserProfile, \
|
|
|
|
get_user_profile_by_email
|
2016-07-26 23:26:39 +02:00
|
|
|
|
|
|
|
def get_status_list(requesting_user_profile):
|
|
|
|
# type: (UserProfile) -> Dict[str, Any]
|
|
|
|
return {'presences': get_status_dict(requesting_user_profile),
|
|
|
|
'server_timestamp': time.time()}
|
|
|
|
|
2017-02-11 08:38:16 +01:00
|
|
|
def get_presence_backend(request, user_profile, email):
|
|
|
|
# type: (HttpRequest, UserProfile, Text) -> HttpResponse
|
|
|
|
try:
|
|
|
|
target = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
return json_error(_('No such user'))
|
|
|
|
if target.realm != user_profile.realm:
|
|
|
|
return json_error(_('No such user'))
|
|
|
|
if not target.is_active:
|
|
|
|
return json_error(_('No such user'))
|
|
|
|
if target.is_bot:
|
|
|
|
return json_error(_('No presence for bot users'))
|
|
|
|
|
|
|
|
presence_dict = UserPresence.get_status_dict_by_user(target)
|
|
|
|
if len(presence_dict) == 0:
|
|
|
|
return json_error(_('No presence data for %s' % (target.email,)))
|
|
|
|
|
|
|
|
# For initial version, we just include the status and timestamp keys
|
|
|
|
result = dict(presence=presence_dict[target.email])
|
|
|
|
for val in result['presence'].values():
|
|
|
|
del val['client']
|
|
|
|
del val['pushable']
|
|
|
|
return json_success(result)
|
|
|
|
|
2016-07-26 23:26:39 +02:00
|
|
|
@has_request_variables
|
|
|
|
def update_active_status_backend(request, user_profile, status=REQ(),
|
|
|
|
new_user_input=REQ(validator=check_bool, default=False)):
|
|
|
|
# type: (HttpRequest, UserProfile, str, bool) -> HttpResponse
|
|
|
|
status_val = UserPresence.status_from_string(status)
|
|
|
|
if status_val is None:
|
2017-01-29 00:08:08 +01:00
|
|
|
raise JsonableError(_("Invalid status: %s") % (status,))
|
2016-07-26 23:26:39 +02:00
|
|
|
else:
|
2016-11-06 02:49:37 +01:00
|
|
|
update_user_presence(user_profile, request.client, timezone.now(),
|
|
|
|
status_val, new_user_input)
|
2016-07-26 23:26:39 +02:00
|
|
|
|
|
|
|
ret = get_status_list(user_profile)
|
2016-07-27 01:45:29 +02:00
|
|
|
if user_profile.realm.is_zephyr_mirror_realm:
|
2016-09-13 23:45:27 +02:00
|
|
|
# In zephyr mirroring realms, users can't see the presence of other
|
|
|
|
# users, but each user **is** interested in whether their mirror bot
|
|
|
|
# (running as their user) has been active.
|
2016-07-26 23:26:39 +02:00
|
|
|
try:
|
|
|
|
activity = UserActivity.objects.get(user_profile = user_profile,
|
|
|
|
query="get_events_backend",
|
|
|
|
client__name="zephyr_mirror")
|
|
|
|
|
|
|
|
ret['zephyr_mirror_active'] = \
|
2016-11-06 02:49:37 +01:00
|
|
|
(activity.last_visit > timezone.now() - datetime.timedelta(minutes=5))
|
2016-07-26 23:26:39 +02:00
|
|
|
except UserActivity.DoesNotExist:
|
|
|
|
ret['zephyr_mirror_active'] = False
|
|
|
|
|
|
|
|
return json_success(ret)
|