Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user-status: Update `presence_enabled` with changes to user status `away`.

Lauryn Menard843eb4e4fc27
diff --git a/zerver/actions/user_status.py b/zerver/actions/user_status.py
index a59ef40c15b..a1e11b3425a 100644
--- a/zerver/actions/user_status.py
+++ b/zerver/actions/user_status.py
@@ -1,5 +1,6 @@
from typing import Optional
+from zerver.actions.user_settings import do_change_user_setting
from zerver.lib.user_status import update_user_status
from zerver.models import UserProfile, UserStatus, active_user_ids
from zerver.tornado.django_api import send_event
@@ -14,6 +15,13 @@ def do_update_user_status(
emoji_code: Optional[str],
reaction_type: Optional[str],
) -> None:
+ # Deprecated way for clients to access the user's `presence_enabled`
+ # setting, with away != presence_enabled.
+ if away is not None:
+ user_setting = "presence_enabled"
+ value = not away
+ do_change_user_setting(user_profile, user_setting, value, acting_user=user_profile)
+
if away is None:
status = None
elif away:
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 47608e6eb23..8f52b23f8ff 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -1143,16 +1143,20 @@ class NormalActionsTest(BaseAction):
def test_away_events(self) -> None:
client = get_client("website")
+
+ # Set all
+ away_val = True
events = self.verify_action(
lambda: do_update_user_status(
user_profile=self.user_profile,
- away=True,
+ away=away_val,
status_text="out to lunch",
emoji_name="car",
emoji_code="1f697",
reaction_type=UserStatus.UNICODE_EMOJI,
client_id=client.id,
- )
+ ),
+ num_events=4,
)
check_user_status(
@@ -1160,16 +1164,29 @@ class NormalActionsTest(BaseAction):
events[0],
{"away", "status_text", "emoji_name", "emoji_code", "reaction_type"},
)
+ check_user_settings_update("events[1]", events[1])
+ check_update_global_notifications("events[2]", events[2], not away_val)
+ check_presence(
+ "events[3]",
+ events[3],
+ has_email=True,
+ presence_key="website",
+ status="active" if not away_val else "idle",
+ )
+
+ # Remove all
+ away_val = False
events = self.verify_action(
lambda: do_update_user_status(
user_profile=self.user_profile,
- away=False,
+ away=away_val,
status_text="",
emoji_name="",
emoji_code="",
reaction_type=UserStatus.UNICODE_EMOJI,
client_id=client.id,
- )
+ ),
+ num_events=4,
)
check_user_status(
@@ -1177,21 +1194,43 @@ class NormalActionsTest(BaseAction):
events[0],
{"away", "status_text", "emoji_name", "emoji_code", "reaction_type"},
)
+ check_user_settings_update("events[1]", events[1])
+ check_update_global_notifications("events[2]", events[2], not away_val)
+ check_presence(
+ "events[3]",
+ events[3],
+ has_email=True,
+ presence_key="website",
+ status="active" if not away_val else "idle",
+ )
+ # Only set away
+ away_val = True
events = self.verify_action(
lambda: do_update_user_status(
user_profile=self.user_profile,
- away=True,
+ away=away_val,
status_text=None,
emoji_name=None,
emoji_code=None,
reaction_type=None,
client_id=client.id,
- )
+ ),
+ num_events=4,
)
check_user_status("events[0]", events[0], {"away"})
+ check_user_settings_update("events[1]", events[1])
+ check_update_global_notifications("events[2]", events[2], not away_val)
+ check_presence(
+ "events[3]",
+ events[3],
+ has_email=True,
+ presence_key="website",
+ status="active" if not away_val else "idle",
+ )
+ # Only set status_text
events = self.verify_action(
lambda: do_update_user_status(
user_profile=self.user_profile,
diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py
index e18d498441b..ccf59f5ad16 100644
--- a/zerver/tests/test_import_export.py
+++ b/zerver/tests/test_import_export.py
@@ -1722,7 +1722,7 @@ class SingleUserExportTest(ExportFile):
do_update_user_status(
user_profile=cordelia,
- away=True,
+ away=None,
status_text="on vacation",
client_id=client.id,
emoji_name=None,
@@ -1744,7 +1744,7 @@ 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.AWAY)
+ 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.py
index d4c07567b63..08825ce8b4f 100644
--- a/zerver/tests/test_user_status.py
+++ b/zerver/tests/test_user_status.py
@@ -174,10 +174,10 @@ class UserStatusTest(ZulipTestCase):
self.assertEqual(away_user_ids, {cordelia.id})
def update_status_and_assert_event(
- self, payload: Dict[str, Any], expected_event: Dict[str, Any]
+ self, payload: Dict[str, Any], expected_event: Dict[str, Any], num_events: int = 1
) -> None:
events: List[Mapping[str, Any]] = []
- with self.tornado_redirected_to_list(events, expected_num_events=1):
+ with self.tornado_redirected_to_list(events, expected_num_events=num_events):
result = self.client_post("/json/users/me/status", payload)
self.assert_json_success(result)
self.assertEqual(events[0]["event"], expected_event)
@@ -232,6 +232,7 @@ class UserStatusTest(ZulipTestCase):
expected_event=dict(
type="user_status", user_id=hamlet.id, away=True, status_text="on vacation"
),
+ num_events=4,
)
self.assertEqual(
user_status_info(hamlet),
@@ -252,6 +253,7 @@ class UserStatusTest(ZulipTestCase):
emoji_code="1f697",
reaction_type=UserStatus.UNICODE_EMOJI,
),
+ num_events=4,
)
self.assertEqual(
user_status_info(hamlet),
@@ -278,6 +280,7 @@ class UserStatusTest(ZulipTestCase):
emoji_code="",
reaction_type=UserStatus.UNICODE_EMOJI,
),
+ num_events=4,
)
self.assertEqual(
user_status_info(hamlet),
@@ -288,6 +291,7 @@ class UserStatusTest(ZulipTestCase):
self.update_status_and_assert_event(
payload=dict(away=orjson.dumps(False).decode()),
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())
@@ -318,6 +322,7 @@ class UserStatusTest(ZulipTestCase):
self.update_status_and_assert_event(
payload=dict(away=orjson.dumps(True).decode()),
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})