2022-11-21 13:29:47 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AnonymousBlockController < ApplicationController
|
2023-02-11 05:41:36 -08:00
|
|
|
include TurboStreamable
|
2022-11-21 13:29:47 -08:00
|
|
|
|
2023-02-11 05:41:36 -08:00
|
|
|
before_action :authenticate_user!
|
2023-02-10 21:19:31 -08:00
|
|
|
|
2023-02-11 05:41:36 -08:00
|
|
|
turbo_stream_actions :create, :destroy
|
2023-02-10 21:19:31 -08:00
|
|
|
|
2022-11-21 13:29:47 -08:00
|
|
|
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,
|
2022-12-26 17:29:29 -08:00
|
|
|
question:,
|
|
|
|
target_user: question.user
|
2022-11-21 13:29:47 -08:00
|
|
|
)
|
|
|
|
|
2023-02-10 21:18:51 -08:00
|
|
|
inbox_id = question.inboxes.first&.id
|
2022-11-21 13:29:47 -08:00
|
|
|
question.inboxes.first&.destroy
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream do
|
2023-02-10 21:19:31 -08:00
|
|
|
render turbo_stream: [
|
2023-02-10 21:18:51 -08:00
|
|
|
inbox_id ? turbo_stream.remove("inbox_#{inbox_id}") : nil,
|
2023-02-10 21:19:31 -08:00
|
|
|
render_toast(t(".success"))
|
|
|
|
].compact
|
2022-11-21 13:29:47 -08:00
|
|
|
end
|
2022-11-21 14:05:04 -08:00
|
|
|
|
|
|
|
format.html { redirect_back(fallback_location: inbox_path) }
|
2022-11-21 13:29:47 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require :id
|
|
|
|
|
|
|
|
block = AnonymousBlock.find(params[:id])
|
|
|
|
authorize block
|
|
|
|
|
|
|
|
block.destroy!
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream do
|
2023-02-10 21:19:31 -08:00
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.remove("block_#{params[:id]}"),
|
|
|
|
render_toast(t(".success"))
|
|
|
|
]
|
2022-11-21 13:29:47 -08:00
|
|
|
end
|
2022-11-21 14:05:04 -08:00
|
|
|
|
|
|
|
format.html { redirect_back(fallback_location: settings_blocks_path) }
|
2022-11-21 13:29:47 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|