FIX: Handle deleted original message for thread index (#21470)

Since we have channel message retention which deletes
messages, we can end up with cases where the thread
is still around but the message is deleted. We will
handle the cascade delete in a different commit --
for now we will ensure the thread list lookup handles
this case and doesn't error.
This commit is contained in:
Martin Brennan 2023-05-10 13:58:15 +02:00 committed by GitHub
parent cbbaeb55b5
commit 91c5658e9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 3 deletions

View File

@ -10,7 +10,9 @@ module Chat
def initialize(object, opts)
super(object, opts)
@opts = opts
original_message.thread = object
# Avoids an N1 to re-load the thread in the serializer for original_message.
object.original_message.thread = object
end
def meta

View File

@ -64,7 +64,9 @@ module Chat
)
.where("chat_messages.user_id = ? OR chat_messages.user_id IS NULL", guardian.user.id)
.where(channel_id: channel.id)
.where("original_messages.deleted_at IS NULL AND chat_messages.deleted_at IS NULL")
.where(
"original_messages.deleted_at IS NULL AND chat_messages.deleted_at IS NULL AND original_messages.id IS NOT NULL",
)
.group("chat_threads.id")
.order("last_posted_at DESC NULLS LAST")
.limit(50)

View File

@ -24,5 +24,6 @@
&__no-threads {
@include thread-list-item;
margin: 0.5rem 0rem 0.5rem 0.5rem;
}
}

View File

@ -3,5 +3,9 @@
.chat-thread-list-item {
margin: 0.5rem;
}
&__no-threads {
margin: 0.5rem;
}
}
}

View File

@ -59,11 +59,16 @@ RSpec.describe Chat::LookupChannelThreads do
expect(result.threads.map(&:id)).to eq([thread_3.id, thread_1.id, thread_2.id])
end
it "does not return threads where the original message is deleted" do
it "does not return threads where the original message is trashed" do
thread_1.original_message.trash!
expect(result.threads.map(&:id)).to eq([thread_3.id, thread_2.id])
end
it "does not return threads where the original message is deleted" do
thread_1.original_message.destroy
expect(result.threads.map(&:id)).to eq([thread_3.id, thread_2.id])
end
it "does not count deleted messages for sort order" do
Chat::Message.find_by(user: current_user, thread: thread_3).trash!
expect(result.threads.map(&:id)).to eq([thread_1.id, thread_2.id])