where i deviate from standard mastodon assets
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.

tags_controller.rb 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. module Admin
  3. class TagsController < BaseController
  4. before_action :set_tag, except: [:index, :batch, :approve_all, :reject_all]
  5. before_action :set_usage_by_domain, except: [:index, :batch, :approve_all, :reject_all]
  6. before_action :set_counters, except: [:index, :batch, :approve_all, :reject_all]
  7. def index
  8. authorize :tag, :index?
  9. @tags = filtered_tags.page(params[:page])
  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_tag
  41. @tag = Tag.find(params[:id])
  42. end
  43. def set_usage_by_domain
  44. @usage_by_domain = @tag.statuses
  45. .with_public_visibility
  46. .excluding_silenced_accounts
  47. .where(Status.arel_table[:id].gteq(Mastodon::Snowflake.id_at(Time.now.utc.beginning_of_day)))
  48. .joins(:account)
  49. .group('accounts.domain')
  50. .reorder('statuses_count desc')
  51. .pluck('accounts.domain, count(*) AS statuses_count')
  52. end
  53. def set_counters
  54. @accounts_today = @tag.history.first[:accounts]
  55. @accounts_week = Redis.current.pfcount(*current_week_days.map { |day| "activity:tags:#{@tag.id}:#{day}:accounts" })
  56. end
  57. def filtered_tags
  58. TagFilter.new(filter_params).results
  59. end
  60. def filter_params
  61. params.slice(:page, *TagFilter::KEYS).permit(:page, *TagFilter::KEYS)
  62. end
  63. def tag_params
  64. params.require(:tag).permit(:name, :trendable, :usable, :listable)
  65. end
  66. def current_week_days
  67. now = Time.now.utc.beginning_of_day.to_date
  68. (Date.commercial(now.cwyear, now.cweek)..now).map do |date|
  69. date.to_time(:utc).beginning_of_day.to_i
  70. end
  71. end
  72. def form_tag_batch_params
  73. params.require(:form_tag_batch).permit(:action, tag_ids: [])
  74. end
  75. def action_from_button
  76. if params[:approve]
  77. 'approve'
  78. elsif params[:reject]
  79. 'reject'
  80. end
  81. end
  82. end
  83. end