diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2021-05-09 12:47:30 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2021-05-11 22:17:40 +0200 |
commit | d933488b7272106a4720591673528ecdf4cef913 (patch) | |
tree | e7d868258f231c44d07b5b2a48e2ab7ac70378a2 | |
parent | 33d167db20408cad4d635e69493ab7dbc67b12b2 (diff) | |
download | wee-slack-d933488b7272106a4720591673528ecdf4cef913.tar.gz |
Support MPIMs in channel list of rtm.start
For some reason, some MPIMs are listed in the channel list of the
response from rtm.start, with is_mpim set to true, instead of in the
mpims list. These incorrectly appeared as private channels instead of
MPIMs in wee-slack.
These entries do not contain a members list, so we have to call
conversations.members and set the name after receiving the response from
that.
-rw-r--r-- | _pytest/data/http/rtm.start.json | 4 | ||||
-rw-r--r-- | _pytest/test_everything.py | 2 | ||||
-rw-r--r-- | wee_slack.py | 50 |
3 files changed, 41 insertions, 15 deletions
diff --git a/_pytest/data/http/rtm.start.json b/_pytest/data/http/rtm.start.json index 972df4b..b827907 100644 --- a/_pytest/data/http/rtm.start.json +++ b/_pytest/data/http/rtm.start.json @@ -312,6 +312,7 @@ "has_pins": false, "is_member": true, "is_private": false, + "is_mpim": false, "last_read": "1485976236.000019", "latest": { "type": "message", @@ -400,6 +401,7 @@ "has_pins": false, "is_member": false, "is_private": false, + "is_mpim": false, "previous_names": [] }, { @@ -415,6 +417,7 @@ "has_pins": false, "is_member": true, "is_private": false, + "is_mpim": false, "last_read": "1485969592.000002", "latest": { "user": "U4096CBHC", @@ -455,6 +458,7 @@ "has_pins": false, "is_member": false, "is_private": false, + "is_mpim": false, "previous_names": [ "some-channel2" ] diff --git a/_pytest/test_everything.py b/_pytest/test_everything.py index 9d0ad8d..922c78d 100644 --- a/_pytest/test_everything.py +++ b/_pytest/test_everything.py @@ -13,4 +13,4 @@ def test_everything(realish_eventrouter, team): realish_eventrouter.receive_ws_callback(team.team_hash, None) realish_eventrouter.handle_next() - assert len(realish_eventrouter.queue) == 18 + assert len(realish_eventrouter.queue) == 19 diff --git a/wee_slack.py b/wee_slack.py index 62e1b0e..26be1a2 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1511,7 +1511,7 @@ class SlackTeam(object): users, bots, channels, - **kwargs + **kwargs, ): self.slack_api_translator = copy.deepcopy(SLACK_API_TRANSLATOR) self.identifier = team_info["id"] @@ -2084,6 +2084,7 @@ class SlackChannel(SlackChannelCommon): self.last_read = SlackTS(kwargs.get("last_read", 0)) self.channel_buffer = None self.got_history = False + self.got_members = False self.history_needs_update = False self.pending_history_requests = set() self.messages = OrderedDict() @@ -2465,14 +2466,8 @@ class SlackChannel(SlackChannelCommon): def is_visible(self): return w.buffer_get_integer(self.channel_buffer, "hidden") == 0 - def get_history(self, slow_queue=False, full=False, no_log=False): - if self.identifier in self.pending_history_requests: - return - - self.print_getting_history() - self.pending_history_requests.add(self.identifier) - - if not self.got_history: + def get_members(self): + if not self.got_members: # Slack has started returning only a few members for some channels # in rtm.start. I don't know how we can check if the member list is # complete, so we have to fetch members for all channels. @@ -2484,6 +2479,14 @@ class SlackChannel(SlackChannelCommon): ) self.eventrouter.receive(s) + def get_history(self, slow_queue=False, full=False, no_log=False): + if self.identifier in self.pending_history_requests: + return + + self.print_getting_history() + self.pending_history_requests.add(self.identifier) + self.get_members() + post_data = {"channel": self.identifier, "count": config.history_fetch_count} if self.got_history and self.messages and not full: post_data["oldest"] = next(reversed(self.messages)) @@ -2853,14 +2856,25 @@ class SlackMPDMChannel(SlackChannel): """ def __init__(self, eventrouter, team_users, myidentifier, **kwargs): - kwargs["name"] = ",".join( + if "members" in kwargs: + kwargs["name"] = self.name_from_members( + team_users, kwargs["members"], myidentifier + ) + super(SlackMPDMChannel, self).__init__(eventrouter, "mpim", **kwargs) + + def name_from_members(self, team_users=None, members=None, myidentifier=None): + return ",".join( sorted( - getattr(team_users.get(user_id), "name", user_id) - for user_id in kwargs["members"] - if user_id != myidentifier + getattr((team_users or self.team.users).get(user_id), "name", user_id) + for user_id in (members or self.members) + if user_id != (myidentifier or self.team.myidentifier) ) ) - super(SlackMPDMChannel, self).__init__(eventrouter, "mpim", **kwargs) + + def create_buffer(self): + if not self.channel_buffer: + self.get_members() + super(SlackMPDMChannel, self).create_buffer() def open(self, update_remote=True): self.create_buffer() @@ -3595,6 +3609,10 @@ def handle_rtmstart(login_data, eventrouter, team, channel, metadata): for item in login_data["channels"]: if item["is_shared"]: channels[item["id"]] = SlackSharedChannel(eventrouter, **item) + elif item["is_mpim"]: + channels[item["id"]] = SlackMPDMChannel( + eventrouter, users, login_data["self"]["id"], **item + ) elif item["is_private"]: channels[item["id"]] = SlackPrivateChannel(eventrouter, **item) else: @@ -3784,11 +3802,15 @@ def handle_conversationsreplies(message_json, eventrouter, team, channel, metada def handle_conversationsmembers(members_json, eventrouter, team, channel, metadata): if members_json["ok"]: + channel.got_members = True channel.set_members(members_json["members"]) unknown_users = set(members_json["members"]) - set(team.users.keys()) for user in unknown_users: s = SlackRequest(team, "users.info", {"user": user}, channel=channel) eventrouter.receive(s) + if channel.type == "mpim": + name = channel.name_from_members() + channel.set_name(name) else: w.prnt( team.channel_buffer, |