From 8638e715cb11206e60c95e7195af571e0238de70 Mon Sep 17 00:00:00 2001 From: KMY Date: Mon, 22 May 2023 10:44:32 +0900 Subject: [PATCH] Add emoji alias-names support --- app/controllers/admin/custom_emojis_controller.rb | 2 +- app/javascript/mastodon/features/emoji/emoji.js | 4 +++- app/models/custom_emoji.rb | 12 ++++++++++++ app/models/form/custom_emoji_batch.rb | 5 +++-- app/serializers/rest/custom_emoji_serializer.rb | 5 +++++ .../admin/custom_emojis/_custom_emoji.html.haml | 2 ++ app/views/admin/custom_emojis/index.html.haml | 6 ++++++ .../20230521122642_add_aliases_to_custom_emoji.rb | 7 +++++++ db/schema.rb | 5 +++-- 9 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20230521122642_add_aliases_to_custom_emoji.rb diff --git a/app/controllers/admin/custom_emojis_controller.rb b/app/controllers/admin/custom_emojis_controller.rb index 00d069cdfb..507acde98a 100644 --- a/app/controllers/admin/custom_emojis_controller.rb +++ b/app/controllers/admin/custom_emojis_controller.rb @@ -74,7 +74,7 @@ module Admin end def form_custom_emoji_batch_params - params.require(:form_custom_emoji_batch).permit(:action, :category_id, :category_name, custom_emoji_ids: []) + params.require(:form_custom_emoji_batch).permit(:action, :category_id, :category_name, :aliases_raw, custom_emoji_ids: []) end end end diff --git a/app/javascript/mastodon/features/emoji/emoji.js b/app/javascript/mastodon/features/emoji/emoji.js index 06800d4329..c442820804 100644 --- a/app/javascript/mastodon/features/emoji/emoji.js +++ b/app/javascript/mastodon/features/emoji/emoji.js @@ -143,6 +143,8 @@ export const buildCustomEmojis = (customEmojis) => { const shortcode = emoji.get('shortcode'); const url = autoPlayGif ? emoji.get('url') : emoji.get('static_url'); const name = shortcode.replace(':', ''); + const aliases = emoji.get('aliases'); + const keywords = aliases ? [name, ...aliases] : [name]; emojis.push({ id: name, @@ -150,7 +152,7 @@ export const buildCustomEmojis = (customEmojis) => { short_names: [name], text: '', emoticons: [], - keywords: [name], + keywords, imageUrl: url, custom: true, customCategory: emoji.get('category'), diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index 5f463ad0ca..4855c188dc 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -21,6 +21,7 @@ # image_storage_schema_version :integer # image_width :integer # image_height :integer +# aliases :jsonb # class CustomEmoji < ApplicationRecord @@ -80,6 +81,17 @@ class CustomEmoji < ApplicationRecord size(Rails.configuration.x.use_s3 ? image.url : image.path) end + def aliases_raw + return '' if aliases.nil? || aliases.blank? + + aliases.join(',') + end + + def aliases_raw=(raw) + aliases = raw.split(',').filter(&:present?).uniq + self[:aliases] = aliases + end + class << self def from_text(text, domain = nil) return [] if text.blank? diff --git a/app/models/form/custom_emoji_batch.rb b/app/models/form/custom_emoji_batch.rb index 484415f902..3773f4d3da 100644 --- a/app/models/form/custom_emoji_batch.rb +++ b/app/models/form/custom_emoji_batch.rb @@ -6,7 +6,7 @@ class Form::CustomEmojiBatch include AccountableConcern attr_accessor :custom_emoji_ids, :action, :current_account, - :category_id, :category_name, :visible_in_picker + :category_id, :category_name, :aliases_raw, :visible_in_picker def save case action @@ -43,7 +43,8 @@ class Form::CustomEmojiBatch end custom_emojis.each do |custom_emoji| - custom_emoji.update(category_id: category&.id) + new_aliases_raw = (aliases_raw.presence || custom_emoji.aliases_raw) + custom_emoji.update(category_id: category&.id, aliases_raw: new_aliases_raw) log_action :update, custom_emoji end end diff --git a/app/serializers/rest/custom_emoji_serializer.rb b/app/serializers/rest/custom_emoji_serializer.rb index 2d47068642..27795f2a38 100644 --- a/app/serializers/rest/custom_emoji_serializer.rb +++ b/app/serializers/rest/custom_emoji_serializer.rb @@ -8,6 +8,7 @@ class REST::CustomEmojiSerializer < ActiveModel::Serializer attribute :category, if: :category_loaded? attribute :width, if: :width? attribute :height, if: :height? + attribute :aliases, if: :aliases? def url full_asset_url(object.image.url) @@ -40,4 +41,8 @@ class REST::CustomEmojiSerializer < ActiveModel::Serializer def height object.respond_to?(:image_height) ? object.image_height : object.height end + + def aliases? + object.respond_to?(:aliases) && object.aliases.present? + end end diff --git a/app/views/admin/custom_emojis/_custom_emoji.html.haml b/app/views/admin/custom_emojis/_custom_emoji.html.haml index 8d34d38e58..28903ca6e8 100644 --- a/app/views/admin/custom_emojis/_custom_emoji.html.haml +++ b/app/views/admin/custom_emojis/_custom_emoji.html.haml @@ -10,6 +10,8 @@ - if custom_emoji.local? %span.account-role.bot= custom_emoji.category&.name || t('admin.custom_emojis.uncategorized') + %br/ + %span= custom_emoji.aliases_raw .batch-table__row__content__extra - if custom_emoji.local? diff --git a/app/views/admin/custom_emojis/index.html.haml b/app/views/admin/custom_emojis/index.html.haml index 6ded4b4332..941f50102c 100644 --- a/app/views/admin/custom_emojis/index.html.haml +++ b/app/views/admin/custom_emojis/index.html.haml @@ -78,6 +78,12 @@ .label_input = f.text_field :category_name, class: 'string optional', placeholder: t('admin.custom_emojis.create_new_category'), 'aria-label': t('admin.custom_emojis.create_new_category') + .fields-row + .fields-group.fields-row__column + .input.string.optional + .label_input + = f.text_field :aliases_raw, class: 'string optional', placeholder: 'Alias names', 'aria-label': 'Alias names' + .batch-table__body - if @custom_emojis.empty? = nothing_here 'nothing-here--under-tabs' diff --git a/db/migrate/20230521122642_add_aliases_to_custom_emoji.rb b/db/migrate/20230521122642_add_aliases_to_custom_emoji.rb new file mode 100644 index 0000000000..3717f4172c --- /dev/null +++ b/db/migrate/20230521122642_add_aliases_to_custom_emoji.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddAliasesToCustomEmoji < ActiveRecord::Migration[6.1] + def change + add_column :custom_emojis, :aliases, :jsonb + end +end diff --git a/db/schema.rb b/db/schema.rb index 8d14ab1315..e931742255 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_05_14_030455) do +ActiveRecord::Schema.define(version: 2023_05_21_122642) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -438,6 +438,7 @@ ActiveRecord::Schema.define(version: 2023_05_14_030455) do t.integer "image_storage_schema_version" t.integer "image_width" t.integer "image_height" + t.jsonb "aliases" t.index ["shortcode", "domain"], name: "index_custom_emojis_on_shortcode_and_domain", unique: true end @@ -1487,4 +1488,4 @@ ActiveRecord::Schema.define(version: 2023_05_14_030455) do end -# rubocop:enable all +#rubocop:enable all