scinote-web/app/controllers/user_notifications_controller.rb

48 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-10-03 20:40:15 +08:00
class UserNotificationsController < ApplicationController
prepend_before_action -> { request.env['devise.skip_trackable'] = true }, only: :unseen_counter
2016-10-03 20:40:15 +08:00
def index
page = (params[:page] || 1).to_i
notifications = load_notifications.page(page).per(Constants::INFINITE_SCROLL_LIMIT).without_count
2016-09-29 20:49:58 +08:00
render json: {
notifications: notification_serializer(notifications),
next_page: notifications.next_page
}
UserNotification.where(
notification_id: notifications.except(:select).where.not(type_of: 2).select(:id)
).seen_by_user(current_user)
end
def unseen_counter
render json: {
unseen: load_notifications.where('user_notifications.checked = ?', false).size
}
2016-09-29 20:49:58 +08:00
end
private
def load_notifications
current_user.notifications
.select(:id, :type_of, :title, :message, :created_at, 'user_notifications.checked')
.order(created_at: :desc)
2016-09-28 22:03:52 +08:00
end
2023-04-14 17:38:28 +08:00
def notification_serializer(notifications)
notifications.map do |notification|
{
id: notification.id,
type_of: notification.type_of,
title: notification.title,
message: notification.message,
created_at: I18n.l(notification.created_at, format: :full),
today: notification.created_at.today?,
checked: notification.checked
2023-04-14 17:38:28 +08:00
}
end
end
2016-09-28 22:03:52 +08:00
end