2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::InboxController < AjaxController
|
2014-12-09 14:21:41 -08:00
|
|
|
def create
|
|
|
|
unless user_signed_in?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :noauth
|
|
|
|
@response[:message] = I18n.t('messages.noauth')
|
2014-12-09 14:21:41 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-07-06 10:46:30 -07:00
|
|
|
question = Question.create!(content: QuestionGenerator.generate,
|
2014-12-09 14:21:41 -08:00
|
|
|
author_is_anonymous: true,
|
2022-07-06 10:46:30 -07:00
|
|
|
author_identifier: "justask",
|
|
|
|
user: current_user)
|
2014-12-09 14:21:41 -08:00
|
|
|
|
|
|
|
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.inbox.create.okay')
|
|
|
|
@response[:success] = true
|
|
|
|
@response[:render] = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
|
2014-12-09 14:21:41 -08:00
|
|
|
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
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :fail
|
|
|
|
@response[:message] = I18n.t('messages.inbox.remove.fail')
|
2014-11-30 09:05:51 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
inbox.remove
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
|
|
|
@response[:message] = I18n.t('messages.error')
|
2014-11-30 09:05:51 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.inbox.remove.okay')
|
|
|
|
@response[:success] = true
|
2014-11-30 09:05:51 -08:00
|
|
|
end
|
2014-12-21 04:41:57 -08:00
|
|
|
|
|
|
|
def remove_all
|
2020-04-30 10:57:39 -07:00
|
|
|
raise unless user_signed_in?
|
|
|
|
|
2014-12-21 04:41:57 -08:00
|
|
|
begin
|
|
|
|
Inbox.where(user: current_user).each { |i| i.remove }
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
|
|
|
@response[:message] = I18n.t('messages.error')
|
2014-12-21 04:41:57 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.inbox.remove_all.okay')
|
|
|
|
@response[:success] = true
|
2014-12-21 04:41:57 -08:00
|
|
|
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 }
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
|
|
|
@response[:message] = I18n.t('messages.error')
|
2015-07-17 11:29:19 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.inbox.remove_all.okay')
|
|
|
|
@response[:success] = true
|
2015-07-17 11:29:19 -07:00
|
|
|
end
|
2014-11-11 07:19:20 -08:00
|
|
|
end
|