Repository: zulip/zulip-terminal

Issue:
TRACKING: Update to use modern API elements (retaining support to 2.1)
There are various situations where we currently use older elements of the API which have been deprecated. We should migrate to the newer forms, while possibly retaining (or extending) compatibility down to our stated lowest supported version if the server is older (from feature level `None` to `1+`, ie. the current dev release).

Some API elements to consider: (more likely can be added)
* [ ] message destinations (recipient id rather than name)
* [ ] various stream properties
* [ ] various message properties
* [ ] `subject` -> `topic` for stream messages
* [ ] handling `subject_links` (we may be missing support for 2.1-2.2)

Other than the number of different API elements to review and update, the other aspect of this work is considering where the external and internal data names are updated/handled. Currently these are the same in many cases, but this may not be straightforward to support across multiple server versions and would be stable if there was a mapping layer between them in any case (rather than just using the raw server data).

Related tasks: (possibly to expand into separate issues)
* Add a TypedDict for useful objects
* Adjust TypedDicts to be total (in the API)
* Adjusting fixtures to match

Discussion:
zulipbot: Hello @zulip/server-refactoring members, this pull request was labeled with the "area: refactoring" label, so you may want to check it out!

<!-- areaLabelAddition -->

zulipbot: Hello @mounilKshah, it seems like you have referenced #965 in your pull request description, but you have not referenced them in your commit message description(s). Referencing an issue in a commit message automatically closes the corresponding issue when the commit is merged, which makes the issue tracker easier to manage.

Please run `git commit --amend` in your command line client to amend your commit message description with `Fixes #965.`.

An example of a correctly-formatted commit:

```
commit fabd5e450374c8dde65ec35f02140383940fe146
Author: zulipbot
Date:   Sat Mar 18 13:42:40 2017 -0700

    pull requests: Check PR commits reference when issue is referenced.

    Fixes #51.
```

To learn how to write a great commit message, please refer to our [guide](https://zulip.readthedocs.io/en/latest/contributing/version-control.html#commit-messages).

<!-- fixCommitWarning -->

neiljp: @mounilKshah Following the API discussion, I just squashed your commits to ensure we pass tests on a per-commit basis (as per `tools/check-branch`), and with an updated commit text, and pushed a slight update as per above comment. Also adjusted direct text of `fixes #id` since we don't want to close that PR quite yet when this is now merged :tada:

Patch:
diff --git a/zulipterminal/api_types.py b/zulipterminal/api_types.py
index 17dc1a1..727bdb8 100644
--- a/zulipterminal/api_types.py
+++ b/zulipterminal/api_types.py
@@ -79,8 +79,7 @@ class Subscription(TypedDict):
     pin_to_top: bool
     email_address: str
 
-    is_muted: bool  # NOTE: new in Zulip 2.1 (in_home_view still present)
-    in_home_view: bool  # TODO: Migrate to is_muted (note inversion)
+    is_muted: bool
 
     is_announcement_only: bool  # Deprecated in Zulip 3.0 -> stream_post_policy
     stream_post_policy: int  # NOTE: new in Zulip 3.0 / ZFL 1
@@ -93,6 +92,9 @@ class Subscription(TypedDict):
     first_message_id: Optional[int]
     stream_weekly_traffic: Optional[int]
 
+    # Deprecated fields
+    # in_home_view: bool  # Replaced by is_muted in Zulip 2.1; still present in updates
+
 
 class RealmUser(TypedDict):
     user_id: int
diff --git a/zulipterminal/model.py b/zulipterminal/model.py
index d9452ac..48b7f0e 100644
--- a/zulipterminal/model.py
+++ b/zulipterminal/model.py
@@ -1065,7 +1065,7 @@ class Model:
                 new_pinned_streams.append(streamData)
             else:
                 new_unpinned_streams.append(streamData)
-            if not subscription["in_home_view"]:
+            if subscription["is_muted"]:
                 new_muted_streams.add(subscription["stream_id"])
             if subscription["desktop_notifications"]:
                 new_visual_notified_streams.add(subscription["stream_id"])
@@ -1178,6 +1178,8 @@ class Model:
 
         if event["op"] == "update":
             if hasattr(self.controller, "view"):
+                # NOTE: Currently, the response from the API still contains in_home_view
+                # and not is_muted, hence, the conditional is left unaltered, for now.
                 if event.get("property", None) == "in_home_view":
                     stream_id = event["stream_id"]
 


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index 7155a0a..8943b5f 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -212,7 +212,7 @@ def general_stream() -> Dict[str, Any]:
         "color": "#b0a5fd",  # Color in '#xxxxxx' format
         "pin_to_top": False,
         "stream_id": 1000,
-        "in_home_view": True,
+        "is_muted": False,
         "audible_notifications": False,
         "description": "General Stream",
         "rendered_description": "General Stream",
@@ -241,7 +241,7 @@ def secret_stream() -> Dict[str, Any]:
         "email_address": "secret@example.com",
         "rendered_description": "Some private stream",
         "color": "#ccc",  # Color in '#xxx' format
-        "in_home_view": True,
+        "is_muted": False,
         "audible_notifications": False,
         "is_old_stream": True,
         "desktop_notifications": False,
@@ -266,7 +266,7 @@ def web_public_stream() -> Dict[str, Any]:
         "email_address": "web_public@example.com",
         "rendered_description": "Some web public stream",
         "color": "#ddd",  # Color in '#xxx' format
-        "in_home_view": True,
+        "is_muted": False,
         "audible_notifications": False,
         "is_old_stream": True,
         "desktop_notifications": False,
@@ -295,7 +295,7 @@ def streams_fixture(
                 "color": "#b0a5fd",
                 "pin_to_top": False,
                 "stream_id": i,
-                "in_home_view": True,
+                "is_muted": False,
                 "audible_notifications": False,
                 "description": f"A description of stream {i}",
                 "rendered_description": f"A description of stream {i}",
@@ -645,7 +645,7 @@ def initial_data(
                 "push_notifications": False,
                 "email_address": "",
                 "color": "#bfd56f",
-                "in_home_view": True,
+                "is_muted": False,
                 "history_public_to_subscribers": True,
             }
         ],
diff --git a/tests/model/test_model.py b/tests/model/test_model.py
index 43fe7f1..6db23cf 100644
--- a/tests/model/test_model.py
+++ b/tests/model/test_model.py
@@ -1386,7 +1386,7 @@ class TestModel:
         subs = [
             dict(
                 entry,
-                in_home_view=entry["stream_id"] not in muted,
+                is_muted=entry["stream_id"] in muted,
                 desktop_notifications=entry["stream_id"] in visual_notification_enabled,
             )
             for entry in initial_data["subscriptions"]