FIX: Do not error if admin/owner checks target message (#21721)

In the ChannelViewBuilder, we introduced a check to see if
the target message exists, which errors if the message has
been trashed. However if the user is the creator of the message
or admin then they are able to see trashed messages, so
we need to take this into account.
This commit is contained in:
Martin Brennan 2023-05-24 12:01:41 +02:00 committed by GitHub
parent 8076a84ff0
commit 2b4dc97551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -65,9 +65,12 @@ module Chat
guardian.can_preview_chat_channel?(channel)
end
def target_message_exists(contract:, **)
def target_message_exists(contract:, guardian:, **)
return true if contract.target_message_id.blank?
Chat::Message.exists?(id: contract.target_message_id)
target_message = Chat::Message.unscoped.find_by(id: contract.target_message_id)
return false if target_message.blank?
return true if !target_message.trashed?
target_message.user_id == guardian.user.id || guardian.is_staff?
end
def determine_threads_enabled(channel:, **)

View File

@ -228,6 +228,18 @@ RSpec.describe Chat::ChannelViewBuilder do
before { message.trash! }
it { is_expected.to fail_a_policy(:target_message_exists) }
context "when the user is the owner of the trashed message" do
before { message.update!(user: current_user) }
it { is_expected.not_to fail_a_policy(:target_message_exists) }
end
context "when the user is admin" do
before { current_user.update!(admin: true) }
it { is_expected.not_to fail_a_policy(:target_message_exists) }
end
end
end
end