Skip to content

Commit

Permalink
Load authorized identities for channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj725 committed Sep 24, 2024
1 parent e10361c commit a1c65b7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
22 changes: 18 additions & 4 deletions libs/community/langchain_community/document_loaders/slack_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ def lazy_load(self) -> Iterator[Document]:
messages = self.client.get_messages(
channel=channel_id, limit=self.message_limit
)
authorized_identities = []
if messages and self.load_auth:
# Load authorized identities if load_auth is True
authorized_identities = self.client.get_authorized_identities(
channel_name, self.user_details_map, self.channel_details_map
)

for message in messages:
yield self._convert_message_to_document(message, channel_name)
yield self._convert_message_to_document(
message, channel_name, authorized_identities
)

def _convert_message_to_document(
self, message: dict, channel_name: str
self, message: dict, channel_name: str, authorized_identities: list = []
) -> Document:
"""
Convert a message to a Document object.
Expand All @@ -89,7 +98,9 @@ def _convert_message_to_document(
Document: A Document object representing the message.
"""
text = self._enriched_message_text(message)
metadata = self._get_message_metadata(message, channel_name)
metadata = self._get_message_metadata(
message, channel_name, authorized_identities
)
return Document(
page_content=text,
metadata=metadata,
Expand All @@ -112,7 +123,9 @@ def _enriched_message_text(self, message: dict) -> str:
reply_texts = [reply.get("text", "") for reply in replies]
return "\n\n".join(reply_texts)

def _get_message_metadata(self, message: dict, channel_name: str) -> dict:
def _get_message_metadata(
self, message: dict, channel_name: str, authorized_identities: list = []
) -> dict:
"""Create and return metadata for a given message and channel."""
timestamp = message.get("ts", "")
user = message.get("user", "")
Expand All @@ -122,6 +135,7 @@ def _get_message_metadata(self, message: dict, channel_name: str) -> dict:
"channel": channel_name,
"timestamp": timestamp,
"user": user,
"authorized_identities": authorized_identities,
}

def _get_message_source(self, channel_name: str, user: str, timestamp: str) -> str:
Expand Down
50 changes: 50 additions & 0 deletions libs/community/langchain_community/utilities/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,53 @@ def get_messages(self, channel: str, limit: int = 1000) -> Any:
except Exception as e:
logger.error(f"Error getting messages from Slack: {e}")
return None

def get_channel_members(self, channel: str) -> list:
"""
Get a list of members in a conversation
"""
try:
response = self.slack_client.conversations_members(channel=channel)
return response.get("members", [])
except Exception as e:
logger.error(f"Error getting members for channel {channel}: {e}")
return []

def get_authorized_identities(
self, channel_name: str, user_details_map: dict, channel_details_map: dict
):
"""
Get a list of authorized identities for a given channel.
An authorized identity is a user who has posted a message in the channel.
Args:
channel_name (str): The channel name.
user_details_map (dict): A dictionary mapping user IDs to their respective details.

Check failure on line 129 in libs/community/langchain_community/utilities/slack.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/utilities/slack.py:129:89: E501 Line too long (95 > 88)

Check failure on line 129 in libs/community/langchain_community/utilities/slack.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/utilities/slack.py:129:89: E501 Line too long (95 > 88)
channel_details_map (dict): A dictionary mapping channel names to their respective details.

Check failure on line 130 in libs/community/langchain_community/utilities/slack.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/utilities/slack.py:130:89: E501 Line too long (103 > 88)

Check failure on line 130 in libs/community/langchain_community/utilities/slack.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/utilities/slack.py:130:89: E501 Line too long (103 > 88)
Returns:
list: A list of authorized identities(user details) for the given channel.
"""
try:
authorized_identities = []

channel_details = channel_details_map.get(channel_name, {})

_is_private = channel_details.get("is_private", False)

if _is_private:
members = self.get_channel_members(channel_details.get("id"))
else:
members = user_details_map.keys()

for member in members:
user = user_details_map.get(member, {})
user_email = user.get("email")
if user_email:
authorized_identities.append(user_email)
return authorized_identities
except Exception as e:
logger.error(
f"Error getting authorized identities for channel {channel_name}: {e}"
)
return []

0 comments on commit a1c65b7

Please sign in to comment.