Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch actions and categories to admin UI for custom emojis #11793

Merged
merged 1 commit into from Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 34 additions & 68 deletions app/controllers/admin/custom_emojis_controller.rb
Expand Up @@ -2,19 +2,20 @@

module Admin
class CustomEmojisController < BaseController
before_action :set_custom_emoji, except: [:index, :new, :create]
before_action :set_filter_params

include ObfuscateFilename

obfuscate_filename [:custom_emoji, :image]

def index
authorize :custom_emoji, :index?

@custom_emojis = filtered_custom_emojis.eager_load(:local_counterpart).page(params[:page])
@form = Form::CustomEmojiBatch.new
end

def new
authorize :custom_emoji, :create?

@custom_emoji = CustomEmoji.new
end

Expand All @@ -31,69 +32,17 @@ def create
end
end

def update
authorize @custom_emoji, :update?

if @custom_emoji.update(resource_params)
log_action :update, @custom_emoji
flash[:notice] = I18n.t('admin.custom_emojis.updated_msg')
else
flash[:alert] = I18n.t('admin.custom_emojis.update_failed_msg')
end
redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
end

def destroy
authorize @custom_emoji, :destroy?
@custom_emoji.destroy!
log_action :destroy, @custom_emoji
flash[:notice] = I18n.t('admin.custom_emojis.destroyed_msg')
redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
end

def copy
authorize @custom_emoji, :copy?

emoji = CustomEmoji.find_or_initialize_by(domain: nil,
shortcode: @custom_emoji.shortcode)
emoji.image = @custom_emoji.image

if emoji.save
log_action :create, emoji
flash[:notice] = I18n.t('admin.custom_emojis.copied_msg')
else
flash[:alert] = I18n.t('admin.custom_emojis.copy_failed_msg')
end

redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
end

def enable
authorize @custom_emoji, :enable?
@custom_emoji.update!(disabled: false)
log_action :enable, @custom_emoji
flash[:notice] = I18n.t('admin.custom_emojis.enabled_msg')
redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
end

def disable
authorize @custom_emoji, :disable?
@custom_emoji.update!(disabled: true)
log_action :disable, @custom_emoji
flash[:notice] = I18n.t('admin.custom_emojis.disabled_msg')
redirect_to admin_custom_emojis_path(page: params[:page], **@filter_params)
def batch
@form = Form::CustomEmojiBatch.new(form_custom_emoji_batch_params.merge(current_account: current_account, action: action_from_button))
@form.save
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have a flash message to tell the changes were processed?

rescue ActionController::ParameterMissing
flash[:alert] = I18n.t('admin.accounts.no_account_selected')
ensure
redirect_to admin_custom_emojis_path(filter_params)
end

private

def set_custom_emoji
@custom_emoji = CustomEmoji.find(params[:id])
end

def set_filter_params
@filter_params = filter_params.to_hash.symbolize_keys
end

def resource_params
params.require(:custom_emoji).permit(:shortcode, :image, :visible_in_picker)
end
Expand All @@ -103,12 +52,29 @@ def filtered_custom_emojis
end

def filter_params
params.permit(
:local,
:remote,
:by_domain,
:shortcode
)
params.slice(:local, :remote, :by_domain, :shortcode, :page).permit(:local, :remote, :by_domain, :shortcode, :page)
end

def action_from_button
if params[:update]
'update'
elsif params[:list]
'list'
elsif params[:unlist]
'unlist'
elsif params[:enable]
'enable'
elsif params[:disable]
'disable'
elsif params[:copy]
'copy'
elsif params[:delete]
'delete'
end
end

def form_custom_emoji_batch_params
params.require(:form_custom_emoji_batch).permit(:action, :category_id, :category_name, custom_emoji_ids: [])
end
end
end
41 changes: 41 additions & 0 deletions app/javascript/styles/mastodon/tables.scss
Expand Up @@ -180,6 +180,18 @@ a.table-action-link {
}
}

&__form {
padding: 16px;
border: 1px solid darken($ui-base-color, 8%);
border-top: 0;
background: $ui-base-color;

.fields-row {
padding-top: 0;
margin-bottom: 0;
}
}

