Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorJonne Haß <me@jhass.eu>2020-03-12 15:49:14 +0300
committerJonne Haß <me@jhass.eu>2020-03-20 14:38:27 +0300
commit2d28ddc1ef3cb54002118856f27601afd091f875 (patch)
treeadc56088617eda341c0e40a01f55bb348ed2b498 /app
parent6278925ce2a77d9c19721aca9c3cafc81c5cb2d3 (diff)
Add API route to (un)block a user
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/v1/users_controller.rb26
-rw-r--r--app/controllers/blocks_controller.rb35
-rw-r--r--app/services/block_service.rb29
3 files changed, 70 insertions, 20 deletions
diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb
index a7cf3effb..065a4057b 100644
--- a/app/controllers/api/v1/users_controller.rb
+++ b/app/controllers/api/v1/users_controller.rb
@@ -17,6 +17,10 @@ module Api
require_access_token %w[contacts:read]
end
+ before_action only: %i[block] do
+ require_access_token %w[contacts:modify]
+ end
+
before_action only: %i[show] do
require_access_token %w[profile]
end
@@ -81,6 +85,28 @@ module Api
render_paged_api_response posts_page
end
+ def block
+ person = Person.find_by!(guid: params[:user_id])
+ service = BlockService.new(current_user)
+ if request.request_method_symbol == :post
+ begin
+ service.block(person)
+ head :created
+ rescue ActiveRecord::RecordNotUnique
+ render_error 409, "User is already blocked"
+ end
+ elsif request.request_method_symbol == :delete
+ begin
+ service.unblock(person)
+ head :no_content
+ rescue ActiveRecord::RecordNotFound
+ render_error 410, "User is not blocked"
+ end
+ else
+ raise AbstractController::ActionNotFound
+ end
+ end
+
private
def aspects_service
diff --git a/app/controllers/blocks_controller.rb b/app/controllers/blocks_controller.rb
index 39d8cb91c..480bd7538 100644
--- a/app/controllers/blocks_controller.rb
+++ b/app/controllers/blocks_controller.rb
@@ -4,9 +4,10 @@ class BlocksController < ApplicationController
before_action :authenticate_user!
def create
- block = current_user.blocks.new(block_params)
-
- send_message(block) if block.save
+ begin
+ block_service.block(Person.find_by!(id: block_params[:person_id]))
+ rescue ActiveRecord::RecordNotUnique
+ end
respond_to do |format|
format.json { head :no_content }
@@ -15,13 +16,13 @@ class BlocksController < ApplicationController
end
def destroy
- block = current_user.blocks.find_by(id: params[:id])
- notice = if block&.delete
- ContactRetraction.for(block).defer_dispatch(current_user)
- {notice: t("blocks.destroy.success")}
- else
- {error: t("blocks.destroy.failure")}
- end
+ notice = nil
+ begin
+ block_service.remove_block(current_user.blocks.find_by!(id: params[:id]))
+ notice = {notice: t("blocks.destroy.success")}
+ rescue ActiveRecord::RecordNotFound
+ notice = {error: t("blocks.destroy.failure")}
+ end
respond_to do |format|
format.json { head :no_content }
@@ -31,17 +32,11 @@ class BlocksController < ApplicationController
private
- def send_message(block)
- contact = current_user.contact_for(block.person)
-
- if contact
- current_user.disconnect(contact)
- elsif block.person.remote?
- Diaspora::Federation::Dispatcher.defer_dispatch(current_user, block)
- end
- end
-
def block_params
params.require(:block).permit(:person_id)
end
+
+ def block_service
+ BlockService.new(current_user)
+ end
end
diff --git a/app/services/block_service.rb b/app/services/block_service.rb
new file mode 100644
index 000000000..bc768287e
--- /dev/null
+++ b/app/services/block_service.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class BlockService
+ def initialize(user)
+ @user = user
+ end
+
+ def block(person)
+ raise ActiveRecord::RecordNotUnique if @user.blocks.exists?(person: person)
+
+ block = @user.blocks.create!(person: person)
+ contact = @user.contact_for(person)
+
+ if contact
+ @user.disconnect(contact)
+ elsif block.person.remote?
+ Diaspora::Federation::Dispatcher.defer_dispatch(@user, block)
+ end
+ end
+
+ def unblock(person)
+ remove_block(@user.blocks.find_by!(person: person))
+ end
+
+ def remove_block(block)
+ block.destroy
+ ContactRetraction.for(block).defer_dispatch(@user)
+ end
+end