From bd839a8abc6db1f8c32fe1273388aafabe3ee30c Mon Sep 17 00:00:00 2001 From: KMY Date: Tue, 28 Feb 2023 09:16:47 +0900 Subject: [PATCH] Add admin attachments panel --- .../admin/media_attachments_controller.rb | 18 +++++++++++ app/models/form/media_attachments_batch.rb | 13 ++++++++ app/models/media_attachment.rb | 6 ++++ .../_media_attachment.html.haml | 11 +++++++ .../admin/media_attachments/index.html.haml | 30 +++++++++++++++++++ config/initializers/doorkeeper.rb | 2 ++ config/navigation.rb | 1 + config/routes.rb | 1 + 8 files changed, 82 insertions(+) create mode 100644 app/controllers/admin/media_attachments_controller.rb create mode 100644 app/models/form/media_attachments_batch.rb create mode 100644 app/views/admin/media_attachments/_media_attachment.html.haml create mode 100644 app/views/admin/media_attachments/index.html.haml diff --git a/app/controllers/admin/media_attachments_controller.rb b/app/controllers/admin/media_attachments_controller.rb new file mode 100644 index 0000000000..35de41d89b --- /dev/null +++ b/app/controllers/admin/media_attachments_controller.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Admin + class MediaAttachmentsController < BaseController + def index + authorize :account, :index? + + @media_attachments = filtered_attachments.page(params[:page]) + @form = Form::MediaAttachmentsBatch.new + end + + private + + def filtered_attachments + MediaAttachment.recently_attachments + end + end +end diff --git a/app/models/form/media_attachments_batch.rb b/app/models/form/media_attachments_batch.rb new file mode 100644 index 0000000000..9a2aa4cb15 --- /dev/null +++ b/app/models/form/media_attachments_batch.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class Form::MediaAttachmentsBatch + include ActiveModel::Model + include Authorization + include AccountableConcern + include Payloadable + + attr_accessor :query + + def save + end +end diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 08abd4e43b..d81c5332a2 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -33,6 +33,7 @@ class MediaAttachment < ApplicationRecord self.inheritance_column = nil include Attachmentable + include RoutingHelper enum type: { :image => 0, :gifv => 1, :video => 2, :unknown => 3, :audio => 4 } enum processing: { :queued => 0, :in_progress => 1, :complete => 2, :failed => 3 }, _prefix: true @@ -208,6 +209,7 @@ class MediaAttachment < ApplicationRecord scope :local, -> { where(remote_url: '') } scope :remote, -> { where.not(remote_url: '') } scope :cached, -> { remote.where.not(file_file_name: nil) } + scope :recently_attachments, -> { attached.order(status_id: 'DESC') } default_scope { order(id: :asc) } @@ -271,6 +273,10 @@ class MediaAttachment < ApplicationRecord delay_processing? && attachment_name == :file end + def url + full_asset_url(file.url(:original)) + end + after_commit :enqueue_processing, on: :create after_commit :reset_parent_cache, on: :update diff --git a/app/views/admin/media_attachments/_media_attachment.html.haml b/app/views/admin/media_attachments/_media_attachment.html.haml new file mode 100644 index 0000000000..cd04a1cd7a --- /dev/null +++ b/app/views/admin/media_attachments/_media_attachment.html.haml @@ -0,0 +1,11 @@ +.batch-table__row{ class: ['batch-table__row--attention'] } + .batch-table__row__content.batch-table__row__content--unpadded + %table.accounts-table + %tbody + %tr + %td + %img{ src: media_attachment.url } + %div + = media_attachment.status.uri + %div + = media_attachment.status.visibility diff --git a/app/views/admin/media_attachments/index.html.haml b/app/views/admin/media_attachments/index.html.haml new file mode 100644 index 0000000000..b8dde62456 --- /dev/null +++ b/app/views/admin/media_attachments/index.html.haml @@ -0,0 +1,30 @@ +- content_for :page_title do + = t('admin.accounts.title') + +- content_for :header_tags do + = javascript_pack_tag 'admin', async: true, crossorigin: 'anonymous' + += form_for(@form, url: admin_media_attachments_path) do |f| + = hidden_field_tag :page, params[:page] || 1 + = hidden_field_tag :select_all_matching, '0' + + - AccountFilter::KEYS.each do |key| + = hidden_field_tag key, params[key] if params[key].present? + + .batch-table + .batch-table__toolbar + - if @media_attachments.total_count > @media_attachments.size + .batch-table__select-all + .not-selected.active + %span= t('generic.all_items_on_page_selected_html', count: @media_attachments.size) + %button{ type: 'button' }= t('generic.select_all_matching_items', count: @media_attachments.total_count) + .selected + %span= t('generic.all_matching_items_selected_html', count: @media_attachments.total_count) + %button{ type: 'button' }= t('generic.deselect') + .batch-table__body + - if @media_attachments.empty? + = nothing_here 'nothing-here--under-tabs' + - else + = render partial: 'media_attachment', collection: @media_attachments, locals: { f: f } + += paginate @media_attachments diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 43aac5769f..ee9e32d253 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -97,6 +97,7 @@ Doorkeeper.configure do :push, :'admin:read', :'admin:read:accounts', + :'admin:read:media_attachments', :'admin:read:reports', :'admin:read:domain_allows', :'admin:read:domain_blocks', @@ -105,6 +106,7 @@ Doorkeeper.configure do :'admin:read:canonical_email_blocks', :'admin:write', :'admin:write:accounts', + :'admin:write:media_attachments', :'admin:write:reports', :'admin:write:domain_allows', :'admin:write:domain_blocks', diff --git a/config/navigation.rb b/config/navigation.rb index 30817d0252..4f89cedb46 100644 --- a/config/navigation.rb +++ b/config/navigation.rb @@ -42,6 +42,7 @@ SimpleNavigation::Configuration.run do |navigation| n.item :moderation, safe_join([fa_icon('gavel fw'), t('moderation.title')]), nil, if: -> { current_user.can?(:manage_reports, :view_audit_log, :manage_users, :manage_invites, :manage_taxonomies, :manage_federation, :manage_blocks) } do |s| s.item :reports, safe_join([fa_icon('flag fw'), t('admin.reports.title')]), admin_reports_path, highlights_on: %r{/admin/reports}, if: -> { current_user.can?(:manage_reports) } s.item :accounts, safe_join([fa_icon('users fw'), t('admin.accounts.title')]), admin_accounts_path(origin: 'local'), highlights_on: %r{/admin/accounts|/admin/pending_accounts|/admin/disputes|/admin/users}, if: -> { current_user.can?(:manage_users) } + s.item :media_attachments, safe_join([fa_icon('users fw'), t('admin.accounts.title')]), admin_media_attachments_path, highlights_on: %r{/admin/media_attachments}, if: -> { current_user.can?(:manage_users) } s.item :invites, safe_join([fa_icon('user-plus fw'), t('admin.invites.title')]), admin_invites_path, if: -> { current_user.can?(:manage_invites) } s.item :follow_recommendations, safe_join([fa_icon('user-plus fw'), t('admin.follow_recommendations.title')]), admin_follow_recommendations_path, highlights_on: %r{/admin/follow_recommendations}, if: -> { current_user.can?(:manage_taxonomies) } s.item :instances, safe_join([fa_icon('cloud fw'), t('admin.instances.title')]), admin_instances_path(limited: whitelist_mode? ? nil : '1'), highlights_on: %r{/admin/instances|/admin/domain_blocks|/admin/domain_allows}, if: -> { current_user.can?(:manage_federation) } diff --git a/config/routes.rb b/config/routes.rb index 51cf7f72e1..82ec5a5f4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -257,6 +257,7 @@ Rails.application.routes.draw do resources :action_logs, only: [:index] resources :warning_presets, except: [:new] + resources :media_attachments, only: [:index] resources :announcements, except: [:show] do member do