2014-11-10 12:56:30 -08:00
|
|
|
class Ajax::QuestionController < ApplicationController
|
2015-04-18 15:42:58 -07:00
|
|
|
include MarkdownHelper
|
|
|
|
|
2015-05-08 16:31:31 -07:00
|
|
|
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
|
|
|
|
@status = :parameter_error
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
|
2015-05-08 16:31:31 -07:00
|
|
|
@success = false
|
|
|
|
render partial: "ajax/shared/status"
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:36:25 -07:00
|
|
|
def destroy
|
|
|
|
params.require :question
|
|
|
|
|
|
|
|
question = Question.find params[:question]
|
|
|
|
if question.nil?
|
|
|
|
@status = :not_found
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.destroy.not_found')
|
2015-04-25 18:36:25 -07:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:39:51 -07:00
|
|
|
if not (current_user.mod? or question.user == current_user)
|
|
|
|
@status = :not_authorized
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.destroy.not_authorized')
|
2015-04-25 18:39:51 -07:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:36:25 -07:00
|
|
|
question.destroy!
|
|
|
|
|
|
|
|
@status = :okay
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.destroy.okay')
|
2015-04-25 18:36:25 -07:00
|
|
|
@success = true
|
|
|
|
end
|
|
|
|
|
2014-11-10 12:56:30 -08:00
|
|
|
def create
|
|
|
|
params.require :question
|
|
|
|
params.require :anonymousQuestion
|
|
|
|
params.require :rcpt
|
|
|
|
|
2014-12-08 06:34:37 -08:00
|
|
|
begin
|
|
|
|
question = Question.create!(content: params[:question],
|
|
|
|
author_is_anonymous: params[:anonymousQuestion],
|
|
|
|
user: current_user)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
@status = :rec_inv
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.create.rec_inv')
|
2014-12-08 06:34:37 -08:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
|
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'
|
|
|
|
unless current_user.nil?
|
2015-07-26 23:59:56 -07:00
|
|
|
QuestionWorker.perform_async params[:rcpt], current_user.id, question.id
|
2014-12-07 11:13:45 -08:00
|
|
|
end
|
2015-01-13 06:45:32 -08:00
|
|
|
elsif params[:rcpt].start_with? 'grp:'
|
|
|
|
unless current_user.nil?
|
2015-07-27 00:12:12 -07:00
|
|
|
begin
|
2015-07-26 23:59:56 -07:00
|
|
|
current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '')
|
|
|
|
QuestionWorker.perform_async params[:rcpt], current_user.id, question.id
|
2015-01-13 06:45:32 -08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
@status = :not_found
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.create.not_found')
|
2015-01-13 06:45:32 -08:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2014-12-07 11:13:45 -08:00
|
|
|
else
|
2015-07-26 23:59:56 -07:00
|
|
|
if User.find(params[:rcpt]).nil?
|
|
|
|
@status = :not_found
|
|
|
|
@message = I18n.t('messages.question.create.not_found')
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-12-07 11:13:45 -08:00
|
|
|
Inbox.create!(user_id: params[:rcpt], question_id: question.id, new: true)
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
|
|
|
|
@status = :okay
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.create.okay')
|
2014-11-10 12:56:30 -08:00
|
|
|
@success = true
|
|
|
|
end
|
2015-04-18 15:17:13 -07:00
|
|
|
|
|
|
|
def preview
|
|
|
|
params.require :md
|
|
|
|
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.preview.fail')
|
2015-04-18 15:17:13 -07:00
|
|
|
begin
|
2015-06-05 17:27:15 -07:00
|
|
|
@markdown = markdown params[:md]
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.question.preview.okay')
|
2015-04-18 15:17:13 -07:00
|
|
|
rescue
|
|
|
|
@status = :fail
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@status = :okay
|
|
|
|
@success = true
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
end
|