Skip to content

Commit e912655

Browse files
committed
Always preserve support threads with >3 bot messages
This is so that if someone runs ?faq, the ticket still counts as being resolved in the DB.
1 parent 1afe037 commit e912655

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

nephthys/events/message_deletion.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff 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"]})

0 commit comments

Comments
 (0)