0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 15:18:34 +08:00
discourse/app/models/custom_emoji.rb
Martin Brennan b736e72b1c
DEV: Retroactive review and refactor of custom emoji bulk import/export (#41860)
Followup bd3906ce64

Converts the new endpoints made in the original PR into
service classes, using AI to drive implementation based
on the service authoring skill:

* CustomEmoji::Export
* CustomEmoji::PreviewImport
* CustomEmoji::ConfirmImport

In addition, some supporting action, lib, and serializer
classes have been added. This fixed a couple of
bugs as well:

* Import names now go through `Emoji.sanitize_emoji_name` like
single-emoji create
* No-group imports no longer persist a literal "default" string
* Two 500s (suspicious zip entries, malformed CSV) became proper 422s
* Duplicate CSV names are flagged at preview instead of exploding the
confirm transaction
*` Emoji.clear_cache `moved outside the transaction
2026-07-22 10:06:54 +10:00

52 lines
1.2 KiB
Ruby
Vendored

# frozen_string_literal: true
class CustomEmoji < ActiveRecord::Base
DEFAULT_GROUP = "default"
MAX_GROUP_LENGTH = 20
belongs_to :upload
belongs_to :user
has_many :upload_references, as: :target, dependent: :destroy
validates :name, presence: true, uniqueness: true
validates :group, length: { maximum: MAX_GROUP_LENGTH }
validates :upload_id, presence: true
validates :user_id, presence: true
def self.normalize_group(group)
normalized = group.to_s.strip.downcase.presence
normalized == DEFAULT_GROUP ? nil : normalized
end
before_validation :set_default_user_id, on: :create
after_save do
if saved_change_to_upload_id?
UploadReference.ensure_exist!(upload_ids: [upload_id], target: self)
end
end
private
def set_default_user_id
self.user_id ||= Discourse.system_user.id
end
end
# == Schema Information
#
# Table name: custom_emojis
#
# id :integer not null, primary key
# group :string(20)
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# upload_id :integer not null
# user_id :integer default(-1), not null
#
# Indexes
#
# index_custom_emojis_on_name (name) UNIQUE
#