This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/app/controllers/api/chat_channels_archives_controller.rb
Martin Brennan 387693e889
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.
2023-01-12 10:04:46 +10:00

56 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class Chat::Api::ChatChannelsArchivesController < Chat::Api::ChatChannelsController
def create
existing_archive = channel_from_params.chat_channel_archive
if existing_archive.present?
guardian.ensure_can_change_channel_status!(channel_from_params, :archived)
raise Discourse::InvalidAccess if !existing_archive.failed?
Chat::ChatChannelArchiveService.retry_archive_process(chat_channel: channel_from_params)
return render json: success_json
end
new_topic = archive_params[:type] == "new_topic"
raise Discourse::InvalidParameters if new_topic && archive_params[:title].blank?
raise Discourse::InvalidParameters if !new_topic && archive_params[:topic_id].blank?
if !guardian.can_change_channel_status?(channel_from_params, :read_only)
raise Discourse::InvalidAccess.new(I18n.t("chat.errors.channel_cannot_be_archived"))
end
begin
Chat::ChatChannelArchiveService.create_archive_process(
chat_channel: channel_from_params,
acting_user: current_user,
topic_params: topic_params,
)
rescue Chat::ChatChannelArchiveService::ArchiveValidationError => err
return render json: failed_json.merge(errors: err.errors), status: 400
end
render json: success_json
end
private
def archive_params
@archive_params ||=
params
.require(:archive)
.tap do |ca|
ca.require(:type)
ca.permit(:title, :topic_id, :category_id, tags: [])
end
end
def topic_params
@topic_params ||= {
topic_id: archive_params[:topic_id],
topic_title: archive_params[:title],
category_id: archive_params[:category_id],
tags: archive_params[:tags],
}
end
end