Clean up question create use case

This commit is contained in:
Karina Kwiatek 2022-12-26 09:59:48 +00:00
parent 2d6f539dfd
commit ee9c48fd06

View file

@ -11,27 +11,16 @@ module UseCase
option :direct, type: Types::Params::Bool, default: proc { true } option :direct, type: Types::Params::Bool, default: proc { true }
def call def call
check_user do_checks!
check_lock
check_anonymous_rules
check_blocks
question = ::Question.create!( question = create_question
content: content,
author_is_anonymous: anonymous,
author_identifier: author_identifier,
user: source_user_id.nil? ? nil : source_user,
direct: direct
)
return if filtered?(question) return if filtered?(question)
increment_asked_count increment_asked_count
inbox = ::Inbox.create!(user: target_user, question: question, new: true) inbox = ::Inbox.create!(user: target_user, question:, new: true)
notify
webpush_app = ::Rpush::App.find_by(name: "webpush")
target_user.push_notification(webpush_app, inbox) if webpush_app
{ {
status: 201, status: 201,
@ -44,6 +33,13 @@ module UseCase
private private
def do_checks!
check_user
check_lock
check_anonymous_rules
check_blocks
end
def check_lock def check_lock
raise Errors::InboxLocked if target_user.inbox_locked? raise Errors::InboxLocked if target_user.inbox_locked?
end end
@ -69,6 +65,21 @@ module UseCase
raise Errors::NotAuthorized if target_user.privacy_require_user && !source_user_id raise Errors::NotAuthorized if target_user.privacy_require_user && !source_user_id
end end
def create_question
::Question.create!(
content:,
author_is_anonymous: anonymous,
author_identifier:,
user: source_user_id.nil? ? nil : source_user,
direct:
)
end
def notify
webpush_app = ::Rpush::App.find_by(name: "webpush")
target_user.push_notification(webpush_app, inbox) if webpush_app
end
def increment_asked_count def increment_asked_count
unless source_user_id && !anonymous && !direct unless source_user_id && !anonymous && !direct
# Only increment the asked count of the source user if the question # Only increment the asked count of the source user if the question