FIX: Don't fail when exporting chat messages from deleted channels (#23131)

This commit is contained in:
Andrei Prigorshnev 2023-08-18 14:22:24 +04:00 committed by GitHub
parent a2f5b1b101
commit 052462a8f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 64 deletions

View File

@ -37,31 +37,33 @@ module Chat
private private
def export(from, to) def export(from, to)
Chat::Message Chat::Channel.unscoped do
.unscoped Chat::Message
.where(created_at: from..to) .unscoped
.includes(:chat_channel) .where(created_at: from..to)
.includes(:user) .includes(:chat_channel)
.includes(:last_editor) .includes(:user)
.find_each do |chat_message| .includes(:last_editor)
yield( .find_each do |chat_message|
[ yield(
chat_message.id, [
chat_message.chat_channel.id, chat_message.id,
chat_message.chat_channel.name, chat_message.chat_channel.id,
chat_message.user.id, chat_message.chat_channel.name,
chat_message.user.username, chat_message.user.id,
chat_message.message, chat_message.user.username,
chat_message.cooked, chat_message.message,
chat_message.created_at, chat_message.cooked,
chat_message.updated_at, chat_message.created_at,
chat_message.deleted_at, chat_message.updated_at,
chat_message.in_reply_to&.id, chat_message.deleted_at,
chat_message.last_editor&.id, chat_message.in_reply_to&.id,
chat_message.last_editor&.username, chat_message.last_editor&.id,
] chat_message.last_editor&.username,
) ]
end )
end
end
end end
end end
end end

View File

@ -1,53 +1,76 @@
# frozen_string_literal: true # frozen_string_literal: true
describe Chat::MessagesExporter do describe Chat::MessagesExporter do
fab!(:public_channel) { Fabricate(:chat_channel) } context "with different kinds of channels" do
fab!(:public_channel_message_1) { Fabricate(:chat_message, chat_channel: public_channel) } fab!(:public_channel) { Fabricate(:chat_channel) }
fab!(:public_channel_message_2) { Fabricate(:chat_message, chat_channel: public_channel) } fab!(:public_channel_message_1) { Fabricate(:chat_message, chat_channel: public_channel) }
# this message is deleted in the before block: fab!(:public_channel_message_2) { Fabricate(:chat_message, chat_channel: public_channel) }
fab!(:deleted_message) { Fabricate(:chat_message, chat_channel: public_channel) } # this message is deleted in the before block:
fab!(:deleted_message) { Fabricate(:chat_message, chat_channel: public_channel) }
fab!(:private_channel) { Fabricate(:private_category_channel, group: Fabricate(:group)) } fab!(:private_channel) { Fabricate(:private_category_channel, group: Fabricate(:group)) }
fab!(:private_channel_message_1) { Fabricate(:chat_message, chat_channel: private_channel) } fab!(:private_channel_message_1) { Fabricate(:chat_message, chat_channel: private_channel) }
fab!(:private_channel_message_2) { Fabricate(:chat_message, chat_channel: private_channel) } fab!(:private_channel_message_2) { Fabricate(:chat_message, chat_channel: private_channel) }
fab!(:user_1) { Fabricate(:user) } fab!(:user_1) { Fabricate(:user) }
fab!(:user_2) { Fabricate(:user) } fab!(:user_2) { Fabricate(:user) }
fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [user_1, user_2]) } fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [user_1, user_2]) }
fab!(:direct_message_1) { Fabricate(:chat_message, chat_channel: private_channel, user: user_1) } fab!(:direct_message_1) do
fab!(:direct_message_2) { Fabricate(:chat_message, chat_channel: private_channel, user: user_2) } Fabricate(:chat_message, chat_channel: private_channel, user: user_1)
end
fab!(:direct_message_2) do
Fabricate(:chat_message, chat_channel: private_channel, user: user_2)
end
before { deleted_message.trash! } before { deleted_message.trash! }
it "exports messages" do it "exports messages" do
exporter = Class.new.extend(Chat::MessagesExporter) exporter = Class.new.extend(Chat::MessagesExporter)
result = [] result = []
exporter.chat_message_export { |data_row| result << data_row } exporter.chat_message_export { |data_row| result << data_row }
expect(result.length).to be(7) expect(result.length).to be(7)
assert_exported_message(result[0], public_channel_message_1) assert_exported_message(result[0], public_channel_message_1)
assert_exported_message(result[1], public_channel_message_2) assert_exported_message(result[1], public_channel_message_2)
assert_exported_message(result[2], deleted_message) assert_exported_message(result[2], deleted_message)
assert_exported_message(result[3], private_channel_message_1) assert_exported_message(result[3], private_channel_message_1)
assert_exported_message(result[4], private_channel_message_2) assert_exported_message(result[4], private_channel_message_2)
assert_exported_message(result[5], direct_message_1) assert_exported_message(result[5], direct_message_1)
assert_exported_message(result[6], direct_message_2) assert_exported_message(result[6], direct_message_2)
end
end
context "with messages from deleted channels" do
fab!(:channel) { Fabricate(:chat_channel, deleted_at: Time.now) }
fab!(:message) { Fabricate(:chat_message, chat_channel: channel) }
it "exports such messages" do
exporter = Class.new.extend(Chat::MessagesExporter)
result = []
exporter.chat_message_export { |data_row| result << data_row }
expect(result.length).to be(1)
assert_exported_message(result[0], message)
end
end end
def assert_exported_message(data_row, message) def assert_exported_message(data_row, message)
expect(data_row[0]).to eq(message.id) Chat::Channel.unscoped do
expect(data_row[1]).to eq(message.chat_channel.id) expect(data_row[0]).to eq(message.id)
expect(data_row[2]).to eq(message.chat_channel.name) expect(data_row[1]).to eq(message.chat_channel.id)
expect(data_row[3]).to eq(message.user.id) expect(data_row[2]).to eq(message.chat_channel.name)
expect(data_row[4]).to eq(message.user.username) expect(data_row[3]).to eq(message.user.id)
expect(data_row[5]).to eq(message.message) expect(data_row[4]).to eq(message.user.username)
expect(data_row[6]).to eq(message.cooked) expect(data_row[5]).to eq(message.message)
expect(data_row[7]).to eq_time(message.created_at) expect(data_row[6]).to eq(message.cooked)
expect(data_row[8]).to eq_time(message.updated_at) expect(data_row[7]).to eq_time(message.created_at)
expect(data_row[9]).to eq_time(message.deleted_at) expect(data_row[8]).to eq_time(message.updated_at)
expect(data_row[10]).to eq(message.in_reply_to_id) expect(data_row[9]).to eq_time(message.deleted_at)
expect(data_row[11]).to eq(message.last_editor.id) expect(data_row[10]).to eq(message.in_reply_to_id)
expect(data_row[12]).to eq(message.last_editor.username) expect(data_row[11]).to eq(message.last_editor.id)
expect(data_row[12]).to eq(message.last_editor.username)
end
end end
end end