relationships_controller.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_relationships, only: :show
  7. before_action :set_body_classes
  8. before_action :set_cache_headers
  9. helper_method :following_relationship?, :followed_by_relationship?, :mutual_relationship?
  10. def show
  11. @form = Form::AccountBatch.new
  12. end
  13. def update
  14. @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button))
  15. @form.save
  16. rescue ActionController::ParameterMissing
  17. # Do nothing
  18. rescue Mastodon::NotPermittedError, ActiveRecord::RecordNotFound
  19. flash[:alert] = I18n.t('relationships.follow_failure') if action_from_button == 'follow'
  20. ensure
  21. redirect_to relationships_path(filter_params)
  22. end
  23. private
  24. def set_accounts
  25. @accounts = RelationshipFilter.new(current_account, filter_params).results.page(params[:page]).per(40)
  26. end
  27. def set_relationships
  28. @relationships = AccountRelationshipsPresenter.new(@accounts.pluck(:id), current_user.account_id)
  29. end
  30. def form_account_batch_params
  31. params.require(:form_account_batch).permit(:action, account_ids: [])
  32. end
  33. def following_relationship?
  34. params[:relationship].blank? || params[:relationship] == 'following'
  35. end
  36. def mutual_relationship?
  37. params[:relationship] == 'mutual'
  38. end
  39. def followed_by_relationship?
  40. params[:relationship] == 'followed_by'
  41. end
  42. def filter_params
  43. params.slice(:page, *RelationshipFilter::KEYS).permit(:page, *RelationshipFilter::KEYS)
  44. end
  45. def action_from_button
  46. if params[:follow]
  47. 'follow'
  48. elsif params[:unfollow]
  49. 'unfollow'
  50. elsif params[:remove_from_followers]
  51. 'remove_from_followers'
  52. elsif params[:block_domains] || params[:remove_domains_from_followers]
  53. 'remove_domains_from_followers'
  54. end
  55. end
  56. def set_body_classes
  57. @body_classes = 'admin'
  58. end
  59. def set_cache_headers
  60. response.cache_control.replace(private: true, no_store: true)
  61. end
  62. end