relationships_controller.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 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. rescue Mastodon::NotPermittedError, ActiveRecord::RecordNotFound
  18. flash[:alert] = I18n.t('relationships.follow_failure') if action_from_button == 'follow'
  19. ensure
  20. redirect_to relationships_path(filter_params)
  21. end
  22. private
  23. def set_accounts
  24. @accounts = RelationshipFilter.new(current_account, filter_params).results.page(params[:page]).per(40)
  25. end
  26. def set_relationships
  27. @relationships = AccountRelationshipsPresenter.new(@accounts.pluck(:id), current_user.account_id)
  28. end
  29. def form_account_batch_params
  30. params.require(:form_account_batch).permit(:action, account_ids: [])
  31. end
  32. def following_relationship?
  33. params[:relationship].blank? || params[:relationship] == 'following'
  34. end
  35. def mutual_relationship?
  36. params[:relationship] == 'mutual'
  37. end
  38. def followed_by_relationship?
  39. params[:relationship] == 'followed_by'
  40. end
  41. def filter_params
  42. params.slice(:page, *RelationshipFilter::KEYS).permit(:page, *RelationshipFilter::KEYS)
  43. end
  44. def action_from_button
  45. if params[:follow]
  46. 'follow'
  47. elsif params[:unfollow]
  48. 'unfollow'
  49. elsif params[:remove_from_followers]
  50. 'remove_from_followers'
  51. elsif params[:block_domains] || params[:remove_domains_from_followers]
  52. 'remove_domains_from_followers'
  53. end
  54. end
  55. def set_body_classes
  56. @body_classes = 'admin'
  57. end
  58. end