diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index d3593ceb3..064695ef9 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -180,6 +180,7 @@ Discourse.User = Discourse.Model.extend({ 'digest_after_days', 'new_topic_duration_minutes', 'external_links_in_new_tab', + 'watch_new_topics', 'enable_quoting'), type: 'PUT' }).then(function(data) { diff --git a/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars b/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars index c3722ca54..a39c245a4 100644 --- a/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars +++ b/app/assets/javascripts/discourse/templates/user/preferences.js.handlebars @@ -109,6 +109,15 @@ {{combobox valueAttribute="value" content=considerNewTopicOptions value=new_topic_duration_minutes}} + {{#if Discourse.SiteSettings.enable_watch_new_topics}} +
+ +
+ {{/if}} +
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index f4eb30cdc..f6ebe3cec 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -113,12 +113,21 @@ class UserNotifications < ActionMailer::Base notification_type = opts[:notification_type] || Notification.types[@notification.notification_type].to_s 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 + + # make .present? cheaper + context_posts = context_posts.to_a + if context_posts.present? context << "---\n*#{I18n.t('user_notifications.previous_discussion')}*\n" context_posts.each do |cp| @@ -157,6 +166,8 @@ class UserNotifications < ActionMailer::Base email_opts[:from_alias] = username end + TopicUser.change(user.id, @post.topic_id, last_emailed_post_number: @post.post_number) + build_email(user.email, email_opts) end diff --git a/app/models/post_alert_observer.rb b/app/models/post_alert_observer.rb index 9ceb3f097..2440ec25d 100644 --- a/app/models/post_alert_observer.rb +++ b/app/models/post_alert_observer.rb @@ -123,17 +123,16 @@ class PostAlertObserver < ActiveRecord::Observer reply_to_user = post.reply_notification_target notify_users(reply_to_user, :replied, post) - # find all users watching - if post.post_number > 1 - exclude_user_ids = [] - exclude_user_ids << post.user_id - exclude_user_ids << reply_to_user.id if reply_to_user.present? - exclude_user_ids << extract_mentioned_users(post).map(&:id) - exclude_user_ids << extract_quoted_users(post).map(&:id) - exclude_user_ids.flatten! - TopicUser.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching]).includes(:user).each do |tu| + exclude_user_ids = [] + exclude_user_ids << post.user_id + exclude_user_ids << reply_to_user.id if reply_to_user.present? + exclude_user_ids << extract_mentioned_users(post).map(&:id) + exclude_user_ids << extract_quoted_users(post).map(&:id) + exclude_user_ids.flatten! + TopicUser + .where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching]) + .includes(:user).each do |tu| create_notification(tu.user, Notification.types[:posted], post) unless exclude_user_ids.include?(tu.user_id) end - end end end diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 61c158fa2..3e37fe732 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -20,7 +20,7 @@ class TopicUser < ActiveRecord::Base end def notification_reasons - @notification_reasons ||= Enum.new(:created_topic, :user_changed, :user_interacted, :created_post) + @notification_reasons ||= Enum.new(:created_topic, :user_changed, :user_interacted, :created_post, :auto_watch) end def auto_track(user_id, topic_id, reason) @@ -64,6 +64,24 @@ class TopicUser < ActiveRecord::Base TopicUser.where('topic_id = ? and user_id = ?', topic, user).first end + def auto_watch_new_topic(topic_id) + # Can not afford to slow down creation of topics when a pile of users are watching new topics, reverting to SQL for max perf here + sql = <