2022-06-12 04:46:48 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-14 07:11:00 -07:00
|
|
|
require "digest"
|
2022-06-12 04:46:48 -07:00
|
|
|
require "errors"
|
2022-04-18 12:57:54 -07:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::QuestionController < AjaxController
|
2015-04-25 18:36:25 -07:00
|
|
|
def destroy
|
|
|
|
params.require :question
|
|
|
|
|
|
|
|
question = Question.find params[:question]
|
|
|
|
if question.nil?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :not_found
|
|
|
|
@response[:message] = I18n.t('messages.question.destroy.not_found')
|
2015-04-25 18:36:25 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:39:51 -07:00
|
|
|
if not (current_user.mod? or question.user == current_user)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :not_authorized
|
|
|
|
@response[:message] = I18n.t('messages.question.destroy.not_authorized')
|
2015-04-25 18:39:51 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:36:25 -07:00
|
|
|
question.destroy!
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.question.destroy.okay')
|
|
|
|
@response[:success] = true
|
2015-04-25 18:36:25 -07:00
|
|
|
end
|
|
|
|
|
2014-11-10 12:56:30 -08:00
|
|
|
def create
|
|
|
|
params.require :question
|
|
|
|
params.require :anonymousQuestion
|
|
|
|
params.require :rcpt
|
|
|
|
|
2020-05-25 09:33:09 -07:00
|
|
|
is_never_anonymous = user_signed_in? && params[:rcpt] == 'followers'
|
2022-06-14 13:51:31 -07:00
|
|
|
author_is_anonymous = is_never_anonymous ? false : params[:anonymousQuestion]
|
2020-04-30 13:49:15 -07:00
|
|
|
|
2014-12-08 06:34:37 -08:00
|
|
|
begin
|
2022-06-14 07:11:00 -07:00
|
|
|
question = Question.create!(content: params[:question],
|
2022-06-14 13:51:31 -07:00
|
|
|
author_is_anonymous: author_is_anonymous,
|
|
|
|
author_identifier: AnonymousBlock.get_identifier(request.ip),
|
2022-06-14 07:11:00 -07:00
|
|
|
user: current_user,
|
|
|
|
direct: params[:rcpt] != 'followers')
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :rec_inv
|
|
|
|
@response[:message] = I18n.t('messages.question.create.rec_inv')
|
2014-12-08 06:34:37 -08:00
|
|
|
return
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
|
2020-04-30 13:49:15 -07:00
|
|
|
if !user_signed_in? && !question.author_is_anonymous
|
|
|
|
question.delete
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-11-10 22:18:34 -08:00
|
|
|
unless current_user.nil?
|
|
|
|
current_user.increment! :asked_count unless params[:anonymousQuestion] == 'true'
|
|
|
|
end
|
|
|
|
|
2014-12-07 11:13:45 -08:00
|
|
|
if params[:rcpt] == 'followers'
|
2020-05-25 09:33:09 -07:00
|
|
|
QuestionWorker.perform_async(current_user.id, question.id) unless current_user.nil?
|
2014-12-07 11:13:45 -08:00
|
|
|
else
|
2022-04-18 12:57:54 -07:00
|
|
|
target_user = User.find_by(id: params[:rcpt])
|
|
|
|
|
|
|
|
if target_user.nil?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :not_found
|
|
|
|
@response[:message] = I18n.t('messages.question.create.not_found')
|
2020-04-30 13:49:15 -07:00
|
|
|
question.delete
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-06-12 12:19:37 -07:00
|
|
|
if target_user.blocking?(current_user)
|
|
|
|
question.delete
|
|
|
|
raise Errors::AskingOtherBlockedSelf
|
|
|
|
end
|
|
|
|
if current_user&.blocking?(target_user)
|
|
|
|
question.delete
|
|
|
|
raise Errors::AskingSelfBlockedOther
|
|
|
|
end
|
2022-06-12 00:28:40 -07:00
|
|
|
|
2022-04-18 12:57:54 -07:00
|
|
|
if !target_user.privacy_allow_anonymous_questions && question.author_is_anonymous
|
2020-04-30 13:49:15 -07:00
|
|
|
question.delete
|
2015-07-26 23:59:56 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-06-14 13:51:31 -07:00
|
|
|
unless MuteRule.where(user: target_user).any? { |rule| rule.applies_to? question } ||
|
|
|
|
(author_is_anonymous && AnonymousBlock.where(identifier: AnonymousBlock.get_identifier(request.ip)).any?)
|
2022-04-18 12:57:54 -07:00
|
|
|
Inbox.create!(user_id: target_user.id, question_id: question.id, new: true)
|
2021-12-22 10:45:08 -08:00
|
|
|
end
|
2014-12-07 11:13:45 -08:00
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.question.create.okay')
|
|
|
|
@response[:success] = true
|
2015-04-18 15:17:13 -07:00
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
end
|