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.

103 lines
2.6 KiB

  1. # frozen_string_literal: true
  2. class RelationshipsController < ApplicationController
  3. layout 'admin'
  4. before_action :authenticate_user!
  5. before_action :set_accounts, only: :show
  6. before_action :set_body_classes
  7. helper_method :following_relationship?, :followed_by_relationship?, :mutual_relationship?
  8. def show
  9. @form = Form::AccountBatch.new
  10. end
  11. def update
  12. @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button))
  13. @form.save
  14. rescue ActionController::ParameterMissing
  15. # Do nothing
  16. ensure
  17. redirect_to relationships_path(current_params)
  18. end
  19. private
  20. def set_accounts
  21. @accounts = relationships_scope.page(params[:page]).per(40)
  22. end
  23. def relationships_scope
  24. scope = begin
  25. if following_relationship?
  26. current_account.following.joins(:account_stat)
  27. else
  28. current_account.followers.joins(:account_stat)
  29. end
  30. end
  31. scope.merge!(Follow.recent)
  32. scope.merge!(mutual_relationship_scope) if mutual_relationship?
  33. scope.merge!(moved_account_scope) if params[:status] == 'moved'
  34. scope.merge!(primary_account_scope) if params[:status] == 'primary'
  35. scope.merge!(by_domain_scope) if params[:by_domain].present?
  36. scope.merge!(dormant_account_scope) if params[:activity] == 'dormant'
  37. scope
  38. end
  39. def mutual_relationship_scope
  40. Account.where(id: current_account.following)
  41. end
  42. def moved_account_scope
  43. Account.where.not(moved_to_account_id: nil)
  44. end
  45. def primary_account_scope
  46. Account.where(moved_to_account_id: nil)
  47. end
  48. def dormant_account_scope
  49. AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago)))
  50. end
  51. def by_domain_scope
  52. Account.where(domain: params[:by_domain])
  53. end
  54. def form_account_batch_params
  55. params.require(:form_account_batch).permit(:action, account_ids: [])
  56. end
  57. def following_relationship?
  58. params[:relationship].blank? || params[:relationship] == 'following'
  59. end
  60. def mutual_relationship?
  61. params[:relationship] == 'mutual'
  62. end
  63. def followed_by_relationship?
  64. params[:relationship] == 'followed_by'
  65. end
  66. def current_params
  67. params.slice(:page, :status, :relationship, :by_domain, :activity).permit(:page, :status, :relationship, :by_domain, :activity)
  68. end
  69. def action_from_button
  70. if params[:unfollow]
  71. 'unfollow'
  72. elsif params[:remove_from_followers]
  73. 'remove_from_followers'
  74. elsif params[:block_domains]
  75. 'block_domains'
  76. end
  77. end
  78. def set_body_classes
  79. @body_classes = 'admin'
  80. end
  81. end