2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

FEATURE: unread pms go in front of notification report

also refactor fat controller
This commit is contained in:
Sam 2014-02-13 17:12:17 +11:00
parent 4ff6780758
commit e8aa85d783
3 changed files with 72 additions and 11 deletions

View file

@ -8,7 +8,7 @@ class Notification < ActiveRecord::Base
validates_presence_of :notification_type
scope :unread, lambda { where(read: false) }
scope :recent, lambda { order('created_at desc').limit(10) }
scope :recent, lambda {|n=nil| n ||= 10; order('created_at desc').limit(n) }
after_save :refresh_notification_count
after_destroy :refresh_notification_count
@ -89,6 +89,33 @@ class Notification < ActiveRecord::Base
Post.where(topic_id: topic_id, post_number: post_number).first
end
def self.recent_report(user, count = nil)
notifications = user.notifications.recent(count).includes(:topic)
if notifications.present?
notifications += user.notifications
.order('created_at desc')
.where(read: false, notification_type: Notification.types[:private_message])
.where('id < ?', notifications.last.id)
.limit(count)
end
notifications.sort do |x,y|
if x.unread_pm? && !y.unread_pm?
-1
elsif y.unread_pm? && !x.unread_pm?
1
else
y.created_at <=> x.created_at
end
end.take(count)
end
def unread_pm?
Notification.types[:private_message] == self.notification_type && !read
end
protected