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.

hotspots: Refactor hotspots system to use a new type `Hotspot`.

Lalit1089e1352998
diff --git a/zerver/lib/hotspots.py b/zerver/lib/hotspots.py
index cbc537dcdc4..2a2c267886d 100644
--- a/zerver/lib/hotspots.py
+++ b/zerver/lib/hotspots.py
@@ -1,6 +1,7 @@
# See https://zulip.readthedocs.io/en/latest/subsystems/hotspots.html
# for documentation on this subsystem.
-from typing import Dict, List, Union
+from dataclasses import dataclass
+from typing import Dict, List, Optional, Union
from django.conf import settings
from django.utils.translation import gettext_lazy
@@ -8,56 +9,67 @@ from django_stubs_ext import StrPromise
from zerver.models import UserHotspot, UserProfile
-INTRO_HOTSPOTS: Dict[str, Dict[str, Union[StrPromise, str]]] = {
- "intro_streams": {
- "title": gettext_lazy("Catch up on a stream"),
- "description": gettext_lazy(
+
+@dataclass
+class Hotspot:
+ name: str
+ title: Optional[StrPromise]
+ description: Optional[StrPromise]
+ has_trigger: bool = False
+
+ def to_dict(self, delay: float = 0) -> Dict[str, Union[str, float, bool]]:
+ return {
+ "name": self.name,
+ "title": str(self.title),
+ "description": str(self.description),
+ "delay": delay,
+ "has_trigger": self.has_trigger,
+ }
+
+
+INTRO_HOTSPOTS: List[Hotspot] = [
+ Hotspot(
+ name="intro_streams",
+ title=gettext_lazy("Catch up on a stream"),
+ description=gettext_lazy(
"Messages sent to a stream are seen by everyone subscribed "
"to that stream. Try clicking on one of the stream links below."
),
- },
- "intro_topics": {
- "title": gettext_lazy("Topics"),
- "description": gettext_lazy(
+ ),
+ Hotspot(
+ name="intro_topics",
+ title=gettext_lazy("Topics"),
+ description=gettext_lazy(
"Every message has a topic. Topics keep conversations "
"easy to follow, and make it easy to reply to conversations that start "
"while you are offline."
),
- },
- "intro_gear": {
- "title": gettext_lazy("Settings"),
- "description": gettext_lazy(
- "Go to Settings to configure your notifications and preferences."
- ),
- },
- "intro_compose": {
- "title": gettext_lazy("Compose"),
- "description": gettext_lazy(
+ ),
+ Hotspot(
+ name="intro_gear",
+ title=gettext_lazy("Settings"),
+ description=gettext_lazy("Go to Settings to configure your notifications and preferences."),
+ ),
+ Hotspot(
+ name="intro_compose",
+ title=gettext_lazy("Compose"),
+ description=gettext_lazy(
"Click here to start a new conversation. Pick a topic "
"(2-3 words is best), and give it a go!"
),
- },
-}
+ ),
+]
-NON_INTRO_HOTSPOTS: Dict[str, Dict[str, Union[StrPromise, str]]] = {}
+NON_INTRO_HOTSPOTS: List[Hotspot] = []
# We would most likely implement new hotspots in the future that aren't
# a part of the initial tutorial. To that end, classifying them into
# categories which are aggregated in ALL_HOTSPOTS, seems like a good start.
-ALL_HOTSPOTS: Dict[str, Dict[str, Union[StrPromise, str, bool]]] = {
- **{
- hotspot_name: {**INTRO_HOTSPOTS[hotspot_name], "has_trigger": False}
- for hotspot_name in INTRO_HOTSPOTS
- },
- **{
- hotspot_name: {**NON_INTRO_HOTSPOTS[hotspot_name], "has_trigger": True} # type: ignore[arg-type] # reason: Its a temporary hack
- for hotspot_name in NON_INTRO_HOTSPOTS
- },
-}
-
-
-def get_next_hotspots(user: UserProfile) -> List[Dict[str, object]]:
+ALL_HOTSPOTS = [*INTRO_HOTSPOTS, *NON_INTRO_HOTSPOTS]
+
+
+def get_next_hotspots(user: UserProfile) -> List[Dict[str, Union[str, float, bool]]]:
# For manual testing, it can be convenient to set
# ALWAYS_SEND_ALL_HOTSPOTS=True in `zproject/dev_settings.py` to
# make it easy to click on all of the hotspots.
@@ -65,16 +77,7 @@ def get_next_hotspots(user: UserProfile) -> List[Dict[str, object]]:
# Since this is just for development purposes, it's convenient for us to send
# all the hotspots rather than any specific category.
if settings.ALWAYS_SEND_ALL_HOTSPOTS:
- return [
- {
- **base_hotspot,
- "name": name,
- "title": str(base_hotspot["title"]),
- "description": str(base_hotspot["description"]),
- "delay": 0,
- }
- for name, base_hotspot in ALL_HOTSPOTS.items()
- ]
+ return [hotspot.to_dict() for hotspot in ALL_HOTSPOTS]
# If a Zulip server has disabled the tutorial, never send hotspots.
if not settings.TUTORIAL_ENABLED:
@@ -84,39 +87,16 @@ def get_next_hotspots(user: UserProfile) -> List[Dict[str, object]]:
UserHotspot.objects.filter(user=user).values_list("hotspot", flat=True)
)
- hotspots = []
-
- for name, base_hotspot in NON_INTRO_HOTSPOTS.items():
- if name in seen_hotspots:
- continue
-
- hotspot = {
- **base_hotspot,
- "name": name,
- "title": str(base_hotspot["title"]),
- "description": str(base_hotspot["description"]),
- "delay": 0,
- "has_trigger": True,
- }
- hotspots.append(hotspot)
+ hotspots = [hotspot.to_dict() for hotspot in NON_INTRO_HOTSPOTS]
if user.tutorial_status == UserProfile.TUTORIAL_FINISHED:
return hotspots
- for name, base_hotspot in INTRO_HOTSPOTS.items():
- if name in seen_hotspots:
+ for hotspot in INTRO_HOTSPOTS:
+ if hotspot.name in seen_hotspots:
continue
- # Make a copy to set delay and finalize i18n strings.
- hotspot = {
- **base_hotspot,
- "name": name,
- "title": str(base_hotspot["title"]),
- "description": str(base_hotspot["description"]),
- "delay": 0.5,
- "has_trigger": False,
- }
- hotspots.append(hotspot)
+ hotspots.append(hotspot.to_dict(delay=0.5))
return hotspots
user.tutorial_status = UserProfile.TUTORIAL_FINISHED
diff --git a/zerver/tests/test_hotspots.py b/zerver/tests/test_hotspots.py
index ee39341430f..05fd58bead6 100644
--- a/zerver/tests/test_hotspots.py
+++ b/zerver/tests/test_hotspots.py
@@ -22,22 +22,6 @@ class TestGetNextHotspots(ZulipTestCase):
self.assert_length(hotspots, 1)
self.assertEqual(hotspots[0]["name"], "intro_streams")
- # Remove this test once we have a non-intro hotspot.
- def test_non_intro_hotspots(self) -> None:
- NON_INTRO_HOTSPOTS["dummy_hotspot"] = {
- "title": "Dummy Hotspot",
- "description": "This is a dummy hotspot",
- }
- hotspots = get_next_hotspots(self.user)
- self.assert_length(hotspots, 2)
- self.assertEqual(hotspots[0]["name"], "dummy_hotspot")
- self.assertEqual(hotspots[1]["name"], "intro_streams")
- do_mark_hotspot_as_read(self.user, "dummy_hotspot")
- hotspots = get_next_hotspots(self.user)
- self.assert_length(hotspots, 1)
- self.assertEqual(hotspots[0]["name"], "intro_streams")
- NON_INTRO_HOTSPOTS.pop("dummy_hotspot")
-
def test_some_done_some_not(self) -> None:
do_mark_hotspot_as_read(self.user, "intro_streams")
do_mark_hotspot_as_read(self.user, "intro_compose")
@@ -48,12 +32,12 @@ class TestGetNextHotspots(ZulipTestCase):
def test_all_hotspots_done(self) -> None:
with self.settings(TUTORIAL_ENABLED=True):
self.assertNotEqual(self.user.tutorial_status, UserProfile.TUTORIAL_FINISHED)
- for hotspot in NON_INTRO_HOTSPOTS:
- do_mark_hotspot_as_read(self.user, hotspot)
+ for hotspot in NON_INTRO_HOTSPOTS: # nocoverage
+ do_mark_hotspot_as_read(self.user, hotspot.name)
self.assertNotEqual(self.user.tutorial_status, UserProfile.TUTORIAL_FINISHED)
for hotspot in INTRO_HOTSPOTS:
- do_mark_hotspot_as_read(self.user, hotspot)
+ do_mark_hotspot_as_read(self.user, hotspot.name)
self.assertEqual(self.user.tutorial_status, UserProfile.TUTORIAL_FINISHED)
self.assertEqual(get_next_hotspots(self.user), [])
diff --git a/zerver/views/hotspots.py b/zerver/views/hotspots.py
index 11c24a5bd3d..dbc7d7b2371 100644
--- a/zerver/views/hotspots.py
+++ b/zerver/views/hotspots.py
@@ -15,7 +15,7 @@ from zerver.models import UserProfile
def mark_hotspot_as_read(
request: HttpRequest, user: UserProfile, hotspot: str = REQ()
) -> HttpResponse:
- if hotspot not in ALL_HOTSPOTS:
+ if not any(h.name == hotspot for h in ALL_HOTSPOTS):
raise JsonableError(_("Unknown hotspot: {hotspot}").format(hotspot=hotspot))
do_mark_hotspot_as_read(user, hotspot)
return json_success(request)