Reputation: 1
Details of the Problem
I am working on a project where we previously used system messages in our GetStream.io chat application. Recently, our company decided to discontinue the use of system messages. As the backend developer responsible for managing the GetStream.io server, I need to delete all system messages from all channels.
I have attempted to find and delete these system messages using the GetStream.io Python client library, but I've run into some issues.
What I've Tried
I tried querying all channels and then iterating over each channel to find and delete system messages.
def delete_system_messages():
# Step 1: Query all channels
result = client.query_channels(
{}, # No filters to get all channels
{"last_message_at": -1},
limit=100, # Adjust limit as needed
offset=0,
)
channels = result.get("channels")
if not channels:
print("No channels found")
return
total_deleted_messages = 0
for channel in channels:
channel_id = channel['channel']['id']
channel_type = channel['channel']['type']
# Step 2: Retrieve messages from the channel
messages_response = client.get_channel_type(channel_type).get_channel(channel_id).get_messages({
"limit": 100, # Adjust limit as needed
"offset": 0,
})
messages = messages_response.get('messages', [])
# Step 3: Find and delete system messages
for message in messages:
if message.get("type") == "system":
msg_id = message.get("id")
try:
print(f"Deleting message ID: {msg_id} in channel {channel_id}")
client.delete_message(msg_id, hard=True)
total_deleted_messages += 1
except Exception as e:
print(f"Failed to delete message ID {msg_id}: {str(e)}")
print(f"Total system messages deleted: {total_deleted_messages}")
Issues Encountered:
get_messages
method doesn't provide options for message pagination beyond the initial set.I also tried using the search functionality to find system messages across all channels.
def search_and_delete_system_messages():
filter_conditions = {} # No filters to search all channels
message_filter_conditions = {"type": {"$eq": "system"}}
search_params = {
"limit": 100, # Max limit per request
"offset": 0,
}
# Perform the search
response = client.search(
filter_conditions=filter_conditions,
query="", # Empty query since we're using message_filter_conditions
message_filter_conditions=message_filter_conditions,
**search_params,
)
# Extract the messages from the response
messages = response.get("results", [])
if not messages:
print("No system messages found")
return
# Delete the found system messages
for result in messages:
message_data = result["message"]
msg_id = message_data["id"]
try:
print(f"Deleting message ID: {msg_id}")
client.delete_message(msg_id, hard=True)
except Exception as e:
print(f"Failed to delete message ID {msg_id}: {str(e)}")
Issues Encountered:
What I Expected to Happen
type
.What Actually Happened
My Question
Additional Information
client
object is properly initialized and authenticated.Upvotes: 0
Views: 69