about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-10-05 23:42:05 +0200
committerGitHub <noreply@github.com>2017-10-05 23:42:05 +0200
commit49cc0eb3e7d1521079e33a60216df46679082547 (patch)
treee7c7879d07d9a5a6258c14b4e2d49b8c07d5d9fa /app
parentb9c76e2edbc372e1b472f6ba480631b79fe24722 (diff)
Improve admin UI for custom emojis, add copy/disable/enable (#5231)
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin/custom_emojis_controller.rb42
-rw-r--r--app/controllers/api/v1/custom_emojis_controller.rb2
-rw-r--r--app/models/account_filter.rb2
-rw-r--r--app/models/custom_emoji.rb11
-rw-r--r--app/models/custom_emoji_filter.rb34
-rw-r--r--app/views/admin/custom_emojis/_custom_emoji.html.haml13
-rw-r--r--app/views/admin/custom_emojis/index.html.haml20
7 files changed, 119 insertions, 5 deletions
diff --git a/app/controllers/admin/custom_emojis_controller.rb b/app/controllers/admin/custom_emojis_controller.rb
index d70514d9a..dba9f1012 100644
--- a/app/controllers/admin/custom_emojis_controller.rb
+++ b/app/controllers/admin/custom_emojis_controller.rb
@@ -2,8 +2,10 @@
 
 module Admin
   class CustomEmojisController < BaseController
+    before_action :set_custom_emoji, except: [:index, :new, :create]
+
     def index
-      @custom_emojis = CustomEmoji.local
+      @custom_emojis = filtered_custom_emojis.page(params[:page])
     end
 
     def new
@@ -21,14 +23,50 @@ module Admin
     end
 
     def destroy
-      CustomEmoji.find(params[:id]).destroy
+      @custom_emoji.destroy
       redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.destroyed_msg')
     end
 
+    def copy
+      emoji = @custom_emoji.dup
+      emoji.domain = nil
+
+      if emoji.save
+        redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.copied_msg')
+      else
+        redirect_to admin_custom_emojis_path, alert: I18n.t('admin.custom_emojis.copy_failed_msg')
+      end
+    end
+
+    def enable
+      @custom_emoji.update!(disabled: false)
+      redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.enabled_msg')
+    end
+
+    def disable
+      @custom_emoji.update!(disabled: true)
+      redirect_to admin_custom_emojis_path, notice: I18n.t('admin.custom_emojis.disabled_msg')
+    end
+
     private
 
+    def set_custom_emoji
+      @custom_emoji = CustomEmoji.find(params[:id])
+    end
+
     def resource_params
       params.require(:custom_emoji).permit(:shortcode, :image)
     end
+
+    def filtered_custom_emojis
+      CustomEmojiFilter.new(filter_params).results
+    end
+
+    def filter_params
+      params.permit(
+        :local,
+        :remote
+      )
+    end
   end
 end
diff --git a/app/controllers/api/v1/custom_emojis_controller.rb b/app/controllers/api/v1/custom_emojis_controller.rb
index 4dd77fb55..f8cd64455 100644
--- a/app/controllers/api/v1/custom_emojis_controller.rb
+++ b/app/controllers/api/v1/custom_emojis_controller.rb
@@ -4,6 +4,6 @@ class Api::V1::CustomEmojisController < Api::BaseController
   respond_to :json
 
   def index
-    render json: CustomEmoji.local, each_serializer: REST::CustomEmojiSerializer
+    render json: CustomEmoji.local.where(disabled: false), each_serializer: REST::CustomEmojiSerializer
   end
 end
diff --git a/app/models/account_filter.rb b/app/models/account_filter.rb
index 1a8cc5192..189872368 100644
--- a/app/models/account_filter.rb
+++ b/app/models/account_filter.rb
@@ -9,9 +9,11 @@ class AccountFilter
 
   def results
     scope = Account.alphabetic
+
     params.each do |key, value|
       scope.merge!(scope_for(key, value)) if value.present?
     end
+
     scope
   end
 
diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb
index 9e9be5e12..258b50c82 100644
--- a/app/models/custom_emoji.rb
+++ b/app/models/custom_emoji.rb
@@ -12,6 +12,7 @@
 #  image_updated_at   :datetime
 #  created_at         :datetime         not null
 #  updated_at         :datetime         not null
+#  disabled           :boolean          default(FALSE), not null
 #
 
 class CustomEmoji < ApplicationRecord
@@ -26,10 +27,16 @@ class CustomEmoji < ApplicationRecord
   validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { in: 0..50.kilobytes }
   validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
 
-  scope :local, -> { where(domain: nil) }
+  scope :local,      -> { where(domain: nil) }
+  scope :remote,     -> { where.not(domain: nil) }
+  scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
 
   include Remotable
 
+  def local?
+    domain.nil?
+  end
+
   class << self
     def from_text(text, domain)
       return [] if text.blank?
@@ -38,7 +45,7 @@ class CustomEmoji < ApplicationRecord
 
       return [] if shortcodes.empty?
 
-      where(shortcode: shortcodes, domain: domain)
+      where(shortcode: shortcodes, domain: domain, disabled: false)
     end
   end
 end
diff --git a/app/models/custom_emoji_filter.rb b/app/models/custom_emoji_filter.rb
new file mode 100644
index 000000000..2d1394a59
--- /dev/null
+++ b/app/models/custom_emoji_filter.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class CustomEmojiFilter
+  attr_reader :params
+
+  def initialize(params)
+    @params = params
+  end
+
+  def results
+    scope = CustomEmoji.alphabetic
+
+    params.each do |key, value|
+      scope.merge!(scope_for(key, value)) if value.present?
+    end
+
+    scope
+  end
+
+  private
+
+  def scope_for(key, value)
+    case key.to_s
+    when 'local'
+      CustomEmoji.local
+    when 'remote'
+      CustomEmoji.remote
+    when 'by_domain'
+      CustomEmoji.where(domain: value)
+    else
+      raise "Unknown filter: #{key}"
+    end
+  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 ff1aa9925..53263c43f 100644
--- a/app/views/admin/custom_emojis/_custom_emoji.html.haml
+++ b/app/views/admin/custom_emojis/_custom_emoji.html.haml
@@ -4,4 +4,17 @@
   %td
     %samp= ":#{custom_emoji.shortcode}:"
   %td
+    - if custom_emoji.local?
+      = t('admin.accounts.location.local')
+    - else
+      = custom_emoji.domain
+  %td
+    - unless custom_emoji.local?
+      = table_link_to 'copy', t('admin.custom_emojis.copy'), copy_admin_custom_emoji_path(custom_emoji), method: :post
+  %td
+    - if custom_emoji.disabled?
+      = table_link_to 'power-off', t('admin.custom_emojis.enable'), enable_admin_custom_emoji_path(custom_emoji), method: :post, data: { confirm: t('admin.accounts.are_you_sure') }
+    - else
+      = table_link_to 'power-off', t('admin.custom_emojis.disable'), disable_admin_custom_emoji_path(custom_emoji), method: :post, data: { confirm: t('admin.accounts.are_you_sure') }
+  %td
     = table_link_to 'times', t('admin.custom_emojis.delete'), admin_custom_emoji_path(custom_emoji), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') }
diff --git a/app/views/admin/custom_emojis/index.html.haml b/app/views/admin/custom_emojis/index.html.haml
index d5f32e84b..20ffb8529 100644
--- a/app/views/admin/custom_emojis/index.html.haml
+++ b/app/views/admin/custom_emojis/index.html.haml
@@ -1,14 +1,34 @@
 - content_for :page_title do
   = t('admin.custom_emojis.title')
 
+.filters
+  .filter-subset
+    %strong= t('admin.accounts.location.title')
+    %ul
+      %li= filter_link_to t('admin.accounts.location.all'), local: nil, remote: nil
+      %li
+        - if selected? local: '1', remote: nil
+          = filter_link_to t('admin.accounts.location.local'), {local: nil, remote: nil}, {local: '1', remote: nil}
+        - else
+          = filter_link_to t('admin.accounts.location.local'), local: '1', remote: nil
+      %li
+        - if selected? remote: '1', local: nil
+          = filter_link_to t('admin.accounts.location.remote'), {remote: nil, local: nil}, {remote: '1', local: nil}
+        - else
+          = filter_link_to t('admin.accounts.location.remote'), remote: '1', local: nil
+
 .table-wrapper
   %table.table
     %thead
       %tr
         %th= t('admin.custom_emojis.emoji')
         %th= t('admin.custom_emojis.shortcode')
+        %th= t('admin.accounts.domain')
+        %th
+        %th
         %th
     %tbody
       = render @custom_emojis
 
+= paginate @custom_emojis
 = link_to t('admin.custom_emojis.upload'), new_admin_custom_emoji_path, class: 'button'