From 19aff274dec86f0f6b1c4bd02d6b6affd5dc6720 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 29 Jul 2023 18:06:05 +1000 Subject: [PATCH] Add ability to cache bookmark --- app/commands/posts/create_bookmark.rb | 9 ++- app/entities/bookmark_request.rb | 1 + config/providers/post_utilities.rb | 1 + config/routes.rb | 1 - lib/adamantium/micropub_request_parser.rb | 1 + lib/adamantium/page_cacher.rb | 12 ++++ slices/admin/commands/bookmarks/cache.rb | 13 ++-- .../config/authenticated_admin_action.rb | 5 +- slices/admin/config/routes.rb | 62 +++++++++---------- 9 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 lib/adamantium/page_cacher.rb diff --git a/app/commands/posts/create_bookmark.rb b/app/commands/posts/create_bookmark.rb index 7ce34a3..09a2389 100644 --- a/app/commands/posts/create_bookmark.rb +++ b/app/commands/posts/create_bookmark.rb @@ -5,8 +5,9 @@ module Adamantium module Posts class CreateBookmark < Command include Deps["repos.post_repo", + "post_utilities.page_cacher", syndicate: "commands.posts.syndicate", - raindrop: "syndication.raindrop" + raindrop: "syndication.raindrop", ] include Dry::Monads[:result] @@ -17,6 +18,12 @@ module Adamantium syndicate.call(created_bookmark.id, bookmark) raindrop.call(post: created_bookmark) + if bookmark[:cache] + page_cacher.call(url: created_bookmark.url) do |content| + post_repo.update(id: created_bookmark.id, cached_content: content) + end + end + Success(created_bookmark) end end diff --git a/app/entities/bookmark_request.rb b/app/entities/bookmark_request.rb index 6571b82..e7818c1 100644 --- a/app/entities/bookmark_request.rb +++ b/app/entities/bookmark_request.rb @@ -4,6 +4,7 @@ module Adamantium attribute :h, Types::Coercible::String attribute :action, Types::Coercible::String.optional attribute :name, Types::Coercible::String + attribute :cache, Types::Bool.optional attribute :content, Types::Coercible::String.optional attribute :url, Types::Coercible::String attribute :slug, Types::Coercible::String diff --git a/config/providers/post_utilities.rb b/config/providers/post_utilities.rb index b99c9c0..6f719b0 100644 --- a/config/providers/post_utilities.rb +++ b/config/providers/post_utilities.rb @@ -4,5 +4,6 @@ Hanami.app.register_provider :post_utilities, namespace: true do start do register "slugify", Adamantium::SlugCreator.new register "link_finder", Adamantium::LinkFinder.new + register "page_cacher", Adamantium::PageCacher.new end end diff --git a/config/routes.rb b/config/routes.rb index 56bedc6..952828c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,6 @@ module Adamantium use Hanami::Middleware::BodyParser, [:form, :json] # use Adamantium::Middleware::ProcessParams - scope "micropub" do get "/", to: "site.config" post "/", to: "posts.handle" diff --git a/lib/adamantium/micropub_request_parser.rb b/lib/adamantium/micropub_request_parser.rb index ddb9bcd..84ca0ed 100644 --- a/lib/adamantium/micropub_request_parser.rb +++ b/lib/adamantium/micropub_request_parser.rb @@ -91,6 +91,7 @@ module Adamantium }) end new_params[:url] = params[:"bookmark-of"] + new_params[:cache] = params[:cache] || false new_params end diff --git a/lib/adamantium/page_cacher.rb b/lib/adamantium/page_cacher.rb new file mode 100644 index 0000000..4e8d54c --- /dev/null +++ b/lib/adamantium/page_cacher.rb @@ -0,0 +1,12 @@ +require "readability" +require "down" + +module Adamantium + class PageCacher + def call(url:, &block) + tempfile = Down.download(bookmark.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 + end +end diff --git a/slices/admin/commands/bookmarks/cache.rb b/slices/admin/commands/bookmarks/cache.rb index de07e0a..e612142 100644 --- a/slices/admin/commands/bookmarks/cache.rb +++ b/slices/admin/commands/bookmarks/cache.rb @@ -1,21 +1,16 @@ -require "readability" -require "down" - module Admin module Commands module Bookmarks class Cache include Dry::Monads[:result] - include Deps["repos.bookmark_repo"] + include Deps["repos.bookmark_repo", "post_utilities.page_cacher"] def call(bookmark_id:) bookmark = bookmark_repo.fetch(id: bookmark_id) - bookmark.url - tempfile = Down.download(bookmark.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 - - bookmark_repo.update(id: bookmark_id, cached_content: content) + page_cacher.call(url: bookmark.url) do |content| + bookmark_repo.update(id: bookmark_id, cached_content: content) + end Success() end diff --git a/slices/admin/config/authenticated_admin_action.rb b/slices/admin/config/authenticated_admin_action.rb index 514ff55..097c607 100644 --- a/slices/admin/config/authenticated_admin_action.rb +++ b/slices/admin/config/authenticated_admin_action.rb @@ -1,8 +1,7 @@ module Adamantium class AuthenticatedAdminAction def self.call(action:) - - action_proc = ->(env) { Admin::Container["actions.#{action}"].(env) } + action_proc = ->(env) { Admin::Container["actions.#{action}"].call(env) } if Hanami.app.settings.basic_auth_username && Hanami.app.settings.basic_auth_password Rack::Auth::Basic.new(action_proc) do |username, password| @@ -14,4 +13,4 @@ module Adamantium end end end -end \ No newline at end of file +end diff --git a/slices/admin/config/routes.rb b/slices/admin/config/routes.rb index fc9d52c..9ec6391 100644 --- a/slices/admin/config/routes.rb +++ b/slices/admin/config/routes.rb @@ -9,43 +9,43 @@ module Admin Auth = Adamantium::AuthenticatedAdminAction - get "/", to: Auth.(action: "index") + get "/", to: Auth.call(action: "index") - get "/tags", to: Auth.(action: "tags.index") - delete "/tags/:id", to: Auth.(action: "tags.delete") + get "/tags", to: Auth.call(action: "tags.index") + delete "/tags/:id", to: Auth.call(action: "tags.delete") - get "/tags/auto_tagging", to: Auth.(action: "auto_tagging.index") - get "/tags/auto_tagging/new", to: Auth.(action: "auto_tagging.new") - post "/tags/auto_tagging", to: Auth.(action: "auto_tagging.create") - delete "/tags/auto_taggings/:id", to: Auth.(action: "auto_tagging.delete") + get "/tags/auto_tagging", to: Auth.call(action: "auto_tagging.index") + get "/tags/auto_tagging/new", to: Auth.call(action: "auto_tagging.new") + post "/tags/auto_tagging", to: Auth.call(action: "auto_tagging.create") + delete "/tags/auto_taggings/:id", to: Auth.call(action: "auto_tagging.delete") - get "/tags/merge", to: Auth.(action: "merge_tags.index") - get "/tags/merge/:id", to: Auth.(action: "merge_tags.new") - post "/tags/merge", to: Auth.(action: "merge_tags.merge") + get "/tags/merge", to: Auth.call(action: "merge_tags.index") + get "/tags/merge/:id", to: Auth.call(action: "merge_tags.new") + post "/tags/merge", to: Auth.call(action: "merge_tags.merge") - get "/bookmarks", to: Auth.(action: "bookmarks.index") - delete "/bookmarks/:id", to: Auth.(action: "bookmarks.delete") - post "/bookmarks/clean", to: Auth.(action: "bookmarks.clean") - post "/bookmarks/cache/:id", to: Auth.(action: "bookmarks.cache") - post "/bookmarks/:id/archive", to: Auth.(action: "bookmarks.archive") - post "/bookmarks/:id/publish", to: Auth.(action: "bookmarks.publish") + get "/bookmarks", to: Auth.call(action: "bookmarks.index") + delete "/bookmarks/:id", to: Auth.call(action: "bookmarks.delete") + post "/bookmarks/clean", to: Auth.call(action: "bookmarks.clean") + post "/bookmarks/cache/:id", to: Auth.call(action: "bookmarks.cache") + post "/bookmarks/:id/archive", to: Auth.call(action: "bookmarks.archive") + post "/bookmarks/:id/publish", to: Auth.call(action: "bookmarks.publish") - get "/posts", to: Auth.(action: "posts.index") - delete "/posts/:id", to: Auth.(action: "posts.delete") - post "/posts/:id/archive", to: Auth.(action: "posts.archive") - post "/posts/:id/publish", to: Auth.(action: "posts.publish") - get "/posts/:id", to: Auth.(action: "posts.show") - post "/posts/:id/syndicate/:target", to: Auth.(action: "posts.syndicate") + get "/posts", to: Auth.call(action: "posts.index") + delete "/posts/:id", to: Auth.call(action: "posts.delete") + post "/posts/:id/archive", to: Auth.call(action: "posts.archive") + post "/posts/:id/publish", to: Auth.call(action: "posts.publish") + get "/posts/:id", to: Auth.call(action: "posts.show") + post "/posts/:id/syndicate/:target", to: Auth.call(action: "posts.syndicate") - get "/media", to: Auth.(action: "photos.index") - delete "/media/public/media/:year/:path", to: Auth.(action: "photos.delete") + get "/media", to: Auth.call(action: "photos.index") + delete "/media/public/media/:year/:path", to: Auth.call(action: "photos.delete") - get "/trips", to: Auth.(action: "trips.index") - get "/trips/:id", to: Auth.(action: "trips.show") - post "/trips", to: Auth.(action: "trips.create") - post "/trips/add_post", to: Auth.(action: "trips.add_post") - post "/trips/remove_post", to: Auth.(action: "trips.remove_post") - get "/trips/new", to: Auth.(action: "trips.new") - post "/trips/:id", to: Auth.(action: "trips.update") + get "/trips", to: Auth.call(action: "trips.index") + get "/trips/:id", to: Auth.call(action: "trips.show") + post "/trips", to: Auth.call(action: "trips.create") + post "/trips/add_post", to: Auth.call(action: "trips.add_post") + post "/trips/remove_post", to: Auth.call(action: "trips.remove_post") + get "/trips/new", to: Auth.call(action: "trips.new") + post "/trips/:id", to: Auth.call(action: "trips.update") end end