about summary refs log tree commit diff
path: root/app/controllers/authorize_interactions_controller.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-08-18 18:04:49 +0200
committerThibaut Girka <thib@sitedethib.com>2018-08-18 18:04:49 +0200
commitbfeac6747a58b660f0d1b3766c7cfb3ea2655fce (patch)
treee9d1366c30b94df21ec767276fbde175428399fb /app/controllers/authorize_interactions_controller.rb
parent7423c0c1308555db3072ae64141250fdd33ce235 (diff)
parent298ee84488758fc08a0ae9233e04c9637aa2fe02 (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
	.circleci/config.yml
	app/controllers/authorize_follows_controller.rb
	app/javascript/packs/public.js

Moved new stuff from packs/public.js to core/public.js.
Added appropriate use_pack in new controllers.
Diffstat (limited to 'app/controllers/authorize_interactions_controller.rb')
-rw-r--r--app/controllers/authorize_interactions_controller.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/app/controllers/authorize_interactions_controller.rb b/app/controllers/authorize_interactions_controller.rb
new file mode 100644
index 000000000..20b3fa94b
--- /dev/null
+++ b/app/controllers/authorize_interactions_controller.rb
@@ -0,0 +1,71 @@
+# frozen_string_literal: true
+
+class AuthorizeInteractionsController < ApplicationController
+  include Authorization
+
+  layout 'modal'
+
+  before_action :authenticate_user!
+  before_action :set_body_classes
+  before_action :set_resource
+  before_action :set_pack
+
+  def show
+    if @resource.is_a?(Account)
+      render :show
+    elsif @resource.is_a?(Status)
+      redirect_to web_url("statuses/#{@resource.id}")
+    else
+      render :error
+    end
+  end
+
+  def create
+    if @resource.is_a?(Account) && FollowService.new.call(current_account, @resource)
+      render :success
+    else
+      render :error
+    end
+  rescue ActiveRecord::RecordNotFound, Mastodon::NotPermittedError
+    render :error
+  end
+
+  private
+
+  def set_resource
+    @resource = located_resource || render(:error)
+    authorize(@resource, :show?) if @resource.is_a?(Status)
+  end
+
+  def located_resource
+    if uri_param_is_url?
+      ResolveURLService.new.call(uri_param)
+    else
+      account_from_remote_follow
+    end
+  end
+
+  def account_from_remote_follow
+    ResolveAccountService.new.call(uri_param)
+  end
+
+  def uri_param_is_url?
+    parsed_uri.path && %w(http https).include?(parsed_uri.scheme)
+  end
+
+  def parsed_uri
+    Addressable::URI.parse(uri_param).normalize
+  end
+
+  def uri_param
+    params[:uri] || params.fetch(:acct, '').gsub(/\Aacct:/, '')
+  end
+
+  def set_body_classes
+    @body_classes = 'modal-layout'
+  end
+
+  def set_pack
+    use_pack 'modal'
+  end
+end