Skip to content

Commit

Permalink
Merge pull request #485 from arusahni/feat/channel-member-list
Browse files Browse the repository at this point in the history
  • Loading branch information
DonDebonair committed Aug 1, 2021
2 parents 9d5e11a + f8a3bba commit c0ee83b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion machine/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__description__ = "A sexy, simple, yet powerful and extendable Slack bot"
__uri__ = "https://github.com/DandyDev/slack-machine"

__version_info__ = (0, 21, 0)
__version_info__ = (0, 21, 1)
__version__ = '.'.join(map(str, __version_info__))

__author__ = "Daan Debie"
Expand Down
17 changes: 16 additions & 1 deletion machine/clients/singletons/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self):
self.web_client = WebClient(token=slack_api_token, proxy=http_proxy)
self._bot_info = {}
self._users = {}
self._channels = {}
self._channels: Dict[str, Channel] = {}

@staticmethod
def get_instance() -> 'LowLevelSlackClient':
Expand Down Expand Up @@ -103,6 +103,19 @@ def _on_channel_deleted(self, **payload):
del self._channels[payload['data']['channel']]
logger.debug("Channel %s deleted" % channel.name)

def _on_member_left_channel(self, **payload):
channel = self._channels[payload['data']['channel']]
channel._members.remove(payload['data']['user'])
logger.debug('Member left channel %s', channel.name)

def _on_member_joined_channel(self, **payload):
channel = self._channels[payload['data']['channel']]
# Since the member list is cached on first query, only update the cache if the list has been
# retrieved at least once.
if channel._members:
channel._members.append(payload['data']['user'])
logger.debug('Member joined %s', channel.name)

@property
def bot_info(self) -> Dict[str, str]:
return self._bot_info
Expand All @@ -119,6 +132,8 @@ def start(self):
RTMClient.on(event='channel_rename', callback=self._on_channel_updated)
RTMClient.on(event='channel_archive', callback=self._on_channel_updated)
RTMClient.on(event='channel_unarchive', callback=self._on_channel_updated)
RTMClient.on(event='member_joined_channel', callback=self._on_member_joined_channel)
RTMClient.on(event='member_left_channel', callback=self._on_member_left_channel)
RTMClient.on(event='user_change', callback=self._on_user_change)
self.rtm_client.start()

Expand Down
23 changes: 20 additions & 3 deletions machine/models/channel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import List, Dict, Any, Optional

from dacite import from_dict


@dataclass(frozen=True)
class PurposeTopic:
value: str
Expand Down Expand Up @@ -33,10 +32,10 @@ class Channel:
is_group: Optional[bool]
is_im: Optional[bool]
user: Optional[str]
members: Optional[List[str]]
topic: Optional[PurposeTopic]
purpose: Optional[PurposeTopic]
previous_names: Optional[List[str]]
_members: List[str] = field(default_factory=list)

@property
def identifier(self):
Expand All @@ -45,6 +44,24 @@ def identifier(self):
else:
return self.id

@property
def members(self) -> Optional[List[str]]:
"""Get the member IDs for the given users."""
if not self._members:
self._load_members()
return self._members

def _load_members(self):
"""Load a fresh set of members."""
from machine.clients.singletons.slack import LowLevelSlackClient, call_paginated_endpoint
web_client = LowLevelSlackClient.get_instance().web_client
all_members = call_paginated_endpoint(
web_client.conversations_members, 'members', channel=self.id
)
self._members.clear()
for member in all_members:
self._members.append(member)

@staticmethod
def from_api_response(user_reponse: Dict[str, Any]) -> 'Channel':
return from_dict(data_class=Channel, data=user_reponse)

0 comments on commit c0ee83b

Please sign in to comment.