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