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.

71 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class AuthorizeInteractionsController < ApplicationController
  3. include Authorization
  4. layout 'modal'
  5. before_action :authenticate_user!
  6. before_action :set_body_classes
  7. before_action :set_resource
  8. before_action :set_pack
  9. def show
  10. if @resource.is_a?(Account)
  11. render :show
  12. elsif @resource.is_a?(Status)
  13. redirect_to web_url("statuses/#{@resource.id}")
  14. else
  15. render :error
  16. end
  17. end
  18. def create
  19. if @resource.is_a?(Account) && FollowService.new.call(current_account, @resource)
  20. render :success
  21. else
  22. render :error
  23. end
  24. rescue ActiveRecord::RecordNotFound, Mastodon::NotPermittedError
  25. render :error
  26. end
  27. private
  28. def set_resource
  29. @resource = located_resource || render(:error)
  30. authorize(@resource, :show?) if @resource.is_a?(Status)
  31. end
  32. def located_resource
  33. if uri_param_is_url?
  34. ResolveURLService.new.call(uri_param)
  35. else
  36. account_from_remote_follow
  37. end
  38. end
  39. def account_from_remote_follow
  40. ResolveAccountService.new.call(uri_param)
  41. end
  42. def uri_param_is_url?
  43. parsed_uri.path && %w(http https).include?(parsed_uri.scheme)
  44. end
  45. def parsed_uri
  46. Addressable::URI.parse(uri_param).normalize
  47. end
  48. def uri_param
  49. params[:uri] || params.fetch(:acct, '').gsub(/\Aacct:/, '')
  50. end
  51. def set_body_classes
  52. @body_classes = 'modal-layout'
  53. end
  54. def set_pack
  55. use_pack 'modal'
  56. end
  57. end