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