From 1f9187e35cb0bd898a66ad84190998c981700993 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Thu, 25 Feb 2021 02:43:14 +0100 Subject: [PATCH] Add ActionMailer to inform admins about new screenshots This version only informs Christoph as a test --- app/controllers/packages_controller.rb | 5 +++++ app/mailers/admin_mailer.rb | 9 +++++++++ app/mailers/application_mailer.rb | 5 +++++ app/views/admin_mailer/new_uploads_email.text.erb | 7 +++++++ app/views/layouts/mailer.text.slim | 1 + config/environments/development.rb | 11 ++++++++++- config/environments/production.rb | 10 ++++++++++ test/fixtures/action_text/rich_texts.yml | 4 ++++ test/mailers/previews/admin_mailer_preview.rb | 4 ++++ 9 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 app/mailers/admin_mailer.rb create mode 100644 app/mailers/application_mailer.rb create mode 100644 app/views/admin_mailer/new_uploads_email.text.erb create mode 100644 app/views/layouts/mailer.text.slim create mode 100644 test/fixtures/action_text/rich_texts.yml create mode 100644 test/mailers/previews/admin_mailer_preview.rb diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 190e902..3677eb7 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -123,6 +123,11 @@ class PackagesController < ApplicationController # if @invalid_images… # ' #{image.image_file_name} (#{image.errors[:image].join(' and ')}) + # Inform the admins about the upload + if @valid_images.any? + AdminMailer.with(package: @package).new_uploads_email.deliver_now + end + # Rails does not allow dots in the URL. So we cannot use the 'respond_to' # and 'format' ways to handle parameters. Instead the 'returns' parameters # is set in routes.rb to signal that this method was called by AJAX. diff --git a/app/mailers/admin_mailer.rb b/app/mailers/admin_mailer.rb new file mode 100644 index 0000000..918ee7f --- /dev/null +++ b/app/mailers/admin_mailer.rb @@ -0,0 +1,9 @@ +class AdminMailer < ApplicationMailer + default from: 'notifications@mentors.debian.net' + + def new_uploads_email + @package = params[:package] + #@url = 'http://example.com/login' + mail(to: 'email@christoph-haas.de', subject: 'Please moderate new screenshot uploads') + end +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb new file mode 100644 index 0000000..a8c60c6 --- /dev/null +++ b/app/mailers/application_mailer.rb @@ -0,0 +1,5 @@ +class ApplicationMailer < ActionMailer::Base + default from: 'from@example.com' + layout 'mailer' +end + diff --git a/app/views/admin_mailer/new_uploads_email.text.erb b/app/views/admin_mailer/new_uploads_email.text.erb new file mode 100644 index 0000000..0629cc4 --- /dev/null +++ b/app/views/admin_mailer/new_uploads_email.text.erb @@ -0,0 +1,7 @@ +test + +package is <%= @package.name %> + +link would be <%= url_for @package %> + +goodbye diff --git a/app/views/layouts/mailer.text.slim b/app/views/layouts/mailer.text.slim new file mode 100644 index 0000000..0a90f09 --- /dev/null +++ b/app/views/layouts/mailer.text.slim @@ -0,0 +1 @@ += yield diff --git a/config/environments/development.rb b/config/environments/development.rb index e2ec649..31dc633 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -32,7 +32,16 @@ Rails.application.configure do config.active_storage.service = :local # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true + config.action_mailer.smtp_settings = { + address: 'localhost', + port: 25, + enable_starttls_auto: false + } + config.action_mailer.default_url_options = { + host: "screenshots.debian.net", + protocol: 'https' + } config.action_mailer.perform_caching = false diff --git a/config/environments/production.rb b/config/environments/production.rb index e938661..078b8d0 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -70,6 +70,16 @@ Rails.application.configure do config.action_mailer.perform_caching = false + config.action_mailer.smtp_settings = { + address: 'localhost', + port: 25, + enable_starttls_auto: false + } + config.action_mailer.default_url_options = { + host: "screenshots.debian.net", + protocol: 'https' + } + # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. # config.action_mailer.raise_delivery_errors = false diff --git a/test/fixtures/action_text/rich_texts.yml b/test/fixtures/action_text/rich_texts.yml new file mode 100644 index 0000000..8b371ea --- /dev/null +++ b/test/fixtures/action_text/rich_texts.yml @@ -0,0 +1,4 @@ +# one: +# record: name_of_fixture (ClassOfFixture) +# name: content +# body:

In a million stars!

diff --git a/test/mailers/previews/admin_mailer_preview.rb b/test/mailers/previews/admin_mailer_preview.rb new file mode 100644 index 0000000..73ea1b7 --- /dev/null +++ b/test/mailers/previews/admin_mailer_preview.rb @@ -0,0 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/admin_mailer +class AdminMailerPreview < ActionMailer::Preview + +end