user status: Restructure code.

We now have single function that handle both away
and not-away.

This refactoring sets us up to piggyback "info" more
easily onto status updates.

The only thing that changes here is that we don't
delete database rows any more when users revoke
their away status.  Instead we just set the status
to NORMAL.
This commit is contained in:
Steve Howell
2019-01-21 17:19:59 +00:00
committed by Tim Abbott
parent 7e904c3a1a
commit ac861f2b7d
5 changed files with 57 additions and 57 deletions

View File

@@ -79,15 +79,14 @@ from zerver.lib.users import (
user_ids_to_users
)
from zerver.lib.user_status import (
revoke_away_status,
set_away_status,
update_user_status,
)
from zerver.lib.user_groups import create_user_group, access_user_group_by_id
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \
RealmDomain, Service, SubMessage, \
Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \
UserHotspot, MultiuseInvite, ScheduledMessage, \
UserHotspot, MultiuseInvite, ScheduledMessage, UserStatus, \
Client, DefaultStream, DefaultStreamGroup, UserPresence, PushDeviceToken, \
ScheduledEmail, MAX_TOPIC_NAME_LENGTH, \
MAX_MESSAGE_LENGTH, get_client, get_stream, get_personal_recipient, get_huddle, \
@@ -3771,29 +3770,26 @@ def do_update_pointer(user_profile: UserProfile, client: Client,
event = dict(type='pointer', pointer=pointer)
send_event(user_profile.realm, event, [user_profile.id])
def do_set_away_status(user_profile: UserProfile,
client_id: int) -> None:
def do_update_user_status(user_profile: UserProfile,
client_id: int,
away: bool) -> None:
if away:
status = UserStatus.AWAY
else:
status = UserStatus.NORMAL
realm = user_profile.realm
set_away_status(
update_user_status(
user_profile_id=user_profile.id,
status=status,
client_id=client_id,
)
event = dict(
type='user_status',
user_id=user_profile.id,
away=True,
)
send_event(realm, event, active_user_ids(realm.id))
def do_revoke_away_status(user_profile: UserProfile) -> None:
realm = user_profile.realm
revoke_away_status(
user_profile_id=user_profile.id,
)
event = dict(
type='user_status',
user_id=user_profile.id,
away=False,
away=away,
)
send_event(realm, event, active_user_ids(realm.id))

View File

@@ -15,22 +15,20 @@ def get_away_user_ids(realm_id: int) -> Set[int]:
return set(user_ids)
def set_away_status(user_profile_id: int,
def update_user_status(user_profile_id: int,
status: int,
client_id: int) -> None:
timestamp = timezone_now()
status = UserStatus.AWAY
UserStatus.objects.update_or_create(
user_profile_id=user_profile_id,
defaults = dict(
client_id=client_id,
timestamp=timestamp,
status=status,
),
)
def revoke_away_status(user_profile_id: int) -> None:
UserStatus.objects.filter(
defaults['status'] = status
UserStatus.objects.update_or_create(
user_profile_id=user_profile_id,
).delete()
defaults=defaults,
)

View File

@@ -16,7 +16,7 @@ from zerver.models import (
get_client, get_realm, get_stream_recipient, get_stream, get_user,
Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile,
Realm, Subscription, Stream, flush_per_request_caches, UserGroup, Service,
Attachment, PreregistrationUser,
Attachment, PreregistrationUser, UserStatus,
)
from zerver.lib.actions import (
@@ -69,9 +69,7 @@ from zerver.lib.actions import (
do_remove_realm_filter,
do_remove_streams_from_default_stream_group,
do_rename_stream,
do_revoke_away_status,
do_revoke_user_invite,
do_set_away_status,
do_set_realm_authentication_methods,
do_set_realm_message_editing,
do_set_realm_property,
@@ -85,6 +83,7 @@ from zerver.lib.actions import (
do_update_outgoing_webhook_service,
do_update_pointer,
do_update_user_presence,
do_update_user_status,
get_typing_user_profiles,
log_event,
lookup_default_stream_groups,
@@ -1210,12 +1209,15 @@ class EventsRegisterTest(ZulipTestCase):
])
client = get_client("website")
events = self.do_test(lambda: do_set_away_status(user_profile=self.user_profile,
events = self.do_test(lambda: do_update_user_status(user_profile=self.user_profile,
away=True,
client_id=client.id))
error = checker('events[0]', events[0])
self.assert_on_error(error)
events = self.do_test(lambda: do_revoke_away_status(user_profile=self.user_profile))
events = self.do_test(lambda: do_update_user_status(user_profile=self.user_profile,
away=False,
client_id=client.id))
error = checker('events[0]', events[0])
self.assert_on_error(error)

View File

@@ -9,8 +9,7 @@ from zerver.lib.test_helpers import (
)
from zerver.lib.user_status import (
get_away_user_ids,
revoke_away_status,
set_away_status,
update_user_status,
)
from zerver.models import (
@@ -34,8 +33,9 @@ class UserStatusTest(ZulipTestCase):
client1 = get_client('web')
client2 = get_client('ZT')
set_away_status(
update_user_status(
user_profile_id=hamlet.id,
status=UserStatus.AWAY,
client_id=client1.id,
)
@@ -48,8 +48,9 @@ class UserStatusTest(ZulipTestCase):
# clients; we only store the client for
# reference and to maybe reconcile timeout
# situations.
set_away_status(
update_user_status(
user_profile_id=hamlet.id,
status=UserStatus.AWAY,
client_id=client2.id,
)
@@ -59,8 +60,10 @@ class UserStatusTest(ZulipTestCase):
rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count()
self.assertEqual(rec_count, 1)
revoke_away_status(
update_user_status(
user_profile_id=hamlet.id,
status=UserStatus.NORMAL,
client_id=client2.id,
)
away_user_ids = get_away_user_ids(realm_id=realm_id)
@@ -68,16 +71,19 @@ class UserStatusTest(ZulipTestCase):
# Now set away status for three different users across
# two realms.
set_away_status(
update_user_status(
user_profile_id=hamlet.id,
status=UserStatus.AWAY,
client_id=client1.id,
)
set_away_status(
update_user_status(
user_profile_id=cordelia.id,
status=UserStatus.AWAY,
client_id=client2.id,
)
set_away_status(
update_user_status(
user_profile_id=king_lear.id,
status=UserStatus.AWAY,
client_id=client2.id,
)
@@ -88,8 +94,10 @@ class UserStatusTest(ZulipTestCase):
self.assertEqual(away_user_ids, {king_lear.id})
# Revoke Hamlet again.
revoke_away_status(
update_user_status(
user_profile_id=hamlet.id,
status=UserStatus.NORMAL,
client_id=client2.id,
)
away_user_ids = get_away_user_ids(realm_id=realm_id)

View File

@@ -11,8 +11,7 @@ from django.utils.translation import ugettext as _
from zerver.decorator import human_users_only
from zerver.lib.actions import (
do_revoke_away_status,
do_set_away_status,
do_update_user_status,
get_status_dict,
update_user_presence,
)
@@ -57,14 +56,11 @@ def update_user_status_backend(request: HttpRequest,
user_profile: UserProfile,
away: bool=REQ(validator=check_bool),
) -> HttpResponse:
if away:
do_set_away_status(
do_update_user_status(
user_profile=user_profile,
client_id=request.client.id,
)
else:
do_revoke_away_status(
user_profile=user_profile,
away=away,
)
return json_success()