From e9125c8d1658895cb45385630c87f82abc8a8935 Mon Sep 17 00:00:00 2001
From: Aidan Cornelius-Bell <aidan@cornelius-bell.com>
Date: Sat, 23 Nov 2024 09:10:12 +1030
Subject: [PATCH] Bookmarks now include posts for a weekly summary, uses date
 sorting like the home page, might migrate to this over 'every dispatch as a
 post' not sure yet.

---
 app/jobs/weekly_bookmarks_digest_job.rb       |  2 +-
 app/mailers/digest_mailer.rb                  |  2 +-
 .../weekly_bookmarks_digest.html.erb          | 37 ++++++++++++-------
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/app/jobs/weekly_bookmarks_digest_job.rb b/app/jobs/weekly_bookmarks_digest_job.rb
index 87e7f74..b38746f 100644
--- a/app/jobs/weekly_bookmarks_digest_job.rb
+++ b/app/jobs/weekly_bookmarks_digest_job.rb
@@ -4,7 +4,7 @@ class WeeklyBookmarksDigestJob < ApplicationJob
   def perform(user = nil)
     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)
+    bookmarks = Post.where(created_at: start_date..end_date)
     # or do User.find_each do |user| for everyone, but that'll break fastmail
     if user.present?
       DigestMailer.weekly_bookmarks_digest(user, bookmarks).deliver_now
diff --git a/app/mailers/digest_mailer.rb b/app/mailers/digest_mailer.rb
index 92e1ab0..803c44d 100644
--- a/app/mailers/digest_mailer.rb
+++ b/app/mailers/digest_mailer.rb
@@ -7,6 +7,6 @@ class DigestMailer < ApplicationMailer
   def weekly_bookmarks_digest(user, bookmarks)
     @user = user
     @bookmarks = bookmarks
-    mail(to: @user.email, subject: 'mind reader :: weekly bookmarks digest')
+    mail(to: @user.email, subject: 'mind reader :: weekly 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
index 03fc0ba..ace210c 100644
--- a/app/views/digest_mailer/weekly_bookmarks_digest.html.erb
+++ b/app/views/digest_mailer/weekly_bookmarks_digest.html.erb
@@ -1,19 +1,28 @@
-<h1>Your weekly bookmarks digest</h1>
+<p>Dear <%= @user.first_name %>,</p>
 
-<p>Hello <%= @user.first_name %>,</p>
+<p>Here's what's new from mind reader this week:</p>
 
-<p>Here are the bookmarks added this week:</p>
+<% @bookmarks.group_by { |bookmark| bookmark.created_at.to_date }.each do |date, bookmarks| %>
+  <p>On <%= date.strftime('%B %e, %Y') %>:</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>
+  <ul>
+    <% bookmarks.each do |bookmark| %>
+      <li>
+        <% if bookmark.url.present? %>
+          <strong><a href="<%= bookmark.url %>" target="_blank"><%= bookmark.title %></a></strong>
+        <% else %>
+          <strong><%= bookmark.title %></strong>
+        <% end %>
+        <% if bookmark.content.present? %>
+          <br><em><%= bookmark.content %></em>
+        <% end %>
+        <% if bookmark.created_at.present? %>
+          <br><small>Added at <%= bookmark.created_at.strftime('%l:%M%P') %></small>
+        <% end %>
+      </li>
+    <% end %>
+  </ul>
+<% end %>
 
 <p>Have a great weekend,<br>
-Aidan.</p>
\ No newline at end of file
+Aidan.</p>
-- 
2.39.5