2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Notification
|
|
|
|
# Notifications are scoped by user
|
2015-05-05 03:10:25 +02:00
|
|
|
class API::NotificationsController < API::ApiController
|
|
|
|
include NotifyWith::NotificationsApi
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2022-01-11 16:04:14 +01:00
|
|
|
# notifications can have anything attached, so we won't eager load the whole database
|
|
|
|
around_action :skip_bullet, if: -> { defined?(Bullet) }
|
|
|
|
|
2018-11-21 11:32:50 +01:00
|
|
|
# Number of notifications added to the page when the user clicks on 'load next notifications'
|
|
|
|
NOTIFICATIONS_PER_PAGE = 15
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
def index
|
2017-01-05 15:06:54 +01:00
|
|
|
loop do
|
2022-01-11 16:04:14 +01:00
|
|
|
@notifications = current_user.notifications.includes(:attached_object).page(params[:page]).per(NOTIFICATIONS_PER_PAGE).order('created_at DESC')
|
2017-01-05 15:06:54 +01:00
|
|
|
# we delete obsolete notifications on first access
|
|
|
|
break unless delete_obsoletes(@notifications)
|
|
|
|
end
|
|
|
|
@totals = {
|
2019-01-16 16:28:25 +01:00
|
|
|
total: current_user.notifications.count,
|
|
|
|
unread: current_user.notifications.where(is_read: false).count
|
2017-01-05 15:06:54 +01:00
|
|
|
}
|
|
|
|
render :index
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_unread
|
|
|
|
loop do
|
2022-01-11 16:04:14 +01:00
|
|
|
@notifications = current_user.notifications.includes(:attached_object).where(is_read: false).limit(3).order('created_at DESC')
|
2017-01-05 15:06:54 +01:00
|
|
|
# we delete obsolete notifications on first access
|
|
|
|
break unless delete_obsoletes(@notifications)
|
|
|
|
end
|
|
|
|
@totals = {
|
2019-01-16 16:28:25 +01:00
|
|
|
total: current_user.notifications.count,
|
|
|
|
unread: current_user.notifications.where(is_read: false).count
|
2017-01-05 15:06:54 +01:00
|
|
|
}
|
|
|
|
render :index
|
|
|
|
end
|
|
|
|
|
|
|
|
def polling
|
2019-01-16 16:28:25 +01:00
|
|
|
@notifications = current_user.notifications
|
|
|
|
.where('is_read = false AND created_at >= :date', date: params[:last_poll])
|
|
|
|
.order('created_at DESC')
|
2017-01-05 15:06:54 +01:00
|
|
|
@totals = {
|
2019-01-16 16:28:25 +01:00
|
|
|
total: current_user.notifications.count,
|
|
|
|
unread: current_user.notifications.where(is_read: false).count
|
2017-01-05 15:06:54 +01:00
|
|
|
}
|
|
|
|
render :index
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2018-11-21 11:32:50 +01:00
|
|
|
|
2017-01-05 15:06:54 +01:00
|
|
|
def delete_obsoletes(notifications)
|
|
|
|
cleaned = false
|
|
|
|
notifications.each do |n|
|
2019-01-16 16:28:25 +01:00
|
|
|
if !Module.const_get(n.attached_object_type) || !n.attached_object
|
2017-01-05 15:06:54 +01:00
|
|
|
n.destroy!
|
|
|
|
cleaned = true
|
2016-12-19 17:08:11 +01:00
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2017-01-05 15:06:54 +01:00
|
|
|
cleaned
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
end
|