0
0
Просмотр исходного кода

fix(slack-client): force update Google Calendar app view

Joe 1 год назад
Родитель
Сommit
870fd31c0a
1 измененных файлов с 73 добавлено и 8 удалено
  1. 73 8
      .scripts/slack_client.py

+ 73 - 8
.scripts/slack_client.py

@@ -25,7 +25,7 @@ from subprocess import check_output, CalledProcessError, DEVNULL, STDOUT
 from sys import exit, argv
 from sys import exit, argv
 from sys import platform
 from sys import platform
 from tempfile import TemporaryDirectory
 from tempfile import TemporaryDirectory
-from time import time
+from time import sleep, time
 from typing import cast
 from typing import cast
 from urllib import request, parse
 from urllib import request, parse
 from datetime import datetime, timedelta
 from datetime import datetime, timedelta
@@ -450,7 +450,7 @@ class CalendarParser:
         current: list[list[str]] = []
         current: list[list[str]] = []
         items: list[tuple[list[int], str]] = []
         items: list[tuple[list[int], str]] = []
         for block in self._blocks:
         for block in self._blocks:
-            if block[-1] == "All-day":
+            if block[-1] == "All-day" or block[-1].count("date_long_pretty") >= 2:
                 if (
                 if (
                     "|"
                     "|"
                     + self._env_config[
                     + self._env_config[
@@ -459,7 +459,7 @@ class CalendarParser:
                     in block[0]
                     in block[0]
                 ):
                 ):
                     location = CalendarWorkLocation.OFFICE
                     location = CalendarWorkLocation.OFFICE
-                elif (
+                if (
                     "|"
                     "|"
                     + self._env_config[
                     + self._env_config[
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_HOME
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_HOME
@@ -467,7 +467,7 @@ class CalendarParser:
                     in block[0]
                     in block[0]
                 ):
                 ):
                     location = CalendarWorkLocation.HOME
                     location = CalendarWorkLocation.HOME
-                elif (
+                if (
                     "|"
                     "|"
                     + self._env_config[
                     + self._env_config[
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_BREAK
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_BREAK
@@ -475,7 +475,7 @@ class CalendarParser:
                     in block[0]
                     in block[0]
                 ):
                 ):
                     vacation = True
                     vacation = True
-                elif (
+                if (
                     "|"
                     "|"
                     + self._env_config[
                     + self._env_config[
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_SICK
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_SICK
@@ -483,7 +483,7 @@ class CalendarParser:
                     in block[0]
                     in block[0]
                 ):
                 ):
                     sick = True
                     sick = True
-                elif (
+                if (
                     "|"
                     "|"
                     + self._env_config[
                     + self._env_config[
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_OUT
                         EnvConfigKey.SLACK_CLIENT_CALENDAR_EVENT_NAME_OUT
@@ -597,6 +597,7 @@ class SlackClient(Cacheable):
             and "real_name" in i
             and "real_name" in i
             and i["real_name"] == "Google Calendar"
             and i["real_name"] == "Google Calendar"
         )
         )
+        bot_id: str = cast(str, cast(JSONDict, calendar_user["profile"])["bot_id"])
         bot_convo = self._call(
         bot_convo = self._call(
             "conversations.open", {"users": calendar_user["id"]}, cache=True
             "conversations.open", {"users": calendar_user["id"]}, cache=True
         )
         )
@@ -604,11 +605,75 @@ class SlackClient(Cacheable):
         convo_info = self._call(
         convo_info = self._call(
             "conversations.info", {"channel": convo_id, "return_app_home": True}, False
             "conversations.info", {"channel": convo_id, "return_app_home": True}, False
         )
         )
+        # The content of this view might not be properly updated - try some tricks to
+        # force it to refresh!
+        team_id: str = cast(
+            str,
+            cast(
+                JSONDict,
+                convo_info["home_view"],
+            )["team_id"],
+        )
+        _ = self._call(
+            "apps.home.dispatchOpenEvent",
+            {
+                "id": convo_id,
+                "type": "home",
+                "service_team_id": team_id,
+            },
+            False,
+        )
+        _ = self._call("apps.profile.get", {"bot": bot_id}, False)
+        app_view = self._call(
+            "views.get",
+            {
+                "view_id": cast(JSONDict, convo_info["home_view"])["id"],
+                "_x_reason": "fetchView",
+            },
+            False,
+        )
+
+        # Click the "Tomorrow" button, then the "Today" button
+        view: JSONDict = cast(JSONDict, app_view["view"])
+        view_id: str = cast(str, view["id"])
+        blocks: list[JSONDict] = cast(list[JSONDict], view["blocks"])
+        hash: str = cast(str, view["hash"])
+
+        def click_block_action(value: str):
+            for block in blocks:
+                if block["type"] != "actions":
+                    continue
+                for button in cast(list[JSONDict], block["elements"]):
+                    if "value" not in button or button["value"] != value:
+                        continue
+                    button["block_id"] = block["block_id"]
+                    _ = self._call(
+                        "blocks.actions",
+                        {
+                            "service_id": bot_id,
+                            "service_team_id": team_id,
+                            "actions": dumps([button]),
+                            "container": dumps({"type": "view", "view_id": view_id}),
+                            "client_token": "web-" + hash.split(".")[0],
+                        },
+                        False,
+                    )
+                    break
+
+        for button in ["AGENDA_TOMORROW", "AGENDA_TODAY"]:
+            click_block_action(button)
+            # Gotta sleep, to ensure that the app view has time to update
+            sleep(5)
+
         app_view = self._call(
         app_view = self._call(
             "views.get",
             "views.get",
-            {"view_id": cast(JSONDict, convo_info["home_view"])["id"]},
+            {
+                "view_id": cast(JSONDict, convo_info["home_view"])["id"],
+                "_x_reason": "fetchView",
+            },
             False,
             False,
         )
         )
+
         texts = [
         texts = [
             cast(str, cast(JSONDict, b["text"])["text"]).split("\n")
             cast(str, cast(JSONDict, b["text"])["text"]).split("\n")
             for b in cast(list[JSONDict], cast(JSONDict, app_view["view"])["blocks"])
             for b in cast(list[JSONDict], cast(JSONDict, app_view["view"])["blocks"])
@@ -801,7 +866,7 @@ if __name__ == "__main__":
         exit(1)
         exit(1)
     for program in ["networksetup", "leveldbutil", "openssl"]:
     for program in ["networksetup", "leveldbutil", "openssl"]:
         try:
         try:
-            _ = check_output([program], stderr=DEVNULL)
+            _ = check_output([program, "--version"], stderr=DEVNULL)
         except CalledProcessError:
         except CalledProcessError:
             pass
             pass
         except:
         except: