From baff59d752ffcb4ab874e792ccb0ec8ae7e97105 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 16 Aug 2013 17:28:36 -0400 Subject: [PATCH] Send email to contact_email when a new version of Discourse is found. Disable with the new_version_emails setting. --- app/mailers/version_mailer.rb | 14 ++++++++++++++ app/models/site_setting.rb | 1 + config/locales/server.en.yml | 12 ++++++++++++ lib/jobs/version_check.rb | 7 +++++++ spec/mailers/version_mailer_spec.rb | 18 ++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 app/mailers/version_mailer.rb create mode 100644 spec/mailers/version_mailer_spec.rb diff --git a/app/mailers/version_mailer.rb b/app/mailers/version_mailer.rb new file mode 100644 index 00000000000..42019f0ad07 --- /dev/null +++ b/app/mailers/version_mailer.rb @@ -0,0 +1,14 @@ +require_dependency 'email/message_builder' + +class VersionMailer < ActionMailer::Base + include Email::BuildEmailHelper + + def send_notice + if SiteSetting.contact_email.present? + build_email( SiteSetting.contact_email, + template: 'new_version_mailer', + new_version: DiscourseUpdates.latest_version, + installed_version: Discourse::VERSION::STRING ) + end + end +end diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index b2d2acee20d..9803d4fd6f4 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -45,6 +45,7 @@ class SiteSetting < ActiveRecord::Base client_setting(:email_domains_blacklist, 'mailinator.com') client_setting(:email_domains_whitelist) client_setting(:version_checks, true) + setting(:new_version_emails, true) client_setting(:min_title_similar_length, 10) client_setting(:min_body_similar_length, 15) # cf. https://github.com/discourse/discourse/pull/462#issuecomment-14991562 diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index dd55cddd1c3..43b2fc42c16 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -529,6 +529,7 @@ en: email_domains_blacklist: "A pipe-delimited list of email domains that are not allowed. Example: mailinator.com|trashmail.net" email_domains_whitelist: "A pipe-delimited list of email domains that users may register with. WARNING: Users with email domains other than those listed will not be allowed." version_checks: "Ping the Discourse Hub for version updates and show version messages on the /admin dashboard" + new_version_emails: "Send an email to the contact_email address when a new version is available." port: "DEVELOPER ONLY! WARNING! Use this HTTP port rather than the default of port 80. Leave blank for default of 80." force_hostname: "DEVELOPER ONLY! WARNING! Specify a hostname in the URL. Leave blank for default." @@ -794,6 +795,17 @@ en: There should be an unsubscribe footer on every email you send, so let's mock one up. This email was sent by Name of Company, 55 Main Street, Anytown, USA 12345. If you would like to opt out of future emails, [click here to unsubscribe][5]. + new_version_mailer: + subject_template: "[%{site_name}] Updates Are Available" + text_body_template: | + A new version of Discourse is available. + + **New version: %{new_version}** + + Your version: %{installed_version} + + Please upgrade as soon as possible to get the latest fixes and new features. + system_messages: post_hidden: subject_template: "Post hidden due to community flagging" diff --git a/lib/jobs/version_check.rb b/lib/jobs/version_check.rb index dd9b6605607..91528bf7c54 100644 --- a/lib/jobs/version_check.rb +++ b/lib/jobs/version_check.rb @@ -8,11 +8,18 @@ module Jobs def execute(args) if SiteSetting.version_checks? and (DiscourseUpdates.updated_at.nil? or DiscourseUpdates.updated_at < 1.minute.ago) begin + should_send_email = (SiteSetting.new_version_emails and DiscourseUpdates.missing_versions_count and DiscourseUpdates.missing_versions_count == 0) + json = DiscourseHub.discourse_version_check DiscourseUpdates.latest_version = json['latestVersion'] DiscourseUpdates.critical_updates_available = json['criticalUpdates'] DiscourseUpdates.missing_versions_count = json['missingVersionsCount'] DiscourseUpdates.updated_at = Time.zone.now + + if should_send_email and json['missingVersionsCount'] > 0 + message = VersionMailer.send_notice + Email::Sender.new(message, :new_version).send + end rescue => e raise e unless Rails.env == 'development' # Fail version check silently in development mode end diff --git a/spec/mailers/version_mailer_spec.rb b/spec/mailers/version_mailer_spec.rb new file mode 100644 index 00000000000..eb0df5fbecf --- /dev/null +++ b/spec/mailers/version_mailer_spec.rb @@ -0,0 +1,18 @@ +require "spec_helper" + +describe VersionMailer do + subject { VersionMailer.send_notice } + + context 'contact_email is blank' do + before { SiteSetting.stubs(:contact_email).returns('') } + its(:to) { should be_blank } + end + + context 'contact_email is set' do + before { SiteSetting.stubs(:contact_email).returns('me@example.com') } + its(:to) { should == ['me@example.com'] } + its(:subject) { should be_present } + its(:from) { should == [SiteSetting.notification_email] } + its(:body) { should be_present } + end +end