scinote-web/app/controllers/user_notifications_controller.rb

43 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-10-03 20:40:15 +08:00
class UserNotificationsController < ApplicationController
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
}
2023-10-11 19:43:20 +08:00
notifications.mark_as_read!
end
def unseen_counter
render json: {
2023-10-11 19:43:20 +08:00
unseen: load_notifications.where(read_at: nil).size
}
2016-09-29 20:49:58 +08:00
end
private
def load_notifications
current_user.notifications
.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,
2023-10-11 19:43:20 +08:00
type_of: notification.type,
title: notification.to_notification.title,
message: notification.to_notification.message,
2023-04-14 17:38:28 +08:00
created_at: I18n.l(notification.created_at, format: :full),
today: notification.created_at.today?,
2023-10-11 19:43:20 +08:00
checked: notification.read_at.present?
2023-04-14 17:38:28 +08:00
}
end
end
2016-09-28 22:03:52 +08:00
end