1
0

relationships_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.eager_load(:account_stat).reorder(nil)
  27. else
  28. current_account.followers.eager_load(:account_stat).reorder(nil)
  29. end
  30. end
  31. scope.merge!(Follow.recent) if params[:order].blank? || params[:order] == 'recent'
  32. scope.merge!(Account.by_recent_status) if params[:order] == 'active'
  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, :order).permit(:page, :status, :relationship, :by_domain, :activity, :order)
  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. end