2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
class Admin::EmojisController < Admin::AdminController
|
|
|
|
def index
|
|
|
|
render_serialized(Emoji.custom, EmojiSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2024-07-01 13:32:30 +08:00
|
|
|
# NOTE: This kind of custom logic also needs to be implemented to
|
2021-11-04 09:31:58 +08:00
|
|
|
# be run in the ExternalUploadManager when a direct S3 upload is completed,
|
|
|
|
# related to preventDirectS3Uploads in the UppyUploadMixin.
|
2024-07-01 13:32:30 +08:00
|
|
|
#
|
|
|
|
# Until then, preventDirectS3Uploads is set to true in the UppyUploadMixin.
|
2014-12-23 08:12:26 +08:00
|
|
|
def create
|
|
|
|
file = params[:file] || params[:files].first
|
|
|
|
name = params[:name] || File.basename(file.original_filename, ".*")
|
2020-03-31 02:16:10 +08:00
|
|
|
group = params[:group] ? params[:group].downcase : nil
|
2014-12-23 08:12:26 +08:00
|
|
|
|
2017-12-18 07:31:19 +08:00
|
|
|
hijack do
|
2015-05-20 22:45:31 +08:00
|
|
|
# fix the name
|
2021-11-17 07:20:44 +08:00
|
|
|
name = File.basename(name, ".*")
|
2024-05-27 21:24:55 +08:00
|
|
|
name = Emoji.sanitize_emoji_name(name)
|
2017-05-11 06:16:57 +08:00
|
|
|
upload =
|
2017-02-02 17:41:57 +08:00
|
|
|
UploadCreator.new(file.tempfile, file.original_filename, type: "custom_emoji").create_for(
|
2017-05-11 06:16:57 +08:00
|
|
|
current_user.id,
|
|
|
|
)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-12-18 07:31:19 +08:00
|
|
|
good = true
|
|
|
|
|
2017-02-02 17:41:57 +08:00
|
|
|
data =
|
|
|
|
if upload.persisted?
|
2024-07-22 14:44:49 +08:00
|
|
|
custom_emoji =
|
|
|
|
CustomEmoji.new(name: name, upload: upload, group: group, user: current_user)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
|
|
|
if custom_emoji.save
|
2024-07-22 14:44:49 +08:00
|
|
|
StaffActionLogger.new(current_user).log_custom_emoji_create(name, group: group)
|
|
|
|
|
2017-02-02 17:41:57 +08:00
|
|
|
Emoji.clear_cache
|
2020-03-31 02:16:10 +08:00
|
|
|
{ name: custom_emoji.name, url: custom_emoji.upload.url, group: group }
|
2017-02-02 17:41:57 +08:00
|
|
|
else
|
2017-12-18 07:31:19 +08:00
|
|
|
good = false
|
2017-02-02 17:41:57 +08:00
|
|
|
failed_json.merge(errors: custom_emoji.errors.full_messages)
|
|
|
|
end
|
|
|
|
else
|
2017-12-18 07:31:19 +08:00
|
|
|
good = false
|
2017-02-02 17:41:57 +08:00
|
|
|
failed_json.merge(errors: upload.errors.full_messages)
|
|
|
|
end
|
2015-05-20 22:45:31 +08:00
|
|
|
|
2017-12-18 07:31:19 +08:00
|
|
|
render json: data.as_json, status: good ? 200 : 422
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
name = params.require(:id)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-11-21 06:50:23 +08:00
|
|
|
# NOTE: the upload will automatically be removed by the 'clean_up_uploads' job
|
2024-07-22 14:44:49 +08:00
|
|
|
emoji = CustomEmoji.find_by(name: name)
|
|
|
|
|
|
|
|
if emoji.present?
|
|
|
|
StaffActionLogger.new(current_user).log_custom_emoji_destroy(name)
|
|
|
|
emoji.destroy!
|
|
|
|
end
|
2017-02-02 17:41:57 +08:00
|
|
|
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
Jobs.enqueue(:rebake_custom_emoji_posts, name: name)
|
|
|
|
|
|
|
|
render json: success_json
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|