]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Added job to send bookmark digest
authorAidan Cornelius-Bell <[email protected]>
Wed, 9 Oct 2024 20:50:07 +0000 (07:20 +1030)
committerAidan Cornelius-Bell <[email protected]>
Wed, 9 Oct 2024 20:50:07 +0000 (07:20 +1030)
Gemfile
Gemfile.lock
app/jobs/weekly_bookmarks_digest_job.rb [new file with mode: 0644]
app/mailers/digest_mailer.rb [new file with mode: 0644]
app/views/digest_mailer/.DS_Store [copied from app/assets/images/.DS_Store with 100% similarity]
app/views/digest_mailer/weekly_bookmarks_digest.html.erb [new file with mode: 0644]
config/schedule.rb [new file with mode: 0644]
lib/tasks/lib-tasks-bookmarks_digest.rake [new file with mode: 0644]
test/jobs/weekly_bookmarks_digest_job_test.rb [copied from test/models/post_test.rb with 57% similarity]
test/mailers/digest_mailer_test.rb [new file with mode: 0644]
test/mailers/previews/digest_mailer_preview.rb [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
index 09f2d0bc2d3180fffcc32ae17ebd2cd4dbdbbe6b..6213de99b9c291f3e2fd00930167a1affd9a9388 100644 (file)
--- 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
index b9066b79e4aae7109682b99eb4a40ad599935a29..d37a19df22ab3dbf652a6d6aea530e370dfc43d9 100644 (file)
@@ -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 (file)
index 0000000..f664229
--- /dev/null
@@ -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 (file)
index 0000000..92e1ab0
--- /dev/null
@@ -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/views/digest_mailer/weekly_bookmarks_digest.html.erb b/app/views/digest_mailer/weekly_bookmarks_digest.html.erb
new file mode 100644 (file)
index 0000000..38997d3
--- /dev/null
@@ -0,0 +1,18 @@
+<h1>Your weekly bookmarks digest</h1>
+
+<p>Hello <%= @user.first_name %>,</p>
+
+<p>Here are the bookmarks added this week:</p>
+
+<ul>
+  <% @bookmarks.each do |bookmark| %>
+    <li>
+      <strong><%= link_to bookmark.title, bookmark.url %></strong>
+      <% if bookmark.content.present? %>
+        <br><em><%= bookmark.content %></em>
+      <% end %>
+    </li>
+  <% end %>
+</ul>
+
+<p>Have a great weekend!</p>
\ No newline at end of file
diff --git a/config/schedule.rb b/config/schedule.rb
new file mode 100644 (file)
index 0000000..d726e10
--- /dev/null
@@ -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 (file)
index 0000000..a06fa5d
--- /dev/null
@@ -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
similarity index 57%
copy from test/models/post_test.rb
copy to test/jobs/weekly_bookmarks_digest_job_test.rb
index ff155c49a649e1b735baeac60f589ba101c94fa0..f8c61731321332ee77766a5e5f4e08195b1fd54f 100644 (file)
@@ -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 (file)
index 0000000..b87ee99
--- /dev/null
@@ -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 [ "[email protected]" ], mail.to
+    assert_equal [ "[email protected]" ], 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 (file)
index 0000000..7c29e21
--- /dev/null
@@ -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