mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: collapse PM notifications
This commit is contained in:
parent
a7730f4b52
commit
b85e5dc191
@ -60,8 +60,12 @@ class PostAlertObserver < ActiveRecord::Observer
|
|||||||
def after_create_post(post)
|
def after_create_post(post)
|
||||||
if post.topic.private_message?
|
if post.topic.private_message?
|
||||||
# If it's a private message, notify the topic_allowed_users
|
# 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|
|
post.topic.all_allowed_users.reject{ |user| user.id == post.user_id }.each do |user|
|
||||||
create_notification(a, Notification.types[:private_message], post)
|
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
|
end
|
||||||
elsif post.post_type != Post.types[:moderator_action]
|
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
|
# 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(/.+\//, '')}"
|
"#{action}_#{model.class.name.underscore.gsub(/.+\//, '')}"
|
||||||
end
|
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={})
|
def create_notification(user, type, post, opts={})
|
||||||
return if user.blank?
|
return if user.blank?
|
||||||
|
|
||||||
|
@ -129,13 +129,17 @@ describe Notification do
|
|||||||
@target = @post.topic.topic_allowed_users.reject{|a| a.user_id == @post.user_id}[0].user
|
@target = @post.topic.topic_allowed_users.reject{|a| a.user_id == @post.user_id}[0].user
|
||||||
end
|
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]
|
@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
|
end
|
||||||
|
|
||||||
it 'should not add a pm notification for the creator' do
|
|
||||||
@post.user.unread_notifications.should == 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.post' do
|
describe '.post' do
|
||||||
@ -165,7 +169,7 @@ describe Notification do
|
|||||||
it 'correctly updates the read state' do
|
it 'correctly updates the read state' do
|
||||||
user = Fabricate(:user)
|
user = Fabricate(:user)
|
||||||
|
|
||||||
pm = Notification.create!(read: false,
|
Notification.create!(read: false,
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
topic_id: 2,
|
topic_id: 2,
|
||||||
post_number: 1,
|
post_number: 1,
|
||||||
@ -192,7 +196,7 @@ describe Notification do
|
|||||||
it "marks multiple posts as read if needed" do
|
it "marks multiple posts as read if needed" do
|
||||||
user = Fabricate(:user)
|
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)
|
Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '[]', notification_type: 1)
|
||||||
end
|
end
|
||||||
Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '[]', notification_type: 1)
|
Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '[]', notification_type: 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user