diff --git a/app/assets/javascripts/discourse/controllers/preferences.js.es6 b/app/assets/javascripts/discourse/controllers/preferences.js.es6 index 1efdaff99..5231e4fa4 100644 --- a/app/assets/javascripts/discourse/controllers/preferences.js.es6 +++ b/app/assets/javascripts/discourse/controllers/preferences.js.es6 @@ -62,6 +62,12 @@ export default Ember.Controller.extend(CanCheckEmails, { return this.siteSettings.available_locales.split('|').map(s => ({ name: s, value: s })); }, + previousRepliesOptions: [ + {name: I18n.t('user.email_previous_replies.always'), value: 0}, + {name: I18n.t('user.email_previous_replies.unless_emailed'), value: 1}, + {name: I18n.t('user.email_previous_replies.never'), value: 2} + ], + digestFrequencies: [{ name: I18n.t('user.email_digests.daily'), value: 1 }, { name: I18n.t('user.email_digests.every_three_days'), value: 3 }, { name: I18n.t('user.email_digests.weekly'), value: 7 }, diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index c57574431..9eeb9f87b 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -159,6 +159,7 @@ const User = RestModel.extend({ 'email_digests', 'email_direct', 'email_private_messages', + 'email_previous_replies', 'dynamic_favicon', 'enable_quoting', 'disable_jump_reply', diff --git a/app/assets/javascripts/discourse/templates/user/preferences.hbs b/app/assets/javascripts/discourse/templates/user/preferences.hbs index 06bab9cbf..6e8c026a4 100644 --- a/app/assets/javascripts/discourse/templates/user/preferences.hbs +++ b/app/assets/javascripts/discourse/templates/user/preferences.hbs @@ -176,6 +176,10 @@ {{/if}} {{/if}} +
+ + {{combo-box valueAttribute="value" content=previousRepliesOptions value=model.user_option.email_previous_replies}} +
{{preference-checkbox labelKey="user.email_private_messages" checked=model.user_option.email_private_messages}} {{preference-checkbox labelKey="user.email_direct" checked=model.user_option.email_direct}} {{preference-checkbox labelKey="user.mailing_list_mode" checked=model.user_option.mailing_list_mode}} @@ -188,6 +192,9 @@ {{i18n 'user.email.frequency_immediately'}} {{/if}} + + +
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 52db55f8a..e8def9ecc 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -193,6 +193,11 @@ class UserNotifications < ActionMailer::Base end def self.get_context_posts(post, topic_user) + user_option = topic_user.try(:user).try(:user_option) + if user_option && (user_option.email_previous_replies == UserOption.previous_replies_type[:never]) + return [] + end + allowed_post_types = [Post.types[:regular]] allowed_post_types << Post.types[:whisper] if topic_user.try(:user).try(:staff?) @@ -204,7 +209,7 @@ class UserNotifications < ActionMailer::Base .order('created_at desc') .limit(SiteSetting.email_posts_context) - if topic_user && topic_user.last_emailed_post_number + if topic_user && topic_user.last_emailed_post_number && user_option.try(:email_previous_replies) == UserOption.previous_replies_type[:unless_emailed] context_posts = context_posts.where("post_number > ?", topic_user.last_emailed_post_number) end diff --git a/app/models/previous_replies_site_setting.rb b/app/models/previous_replies_site_setting.rb new file mode 100644 index 000000000..eac55ae52 --- /dev/null +++ b/app/models/previous_replies_site_setting.rb @@ -0,0 +1,22 @@ +require_dependency 'enum_site_setting' + +class PreviousRepliesSiteSetting < EnumSiteSetting + + def self.valid_value?(val) + val.to_i.to_s == val.to_s && + values.any? { |v| v[:value] == val.to_i } + end + + def self.values + @values ||= [ + { name: 'user.email_previous_replies.always', value: 0 }, + { name: 'user.email_previous_replies.unless_emailed', value: 1 }, + { name: 'user.email_previous_replies.never', value: 2 }, + ] + end + + def self.translate_names? + true + end + +end diff --git a/app/models/user_option.rb b/app/models/user_option.rb index 46d7e6730..e6384f061 100644 --- a/app/models/user_option.rb +++ b/app/models/user_option.rb @@ -5,12 +5,17 @@ class UserOption < ActiveRecord::Base after_save :update_tracked_topics + def self.previous_replies_type + @previous_replies_type ||= Enum.new(always: 0, unless_emailed: 1, never: 2) + end + def set_defaults self.email_always = SiteSetting.default_email_always self.mailing_list_mode = SiteSetting.default_email_mailing_list_mode self.email_direct = SiteSetting.default_email_direct self.automatically_unpin_topics = SiteSetting.default_topics_automatic_unpin self.email_private_messages = SiteSetting.default_email_private_messages + self.email_previous_replies = SiteSetting.default_email_previous_replies self.enable_quoting = SiteSetting.default_other_enable_quoting self.external_links_in_new_tab = SiteSetting.default_other_external_links_in_new_tab diff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rb index 440587a4d..bf43b41a3 100644 --- a/app/serializers/user_option_serializer.rb +++ b/app/serializers/user_option_serializer.rb @@ -13,7 +13,8 @@ class UserOptionSerializer < ApplicationSerializer :automatically_unpin_topics, :edit_history_public, :auto_track_topics_after_msecs, - :new_topic_duration_minutes + :new_topic_duration_minutes, + :email_previous_replies def include_edit_history_public? diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb index b3b8e9031..3b24efff0 100644 --- a/app/services/user_updater.rb +++ b/app/services/user_updater.rb @@ -20,7 +20,8 @@ class UserUpdater :automatically_unpin_topics, :digest_after_days, :new_topic_duration_minutes, - :auto_track_topics_after_msecs + :auto_track_topics_after_msecs, + :email_previous_replies ] def initialize(actor, user) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index cdb5b7bc8..c6bdcc24b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -627,6 +627,11 @@ en: website: "Web Site" email_settings: "Email" + email_previous_replies: + title: "Include previous replies" + unless_emailed: "unless previously sent" + always: "always" + never: "never" email_digests: title: "When I don't visit here, send an email digest of what's new:" daily: "daily" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 4fe2c9cc7..ad4fbd5c7 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1243,6 +1243,7 @@ en: default_email_direct: "Send an email when someone quotes/replies to/mentions or invites the user by default." default_email_mailing_list_mode: "Send an email for every new post by default." default_email_always: "Send an email notification even when the user is active by default." + default_email_previous_replies: "Include previous replies in emails by default." default_other_new_topic_duration_minutes: "Global default condition for which a topic is considered new." default_other_auto_track_topics_after_msecs: "Global default time before a topic is automatically tracked." diff --git a/config/site_settings.yml b/config/site_settings.yml index 6937a8de3..dbf80b3a3 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1064,6 +1064,9 @@ user_preferences: default_email_direct: true default_email_mailing_list_mode: false default_email_always: false + default_email_previous_replies: + enum: 'PreviousRepliesSiteSetting' + default: 1 default_other_new_topic_duration_minutes: enum: 'NewTopicDurationSiteSetting' diff --git a/db/migrate/20160225050320_add_email_previous_replies_to_user_options.rb b/db/migrate/20160225050320_add_email_previous_replies_to_user_options.rb new file mode 100644 index 000000000..5f9aec18a --- /dev/null +++ b/db/migrate/20160225050320_add_email_previous_replies_to_user_options.rb @@ -0,0 +1,5 @@ +class AddEmailPreviousRepliesToUserOptions < ActiveRecord::Migration + def change + add_column :user_options, :email_previous_replies, :integer, null: false, default: 1 + end +end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 947dd9a46..a58c2d16f 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -7,12 +7,12 @@ describe UserNotifications do describe "#get_context_posts" do it "does not include hidden/deleted/user_deleted posts in context" do post1 = create_post - post2 = Fabricate(:post, topic: post1.topic, deleted_at: 1.day.ago) - post3 = Fabricate(:post, topic: post1.topic, user_deleted: true) - post4 = Fabricate(:post, topic: post1.topic, hidden: true) - post5 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:moderator_action]) - post6 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:small_action]) - post7 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:whisper]) + _post2 = Fabricate(:post, topic: post1.topic, deleted_at: 1.day.ago) + _post3 = Fabricate(:post, topic: post1.topic, user_deleted: true) + _post4 = Fabricate(:post, topic: post1.topic, hidden: true) + _post5 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:moderator_action]) + _post6 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:small_action]) + _post7 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:whisper]) last = Fabricate(:post, topic: post1.topic) # default is only post #1 @@ -21,6 +21,26 @@ describe UserNotifications do tu = TopicUser.new(topic: post1.topic, user: build(:moderator)) expect(UserNotifications.get_context_posts(last, tu).count).to eq(2) end + + it "allows users to control context" do + post1 = create_post + _post2 = Fabricate(:post, topic: post1.topic) + post3 = Fabricate(:post, topic: post1.topic) + + user = Fabricate(:user) + TopicUser.change(user.id, post1.topic_id, last_emailed_post_number: 1) + topic_user = TopicUser.find_by(user_id: user.id, topic_id: post1.topic_id) + # to avoid reloads after update_columns + user = topic_user.user + expect(UserNotifications.get_context_posts(post3, topic_user).count).to eq(1) + + user.user_option.update_columns(email_previous_replies: UserOption.previous_replies_type[:never]) + expect(UserNotifications.get_context_posts(post3, topic_user).count).to eq(0) + + user.user_option.update_columns(email_previous_replies: UserOption.previous_replies_type[:always]) + expect(UserNotifications.get_context_posts(post3, topic_user).count).to eq(2) + + end end describe ".signup" do