FEATURE: allow users to control how many previous replies they get

- always means we always send previous replies with every email
- never means we do not
- "unless previously sent" ... is the default, in which we only email you each reply once

The default_email_previous_replies site setting can control this toggle
This commit is contained in:
Sam 2016-02-19 13:56:52 +11:00
parent 3de390c067
commit ab06f86fbe
13 changed files with 91 additions and 9 deletions

View file

@ -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 },

View file

@ -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',

View file

@ -176,6 +176,10 @@
</div>
{{/if}}
{{/if}}
<div class='controls controls-dropdown'>
<label>{{i18n 'user.email_previous_replies.title'}}</label>
{{combo-box valueAttribute="value" content=previousRepliesOptions value=model.user_option.email_previous_replies}}
</div>
{{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}}
<span class="pref-mailing-list-mode">{{preference-checkbox labelKey="user.mailing_list_mode" checked=model.user_option.mailing_list_mode}}</span>
@ -188,6 +192,9 @@
{{i18n 'user.email.frequency_immediately'}}
{{/if}}
</div>
</div>
<div class="control-group notifications">

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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?

View file

@ -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)

View file

@ -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"

View file

@ -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."

View file

@ -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'

View file

@ -0,0 +1,5 @@
class AddEmailPreviousRepliesToUserOptions < ActiveRecord::Migration
def change
add_column :user_options, :email_previous_replies, :integer, null: false, default: 1
end
end

View file

@ -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