@@ -23,7 +23,13 @@ def connect_slack(args, token):
2323 return None
2424
2525def check_slack_messages (args , client , patterns , profile_name , channel_types , isExternal , read_from , channel_ids = None , limit_mins = 60 , archived_channels = False , onlyArchived = False , blacklisted_channel_ids = None ):
26+
2627 results = []
28+
29+ # Initalize blacklisted_channel_ids if not provided
30+ if blacklisted_channel_ids is None :
31+ blacklisted_channel_ids = []
32+
2733 try :
2834 connection = system .get_connection (args )
2935 options = connection .get ('options' , {})
@@ -35,10 +41,6 @@ def check_slack_messages(args, client, patterns, profile_name, channel_types, is
3541
3642 team_info = client .team_info ()
3743 workspace_url = team_info ["team" ]["url" ].rstrip ('/' )
38-
39- # Initalize blacklisted_channel_ids if not provided
40- if blacklisted_channel_ids is None :
41- blacklisted_channel_ids = []
4244
4345 # Helper function to handle rate limits
4446 hawk_args = args
@@ -59,30 +61,45 @@ def rate_limit_retry(func, *args, **kwargs):
5961
6062 if not channel_ids :
6163 system .print_info (args , "Getting all channels because no channel_ids provided" )
64+ system .print_info (args , f"Active blacklist: { blacklisted_channel_ids } " )
6265
6366 # Pagination logic to fetch all non-archived channels
6467 cursor = None
6568 while True :
6669 try :
70+ response = rate_limit_retry (
71+ client .conversations_list ,
72+ types = channel_types ,
73+ limit = 1000 ,
74+ cursor = cursor ,
75+ exclude_archived = not archived_channels
76+ )
77+
78+ # Filter blacklisted channels immediately
79+ batch_channels = response .get ("channels" , [])
80+ filtered_batch = [
81+ ch for ch in batch_channels
82+ if ch ['id' ] not in blacklisted_channel_ids
83+ ]
84+
85+ # Log filtering results
86+ system .print_debug (args ,
87+ f"Batch: { len (batch_channels )} channels before filtering, "
88+ f"{ len (filtered_batch )} after blacklist" )
89+
6790 if onlyArchived :
6891 archived_channels = True
6992 if archived_channels :
7093 system .print_debug (args , f"Considering archived channels, you may want to set archived_channels to False" )
7194 else :
7295 system .print_debug (args , f"Skipping archived channels, you may want to set archived_channels to True" )
7396
74- response = rate_limit_retry (
75- client .conversations_list ,
76- types = channel_types ,
77- limit = 1000 ,
78- cursor = cursor ,
79- exclude_archived = not archived_channels
80- )
97+
8198 if onlyArchived :
8299 system .print_info (args , "Getting only archived channels...." )
83- channels .extend ([channel for channel in response . get ( "channels" , []) if channel .get ("is_archived" )])
100+ channels .extend ([ch for ch in batch_channels if ch .get ("is_archived" )])
84101 else :
85- channels .extend (response . get ( "channels" , []) )
102+ channels .extend (filtered_batch )
86103 # Update the cursor for the next batch
87104 cursor = response .get ("response_metadata" , {}).get ("next_cursor" )
88105
@@ -106,6 +123,7 @@ def rate_limit_retry(func, *args, **kwargs):
106123 except SlackApiError as e :
107124 system .print_error (args , f"Failed to fetch channel with id { channel_id } with error: { e .response ['error' ]} " )
108125 system .print_info (args , f"Found { len (channels )} channels" )
126+
109127 filtered_channels = []
110128 for channel in channels :
111129 channel_is_external = channel .get ("is_ext_shared" )
@@ -373,6 +391,22 @@ def execute(args):
373391 onlyArchived = config .get ('onlyArchived' , False )
374392 archived_channels = config .get ('archived_channels' , False )
375393
394+ # Always apply blacklist, regardless of channel_ids
395+ if blacklisted_channel_ids :
396+ system .print_info (args , f"Filtering out blacklisted channels from { blacklisted_channel_ids } " )
397+ # If specific channels are specified, filter them
398+ if channel_ids :
399+ original_count = len (channel_ids )
400+ channel_ids = [
401+ cid for cid in channel_ids
402+ if cid not in blacklisted_channel_ids
403+ ]
404+ removed = original_count - len (channel_ids )
405+ system .print_info (args ,
406+ f"Filtered { removed } blacklisted channels from explicit list. "
407+ f"Remaining: { channel_ids } " )
408+
409+
376410 # Filter out blacklisted channels, If specific channels are specified
377411 if channel_ids :
378412 system .print_info (args , f"Filtering out blacklisted channels from { channel_ids } " )
0 commit comments