2023-01-19 00:13:48 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-11 07:10:41 +01:00
|
|
|
class InboxController < ApplicationController
|
2020-04-19 01:45:50 +02:00
|
|
|
before_action :authenticate_user!
|
2014-11-11 19:53:25 +01:00
|
|
|
|
2023-10-15 10:21:46 +02:00
|
|
|
def show
|
2023-01-19 16:21:21 +01:00
|
|
|
find_inbox_entries
|
2023-01-28 22:42:14 +01:00
|
|
|
|
2023-01-19 16:21:21 +01:00
|
|
|
@delete_id = find_delete_id
|
2015-07-17 22:31:10 +02:00
|
|
|
@disabled = true if @inbox.empty?
|
2023-01-28 21:42:25 +01:00
|
|
|
|
2023-06-16 18:19:31 +02:00
|
|
|
mark_inbox_entries_as_read
|
2022-11-24 08:02:42 +01:00
|
|
|
|
2023-06-16 18:19:31 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.turbo_stream
|
2015-02-12 22:09:11 +01:00
|
|
|
end
|
2014-11-11 07:10:41 +01:00
|
|
|
end
|
2022-11-17 21:55:01 +01:00
|
|
|
|
|
|
|
def create
|
|
|
|
question = Question.create!(content: QuestionGenerator.generate,
|
|
|
|
author_is_anonymous: true,
|
|
|
|
author_identifier: "justask",
|
|
|
|
user: current_user)
|
|
|
|
|
2024-01-27 13:02:58 +01:00
|
|
|
inbox = InboxEntry.create!(user: current_user, question_id: question.id, new: true)
|
2023-02-13 20:13:32 +01:00
|
|
|
increment_metric
|
2022-11-17 21:55:01 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream do
|
2023-02-05 19:35:59 +01:00
|
|
|
render turbo_stream: turbo_stream.prepend("entries", partial: "inbox/entry", locals: { i: inbox })
|
2022-11-18 19:43:39 +01:00
|
|
|
|
|
|
|
inbox.update(new: false)
|
2022-11-17 21:55:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
format.html { redirect_to inbox_path }
|
|
|
|
end
|
|
|
|
end
|
2023-01-19 00:13:48 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-01-14 21:13:30 +01:00
|
|
|
def filter_params
|
|
|
|
params.slice(*InboxFilter::KEYS).permit(*InboxFilter::KEYS)
|
2023-01-19 00:13:48 +01:00
|
|
|
end
|
|
|
|
|
2023-01-19 16:21:21 +01:00
|
|
|
def find_inbox_entries
|
2024-01-14 21:13:30 +01:00
|
|
|
filter = InboxFilter.new(current_user, filter_params)
|
|
|
|
@inbox = filter.cursored_results(last_id: params[:last_id])
|
2023-01-19 16:21:21 +01:00
|
|
|
@inbox_last_id = @inbox.map(&:id).min
|
2024-01-14 21:13:30 +01:00
|
|
|
@more_data_available = filter.cursored_results(last_id: @inbox_last_id, size: 1).count.positive?
|
|
|
|
@inbox_count = filter.results.count
|
2023-01-19 16:21:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_delete_id
|
2024-01-14 21:13:30 +01:00
|
|
|
return "ib-delete-all-author" if params[:author].present? && @inbox_count.positive?
|
2023-01-19 16:21:21 +01:00
|
|
|
|
|
|
|
"ib-delete-all"
|
|
|
|
end
|
|
|
|
|
2023-05-07 20:39:09 +02:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2023-01-27 20:31:38 +01:00
|
|
|
def mark_inbox_entries_as_read
|
|
|
|
# using .dup to not modify @inbox -- useful in tests
|
2023-06-16 18:19:31 +02:00
|
|
|
updated = @inbox&.dup&.update_all(new: false)
|
|
|
|
current_user.touch(:inbox_updated_at) if updated.positive?
|
2023-01-27 20:31:38 +01:00
|
|
|
end
|
2023-05-07 20:39:09 +02:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2023-02-13 20:13:32 +01:00
|
|
|
|
|
|
|
def increment_metric
|
|
|
|
Retrospring::Metrics::QUESTIONS_ASKED.increment(
|
|
|
|
labels: {
|
|
|
|
anonymous: true,
|
|
|
|
followers: false,
|
|
|
|
generated: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2014-11-11 07:10:41 +01:00
|
|
|
end
|