From: Aidan Cornelius-Bell Date: Wed, 9 Oct 2024 20:50:07 +0000 (+1030) Subject: Added job to send bookmark digest X-Git-Url: https://gitweb.mndrdr.org/?a=commitdiff_plain;h=8a74753416297f39ab3be8075c9e3a517cbed73a;p=arelpe.git Added job to send bookmark digest --- diff --git a/Gemfile b/Gemfile index 09f2d0b..6213de9 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,8 @@ gem "dotenv" gem "hcaptcha" # subscribers gem "stripe" +# Scheduling +gem "whenever", require: false # Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] # gem "kredis" #api stuff diff --git a/Gemfile.lock b/Gemfile.lock index b9066b7..d37a19d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -94,6 +94,7 @@ GEM xpath (~> 3.2) childprocess (5.1.0) logger (~> 1.5) + chronic (0.10.2) concurrent-ruby (1.3.4) connection_pool (2.4.1) crass (1.0.6) @@ -310,6 +311,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) xpath (3.2.0) nokogiri (~> 1.8) zeitwerk (2.6.18) @@ -345,6 +348,7 @@ DEPENDENCIES stripe tzinfo-data web-console + whenever BUNDLED WITH 2.5.11 diff --git a/app/jobs/weekly_bookmarks_digest_job.rb b/app/jobs/weekly_bookmarks_digest_job.rb new file mode 100644 index 0000000..f664229 --- /dev/null +++ b/app/jobs/weekly_bookmarks_digest_job.rb @@ -0,0 +1,11 @@ +class WeeklyBookmarksDigestJob < ApplicationJob + queue_as :default + + def perform(*args) + start_date = 1.week.ago.beginning_of_week + end_date = Time.current.end_of_week + bookmarks = Post.bookmarks.where(created_at: start_date..end_date) + # or do User.find_each do |user| for everyone, but that'll break fastmail + DigestMailer.weekly_bookmarks_digest(User.first, bookmarks).deliver_now + end +end diff --git a/app/mailers/digest_mailer.rb b/app/mailers/digest_mailer.rb new file mode 100644 index 0000000..92e1ab0 --- /dev/null +++ b/app/mailers/digest_mailer.rb @@ -0,0 +1,12 @@ +class DigestMailer < ApplicationMailer + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.digest_mailer.weekly_bookmarks_digest.subject + # + def weekly_bookmarks_digest(user, bookmarks) + @user = user + @bookmarks = bookmarks + mail(to: @user.email, subject: 'mind reader :: weekly bookmarks digest') + end +end diff --git a/app/assets/images/.DS_Store b/app/views/digest_mailer/.DS_Store similarity index 100% copy from app/assets/images/.DS_Store copy to app/views/digest_mailer/.DS_Store diff --git a/app/views/digest_mailer/weekly_bookmarks_digest.html.erb b/app/views/digest_mailer/weekly_bookmarks_digest.html.erb new file mode 100644 index 0000000..38997d3 --- /dev/null +++ b/app/views/digest_mailer/weekly_bookmarks_digest.html.erb @@ -0,0 +1,18 @@ +

Your weekly bookmarks digest

+ +

Hello <%= @user.first_name %>,

+ +

Here are the bookmarks added this week:

+ + + +

Have a great weekend!

\ No newline at end of file diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 0000000..d726e10 --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,24 @@ +# 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 :friday, at: '4pm' do + rake "bookmarks_digest:send_weekly" +end \ No newline at end of file diff --git a/lib/tasks/lib-tasks-bookmarks_digest.rake b/lib/tasks/lib-tasks-bookmarks_digest.rake new file mode 100644 index 0000000..a06fa5d --- /dev/null +++ b/lib/tasks/lib-tasks-bookmarks_digest.rake @@ -0,0 +1,6 @@ +namespace :bookmarks_digest do + desc "Send weekly bookmarks digest" + task send_weekly: :environment do + WeeklyBookmarksDigestJob.perform_later + end +end \ No newline at end of file diff --git a/test/models/post_test.rb b/test/jobs/weekly_bookmarks_digest_job_test.rb similarity index 57% copy from test/models/post_test.rb copy to test/jobs/weekly_bookmarks_digest_job_test.rb index ff155c4..f8c6173 100644 --- a/test/models/post_test.rb +++ b/test/jobs/weekly_bookmarks_digest_job_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class PostTest < ActiveSupport::TestCase +class WeeklyBookmarksDigestJobTest < ActiveJob::TestCase # test "the truth" do # assert true # end diff --git a/test/mailers/digest_mailer_test.rb b/test/mailers/digest_mailer_test.rb new file mode 100644 index 0000000..b87ee99 --- /dev/null +++ b/test/mailers/digest_mailer_test.rb @@ -0,0 +1,11 @@ +require "test_helper" + +class DigestMailerTest < ActionMailer::TestCase + test "weekly_bookmarks_digest" do + mail = DigestMailer.weekly_bookmarks_digest + assert_equal "Weekly bookmarks digest", mail.subject + assert_equal [ "to@example.org" ], mail.to + assert_equal [ "from@example.com" ], mail.from + assert_match "Hi", mail.body.encoded + end +end diff --git a/test/mailers/previews/digest_mailer_preview.rb b/test/mailers/previews/digest_mailer_preview.rb new file mode 100644 index 0000000..7c29e21 --- /dev/null +++ b/test/mailers/previews/digest_mailer_preview.rb @@ -0,0 +1,7 @@ +# Preview all emails at http://localhost:3000/rails/mailers/digest_mailer +class DigestMailerPreview < ActionMailer::Preview + # Preview this email at http://localhost:3000/rails/mailers/digest_mailer/weekly_bookmarks_digest + def weekly_bookmarks_digest + DigestMailer.weekly_bookmarks_digest + end +end