You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.4 KiB

  1. # frozen_string_literal: true
  2. module Admin
  3. class TagsController < BaseController
  4. before_action :set_tags, only: :index
  5. before_action :set_tag, except: [:index, :batch, :approve_all, :reject_all]
  6. before_action :set_usage_by_domain, except: [:index, :batch, :approve_all, :reject_all]
  7. before_action :set_counters, except: [:index, :batch, :approve_all, :reject_all]
  8. def index
  9. authorize :tag, :index?
  10. @form = Form::TagBatch.new
  11. end
  12. def batch
  13. @form = Form::TagBatch.new(form_tag_batch_params.merge(current_account: current_account, action: action_from_button))
  14. @form.save
  15. rescue ActionController::ParameterMissing
  16. flash[:alert] = I18n.t('admin.accounts.no_account_selected')
  17. ensure
  18. redirect_to admin_tags_path(filter_params)
  19. end
  20. def approve_all
  21. Form::TagBatch.new(current_account: current_account, tag_ids: Tag.pending_review.pluck(:id), action: 'approve').save
  22. redirect_to admin_tags_path(filter_params)
  23. end
  24. def reject_all
  25. Form::TagBatch.new(current_account: current_account, tag_ids: Tag.pending_review.pluck(:id), action: 'reject').save
  26. redirect_to admin_tags_path(filter_params)
  27. end
  28. def show
  29. authorize @tag, :show?
  30. end
  31. def update
  32. authorize @tag, :update?
  33. if @tag.update(tag_params.merge(reviewed_at: Time.now.utc))
  34. redirect_to admin_tag_path(@tag.id), notice: I18n.t('admin.tags.updated_msg')
  35. else
  36. render :show
  37. end
  38. end
  39. private
  40. def set_tags
  41. @tags = filtered_tags.page(params[:page])
  42. end
  43. def set_tag
  44. @tag = Tag.find(params[:id])
  45. end
  46. def set_usage_by_domain
  47. @usage_by_domain = @tag.statuses
  48. .with_public_visibility
  49. .excluding_silenced_accounts
  50. .where(Status.arel_table[:id].gteq(Mastodon::Snowflake.id_at(Time.now.utc.beginning_of_day)))
  51. .joins(:account)
  52. .group('accounts.domain')
  53. .reorder('statuses_count desc')
  54. .pluck('accounts.domain, count(*) AS statuses_count')
  55. end
  56. def set_counters
  57. @accounts_today = @tag.history.first[:accounts]
  58. @accounts_week = Redis.current.pfcount(*current_week_days.map { |day| "activity:tags:#{@tag.id}:#{day}:accounts" })
  59. end
  60. def filtered_tags
  61. scope = Tag
  62. scope = scope.discoverable if filter_params[:context] == 'directory'
  63. scope = scope.unreviewed if filter_params[:review] == 'unreviewed'
  64. scope = scope.reviewed.order(reviewed_at: :desc) if filter_params[:review] == 'reviewed'
  65. scope = scope.pending_review.order(requested_review_at: :desc) if filter_params[:review] == 'pending_review'
  66. scope.order(max_score: :desc)
  67. end
  68. def filter_params
  69. params.slice(:context, :review, :page).permit(:context, :review, :page)
  70. end
  71. def tag_params
  72. params.require(:tag).permit(:name, :trendable, :usable, :listable)
  73. end
  74. def current_week_days
  75. now = Time.now.utc.beginning_of_day.to_date
  76. (Date.commercial(now.cwyear, now.cweek)..now).map do |date|
  77. date.to_time(:utc).beginning_of_day.to_i
  78. end
  79. end
  80. def form_tag_batch_params
  81. params.require(:form_tag_batch).permit(:action, tag_ids: [])
  82. end
  83. def action_from_button
  84. if params[:approve]
  85. 'approve'
  86. elsif params[:reject]
  87. 'reject'
  88. end
  89. end
  90. end
  91. end