From 9681897e9ace534c4559fa9be20aa86af1a42e13 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 7 Dec 2024 10:33:07 -0500 Subject: Add daily upvote email like AO3 kudos --- Capfile | 1 + Gemfile | 1 + Gemfile.lock | 4 ++++ app/mailers/vote_mailer.rb | 7 +++++++ app/models/quote.rb | 4 ++++ app/views/vote_mailer/daily_report_email.html.haml | 8 ++++++++ app/views/vote_mailer/daily_report_email.text.erb | 5 +++++ config/schedule.rb | 23 ++++++++++++++++++++++ lib/tasks/tasks.rake | 10 ++++++++++ test/mailers/previews/vote_mailer_preview.rb | 11 +++++++++++ test/mailers/vote_mailer_test.rb | 12 +++++++++++ 11 files changed, 86 insertions(+) create mode 100644 app/mailers/vote_mailer.rb create mode 100644 app/views/vote_mailer/daily_report_email.html.haml create mode 100644 app/views/vote_mailer/daily_report_email.text.erb create mode 100644 config/schedule.rb create mode 100644 lib/tasks/tasks.rake create mode 100644 test/mailers/previews/vote_mailer_preview.rb create mode 100644 test/mailers/vote_mailer_test.rb diff --git a/Capfile b/Capfile index 1ace4ce..8915892 100644 --- a/Capfile +++ b/Capfile @@ -14,6 +14,7 @@ require "capistrano/bundler" require "capistrano/rails/assets" require "capistrano/rails/migrations" require "capistrano/passenger" +require "whenever/capistrano" # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } diff --git a/Gemfile b/Gemfile index de04f85..d74e090 100644 --- a/Gemfile +++ b/Gemfile @@ -85,3 +85,4 @@ gem 'active_storage_validations' gem "image_processing", ">= 1.2" gem "meta-tags" gem 'rails_autolink' +gem 'whenever', "~> 1.0.0", require: false diff --git a/Gemfile.lock b/Gemfile.lock index ce12201..2980acc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -135,6 +135,7 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (>= 2.0, < 4.0) + chronic (0.10.2) climate_control (0.2.0) coffee-rails (4.2.2) coffee-script (>= 2.2.0) @@ -364,6 +365,8 @@ GEM websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) + whenever (1.0.0) + chronic (>= 0.6.3) will_paginate (4.0.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -418,6 +421,7 @@ DEPENDENCIES tzinfo-data web-console (>= 3.3.0) webrick (~> 1.7) + whenever (~> 1.0.0) will_paginate (~> 4.0) BUNDLED WITH diff --git a/app/mailers/vote_mailer.rb b/app/mailers/vote_mailer.rb new file mode 100644 index 0000000..77ac3d1 --- /dev/null +++ b/app/mailers/vote_mailer.rb @@ -0,0 +1,7 @@ +class VoteMailer < ApplicationMailer + def daily_report_email + @entries = params[:votes].group_by { |v| "#{v.votable_type}_#{v.votable_id}" }.values + # weird way of getting the admin's email + mail to: User.first.email, subject: "[Four Island] You have upvotes!" + end +end diff --git a/app/models/quote.rb b/app/models/quote.rb index 518d53b..3301667 100644 --- a/app/models/quote.rb +++ b/app/models/quote.rb @@ -17,6 +17,10 @@ class Quote < ApplicationRecord scope :published, -> { where(state: :published) } scope :pending, -> { where(state: :pending) } + def title + "Quote \##{id}" + end + def published_date created_at.strftime("%B %d %Y at %I:%M:%S") + created_at.strftime(" %p").downcase + created_at.strftime(" %Z") end diff --git a/app/views/vote_mailer/daily_report_email.html.haml b/app/views/vote_mailer/daily_report_email.html.haml new file mode 100644 index 0000000..ab54df2 --- /dev/null +++ b/app/views/vote_mailer/daily_report_email.html.haml @@ -0,0 +1,8 @@ +%p + Users have upvoted content on Four Island in the last day! +%ul + - @entries.each do |entry| + %li + %strong= pluralize(entry.size, "person") + left an upvote on + = link_to entry.first.votable.title, entry.first.votable, style: "font-weight: bold; font-style: italic" diff --git a/app/views/vote_mailer/daily_report_email.text.erb b/app/views/vote_mailer/daily_report_email.text.erb new file mode 100644 index 0000000..22ea368 --- /dev/null +++ b/app/views/vote_mailer/daily_report_email.text.erb @@ -0,0 +1,5 @@ +Users have upvoted content on Four Island in the last day! + +<% @entries.each do |entry| %> +- <%= pluralize(entry.size, "person") %> left an upvote on <%= entry.first.votable.title %> (<%= url_for(entry.first.votable) %>) +<% end %> diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 0000000..e53ebf4 --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,23 @@ +# Use this file to easily define all of your cron jobs. +# +# It's helpful, but not entirely necessary to understand cron before proceeding. +# http://en.wikipedia.org/wiki/Cron + +# Example: +# +# set :output, "/path/to/my/cron_log.log" +# +# every 2.hours do +# command "/usr/bin/some_great_command" +# runner "MyModel.some_method" +# rake "some:great:rake:task" +# end +# +# every 4.days do +# runner "AnotherModel.prune_old_records" +# end + +# Learn more: http://github.com/javan/whenever +every 1.day, at: "6:00am" do + rake "thoughts:email_upvote_report" +end diff --git a/lib/tasks/tasks.rake b/lib/tasks/tasks.rake new file mode 100644 index 0000000..fee22f4 --- /dev/null +++ b/lib/tasks/tasks.rake @@ -0,0 +1,10 @@ +namespace :thoughts do + desc "Email a review of the last day's upvotes" + task :email_upvote_report => :environment do + votes = Vote.where("created_at > ?", 1.day.ago).where(upvote: 1).all + + unless votes.empty? + VoteMailer.with(votes: votes).daily_report_email.deliver + end + end +end diff --git a/test/mailers/previews/vote_mailer_preview.rb b/test/mailers/previews/vote_mailer_preview.rb new file mode 100644 index 0000000..dfc5da1 --- /dev/null +++ b/test/mailers/previews/vote_mailer_preview.rb @@ -0,0 +1,11 @@ +# Preview all emails at http://localhost:3000/rails/mailers/vote_mailer +class VoteMailerPreview < ActionMailer::Preview + + # Preview this email at http://localhost:3000/rails/mailers/vote_mailer/daily_report_email + def daily_report_email + # Duplicating logic from the task... love it + votes = Vote.where("created_at > ?", 1.day.ago).where(upvote: 1).all + VoteMailer.with(votes: votes).daily_report_email + end + +end diff --git a/test/mailers/vote_mailer_test.rb b/test/mailers/vote_mailer_test.rb new file mode 100644 index 0000000..efe8236 --- /dev/null +++ b/test/mailers/vote_mailer_test.rb @@ -0,0 +1,12 @@ +require "test_helper" + +class VoteMailerTest < ActionMailer::TestCase + test "daily_report_email" do + mail = VoteMailer.daily_report_email + assert_equal "Daily report email", mail.subject + assert_equal ["to@example.org"], mail.to + assert_equal ["from@example.com"], mail.from + assert_match "Hi", mail.body.encoded + end + +end -- cgit 1.4.1