FIX: Improve error reporting and failure modes for channel archiving (#19791)

There was an issue with channel archiving, where at times the topic
creation could fail which left the archive in a bad state, as read-only
instead of archived. This commit does several things:

* Changes the ChatChannelArchiveService to validate the topic being
  created first and if it is not valid report the topic creation errors
  in the PM we send to the user
* Changes the UI message in the channel with the archive status to reflect
  that topic creation failed
* Validate the new topic when starting the archive process from the UI,
  and show the validation errors to the user straight away instead of
  creating the archive record and starting the process

This also fixes another issue in the discourse_dev config which was
failing because YAML parsing does not enable all classes by default now,
which was making the seeding rake task for chat fail.
This commit is contained in:
Martin Brennan
2023-01-12 10:04:46 +10:00
committed by GitHub
parent 21a95b000e
commit 387693e889
10 changed files with 199 additions and 64 deletions

View File

@ -12,11 +12,11 @@ describe Chat::ChatChannelArchiveService do
let(:topic_params) { { topic_title: "This will be a new topic", category_id: category.id } }
subject { Chat::ChatChannelArchiveService }
describe "#begin_archive_process" do
describe "#create_archive_process" do
before { 3.times { Fabricate(:chat_message, chat_channel: channel) } }
it "marks the channel as read_only" do
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
@ -25,7 +25,7 @@ describe Chat::ChatChannelArchiveService do
end
it "creates the chat channel archive record to save progress and topic params" do
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
@ -40,7 +40,7 @@ describe Chat::ChatChannelArchiveService do
it "enqueues the archive job" do
channel_archive =
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
@ -56,13 +56,13 @@ describe Chat::ChatChannelArchiveService do
end
it "does nothing if there is already an archive record for the channel" do
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
)
expect {
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
@ -74,7 +74,7 @@ describe Chat::ChatChannelArchiveService do
new_message = Fabricate(:chat_message, chat_channel: channel)
new_message.trash!
channel_archive =
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
@ -90,7 +90,7 @@ describe Chat::ChatChannelArchiveService do
def start_archive
@channel_archive =
subject.begin_archive_process(
subject.create_archive_process(
chat_channel: channel,
acting_user: user,
topic_params: topic_params,
@ -167,6 +167,23 @@ describe Chat::ChatChannelArchiveService do
)
end
it "does not continue archiving if the destination topic fails to be created" do
SiteSetting.max_emojis_in_title = 1
create_messages(3) && start_archive
@channel_archive.update!(destination_topic_title: "Wow this is the new title :tada: :joy:")
subject.new(@channel_archive).execute
expect(@channel_archive.reload.complete?).to eq(false)
expect(@channel_archive.reload.failed?).to eq(true)
expect(@channel_archive.archive_error).to eq("Title can't have more than 1 emoji")
pm_topic = Topic.private_messages.last
expect(pm_topic.title).to eq(
I18n.t("system_messages.chat_channel_archive_failed.subject_template"),
)
expect(pm_topic.first_post.raw).to include("Title can't have more than 1 emoji")
end
describe "channel members" do
before do
create_messages(3)

View File

@ -98,6 +98,22 @@ RSpec.describe Chat::Api::ChatChannelsArchivesController do
}.not_to change { ChatChannelArchive.count }
end
context "when archiving to a new topic" do
it "returns validation errors if the topic is not valid" do
SiteSetting.max_emojis_in_title = 1
new_topic_params_invalid = new_topic_params.dup
new_topic_params_invalid[:archive][
:title
] = "Some new topic with too many emoji :joy: :sob: :tada:"
sign_in(admin)
expect {
post "/chat/api/channels/#{channel.id}/archives", params: new_topic_params_invalid
}.not_to change { ChatChannelArchive.count }
expect(response.status).to eq(400)
expect(response.parsed_body["errors"]).to eq(["Title can't have more than 1 emoji"])
end
end
describe "when retrying the archive process" do
fab!(:channel) { Fabricate(:category_channel, chatable: category, status: :read_only) }
fab!(:archive) do

View File

@ -65,6 +65,18 @@ RSpec.describe "Archive channel", type: :system, js: true do
expect(page).to have_css(".chat-channel-archive-status")
end
it "shows an error when the topic is invalid" do
chat.visit_channel_settings(channel_1)
click_button(I18n.t("js.chat.channel_settings.archive_channel"))
find("#split-topic-name").fill_in(
with: "An interesting topic for cats :cat: :cat2: :smile_cat:",
)
click_button(I18n.t("js.chat.channel_archive.title"))
expect(page).not_to have_content(I18n.t("js.chat.channel_archive.process_started"))
expect(page).to have_content("Title can't have more than 1 emoji")
end
context "when archived channels had unreads" do
before { channel_1.add(current_user) }