From 95076050f47a45945c9f1faeab755f69561d5643 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 29 Mar 2016 18:50:17 +1100 Subject: [PATCH] FEATURE: warn about mailing list mode if it is checked --- .../components/preference-checkbox.js.es6 | 14 +++++++++++++- .../discourse/controllers/preferences.js.es6 | 16 ++++++++++++++++ .../discourse/templates/user/preferences.hbs | 2 +- app/models/post.rb | 10 ++++++++++ app/serializers/user_serializer.rb | 8 +++++++- config/locales/client.en.yml | 4 ++++ 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/components/preference-checkbox.js.es6 b/app/assets/javascripts/discourse/components/preference-checkbox.js.es6 index 98dc57079..0ec786b53 100644 --- a/app/assets/javascripts/discourse/components/preference-checkbox.js.es6 +++ b/app/assets/javascripts/discourse/components/preference-checkbox.js.es6 @@ -3,5 +3,17 @@ export default Em.Component.extend({ label: function() { return I18n.t(this.get('labelKey')); - }.property('labelKey') + }.property('labelKey'), + + click() { + const warning = this.get('warning'); + + if (warning && !this.get('checked')) { + debugger; + this.sendAction('warning'); + return false; + } + + return true; + } }); diff --git a/app/assets/javascripts/discourse/controllers/preferences.js.es6 b/app/assets/javascripts/discourse/controllers/preferences.js.es6 index 005ff085a..b237017f2 100644 --- a/app/assets/javascripts/discourse/controllers/preferences.js.es6 +++ b/app/assets/javascripts/discourse/controllers/preferences.js.es6 @@ -106,6 +106,22 @@ export default Ember.Controller.extend(CanCheckEmails, { actions: { + checkMailingList(){ + Em.run.next(()=>{ + const postsPerDay = this.get('model.mailing_list_posts_per_day'); + if (!postsPerDay || postsPerDay < 2) { + this.set('model.user_option.mailing_list_mode', true); + return; + } + + bootbox.confirm(I18n.t("user.enable_mailing_list", {count: postsPerDay}), I18n.t("no_value"), I18n.t("yes_value"), (success) => { + if (success) { + this.set('model.user_option.mailing_list_mode', true); + } + }); + }); + }, + save() { this.set('saved', false); diff --git a/app/assets/javascripts/discourse/templates/user/preferences.hbs b/app/assets/javascripts/discourse/templates/user/preferences.hbs index 6591c9eb8..ea5e4b7e1 100644 --- a/app/assets/javascripts/discourse/templates/user/preferences.hbs +++ b/app/assets/javascripts/discourse/templates/user/preferences.hbs @@ -186,7 +186,7 @@ {{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}} {{#unless siteSettings.disable_mailing_list_mode}} - {{preference-checkbox labelKey="user.mailing_list_mode" checked=model.user_option.mailing_list_mode}} + {{preference-checkbox warning="checkMailingList" labelKey="user.mailing_list_mode" checked=model.user_option.mailing_list_mode}} {{/unless}} {{preference-checkbox labelKey="user.email_always" checked=model.user_option.email_always}} {{#unless model.user_option.email_always}} diff --git a/app/models/post.rb b/app/models/post.rb index 8a63e8d76..31a60e760 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -451,6 +451,16 @@ class Post < ActiveRecord::Base PostCreator.before_create_tasks(self) end + def self.estimate_posts_per_day + val = $redis.get("estimated_posts_per_day") + return val.to_i if val + + posts_per_day = Topic.listable_topics.secured.joins(:posts).merge(Post.created_since(30.days.ago)).count / 30 + $redis.setex("estimated_posts_per_day", 1.day.to_i, posts_per_day.to_s) + posts_per_day + + end + # This calculates the geometric mean of the post timings and stores it along with # each post. def self.calculate_avg_time(min_topic_age=nil) diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index b1acb42fe..143279ba8 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -95,7 +95,8 @@ class UserSerializer < BasicUserSerializer :has_title_badges, :card_image_badge, :card_image_badge_id, - :muted_usernames + :muted_usernames, + :mailing_list_posts_per_day untrusted_attributes :bio_raw, :bio_cooked, @@ -109,6 +110,11 @@ class UserSerializer < BasicUserSerializer ### ATTRIBUTES ### + def mailing_list_posts_per_day + val = Post.estimate_posts_per_day + [val,SiteSetting.max_emails_per_day_per_user].min + end + def groups if scope.is_admin? || object.id == scope.user.try(:id) object.groups diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 704e4e3fb..2fd92789a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -661,6 +661,10 @@ en: other_settings: "Other" categories_settings: "Categories" + enable_mailing_list: + one: "Are you sure you want to be emailed for every new post?" + other: "Are you sure you want to be emailed for every new post?

This will result in approximately {{count}} emails per day." + new_topic_duration: label: "Consider topics new when" not_viewed: "I haven't viewed them yet"