FEATURE: collapse PM notifications

This commit is contained in:
Sam 2014-01-20 16:18:43 +11:00
parent a7730f4b52
commit b85e5dc191
2 changed files with 32 additions and 8 deletions

View file

@ -60,8 +60,12 @@ class PostAlertObserver < ActiveRecord::Observer
def after_create_post(post)
if post.topic.private_message?
# If it's a private message, notify the topic_allowed_users
post.topic.all_allowed_users.reject{ |a| a.id == post.user_id }.each do |a|
create_notification(a, Notification.types[:private_message], post)
post.topic.all_allowed_users.reject{ |user| user.id == post.user_id }.each do |user|
next if user.blank?
destroy_notifications(user, Notification.types[:private_message], post.topic)
unread_post = first_unread_post(user,post.topic)
create_notification(user, Notification.types[:private_message], unread_post)
end
elsif post.post_type != Post.types[:moderator_action]
# If it's not a private message and it's not an automatic post caused by a moderator action, notify the users
@ -75,6 +79,22 @@ class PostAlertObserver < ActiveRecord::Observer
"#{action}_#{model.class.name.underscore.gsub(/.+\//, '')}"
end
def first_unread_post(user, topic)
Post.where('post_number > COALESCE((
SELECT last_read_post_number FROM topic_users tu
WHERE tu.user_id = ? AND tu.topic_id = ? ),0)',
user.id, topic.id)
.order('post_number').first
end
def destroy_notifications(user, type, topic)
return if user.blank?
return unless Guardian.new(user).can_see?(topic)
user.notifications.where(notification_type: type,
topic_id: topic.id).destroy_all
end
def create_notification(user, type, post, opts={})
return if user.blank?

View file

@ -129,13 +129,17 @@ describe Notification do
@target = @post.topic.topic_allowed_users.reject{|a| a.user_id == @post.user_id}[0].user
end
it 'should create a private message notification' do
it 'should create and rollup private message notifications' do
@target.notifications.first.notification_type.should == Notification.types[:private_message]
@post.user.unread_notifications.should == 0
@target.unread_private_messages.should == 1
Fabricate(:post, topic: @topic, user: @topic.user)
@target.reload
@target.unread_private_messages.should == 1
end
it 'should not add a pm notification for the creator' do
@post.user.unread_notifications.should == 0
end
end
describe '.post' do
@ -165,7 +169,7 @@ describe Notification do
it 'correctly updates the read state' do
user = Fabricate(:user)
pm = Notification.create!(read: false,
Notification.create!(read: false,
user_id: user.id,
topic_id: 2,
post_number: 1,
@ -192,7 +196,7 @@ describe Notification do
it "marks multiple posts as read if needed" do
user = Fabricate(:user)
notifications = (1..3).map do |i|
(1..3).map do |i|
Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '[]', notification_type: 1)
end
Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '[]', notification_type: 1)