2022-03-26 18:42:49 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-09 22:35:11 +01:00
|
|
|
class NotificationsController < ApplicationController
|
2020-04-19 01:45:50 +02:00
|
|
|
before_action :authenticate_user!
|
2014-12-12 17:54:13 +01:00
|
|
|
|
2022-03-26 12:18:38 +01:00
|
|
|
TYPE_MAPPINGS = {
|
2022-07-21 16:30:04 +02:00
|
|
|
"answer" => Notification::QuestionAnswered.name,
|
|
|
|
"comment" => Notification::Commented.name,
|
|
|
|
"commentsmile" => Notification::CommentSmiled.name,
|
|
|
|
"relationship" => Notification::StartedFollowing.name,
|
2022-07-21 16:56:05 +02:00
|
|
|
"smile" => Notification::Smiled.name
|
2022-03-26 12:18:38 +01:00
|
|
|
}.freeze
|
|
|
|
|
2014-12-09 22:35:11 +01:00
|
|
|
def index
|
2022-03-26 12:18:38 +01:00
|
|
|
@type = TYPE_MAPPINGS[params[:type]] || params[:type]
|
2020-04-20 23:03:57 +02:00
|
|
|
@notifications = cursored_notifications_for(type: @type, last_id: params[:last_id])
|
2023-02-02 10:38:41 +01:00
|
|
|
paginate_notifications
|
|
|
|
@counters = count_unread_by_type
|
2023-06-16 18:19:31 +02:00
|
|
|
mark_notifications_as_read
|
2020-04-20 23:03:57 +02:00
|
|
|
|
2015-01-08 18:22:27 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2022-09-08 23:30:21 +02:00
|
|
|
format.turbo_stream { render layout: false, status: :see_other }
|
2015-01-08 18:22:27 +01:00
|
|
|
end
|
2014-12-09 22:35:11 +01:00
|
|
|
end
|
2020-04-20 23:03:57 +02:00
|
|
|
|
2023-03-07 19:53:57 +01:00
|
|
|
def read
|
2023-03-10 21:08:28 +01:00
|
|
|
current_user.notifications.where(new: true).update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
|
2023-05-08 18:55:25 +02:00
|
|
|
current_user.touch(:notifications_updated_at)
|
2023-03-07 19:53:57 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream do
|
|
|
|
render "navigation/notifications", locals: { notifications: [], notification_count: nil }
|
2023-03-10 21:08:28 +01:00
|
|
|
end
|
2023-03-07 19:53:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-20 23:03:57 +02:00
|
|
|
private
|
|
|
|
|
2023-02-02 10:38:41 +01:00
|
|
|
def paginate_notifications
|
|
|
|
@notifications_last_id = @notifications.map(&:id).min
|
|
|
|
@more_data_available = !cursored_notifications_for(type: @type, last_id: @notifications_last_id, size: 1).count.zero?
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_unread_by_type
|
|
|
|
Notification.where(recipient: current_user, new: true)
|
|
|
|
.group(:target_type)
|
|
|
|
.count(:target_type)
|
|
|
|
end
|
|
|
|
|
2023-05-07 20:54:54 +02:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2023-01-24 15:54:41 +01:00
|
|
|
def mark_notifications_as_read
|
|
|
|
# using .dup to not modify @notifications -- useful in tests
|
2023-06-16 18:19:31 +02:00
|
|
|
updated = @notifications&.dup&.update_all(new: false)
|
|
|
|
current_user.touch(:notifications_updated_at) if updated.positive?
|
2023-01-24 15:54:41 +01:00
|
|
|
end
|
2023-05-07 20:54:54 +02:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2023-01-24 15:54:41 +01:00
|
|
|
|
2020-04-20 23:03:57 +02:00
|
|
|
def cursored_notifications_for(type:, last_id:, size: nil)
|
|
|
|
cursor_params = { last_id: last_id, size: size }.compact
|
|
|
|
|
|
|
|
case type
|
2022-03-26 18:42:49 +01:00
|
|
|
when "all"
|
2020-04-20 23:03:57 +02:00
|
|
|
Notification.cursored_for(current_user, **cursor_params)
|
2022-03-26 18:42:49 +01:00
|
|
|
when "new"
|
2020-04-20 23:03:57 +02:00
|
|
|
Notification.cursored_for(current_user, new: true, **cursor_params)
|
|
|
|
else
|
|
|
|
Notification.cursored_for_type(current_user, type, **cursor_params)
|
|
|
|
end
|
|
|
|
end
|
2014-12-09 22:35:11 +01:00
|
|
|
end
|