&__row {
border: 1px solid darken($ui-base-color, 8%);
border-top: 0;
Expand Down Expand Up @@ -210,6 +222,35 @@ a.table-action-link {
&--unpadded {
padding: 0;
}

&--with-image {
display: flex;
align-items: center;
}

&__image {
flex: 0 0 auto;
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;

.emojione {
width: 32px;
height: 32px;
}
}

&__text {
flex: 1 1 auto;
}

&__extra {
flex: 0 0 auto;
text-align: right;
color: $darker-text-color;
font-weight: 500;
}
}

.directory__tag {
Expand Down
6 changes: 6 additions & 0 deletions app/models/custom_emoji.rb
Expand Up @@ -59,6 +59,12 @@ def object_type
:emoji
end

def copy!
copy = self.class.find_or_initialize_by(domain: nil, shortcode: shortcode)
copy.image = image
copy.save!
end

class << self
def from_text(text, domain)
return [] if text.blank?
Expand Down
2 changes: 2 additions & 0 deletions app/models/custom_emoji_category.rb
Expand Up @@ -12,4 +12,6 @@

class CustomEmojiCategory < ApplicationRecord
has_many :emojis, class_name: 'CustomEmoji', foreign_key: 'category_id', inverse_of: :category

validates :name, presence: true, uniqueness: true
end
8 changes: 5 additions & 3 deletions app/models/custom_emoji_filter.rb
Expand Up @@ -11,6 +11,8 @@ def results
scope = CustomEmoji.alphabetic

params.each do |key, value|
next if key.to_s == 'page'

scope.merge!(scope_for(key, value)) if value.present?
end

Expand All @@ -22,13 +24,13 @@ def results
def scope_for(key, value)
case key.to_s
when 'local'
CustomEmoji.local
CustomEmoji.local.left_joins(:category).reorder(Arel.sql('custom_emoji_categories.name ASC NULLS FIRST, custom_emojis.shortcode ASC'))
when 'remote'
CustomEmoji.remote
when 'by_domain'
CustomEmoji.where(domain: value.downcase)
CustomEmoji.where(domain: value.strip.downcase)
when 'shortcode'
CustomEmoji.search(value)
CustomEmoji.search(value.strip)
else
raise "Unknown filter: #{key}"
end
Expand Down
106 changes: 106 additions & 0 deletions app/models/form/custom_emoji_batch.rb
@@ -0,0 +1,106 @@
# frozen_string_literal: true

class Form::CustomEmojiBatch
include ActiveModel::Model
include Authorization
include AccountableConcern

attr_accessor :custom_emoji_ids, :action, :current_account,
:category_id, :category_name, :visible_in_picker

def save
case action
when 'update'
update!
when 'list'
list!
when 'unlist'
unlist!
when 'enable'
enable!
when 'disable'
disable!
when 'copy'
copy!
when 'delete'
delete!
end
end

private

def custom_emojis
CustomEmoji.where(id: custom_emoji_ids)
end

def update!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }

category = begin
if category_id.present?
CustomEmojiCategory.find(category_id)
elsif category_name.present?
CustomEmojiCategory.create!(name: category_name)
end
end

custom_emojis.each do |custom_emoji|
custom_emoji.update(category_id: category&.id)
log_action :update, custom_emoji
end
end

def list!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }

custom_emojis.each do |custom_emoji|
custom_emoji.update(visible_in_picker: true)
log_action :update, custom_emoji
end
end

def unlist!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) }

custom_emojis.each do |custom_emoji|
custom_emoji.update(visible_in_picker: false)
log_action :update, custom_emoji
end
end

def enable!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :enable?) }

custom_emojis.each do |custom_emoji|
custom_emoji.update(disabled: false)
log_action :enable, custom_emoji
end
end

def disable!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :disable?) }

custom_emojis.each do |custom_emoji|
custom_emoji.update(disabled: true)
log_action :disable, custom_emoji
end
end

def copy!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :copy?) }

custom_emojis.each do |custom_emoji|
copied_custom_emoji = custom_emoji.copy!
log_action :create, copied_custom_emoji
end
end

def delete!
custom_emojis.each { |custom_emoji| authorize(custom_emoji, :destroy?) }

custom_emojis.each do |custom_emoji|
custom_emoji.destroy
log_action :destroy, custom_emoji
end
end
end