From f9a7e80789843e56fa952d51b3d399d62d73e607 Mon Sep 17 00:00:00 2001 From: lake54 Date: Sun, 22 Feb 2015 19:38:57 +0000 Subject: [PATCH] FEATURE: User preference to always include context --- app/assets/javascripts/discourse/models/user.js | 1 + .../discourse/templates/user/preferences.hbs | 1 + app/mailers/user_notifications.rb | 6 +++--- app/models/user.rb | 1 + app/serializers/user_serializer.rb | 1 + app/services/user_updater.rb | 1 + config/locales/client.en.yml | 1 + ...50221181500_add_email_force_context_to_users.rb | 5 +++++ spec/mailers/user_notifications_spec.rb | 14 +++++++++++++- 9 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20150221181500_add_email_force_context_to_users.rb diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index c650d1704e676..c8edd50ed76df 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -186,6 +186,7 @@ Discourse.User = Discourse.Model.extend({ 'email_digests', 'email_direct', 'email_always', + 'email_force_context', 'email_private_messages', 'dynamic_favicon', 'digest_after_days', diff --git a/app/assets/javascripts/discourse/templates/user/preferences.hbs b/app/assets/javascripts/discourse/templates/user/preferences.hbs index 884de10217233..d5daf46ffda37 100644 --- a/app/assets/javascripts/discourse/templates/user/preferences.hbs +++ b/app/assets/javascripts/discourse/templates/user/preferences.hbs @@ -182,6 +182,7 @@ {{preference-checkbox labelKey="user.email_private_messages" checked=email_private_messages}} {{preference-checkbox labelKey="user.email_direct" checked=email_direct}} {{preference-checkbox labelKey="user.mailing_list_mode" checked=mailing_list_mode}} + {{preference-checkbox labelKey="user.email_force_context" checked=email_force_context}} {{preference-checkbox labelKey="user.email_always" checked=email_always}}
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 5fede4157ef90..d7447e6d778c4 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -153,7 +153,7 @@ class UserNotificationRenderer < ActionView::Base include UserNotificationsHelper end - def self.get_context_posts(post, topic_user) + def self.get_context_posts(post, topic_user, force_context) context_posts = Post.where(topic_id: post.topic_id) .where("post_number < ?", post.post_number) @@ -162,7 +162,7 @@ def self.get_context_posts(post, topic_user) .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 && !force_context context_posts = context_posts.where("post_number > ?", topic_user.last_emailed_post_number) end @@ -230,7 +230,7 @@ def send_notification_email(opts) context = "" tu = TopicUser.get(post.topic_id, user) - context_posts = self.class.get_context_posts(post, tu) + context_posts = self.class.get_context_posts(post, tu, user.email_force_context) # make .present? cheaper context_posts = context_posts.to_a diff --git a/app/models/user.rb b/app/models/user.rb index 8308ad5efbacb..62be666084a35 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -911,6 +911,7 @@ def update_previous_visit(timestamp) # title :string(255) # uploaded_avatar_id :integer # email_always :boolean default(FALSE), not null +# email_force_context :boolean default(FALSE), not null # mailing_list_mode :boolean default(FALSE), not null # locale :string(10) # primary_group_id :integer diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 6cc710935f208..177333d2cc4fb 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -76,6 +76,7 @@ def self.untrusted_attributes(*attrs) :email_private_messages, :email_direct, :email_always, + :email_force_context, :digest_after_days, :mailing_list_mode, :auto_track_topics_after_msecs, diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb index 74a785c4b965a..5c290f9867dec 100644 --- a/app/services/user_updater.rb +++ b/app/services/user_updater.rb @@ -11,6 +11,7 @@ class UserUpdater :email_always, :email_direct, :email_private_messages, + :email_force_context, :external_links_in_new_tab, :enable_quoting, :dynamic_favicon, diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 453000fa4c6a1..8172fe0ff1073 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -457,6 +457,7 @@ en: email_direct: "Send me an email when someone quotes me, replies to my post, or mentions my @username" email_private_messages: "Send me an email when someone private messages me" email_always: "Do not suppress email notifications when I am active on the site" + email_force_context: "Always include previous posts in notifications" other_settings: "Other" categories_settings: "Categories" diff --git a/db/migrate/20150221181500_add_email_force_context_to_users.rb b/db/migrate/20150221181500_add_email_force_context_to_users.rb new file mode 100644 index 0000000000000..74a7ddab08f43 --- /dev/null +++ b/db/migrate/20150221181500_add_email_force_context_to_users.rb @@ -0,0 +1,5 @@ +class AddEmailForceContextToUsers < ActiveRecord::Migration + def change + add_column :users, :email_force_context, :boolean, default: false, null: false + end +end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 18921cfb97b41..15567fad60fc6 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -20,7 +20,17 @@ reply3.hidden = true reply3.save - expect(UserNotifications.get_context_posts(reply4, nil).count).to eq(1) + expect(UserNotifications.get_context_posts(reply4, nil, false).count).to eq(1) + end + + it "respects the user option to always include 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) + + expect(UserNotifications.get_context_posts(reply4, nil, true).count).to eq(4) end end @@ -94,6 +104,8 @@ SiteSetting.display_name_on_posts = true mail = UserNotifications.user_replied(response.user, post: response, notification: notification) + expect(response.user.email_force_context).to eql(false) + # from should include full user name expect(mail[:from].display_names).to eql(['John Doe'])