2023-01-18 15:13:48 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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-11 10:53:25 -08:00
|
|
|
|
2023-01-27 11:31:38 -08:00
|
|
|
after_action :mark_inbox_entries_as_read, only: %i[show]
|
|
|
|
|
2023-01-28 13:42:14 -08:00
|
|
|
def show # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
2023-01-19 07:21:21 -08:00
|
|
|
find_author
|
|
|
|
find_inbox_entries
|
2023-01-28 13:42:14 -08:00
|
|
|
|
|
|
|
if @author_user && @inbox_count.zero?
|
|
|
|
# rubocop disabled because of a false positive
|
|
|
|
flash[:info] = t(".author.info", author: @author) # rubocop:disable Rails/ActionControllerFlashBeforeRender
|
|
|
|
redirect_to inbox_path(last_id: params[:last_id])
|
|
|
|
return
|
|
|
|
end
|
2023-01-18 15:17:49 -08:00
|
|
|
|
2023-01-19 07:21:21 -08:00
|
|
|
@delete_id = find_delete_id
|
2015-07-17 13:31:10 -07:00
|
|
|
@disabled = true if @inbox.empty?
|
2023-01-28 12:42:25 -08:00
|
|
|
|
2015-02-12 13:09:11 -08:00
|
|
|
respond_to do |format|
|
2023-02-05 09:06:48 -08:00
|
|
|
format.html { render "show" }
|
2022-11-23 23:02:42 -08:00
|
|
|
format.turbo_stream do
|
2023-02-05 09:06:48 -08:00
|
|
|
render "show", layout: false, status: :see_other
|
2022-11-23 23:02:42 -08:00
|
|
|
|
2023-01-19 07:21:21 -08:00
|
|
|
# rubocop disabled as just flipping a flag doesn't need to have validations to be run
|
|
|
|
@inbox.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
|
2022-11-23 23:02:42 -08:00
|
|
|
end
|
2015-02-12 13:09:11 -08:00
|
|
|
end
|
2014-11-10 22:10:41 -08:00
|
|
|
end
|
2022-11-17 12:55:01 -08:00
|
|
|
|
|
|
|
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
|
2023-02-05 10:35:59 -08:00
|
|
|
render turbo_stream: turbo_stream.prepend("entries", partial: "inbox/entry", locals: { i: inbox })
|
2022-11-18 10:43:39 -08:00
|
|
|
|
|
|
|
inbox.update(new: false)
|
2022-11-17 12:55:01 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
format.html { redirect_to inbox_path }
|
|
|
|
end
|
|
|
|
end
|
2023-01-18 15:13:48 -08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_author
|
|
|
|
return if params[:author].blank?
|
|
|
|
|
|
|
|
@author = params[:author]
|
|
|
|
|
|
|
|
@author_user = User.where("LOWER(screen_name) = ?", @author.downcase).first
|
|
|
|
flash.now[:error] = t(".author.error", author: @author) unless @author_user
|
|
|
|
end
|
|
|
|
|
2023-01-19 07:21:21 -08:00
|
|
|
def find_inbox_entries
|
|
|
|
@inbox = current_user.cursored_inbox(last_id: params[:last_id]).then(&method(:filter_author_chain))
|
|
|
|
@inbox_last_id = @inbox.map(&:id).min
|
|
|
|
@more_data_available = current_user.cursored_inbox(last_id: @inbox_last_id, size: 1).then(&method(:filter_author_chain)).count.positive?
|
|
|
|
@inbox_count = current_user.inboxes.then(&method(:filter_author_chain)).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_delete_id
|
|
|
|
return "ib-delete-all-author" if @author_user && @inbox_count.positive?
|
|
|
|
|
|
|
|
"ib-delete-all"
|
|
|
|
end
|
|
|
|
|
2023-01-18 15:13:48 -08:00
|
|
|
def filter_author_chain(query)
|
|
|
|
return query unless @author_user
|
|
|
|
|
|
|
|
query
|
|
|
|
.joins(:question)
|
|
|
|
.where(questions: { user: @author_user, author_is_anonymous: false })
|
|
|
|
end
|
2023-01-27 11:31:38 -08:00
|
|
|
|
|
|
|
def mark_inbox_entries_as_read
|
|
|
|
# using .dup to not modify @inbox -- useful in tests
|
|
|
|
@inbox&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
end
|
2014-11-10 22:10:41 -08:00
|
|
|
end
|