2019-05-08 21:28:07 +08:00
|
|
|
# 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
|
|
|
|
2021-02-25 14:43:04 +08:00
|
|
|
after_create :send_email
|
2016-10-05 16:52:27 +08:00
|
|
|
|
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
|
2016-10-04 02:31:00 +08:00
|
|
|
|
|
|
|
def self.seen_by_user(user)
|
2016-10-05 22:19:59 +08:00
|
|
|
where(user: user).where(checked: false).update_all(checked: true)
|
2016-10-04 02:31:00 +08:00
|
|
|
end
|
2016-10-05 16:52:27 +08:00
|
|
|
|
|
|
|
def send_email
|
2021-02-25 14:43:04 +08:00
|
|
|
send_email_notification(user, notification) if user.enabled_notifications_for?(notification.type_of.to_sym, :email)
|
2016-10-05 16:52:27 +08:00
|
|
|
end
|
2016-09-28 20:18:52 +08:00
|
|
|
end
|