From b389abf29d735d370279bff210176adc74c14b3b Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 29 Jul 2023 18:27:45 +1000 Subject: [PATCH] Fix page caching --- app/commands/posts/create_bookmark.rb | 2 +- app/validation/posts/bookmark_contract.rb | 1 + lib/adamantium/page_cacher.rb | 2 +- spec/requests/create_post_spec.rb | 4 +++- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/commands/posts/create_bookmark.rb b/app/commands/posts/create_bookmark.rb index 09a2389..a7d3b73 100644 --- a/app/commands/posts/create_bookmark.rb +++ b/app/commands/posts/create_bookmark.rb @@ -20,7 +20,7 @@ module Adamantium if bookmark[:cache] page_cacher.call(url: created_bookmark.url) do |content| - post_repo.update(id: created_bookmark.id, cached_content: content) + post_repo.update(created_bookmark.id, cached_content: content) end end diff --git a/app/validation/posts/bookmark_contract.rb b/app/validation/posts/bookmark_contract.rb index 9fec079..96091da 100644 --- a/app/validation/posts/bookmark_contract.rb +++ b/app/validation/posts/bookmark_contract.rb @@ -12,6 +12,7 @@ module Adamantium required(:post_type).value(included_in?: %w[bookmark]) required(:syndicate_to).array(:string) required(:photos).array(:hash) + required(:cache).filled(:bool) end end end diff --git a/lib/adamantium/page_cacher.rb b/lib/adamantium/page_cacher.rb index 4e8d54c..b646b52 100644 --- a/lib/adamantium/page_cacher.rb +++ b/lib/adamantium/page_cacher.rb @@ -4,7 +4,7 @@ require "down" module Adamantium class PageCacher def call(url:, &block) - tempfile = Down.download(bookmark.url) + tempfile = Down.download(url) content = Readability::Document.new(tempfile.read, tags: %w[div section header p h1 h2 h3 h4 h5 h6 ol ul li table td tr thead tbody a code pre], attributes: %w[href]).content yield content end diff --git a/spec/requests/create_post_spec.rb b/spec/requests/create_post_spec.rb index fe93b0e..b914868 100644 --- a/spec/requests/create_post_spec.rb +++ b/spec/requests/create_post_spec.rb @@ -49,13 +49,15 @@ RSpec.describe "Post creation", :db, :requests do h: "entry", "bookmark-of": "http://example.com", name: "Name", - content: "Content of theh post" + content: "Content of the post", + cache: "true" } post "/micropub", params expect(last_response).to be_successful expect(post_repo.bookmark_listing.count).to eq 1 + expect(post_repo.bookmark_listing.last.cached_content).to_not eq nil end end