From db3f9be40fa95b8232c7cda9e714fd476313aeb8 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 16 Dec 2023 11:56:37 +1100 Subject: [PATCH] Add time machine --- Gemfile | 2 +- Gemfile.lock | 10 ++-- app/actions/timemachine/show.rb | 19 ++++++++ app/repos/podcast_scrobble_repo.rb | 6 +++ app/repos/post_repo.rb | 14 ++++++ app/templates/more/index.html.slim | 1 + app/templates/shared/_post.html.slim | 2 +- app/templates/timemachine/show.html.slim | 56 ++++++++++++++++++++++ app/views/timemachine/show.rb | 59 ++++++++++++++++++++++++ config/routes.rb | 2 + 10 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 app/actions/timemachine/show.rb create mode 100644 app/templates/timemachine/show.html.slim create mode 100644 app/views/timemachine/show.rb diff --git a/Gemfile b/Gemfile index 3d8af69..0f4a6a4 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source "https://rubygems.org" -ruby "3.2.2" +ruby "3.3.0.rc1" gem "hanami", "2.1.0rc2" gem "hanami-router", "2.1.0rc2" diff --git a/Gemfile.lock b/Gemfile.lock index 4590591..81a6772 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -277,6 +277,7 @@ GEM mime-types-data (3.2023.1003) mini_magick (4.12.0) mini_mime (1.1.5) + mini_portile2 (2.8.5) minitest (5.20.0) multi_xml (0.6.0) mustermann (3.0.0) @@ -300,11 +301,8 @@ GEM net-ssh (7.2.0) netrc (0.11.0) nio4r (2.6.1) - nokogiri (1.15.5-arm64-darwin) - racc (~> 1.4) - nokogiri (1.15.5-x86_64-darwin) - racc (~> 1.4) - nokogiri (1.15.5-x86_64-linux) + nokogiri (1.15.5) + mini_portile2 (~> 2.8.2) racc (~> 1.4) notiffany (0.1.3) nenv (~> 0.1) @@ -546,7 +544,7 @@ DEPENDENCIES webmention RUBY VERSION - ruby 3.2.2p53 + ruby 3.3.0.rc1 BUNDLED WITH 2.3.22 diff --git a/app/actions/timemachine/show.rb b/app/actions/timemachine/show.rb new file mode 100644 index 0000000..9debaeb --- /dev/null +++ b/app/actions/timemachine/show.rb @@ -0,0 +1,19 @@ +module Adamantium + module Actions + module Timemachine + class Show < Action + include Deps["views.timemachine.show"] + + def handle(req, res) + year, month, day = [ + req.params[:year], + req.params[:month], + req.params[:day] + ] + + res.render show, year: year, month: month, day: day + end + end + end + end +end diff --git a/app/repos/podcast_scrobble_repo.rb b/app/repos/podcast_scrobble_repo.rb index bfcbf65..08d93e0 100644 --- a/app/repos/podcast_scrobble_repo.rb +++ b/app/repos/podcast_scrobble_repo.rb @@ -15,6 +15,12 @@ module Adamantium .limit(5) .to_a end + + def for_timemachine(date:) + podcast_scrobbles + .where(listened_at: TimeMath.day.floor(date)...TimeMath.day.advance(date, +1)) + .to_a + end end end end diff --git a/app/repos/post_repo.rb b/app/repos/post_repo.rb index 893122d..f06b826 100644 --- a/app/repos/post_repo.rb +++ b/app/repos/post_repo.rb @@ -140,6 +140,20 @@ module Adamantium .to_a end + def posts_for_timemachine(date:) + posts + .where(post_type: "post") + .where(published_at: TimeMath.day.floor(date)...TimeMath.day.advance(date, +1)) + .to_a + end + + def bookmarks_for_timemachine(date:) + posts + .where(post_type: "bookmark") + .where(published_at: TimeMath.day.floor(date)...TimeMath.day.advance(date, +1)) + .to_a + end + def for_rss posts .where(post_type: "post", location: nil) diff --git a/app/templates/more/index.html.slim b/app/templates/more/index.html.slim index 76ab133..8171f2b 100644 --- a/app/templates/more/index.html.slim +++ b/app/templates/more/index.html.slim @@ -8,6 +8,7 @@ div class="mb-12 max-w-prose mx-auto text-gray-800 dark:text-gray-200" / a class="block p-1 border border-blue-200 bg-blue-300 text-blue-900 hover:bg-blue-200 text-center rounded-lg" href="/years" 🗓️ By year a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/posts" 🪧 All posts a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/tagged/weekly" 🔄 Week posts + a class="block p-1 border border-blue-200 bg-blue-200 text-blue-900 hover:bg-blue-300 text-center rounded-lg" href="/timemachine/#{Time.now.strftime("%Y/%m/%d")}" ⏳ Time machine h2 class="text-xl" Explore everything else diff --git a/app/templates/shared/_post.html.slim b/app/templates/shared/_post.html.slim index d3b3d95..e2dc7e4 100644 --- a/app/templates/shared/_post.html.slim +++ b/app/templates/shared/_post.html.slim @@ -4,7 +4,7 @@ div class="mb-8 h-entry" = post.display_title div class="e-content prose-p:mb-0 prose-img:my-2 prose-a:text-blue-600 prose-a:no-underline hover:prose-a:underline p-name text-base prose prose-ul:list-none prose-ul:pl-0 prose-li:pl-0 text-gray-800 dark:text-gray-200 prose-a:dark:text-gray-100" == post.excerpt - div class="grid gap-4 grid-flow-row grid-cols-4 grid-rows-1" + div class="grid gap-4 grid-flow-row grid-cols-#{post.photos.count} grid-rows-1" -post.photos.each do |photo| img class="w-44 h-44 object-cover rounded" src=photo["value"] / == render "shared/tags", tags: post.tags diff --git a/app/templates/timemachine/show.html.slim b/app/templates/timemachine/show.html.slim new file mode 100644 index 0000000..0e3b2ac --- /dev/null +++ b/app/templates/timemachine/show.html.slim @@ -0,0 +1,56 @@ +div class="prose dark:prose-invert w-full max-w-6xl mx-auto px-4 m-4" + div class="grid md:grid-cols-12 gap-5 p-4 m-2" + div class="md:col-span-8 p-4" + h2 class="mt-0" = display_date + + a href="/timemachine/#{prev_date}" ← #{prev_date} + + a class="inline-block px-2" href="/timemachine/#{today}" Today + + - if next_date + a href="/timemachine/#{next_date}" #{next_date} → + + - if posts.count > 0 + div class="mt-4" + - posts.each_with_index do |post, index| + div class="#{"border-t-2" if index == 0} border-b-2 border-gray-200" + - if post.photos.count == 0 + a href="/post/#{post.slug}" + h3= post.name + == post.excerpt + - else + div class="grid grid-cols-3 gap-2" + - posts.each do |post| + - if post.photos.count > 0 + - post.photos.each do |photo| + a href="/post/#{post.slug}" + img class="mb-0 rounded" src="#{photo["value"]}" + a href="/post/#{post.slug}" + = post.name + - else + p No posts + + aside class="md:col-span-4 md:pt-0 p-2" + - if bookmarks.count > 0 + h3 class="mt-0" Bookmarks + + div + ul + - bookmarks.each do |bookmark| + li + a href="/bookmark/#{bookmark.slug}" + = bookmark.name + + - if podcasts.count > 0 + h3 class="mt-0" Podcasts + + div + ul + - podcasts.each do |podcast| + li + a href=podcast.url + =podcast.title + div + small = podcast.podcast_name + / h3 class="mt-0" Music + / h3 class="mt-0" Reading \ No newline at end of file diff --git a/app/views/timemachine/show.rb b/app/views/timemachine/show.rb new file mode 100644 index 0000000..3382486 --- /dev/null +++ b/app/views/timemachine/show.rb @@ -0,0 +1,59 @@ +require "time_math" + +module Adamantium + module Views + module Timemachine + class Show < Adamantium::View + include Deps[ + "repos.post_repo", + "repos.movie_repo", + "repos.podcast_scrobble_repo", + "repos.trip_repo" + ] + + expose :posts do |date| + post_repo.posts_for_timemachine(date: date.value).map do |post| + Decorators::Posts::Decorator.new(post) + end + end + + expose :bookmarks do |date| + post_repo.bookmarks_for_timemachine(date: date.value).map do |post| + Decorators::Bookmarks::Decorator.new(post) + end + end + + expose :podcasts do |date| + podcast_scrobble_repo.for_timemachine(date: date.value) + end + + expose :display_date do |date| + date.strftime("%A, %d %B, %Y") + end + + expose :next_date do |date| + today_to_tomorrow = TimeMath.measure(TimeMath(date.value).advance(:day, +1).call, Time.now) + + unless today_to_tomorrow[:hours] < 0 + TimeMath.day.advance(date.value, +1) + .strftime("%Y/%m/%d") + end + end + + expose :today do + Time.now + .strftime("%Y/%m/%d") + end + + expose :prev_date do |date| + TimeMath.day.advance(date.value, -1) + .strftime("%Y/%m/%d") + end + + private_expose :date do |year:, month:, day:| + DateTime.parse("#{year}-#{month}-#{day}") + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index e367b11..675d2bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,8 @@ module Adamantium get "/bookshelf", to: "books.index" + get "/timemachine/:year/:month/:day", to: "timemachine.show" + redirect "deploying-a-hanami-app-to-fly-io", to: "/post/deploying-a-hanami-20-app-to-flyio" redirect "deploying-a-hanami-app-to-fly-io/", to: "/post/deploying-a-hanami-20-app-to-flyio"