2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
class Admin::EmojisController < Admin::AdminController
|
|
|
|
def index
|
|
|
|
render_serialized(Emoji.custom, EmojiSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2021-11-03 21:31:58 -04:00
|
|
|
# TODO (martin) Figure out a way that this kind of custom logic can
|
|
|
|
# be run in the ExternalUploadManager when a direct S3 upload is completed,
|
|
|
|
# related to preventDirectS3Uploads in the UppyUploadMixin.
|
2014-12-22 19:12:26 -05:00
|
|
|
def create
|
|
|
|
file = params[:file] || params[:files].first
|
|
|
|
name = params[:name] || File.basename(file.original_filename, ".*")
|
2020-03-30 14:16:10 -04:00
|
|
|
group = params[:group] ? params[:group].downcase : nil
|
2014-12-22 19:12:26 -05:00
|
|
|
|
2017-12-17 18:31:19 -05:00
|
|
|
hijack do
|
2015-05-20 10:45:31 -04:00
|
|
|
# fix the name
|
2021-11-16 18:20:44 -05:00
|
|
|
name = File.basename(name, ".*")
|
2023-01-09 07:20:10 -05:00
|
|
|
name = name.gsub(/[^a-z0-9]+/i, "_").gsub(/_{2,}/, "_").downcase
|
2015-05-20 10:45:31 -04:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
upload =
|
|
|
|
UploadCreator.new(file.tempfile, file.original_filename, type: "custom_emoji").create_for(
|
|
|
|
current_user.id,
|
|
|
|
)
|
2017-02-02 04:41:57 -05:00
|
|
|
|
2017-12-17 18:31:19 -05:00
|
|
|
good = true
|
|
|
|
|
2017-02-02 04:41:57 -05:00
|
|
|
data =
|
|
|
|
if upload.persisted?
|
2020-03-30 14:16:10 -04:00
|
|
|
custom_emoji = CustomEmoji.new(name: name, upload: upload, group: group)
|
2017-02-02 04:41:57 -05:00
|
|
|
|
|
|
|
if custom_emoji.save
|
|
|
|
Emoji.clear_cache
|
2020-03-30 14:16:10 -04:00
|
|
|
{ name: custom_emoji.name, url: custom_emoji.upload.url, group: group }
|
2017-02-02 04:41:57 -05:00
|
|
|
else
|
2017-12-17 18:31:19 -05:00
|
|
|
good = false
|
2017-02-02 04:41:57 -05:00
|
|
|
failed_json.merge(errors: custom_emoji.errors.full_messages)
|
|
|
|
end
|
|
|
|
else
|
2017-12-17 18:31:19 -05:00
|
|
|
good = false
|
2017-02-02 04:41:57 -05:00
|
|
|
failed_json.merge(errors: upload.errors.full_messages)
|
|
|
|
end
|
2015-05-20 10:45:31 -04:00
|
|
|
|
2017-12-17 18:31:19 -05:00
|
|
|
render json: data.as_json, status: good ? 200 : 422
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
name = params.require(:id)
|
2017-02-02 04:41:57 -05:00
|
|
|
|
2017-11-20 17:50:23 -05:00
|
|
|
# NOTE: the upload will automatically be removed by the 'clean_up_uploads' job
|
|
|
|
CustomEmoji.find_by(name: name)&.destroy!
|
2017-02-02 04:41:57 -05:00
|
|
|
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
Jobs.enqueue(:rebake_custom_emoji_posts, name: name)
|
|
|
|
|
|
|
|
render json: success_json
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
end
|