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.

109 lines
2.9 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.eager_load(:account_stat).reorder(nil)
  28. else
  29. current_account.followers.eager_load(:account_stat).reorder(nil)
  30. end
  31. end
  32. scope.merge!(Follow.recent) if params[:order].blank? || params[:order] == 'recent'
  33. scope.merge!(Account.by_recent_status) if params[:order] == 'active'
  34. scope.merge!(mutual_relationship_scope) if mutual_relationship?
  35. scope.merge!(moved_account_scope) if params[:status] == 'moved'
  36. scope.merge!(primary_account_scope) if params[:status] == 'primary'
  37. scope.merge!(by_domain_scope) if params[:by_domain].present?
  38. scope.merge!(dormant_account_scope) if params[:activity] == 'dormant'
  39. scope
  40. end
  41. def mutual_relationship_scope
  42. Account.where(id: current_account.following)
  43. end
  44. def moved_account_scope
  45. Account.where.not(moved_to_account_id: nil)
  46. end
  47. def primary_account_scope
  48. Account.where(moved_to_account_id: nil)
  49. end
  50. def dormant_account_scope
  51. AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago)))
  52. end
  53. def by_domain_scope
  54. Account.where(domain: params[:by_domain])
  55. end
  56. def form_account_batch_params
  57. params.require(:form_account_batch).permit(:action, account_ids: [])
  58. end
  59. def following_relationship?
  60. params[:relationship].blank? || params[:relationship] == 'following'
  61. end
  62. def mutual_relationship?
  63. params[:relationship] == 'mutual'
  64. end
  65. def followed_by_relationship?
  66. params[:relationship] == 'followed_by'
  67. end
  68. def current_params
  69. params.slice(:page, :status, :relationship, :by_domain, :activity, :order).permit(:page, :status, :relationship, :by_domain, :activity, :order)
  70. end
  71. def action_from_button
  72. if params[:unfollow]
  73. 'unfollow'
  74. elsif params[:remove_from_followers]
  75. 'remove_from_followers'
  76. elsif params[:block_domains]
  77. 'block_domains'
  78. end
  79. end
  80. def set_body_classes
  81. @body_classes = 'admin'
  82. end
  83. def set_pack
  84. use_pack 'admin'
  85. end
  86. end