aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-04-18 22:12:29 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2024-04-18 22:12:29 +0200
commit12908fe6cfe53210dfea127a840dd3e6e74b2499 (patch)
tree16ad4b0f063f9a4969ca452231133f9a5a5dde66 /slack
parent4415a318372954a171024c9fd15a429addccc5f8 (diff)
downloadwee-slack-12908fe6cfe53210dfea127a840dd3e6e74b2499.tar.gz
Update user info for open IM conversations when using session tokens
When using session tokens we apparently don't get any user_status_changed events, so when a user updated their status the title in an IM with them wasn't updated. If we specify the query param slack_client=desktop when connecting to the websocket we at least get a user_invalidated event when the user info changes. This doesn't include any info though, so we have to make a request to get the updated user info. On workspaces with a lot of users, we may get user_invalidated very often, so we can't fetch new user info every time we get it, otherwise we would make a lot of requests. So at least for now, only update the user info if we have an open IM with the user. This ensures the status in the IM title is kept up to date at least. In other words, nick changes are not picked up and updated. This is also the case when using OAuth tokens, as we only listen for user_status_changed events, and not the other user updated events. Handling nick changes is also a bit more complex, so I'll leave that for later.
Diffstat (limited to 'slack')
-rw-r--r--slack/slack_user.py4
-rw-r--r--slack/slack_workspace.py23
2 files changed, 21 insertions, 6 deletions
diff --git a/slack/slack_user.py b/slack/slack_user.py
index 3eb00aa..b73d5d5 100644
--- a/slack/slack_user.py
+++ b/slack/slack_user.py
@@ -135,6 +135,10 @@ class SlackUser:
self._rendered_message = None
self._parsed_message = None
+ for conversation in self.workspace.open_conversations.values():
+ if conversation.im_user_id == self.id:
+ conversation.update_buffer_props()
+
class SlackBot:
def __init__(self, workspace: SlackWorkspace, info: SlackBotInfo):
diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py
index a1289a0..17b62a4 100644
--- a/slack/slack_workspace.py
+++ b/slack/slack_workspace.py
@@ -308,7 +308,7 @@ class SlackWorkspace:
self.muted_channels = set(user_boot["prefs"]["muted_channels"].split(","))
await self._connect_ws(
- f"wss://wss-primary.slack.com/?token={self.config.api_token.value}&batch_presence_aware=1"
+ f"wss://wss-primary.slack.com/?token={self.config.api_token.value}&slack_client=desktop&batch_presence_aware=1"
)
conversation_counts = (
@@ -480,11 +480,22 @@ class SlackWorkspace:
channel.update_buffer_props()
return
elif data["type"] == "user_status_changed":
- user = await self.users[data["user"]["id"]]
- user.update_info_json(data["user"])
- for conversation in self.open_conversations.values():
- if conversation.im_user_id == user.id:
- conversation.update_buffer_props()
+ user_id = data["user"]["id"]
+ if user_id in self.users:
+ user = await self.users[user_id]
+ user.update_info_json(data["user"])
+ return
+ elif data["type"] == "user_invalidated":
+ user_id = data["user"]["id"]
+ if user_id in self.users:
+ has_dm_conversation = any(
+ conversation.im_user_id == user_id
+ for conversation in self.open_conversations.values()
+ )
+ if has_dm_conversation:
+ user = await self.users[user_id]
+ user_info = await self.api.fetch_user_info(user_id)
+ user.update_info_json(user_info["user"])
return
elif data["type"] == "channel_joined" or data["type"] == "group_joined":
channel_id = data["channel"]["id"]