From d000ddaae49ed37958e27fd51ae0e366d375b60d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Mon, 21 Nov 2022 22:29:47 +0100 Subject: [PATCH] Add `AnonymousBlockController` --- app/controllers/anonymous_block_controller.rb | 43 +++++++++++++++++++ app/policies/anonymous_block_policy.rb | 18 ++++++++ config/routes.rb | 2 + 3 files changed, 63 insertions(+) create mode 100644 app/controllers/anonymous_block_controller.rb create mode 100644 app/policies/anonymous_block_policy.rb diff --git a/app/controllers/anonymous_block_controller.rb b/app/controllers/anonymous_block_controller.rb new file mode 100644 index 00000000..80c64799 --- /dev/null +++ b/app/controllers/anonymous_block_controller.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +class AnonymousBlockController < ApplicationController + before_action :authenticate_user! + + def create + params.require :question + + question = Question.find(params[:question]) + + authorize AnonymousBlock, :create_global? if params[:global] + + AnonymousBlock.create!( + user: params[:global] ? nil : current_user, + identifier: question.author_identifier, + question: + ) + + inbox_id = question.inboxes.first.id + question.inboxes.first&.destroy + + respond_to do |format| + format.turbo_stream do + render turbo_stream: turbo_stream.remove("inbox_#{inbox_id}") + end + end + end + + def destroy + params.require :id + + block = AnonymousBlock.find(params[:id]) + authorize block + + block.destroy! + + respond_to do |format| + format.turbo_stream do + render turbo_stream: turbo_stream.remove("block_#{params[:id]}") + end + end + end +end diff --git a/app/policies/anonymous_block_policy.rb b/app/policies/anonymous_block_policy.rb new file mode 100644 index 00000000..b16d04bf --- /dev/null +++ b/app/policies/anonymous_block_policy.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class AnonymousBlockPolicy + attr_reader :user, :anonymous_block + + def initialize(user, anonymous_block) + @user = user + @anonymous_block = anonymous_block + end + + def create_global? + user.mod? + end + + def destroy? + user == anonymous_block.user || user.mod? + end +end diff --git a/config/routes.rb b/config/routes.rb index bee8dc2a..8a753e5b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -134,6 +134,8 @@ Rails.application.routes.draw do post "/unsubscribe", to: "subscription#unsubscribe", as: :unsubscribe_answer end + resource :anonymous_block, controller: :anonymous_block, only: %i[create destroy] + get "/discover", to: "discover#index", as: :discover match "/public", to: "timeline#public", via: [:get, :post], as: :public_timeline if APP_CONFIG.dig(:features, :public, :enabled) match "/list/:list_name", to: "timeline#list", via: [:get, :post], as: :list_timeline