File tree Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -31,13 +31,24 @@ async def handle_question_deletion(
3131 raise e
3232 bot_info = await env .slack_client .auth_test ()
3333 bot_user_id = bot_info .get ("user_id" )
34- messages_to_delete = []
34+ bot_replies = []
35+ non_bot_replies = []
3536 for msg in thread_history ["messages" ]:
37+ if msg ["ts" ] == deleted_msg ["ts" ]:
38+ continue # Ignore top-level message
3639 if msg ["user" ] == bot_user_id :
37- messages_to_delete .append (msg )
38- elif msg ["ts" ] != deleted_msg ["ts" ]:
39- # Preserve the thread and ticket if there are non-bot messages in there
40- return
40+ bot_replies .append (msg )
41+ else :
42+ non_bot_replies .append (msg )
43+
44+ should_keep_thread = (
45+ # Preserve if there are any human replies
46+ len (non_bot_replies ) > 0
47+ # More than 2 bot replies implies someone ran ?faq or something, so we'll preserve the ticket
48+ or len (bot_replies ) > 2
49+ )
50+ if should_keep_thread :
51+ return
4152
4253 # Delete ticket from DB and clean up bot messages
4354 ticket = await env .db .ticket .find_first (where = {"msgTs" : deleted_msg ["ts" ]})
You can’t perform that action at this time.
0 commit comments