2014-11-11 07:19:20 -08:00
|
|
|
class Ajax::InboxController < ApplicationController
|
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
|
|
|
|
|
2014-12-09 14:21:41 -08:00
|
|
|
def create
|
|
|
|
unless user_signed_in?
|
|
|
|
@status = :noauth
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.noauth')
|
2014-12-09 14:21:41 -08:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
question = Question.create!(content: QuestionGenerator.generate,
|
|
|
|
author_is_anonymous: true,
|
|
|
|
author_name: 'justask',
|
|
|
|
user: current_user)
|
|
|
|
|
|
|
|
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
|
|
|
|
|
|
|
|
@status = :okay
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.inbox.create.okay')
|
2014-12-09 14:21:41 -08:00
|
|
|
@success = true
|
|
|
|
@render = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
|
|
|
|
inbox.update(new: false)
|
|
|
|
end
|
|
|
|
|
2014-11-30 09:05:51 -08:00
|
|
|
def remove
|
|
|
|
params.require :id
|
|
|
|
|
|
|
|
inbox = Inbox.find(params[:id])
|
|
|
|
|
|
|
|
unless current_user == inbox.user
|
|
|
|
@status = :fail
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.inbox.remove.fail')
|
2014-11-30 09:05:51 -08:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
inbox.remove
|
|
|
|
rescue
|
|
|
|
@status = :err
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.error')
|
2014-11-30 09:05:51 -08:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@status = :okay
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.inbox.remove.okay')
|
2014-11-30 09:05:51 -08:00
|
|
|
@success = true
|
|
|
|
end
|
2014-12-21 04:41:57 -08:00
|
|
|
|
|
|
|
def remove_all
|
|
|
|
begin
|
|
|
|
Inbox.where(user: current_user).each { |i| i.remove }
|
|
|
|
rescue
|
|
|
|
@status = :err
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.error')
|
2014-12-21 04:41:57 -08:00
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@status = :okay
|
2015-06-07 09:24:01 -07:00
|
|
|
@message = I18n.t('messages.inbox.remove_all.okay')
|
2014-12-21 04:41:57 -08:00
|
|
|
@success = true
|
|
|
|
render 'ajax/inbox/remove'
|
|
|
|
end
|
2015-07-17 11:29:19 -07:00
|
|
|
|
|
|
|
def remove_all_author
|
|
|
|
begin
|
2015-07-17 13:46:05 -07:00
|
|
|
@target_user = User.where('LOWER(screen_name) = ?', params[:author].downcase).first!
|
2015-07-17 11:29:19 -07:00
|
|
|
@inbox = current_user.inboxes.joins(:question)
|
2015-07-17 13:46:05 -07:00
|
|
|
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
|
2015-07-17 11:29:19 -07:00
|
|
|
@inbox.each { |i| i.remove }
|
|
|
|
rescue
|
|
|
|
@status = :err
|
|
|
|
@message = I18n.t('messages.error')
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@status = :okay
|
|
|
|
@message = I18n.t('messages.inbox.remove_all.okay')
|
|
|
|
@success = true
|
|
|
|
render 'ajax/inbox/remove'
|
|
|
|
end
|
2014-11-11 07:19:20 -08:00
|
|
|
end
|