Retrospring/app/controllers/inbox_controller.rb

79 lines
2.8 KiB
Ruby
Raw Normal View History

2014-11-10 22:10:41 -08:00
class InboxController < ApplicationController
2020-04-18 16:45:50 -07:00
before_action :authenticate_user!
2014-11-10 22:10:41 -08:00
def show
@inbox = current_user.cursored_inbox(last_id: params[:last_id])
@inbox_last_id = @inbox.map(&:id).min
@more_data_available = !current_user.cursored_inbox(last_id: @inbox_last_id, size: 1).count.zero?
@inbox_count = current_user.inboxes.count
if params[:author].present?
begin
@author = true
2015-07-17 13:46:05 -07:00
@target_user = User.where('LOWER(screen_name) = ?', params[:author].downcase).first!
@inbox_author = @inbox.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
@inbox_author_count = current_user.inboxes
.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
.count
if @inbox_author.empty?
@empty = true
2022-04-19 13:31:08 -07:00
flash.now[:info] = t(".author.info", author: params[:author])
else
@inbox = @inbox_author
@inbox_count = @inbox_author_count
@inbox_last_id = @inbox.map(&:id).min
@more_data_available = !current_user.cursored_inbox(last_id: @inbox_last_id, size: 1)
.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
.count
.zero?
end
rescue => e
2021-12-28 09:32:03 -08:00
Sentry.capture_exception(e)
2022-04-19 13:31:08 -07:00
flash.now[:error] = t(".author.error", author: params[:author])
@not_found = true
end
end
if @empty or @not_found
@delete_id = "ib-delete-all"
elsif @author
@delete_id = "ib-delete-all-author"
else
@delete_id = "ib-delete-all"
end
@disabled = true if @inbox.empty?
2015-02-12 13:09:11 -08:00
respond_to do |format|
format.html
format.turbo_stream do
render "show", layout: false, status: :see_other
@inbox.update_all(new: false)
end
2015-02-12 13:09:11 -08:00
end
2014-11-10 22:10:41 -08:00
end
def create
question = Question.create!(content: QuestionGenerator.generate,
author_is_anonymous: true,
author_identifier: "justask",
user: current_user)
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.prepend("entries", partial: "inbox/entry", locals: { i: inbox })
inbox.update(new: false)
end
format.html { redirect_to inbox_path }
end
end
2014-11-10 22:10:41 -08:00
end