Skip to content

Commit

Permalink
Add batch approve/reject for pending hashtags in admin UI (#11791)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Sep 9, 2019
1 parent 3feacd2 commit 261e522
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 19 deletions.
41 changes: 37 additions & 4 deletions app/controllers/admin/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@
module Admin
class TagsController < BaseController
before_action :set_tags, only: :index
before_action :set_tag, except: :index
before_action :set_usage_by_domain, except: :index
before_action :set_counters, except: :index
before_action :set_tag, except: [:index, :batch, :approve_all, :reject_all]
before_action :set_usage_by_domain, except: [:index, :batch, :approve_all, :reject_all]
before_action :set_counters, except: [:index, :batch, :approve_all, :reject_all]

def index
authorize :tag, :index?

@form = Form::TagBatch.new
end

def batch
@form = Form::TagBatch.new(form_tag_batch_params.merge(current_account: current_account, action: action_from_button))
@form.save
rescue ActionController::ParameterMissing
flash[:alert] = I18n.t('admin.accounts.no_account_selected')
ensure
redirect_to admin_tags_path(filter_params)
end

def approve_all
Form::TagBatch.new(current_account: current_account, tag_ids: Tag.pending_review.pluck(:id), action: 'approve').save
redirect_to admin_tags_path(filter_params)
end

def reject_all
Form::TagBatch.new(current_account: current_account, tag_ids: Tag.pending_review.pluck(:id), action: 'reject').save
redirect_to admin_tags_path(filter_params)
end

def show
Expand Down Expand Up @@ -61,7 +82,7 @@ def filtered_tags
end

def filter_params
params.slice(:context, :review).permit(:context, :review)
params.slice(:context, :review, :page).permit(:context, :review, :page)
end

def tag_params
Expand All @@ -75,5 +96,17 @@ def current_week_days
date.to_time(:utc).beginning_of_day.to_i
end
end

def form_tag_batch_params
params.require(:form_tag_batch).permit(:action, tag_ids: [])
end

def action_from_button
if params[:approve]
'approve'
elsif params[:reject]
'reject'
end
end
end
end
10 changes: 10 additions & 0 deletions app/javascript/styles/mastodon/tables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ a.table-action-link {
padding: 0;
}
}

.directory__tag {
margin: 0;
width: 100%;

a {
background: transparent;
border-radius: 0;
}
}
}

.status__content {
Expand Down
33 changes: 33 additions & 0 deletions app/models/form/tag_batch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

class Form::TagBatch
include ActiveModel::Model
include Authorization

attr_accessor :tag_ids, :action, :current_account

def save
case action
when 'approve'
approve!
when 'reject'
reject!
end
end

private

def tags
Tag.where(id: tag_ids)
end

def approve!
tags.each { |tag| authorize(tag, :update?) }
tags.update_all(trendable: true, reviewed_at: Time.now.utc)
end

def reject!
tags.each { |tag| authorize(tag, :update?) }
tags.update_all(trendable: false, reviewed_at: Time.now.utc)
end
end
30 changes: 17 additions & 13 deletions app/views/admin/tags/_tag.html.haml
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
.directory__tag
= link_to admin_tag_path(tag.id) do
%h4
= fa_icon 'hashtag'
= tag.name
.batch-table__row
%label.batch-table__row__select.batch-table__row__select--aligned.batch-checkbox
= f.check_box :tag_ids, { multiple: true, include_hidden: false }, tag.id

%small
= t('admin.tags.in_directory', count: tag.accounts_count)
&bull;
= t('admin.tags.unique_uses_today', count: tag.history.first[:accounts])
.directory__tag
= link_to admin_tag_path(tag.id) do
%h4
= fa_icon 'hashtag'
= tag.name

- if tag.trending?
= fa_icon 'fire fw'
= t('admin.tags.trending_right_now')
%small
= t('admin.tags.in_directory', count: tag.accounts_count)
&bull;
= t('admin.tags.unique_uses_today', count: tag.history.first[:accounts])

.trends__item__current= number_to_human tag.history.first[:uses], strip_insignificant_zeros: true
- if tag.trending?
= fa_icon 'fire fw'
= t('admin.tags.trending_right_now')

.trends__item__current= number_to_human tag.history.first[:uses], strip_insignificant_zeros: true
37 changes: 36 additions & 1 deletion app/views/admin/tags/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
- content_for :page_title do
= t('admin.tags.title')

- content_for :header_tags do
= javascript_pack_tag 'admin', integrity: true, async: true, crossorigin: 'anonymous'

.filters
.filter-subset
%strong= t('admin.tags.context')
Expand All @@ -18,5 +21,37 @@

%hr.spacer/

= render @tags
= form_for(@form, url: batch_admin_tags_path) do |f|
= hidden_field_tag :page, params[:page] || 1
= hidden_field_tag :context, params[:context]
= hidden_field_tag :review, params[:review]

.batch-table
.batch-table__toolbar
%label.batch-table__toolbar__select.batch-checkbox-all
= check_box_tag :batch_checkbox_all, nil, false
.batch-table__toolbar__actions
- if params[:review] == 'pending_review'
= f.button safe_join([fa_icon('check'), t('admin.accounts.approve')]), name: :approve, class: 'table-action-link', type: :submit, data: { confirm: t('admin.reports.are_you_sure') }

= f.button safe_join([fa_icon('times'), t('admin.accounts.reject')]), name: :reject, class: 'table-action-link', type: :submit, data: { confirm: t('admin.reports.are_you_sure') }
- else
%span.neutral-hint= t('generic.no_batch_actions_available')

.batch-table__body
- if @tags.empty?
= nothing_here 'nothing-here--under-tabs'
- else
= render partial: 'tag', collection: @tags, locals: { f: f }

= paginate @tags

- if params[:review] == 'pending_review'
%hr.spacer/

%div{ style: 'overflow: hidden' }
%div{ style: 'float: right' }
= link_to t('admin.accounts.reject_all'), reject_all_admin_tags_path, method: :post, data: { confirm: t('admin.accounts.are_you_sure') }, class: 'button button--destructive'

%div
= link_to t('admin.accounts.approve_all'), approve_all_admin_tags_path, method: :post, data: { confirm: t('admin.accounts.are_you_sure') }, class: 'button'
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ en:
all: All
changes_saved_msg: Changes successfully saved!
copy: Copy
no_batch_actions_available: No batch actions available on this page
order_by: Order by
save_changes: Save changes
validation_errors:
Expand Down
9 changes: 8 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,14 @@
end

resources :account_moderation_notes, only: [:create, :destroy]
resources :tags, only: [:index, :show, :update]

resources :tags, only: [:index, :show, :update] do
collection do
post :approve_all
post :reject_all
post :batch
end
end
end

get '/admin', to: redirect('/admin/dashboard', status: 302)
Expand Down

0 comments on commit 261e522

Please sign in to comment.