2016-09-28 20:18:52 +08:00
|
|
|
class UserNotification < ActiveRecord::Base
|
2016-10-05 16:52:27 +08:00
|
|
|
include NotificationsHelper
|
|
|
|
|
2016-09-28 20:18:52 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :notification
|
2016-09-28 22:03:52 +08:00
|
|
|
|
2016-10-05 16:52:27 +08:00
|
|
|
after_save :send_email
|
|
|
|
|
2016-10-13 16:00:36 +08:00
|
|
|
def self.last_notifications(
|
|
|
|
user,
|
|
|
|
last_notification_id = nil,
|
|
|
|
per_page = Constants::ACTIVITY_AND_NOTIF_SEARCH_LIMIT
|
|
|
|
)
|
2016-10-13 17:05:11 +08:00
|
|
|
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)
|
2016-10-04 17:51:47 +08:00
|
|
|
.where('notifications.id < ?', last_notification_id)
|
2016-10-03 20:40:15 +08:00
|
|
|
.order(created_at: :DESC)
|
2016-10-04 17:51:47 +08:00
|
|
|
.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)
|
2016-10-13 17:05:11 +08:00
|
|
|
.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
|
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
|
2016-10-07 15:45:30 +08:00
|
|
|
case notification.type_of
|
|
|
|
when 'system_message'
|
2016-10-12 17:50:26 +08:00
|
|
|
send_email_notification(
|
|
|
|
user,
|
|
|
|
notification
|
|
|
|
) if user.system_message_notification_email
|
2016-10-07 15:45:30 +08:00
|
|
|
when 'assignment'
|
|
|
|
send_email_notification(
|
|
|
|
user,
|
|
|
|
notification
|
|
|
|
) if user.assignments_notification_email
|
|
|
|
when 'recent_changes'
|
|
|
|
send_email_notification(
|
|
|
|
user,
|
|
|
|
notification
|
|
|
|
) if user.recent_notification_email
|
2016-10-05 16:52:27 +08:00
|
|
|
end
|
|
|
|
end
|
2016-09-28 20:18:52 +08:00
|
|
|
end
|