Skip to content

Commit

Permalink
FEATURE: collapse PM notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Jan 20, 2014
1 parent a7730f4 commit b85e5dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
24 changes: 22 additions & 2 deletions app/models/post_alert_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ def after_create_post_revision(post_revision)
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
Expand All @@ -75,6 +79,22 @@ def callback_for(action, model)
"#{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?

Expand Down
16 changes: 10 additions & 6 deletions spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@
@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]
end

it 'should not add a pm notification for the creator' do
@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

end

describe '.post' do
Expand Down Expand Up @@ -165,7 +169,7 @@
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,
Expand All @@ -192,7 +196,7 @@
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)
Expand Down

0 comments on commit b85e5dc

Please sign in to comment.