diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 06b93155e..895c80318 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -107,6 +107,22 @@ class UserNotifications < ActionMailer::Base include UserNotificationsHelper end + def self.get_context_posts(post, topic_user) + + context_posts = Post.where(topic_id: post.topic_id) + .where("post_number < ?", post.post_number) + .where(user_deleted: false) + .where(hidden: false) + .order('created_at desc') + .limit(SiteSetting.email_posts_context) + + if topic_user && topic_user.last_emailed_post_number + context_posts = context_posts.where("post_number > ?", topic_user.last_emailed_post_number) + end + + context_posts + end + def notification_email(user, opts) return unless @notification = opts[:notification] return unless @post = opts[:post] @@ -116,16 +132,7 @@ class UserNotifications < ActionMailer::Base context = "" tu = TopicUser.get(@post.topic_id, user) - - context_posts = Post.where(topic_id: @post.topic_id) - .where("post_number < ?", @post.post_number) - .where(user_deleted: false) - .order('created_at desc') - .limit(SiteSetting.email_posts_context) - - if tu && tu.last_emailed_post_number - context_posts = context_posts.where("post_number > ?", tu.last_emailed_post_number) - end + context_posts = self.class.get_context_posts(@post, tu) # make .present? cheaper context_posts = context_posts.to_a diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 410d866db..836ffd675 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -4,6 +4,26 @@ describe UserNotifications do let(:user) { Fabricate(:admin) } + describe "#get_context_posts" do + it "does not include hidden/deleted/user_deleted posts in context" do + post = create_post + reply1 = create_post(topic: post.topic) + reply2 = create_post(topic: post.topic) + reply3 = create_post(topic: post.topic) + reply4 = create_post(topic: post.topic) + + reply1.trash! + + reply2.user_deleted = true + reply2.save + + reply3.hidden = true + reply3.save + + UserNotifications.get_context_posts(reply4, nil).count.should == 1 + end + end + describe ".signup" do subject { UserNotifications.signup(user) }