From 96c1df617f41517ae5bd42decc29cca080e5b62a Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Fri, 23 Feb 2024 18:31:53 +1100 Subject: [PATCH] Make bookmark reminders a bit smarter --- app/repos/post_repo.rb | 18 +++++++++++++++--- lib/adamantium/gently_remind_me.rb | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/repos/post_repo.rb b/app/repos/post_repo.rb index 6cd595f..13ccfcc 100644 --- a/app/repos/post_repo.rb +++ b/app/repos/post_repo.rb @@ -1,16 +1,28 @@ +require "time_math" + module Adamantium module Repos class PostRepo < Adamantium::Repo[:posts] - def for_reminders(limit:) + def recent(limit:) + date = Time.now + posts - .where(post_type: "bookmark", is_read: false) + .where(post_type: "bookmark", is_read: false, published_at: TimeMath.day.advance(date, -7)...TimeMath.day.floor(date)) .limit(limit) .published - .combine(:tags) .order(Sequel.desc(:published_at)) .to_a end + def random(limit:, excluding:) + posts + .where(post_type: "bookmark", is_read: false) + .exclude(id: excluding) + .limit(limit) + .published + .order(Sequel.lit("random()")) + .to_a + end end end end diff --git a/lib/adamantium/gently_remind_me.rb b/lib/adamantium/gently_remind_me.rb index 3fe1e36..52993ca 100644 --- a/lib/adamantium/gently_remind_me.rb +++ b/lib/adamantium/gently_remind_me.rb @@ -6,7 +6,18 @@ module Adamantium repo = Adamantium::Container["repos.post_repo"] app_settings = Adamantium::Container["settings"] - bookmarks = repo.for_reminders(limit: limit) + recent_bookmarks = repo.recent(limit: limit) + + random_fillers = limit - recent_bookmarks.count + + random_bookmarks = if random_fillers > 0 + repo.random(limit: random_fillers, excluding: recent_bookmarks.map(&:id)) + else + [] + end + + bookmarks = recent_bookmarks + random_bookmarks + bookmarks_struct = bookmarks.map do |bookmark| { name: bookmark.name, @@ -46,4 +57,4 @@ module Adamantium mail.deliver end end -end \ No newline at end of file +end