scinote-web/app/models/user_notification.rb

64 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-06-23 21:19:08 +08:00
class UserNotification < ApplicationRecord
2016-10-05 16:52:27 +08:00
include NotificationsHelper
2017-06-28 21:21:32 +08:00
belongs_to :user, optional: true
belongs_to :notification, optional: true
2016-09-28 22:03:52 +08:00
2016-10-05 16:52:27 +08:00
after_save :send_email
def self.last_notifications(
user,
last_notification_id = nil,
per_page = Constants::ACTIVITY_AND_NOTIF_SEARCH_LIMIT
)
last_notification_id = Constants::INFINITY if last_notification_id < 1
2016-10-03 20:40:15 +08:00
Notification.joins(:user_notifications)
.where('user_notifications.user_id = ?', user.id)
.where('notifications.id < ?', last_notification_id)
2016-10-03 20:40:15 +08:00
.order(created_at: :DESC)
.limit(per_page)
2016-10-03 20:40:15 +08:00
end
2016-09-29 20:49:58 +08:00
def self.recent_notifications(user)
Notification.joins(:user_notifications)
.where('user_notifications.user_id = ?', user.id)
.order(created_at: :DESC)
.limit(Constants::ACTIVITY_AND_NOTIF_SEARCH_LIMIT)
2016-09-28 22:03:52 +08:00
end
2016-10-03 14:20:23 +08:00
def self.unseen_notification_count(user)
2016-09-29 20:49:58 +08:00
where('user_id = ? AND checked = false', user.id).count
2016-09-28 22:03:52 +08:00
end
def self.seen_by_user(user)
where(user: user).where(checked: false).update_all(checked: true)
end
2016-10-05 16:52:27 +08:00
def send_email
case notification.type_of
when 'system_message'
send_email_notification(
user,
notification
) if user.system_message_email_notification
when 'assignment'
send_email_notification(
user,
notification
) if user.assignments_email_notification
when 'recent_changes'
send_email_notification(
user,
notification
) if user.recent_email_notification
2017-03-23 22:45:02 +08:00
when 'deliver'
send_email_notification(
user,
notification
)
2016-10-05 16:52:27 +08:00
end
end
end