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-10-15 01:21:46 -07:00
|
|
|
def show
|
2023-01-19 07:21:21 -08:00
|
|
|
find_inbox_entries
|
2023-01-28 13:42:14 -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
|
|
|
|
2023-06-16 09:19:31 -07:00
|
|
|
mark_inbox_entries_as_read
|
2022-11-23 23:02:42 -08:00
|
|
|
|
2023-06-16 09:19:31 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.turbo_stream
|
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)
|
2023-02-13 11:13:32 -08:00
|
|
|
increment_metric
|
2022-11-17 12:55:01 -08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-01-14 12:13:30 -08:00
|
|
|
def filter_params
|
|
|
|
params.slice(*InboxFilter::KEYS).permit(*InboxFilter::KEYS)
|
2023-01-18 15:13:48 -08:00
|
|
|
end
|
|
|
|
|
2023-01-19 07:21:21 -08:00
|
|
|
def find_inbox_entries
|
2024-01-14 12:13:30 -08:00
|
|
|
filter = InboxFilter.new(current_user, filter_params)
|
|
|
|
@inbox = filter.cursored_results(last_id: params[:last_id])
|
2023-01-19 07:21:21 -08:00
|
|
|
@inbox_last_id = @inbox.map(&:id).min
|
2024-01-14 12:13:30 -08:00
|
|
|
@more_data_available = filter.cursored_results(last_id: @inbox_last_id, size: 1).count.positive?
|
|
|
|
@inbox_count = filter.results.count
|
2023-01-19 07:21:21 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_delete_id
|
2024-01-14 12:13:30 -08:00
|
|
|
return "ib-delete-all-author" if params[:author].present? && @inbox_count.positive?
|
2023-01-19 07:21:21 -08:00
|
|
|
|
|
|
|
"ib-delete-all"
|
|
|
|
end
|
|
|
|
|
2023-05-07 11:39:09 -07:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2023-01-27 11:31:38 -08:00
|
|
|
def mark_inbox_entries_as_read
|
|
|
|
# using .dup to not modify @inbox -- useful in tests
|
2023-06-16 09:19:31 -07:00
|
|
|
updated = @inbox&.dup&.update_all(new: false)
|
|
|
|
current_user.touch(:inbox_updated_at) if updated.positive?
|
2023-01-27 11:31:38 -08:00
|
|
|
end
|
2023-05-07 11:39:09 -07:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2023-02-13 11:13:32 -08:00
|
|
|
|
|
|
|
def increment_metric
|
|
|
|
Retrospring::Metrics::QUESTIONS_ASKED.increment(
|
|
|
|
labels: {
|
|
|
|
anonymous: true,
|
|
|
|
followers: false,
|
|
|
|
generated: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2014-11-10 22:10:41 -08:00
|
|
|
end
|