Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user-status: Stop updating the UserStatus model for `away` updates.
Lauryn Menard37aca4ac67ec
diff --git a/zerver/actions/user_status.py b/zerver/actions/user_status.pyindex a1e11b3425a..0493eb9bd3f 100644--- a/zerver/actions/user_status.py+++ b/zerver/actions/user_status.py@@ -2,7 +2,7 @@ from typing import Optionalfrom zerver.actions.user_settings import do_change_user_settingfrom zerver.lib.user_status import update_user_status-from zerver.models import UserProfile, UserStatus, active_user_ids+from zerver.models import UserProfile, active_user_idsfrom zerver.tornado.django_api import send_event@@ -16,24 +16,18 @@ def do_update_user_status(reaction_type: Optional[str],) -> None:# Deprecated way for clients to access the user's `presence_enabled`- # setting, with away != presence_enabled.+ # 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.if away is not None:user_setting = "presence_enabled"value = not awaydo_change_user_setting(user_profile, user_setting, value, acting_user=user_profile)- if away is None:- status = None- elif away:- status = UserStatus.AWAY- else:- status = UserStatus.NORMAL-realm = user_profile.realmupdate_user_status(user_profile_id=user_profile.id,- status=status,status_text=status_text,client_id=client_id,emoji_name=emoji_name,diff --git a/zerver/lib/user_status.py b/zerver/lib/user_status.pyindex 5cfbac323e1..53626947e83 100644--- a/zerver/lib/user_status.py+++ b/zerver/lib/user_status.py@@ -26,7 +26,9 @@ class RawUserInfoDict(TypedDict):def format_user_status(row: RawUserInfoDict) -> UserInfoDict:# Deprecated way for clients to access the user's `presence_enabled`- # setting, with away != presence_enabled.+ # 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.presence_enabled = row["user_profile__presence_enabled"]away = not presence_enabledstatus_text = row["status_text"]@@ -80,7 +82,6 @@ def get_user_status_dict(realm_id: int) -> Dict[str, UserInfoDict]:def update_user_status(user_profile_id: int,- status: Optional[int],status_text: Optional[str],client_id: int,emoji_name: Optional[str],@@ -95,9 +96,6 @@ def update_user_status(timestamp=timestamp,)- if status is not None:- defaults["status"] = status-if status_text is not None:defaults["status_text"] = status_textdiff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex ccf59f5ad16..f25b03e2690 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -1069,13 +1069,13 @@ class RealmImportExportTest(ExportFile):return names@getter- def get_realm_user_statuses(r: Realm) -> Set[Tuple[str, str, int, str]]:+ def get_realm_user_statuses(r: Realm) -> Set[Tuple[str, str, str]]:cordelia = self.example_user("cordelia")tups = {- (rec.user_profile.full_name, rec.emoji_name, rec.status, rec.status_text)+ (rec.user_profile.full_name, rec.emoji_name, rec.status_text)for rec in UserStatus.objects.filter(user_profile__realm_id=r.id)}- assert (cordelia.full_name, "hawaii", UserStatus.AWAY, "in Hawaii") in tups+ assert (cordelia.full_name, "hawaii", "in Hawaii") in tupsreturn tups@getter@@ -1744,7 +1744,6 @@ class SingleUserExportTest(ExportFile):def zerver_userstatus(records: List[Record]) -> None:rec = records[-1]self.assertEqual(rec["status_text"], "on vacation")- self.assertEqual(rec["status"], UserStatus.NORMAL)do_mute_topic(cordelia, scotland, "bagpipe music")do_mute_topic(othello, scotland, "nessie")diff --git a/zerver/tests/test_user_status.py b/zerver/tests/test_user_status.pyindex 6c15f724ceb..b87613b9b00 100644--- a/zerver/tests/test_user_status.py+++ b/zerver/tests/test_user_status.py@@ -1,4 +1,4 @@-from typing import Any, Dict, List, Mapping, Set+from typing import Any, Dict, List, Mappingimport orjson@@ -6,17 +6,6 @@ from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.user_status import UserInfoDict, get_user_status_dict, update_user_statusfrom zerver.models import UserProfile, UserStatus, get_client-AWAY = 1---def get_away_user_ids(realm_id: int) -> Set[int]:- away_ids = set(- UserStatus.objects.filter(status=AWAY, user_profile__realm__id=realm_id).values_list(- "user_profile_id", flat=True- )- )- return away_ids-def user_status_info(user: UserProfile) -> UserInfoDict:user_dict = get_user_status_dict(user.realm_id)@@ -25,30 +14,29 @@ def user_status_info(user: UserProfile) -> UserInfoDict:class UserStatusTest(ZulipTestCase):def test_basics(self) -> None:- cordelia = self.example_user("cordelia")hamlet = self.example_user("hamlet")- king_lear = self.lear_user("king")-- realm_id = hamlet.realm_id-- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, set())client1 = get_client("web")client2 = get_client("ZT")update_user_status(user_profile_id=hamlet.id,- status=UserStatus.AWAY,- status_text=None,+ status_text="working",emoji_name=None,emoji_code=None,reaction_type=None,client_id=client1.id,)- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, {hamlet.id})+ self.assertEqual(+ user_status_info(hamlet),+ dict(+ status_text="working",+ ),+ )++ rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count()+ self.assertEqual(rec_count, 1)# Test that second client just updates# the record. We only store one record@@ -58,18 +46,12 @@ class UserStatusTest(ZulipTestCase):# situations.update_user_status(user_profile_id=hamlet.id,- status=UserStatus.AWAY,status_text="out to lunch",emoji_name="car",emoji_code="1f697",reaction_type=UserStatus.UNICODE_EMOJI,client_id=client2.id,)-- # The user's presence_enabled setting is not updated by- # update_user_status, so the away changes are no longer- # reflected in the object returned by get_user_status_dict.- # We test this below in test_endpoints.self.assertEqual(user_status_info(hamlet),dict(@@ -80,16 +62,12 @@ class UserStatusTest(ZulipTestCase):),)- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, {hamlet.id})-rec_count = UserStatus.objects.filter(user_profile_id=hamlet.id).count()self.assertEqual(rec_count, 1)# Setting status_text and emoji_info to None causes it be ignored.update_user_status(user_profile_id=hamlet.id,- status=UserStatus.NORMAL,status_text=None,emoji_name=None,emoji_code=None,@@ -110,7 +88,6 @@ class UserStatusTest(ZulipTestCase):# Clear the status_text and emoji_info now.update_user_status(user_profile_id=hamlet.id,- status=None,status_text="",emoji_name="",emoji_code="",@@ -123,49 +100,9 @@ class UserStatusTest(ZulipTestCase):{},)- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, set())-- # Now set away status for three different users across- # two realms.+ # Set Hamlet to in a meeting.update_user_status(user_profile_id=hamlet.id,- status=UserStatus.AWAY,- status_text=None,- emoji_name=None,- emoji_code=None,- reaction_type=None,- client_id=client1.id,- )- update_user_status(- user_profile_id=cordelia.id,- status=UserStatus.AWAY,- status_text=None,- emoji_name=None,- emoji_code=None,- reaction_type=None,- client_id=client2.id,- )- update_user_status(- user_profile_id=king_lear.id,- status=UserStatus.AWAY,- status_text=None,- emoji_name=None,- emoji_code=None,- reaction_type=None,- client_id=client2.id,- )-- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, {cordelia.id, hamlet.id})-- away_user_ids = get_away_user_ids(realm_id=king_lear.realm.id)- self.assertEqual(away_user_ids, {king_lear.id})-- # Set Hamlet to NORMAL but in a meeting.- update_user_status(- user_profile_id=hamlet.id,- status=UserStatus.NORMAL,status_text="in a meeting",emoji_name=None,emoji_code=None,@@ -178,9 +115,6 @@ class UserStatusTest(ZulipTestCase):dict(status_text="in a meeting"),)- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, {cordelia.id})-def update_status_and_assert_event(self, payload: Dict[str, Any], expected_event: Dict[str, Any], num_events: int = 1) -> None:@@ -208,7 +142,7 @@ class UserStatusTest(ZulipTestCase):result, "Client must pass emoji_name if they pass either emoji_code or reaction_type.")- # Invalid emoji requests fail+ # Invalid emoji requests fail.payload = {"status_text": "In a meeting", "emoji_code": "1f4bb", "emoji_name": "invalid"}result = self.client_post("/json/users/me/status", payload)self.assert_json_error(result, "Emoji 'invalid' does not exist")@@ -226,12 +160,13 @@ class UserStatusTest(ZulipTestCase):result = self.client_post("/json/users/me/status", payload)self.assert_json_error(result, "Invalid custom emoji.")- # Try a long message.+ # Try a long message--this should be an error.long_text = "x" * 61payload = dict(status_text=long_text)result = self.client_post("/json/users/me/status", payload)self.assert_json_error(result, "status_text is too long (limit: 60 characters)")+ # Set "away" with a normal length message.self.update_status_and_assert_event(payload=dict(away=orjson.dumps(True).decode(),@@ -247,21 +182,24 @@ class UserStatusTest(ZulipTestCase):dict(away=True, status_text="on vacation"),)+ # Setting away is a deprecated way of accessing a user's presence_enabled+ # setting. Can be removed when clients migrate "away" (also referred to as+ # "unavailable") feature to directly use the presence_enabled setting.+ user = UserProfile.objects.get(id=hamlet.id)+ self.assertEqual(user.presence_enabled, False)+# Server should fill emoji_code and reaction_type by emoji_name.self.update_status_and_assert_event(payload=dict(- away=orjson.dumps(True).decode(),emoji_name="car",),expected_event=dict(type="user_status",user_id=hamlet.id,- away=True,emoji_name="car",emoji_code="1f697",reaction_type=UserStatus.UNICODE_EMOJI,),- num_events=4,)self.assertEqual(user_status_info(hamlet),@@ -277,18 +215,15 @@ class UserStatusTest(ZulipTestCase):# Server should remove emoji_code and reaction_type if emoji_name is empty.self.update_status_and_assert_event(payload=dict(- away=orjson.dumps(True).decode(),emoji_name="",),expected_event=dict(type="user_status",user_id=hamlet.id,- away=True,emoji_name="",emoji_code="",reaction_type=UserStatus.UNICODE_EMOJI,),- num_events=4,)self.assertEqual(user_status_info(hamlet),@@ -301,8 +236,16 @@ class UserStatusTest(ZulipTestCase):expected_event=dict(type="user_status", user_id=hamlet.id, away=False),num_events=4,)- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, set())+ self.assertEqual(+ user_status_info(hamlet),+ dict(status_text="on vacation"),+ )++ # Setting away is a deprecated way of accessing a user's presence_enabled+ # setting. Can be removed when clients migrate "away" (also referred to as+ # "unavailable") feature to directly use the presence_enabled setting.+ user = UserProfile.objects.get(id=hamlet.id)+ self.assertEqual(user.presence_enabled, True)# And now just update your info.# The server will trim the whitespace here.@@ -310,7 +253,6 @@ class UserStatusTest(ZulipTestCase):payload=dict(status_text=" in office "),expected_event=dict(type="user_status", user_id=hamlet.id, status_text="in office"),)-self.assertEqual(user_status_info(hamlet),dict(status_text="in office"),@@ -332,8 +274,12 @@ class UserStatusTest(ZulipTestCase):expected_event=dict(type="user_status", user_id=hamlet.id, away=True),num_events=4,)- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, {hamlet.id})++ # Setting away is a deprecated way of accessing a user's presence_enabled+ # setting. Can be removed when clients migrate "away" (also referred to as+ # "unavailable") feature to directly use the presence_enabled setting.+ user = UserProfile.objects.get(id=hamlet.id)+ self.assertEqual(user.presence_enabled, False)# And set status text while away.self.update_status_and_assert_event(@@ -344,6 +290,3 @@ class UserStatusTest(ZulipTestCase):user_status_info(hamlet),dict(status_text="at the beach", away=True),)-- away_user_ids = get_away_user_ids(realm_id=realm_id)- self.assertEqual(away_user_ids, {hamlet.id})