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.

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