From 2b81839127aa04cf577f0d1cd3e528aaf05af617 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sun, 12 Feb 2023 21:58:18 +1100 Subject: [PATCH] Refactor post creation + syndication --- app/actions/posts/handle.rb | 26 +++++++++++------------ app/commands/posts/create_bookmark.rb | 16 ++++++++++++-- app/commands/posts/create_entry.rb | 17 +++++++++++++-- app/commands/posts/update.rb | 5 +++-- lib/adamantium/micropub_request_parser.rb | 15 +------------ 5 files changed, 46 insertions(+), 33 deletions(-) diff --git a/app/actions/posts/handle.rb b/app/actions/posts/handle.rb index dc49ad9..f18e5f4 100644 --- a/app/actions/posts/handle.rb +++ b/app/actions/posts/handle.rb @@ -6,12 +6,13 @@ module Adamantium include Deps[ "settings", + "post_utilities.slugify", + "repos.post_repo", post_param_parser: "param_parser.micropub_post", create_resolver: "commands.posts.creation_resolver", delete_post: "commands.posts.delete", undelete_post: "commands.posts.undelete", update_post: "commands.posts.update", - syndicate: "commands.posts.syndicate", add_post_syndication_source: "commands.posts.add_syndication_source" ] @@ -33,21 +34,14 @@ module Adamantium halt 401 unless verify_scope(req: req, scope: :create) command, contract = create_resolver.call(entry_type: req_entity).values_at(:command, :validation) + post_params = prepare_params(req_entity.to_h) + validation = contract.call(post_params) - validation = contract.call(req_entity.to_h) if validation.success? - - post = command.call(validation.to_h) - - syndicate.call(validation.to_h).bind do |result| - source, url = result - add_post_syndication_source.call(post.id, source, url) + command.call(validation.to_h).bind do |post| + res.status = 201 + res.headers["Location"] = "#{settings.micropub_site_url}/#{post.post_type}/#{post.slug}" end - - res.status = 201 - res.headers.merge!( - "Location" => "#{settings.micropub_site_url}/#{post.post_type}/#{post.slug}" - ) else res.body = {error: validation.errors.to_h}.to_json res.status = 422 @@ -57,6 +51,12 @@ module Adamantium private + def prepare_params(post_params) + post = post_params.to_h + post[:slug] = post[:slug].empty? ? slugify.call(text: post[:name], checker: post_repo.method(:slug_exists?)) : post[:slug] + post + end + def resolve_operation(action) case action when "delete" diff --git a/app/commands/posts/create_bookmark.rb b/app/commands/posts/create_bookmark.rb index 04fef73..1c5c940 100644 --- a/app/commands/posts/create_bookmark.rb +++ b/app/commands/posts/create_bookmark.rb @@ -1,10 +1,22 @@ +require "dry/monads" + module Adamantium module Commands module Posts class CreateBookmark < Command - include Deps["repos.post_repo"] + include Deps["repos.post_repo", syndicate: "commands.posts.syndicate"] + + include Dry::Monads[:result] + def call(bookmark) - post_repo.create(bookmark) + created_bookmark = post_repo.create(bookmark) + + syndicate.call(bookmark).bind do |result| + source, url = result + add_post_syndication_source.call(created_bookmark.id, source, url) + end + + Success(created_bookmark) end end end diff --git a/app/commands/posts/create_entry.rb b/app/commands/posts/create_entry.rb index c3b3314..77c8cec 100644 --- a/app/commands/posts/create_entry.rb +++ b/app/commands/posts/create_entry.rb @@ -1,16 +1,29 @@ +require "dry/monads" + module Adamantium module Commands module Posts class CreateEntry < Command include Deps["repos.post_repo", "post_utilities.slugify", - renderer: "renderers.markdown" + renderer: "renderers.markdown", + syndicate: "commands.posts.syndicate", ] + + include Dry::Monads[:result] + def call(post) attrs = post.to_h attrs[:content] = renderer.call(content: attrs[:content]) - post_repo.create(attrs) + created_post = post_repo.create(attrs) + + syndicate.call(attrs).bind do |result| + source, url = result + add_post_syndication_source.call(created_post.id, source, url) + end + + Success(created_post) end end end diff --git a/app/commands/posts/update.rb b/app/commands/posts/update.rb index 5243df1..4863ad9 100644 --- a/app/commands/posts/update.rb +++ b/app/commands/posts/update.rb @@ -9,7 +9,9 @@ module Adamantium post = post_repo.fetch!(slug) if params.key? :replace - post_repo.update(post.id, {content: markdown.call(content: params[:replace][:content].first)}) + post_repo.update(post.id, { + content: markdown.call(content: params[:replace][:content].first) + }) end if params.key? :add @@ -17,7 +19,6 @@ module Adamantium end if params.key? :delete - end end end diff --git a/lib/adamantium/micropub_request_parser.rb b/lib/adamantium/micropub_request_parser.rb index a36f868..b0127e0 100644 --- a/lib/adamantium/micropub_request_parser.rb +++ b/lib/adamantium/micropub_request_parser.rb @@ -1,9 +1,5 @@ -require "securerandom" - module Adamantium class MicropubRequestParser - include Deps["post_utilities.slugify", "repos.post_repo"] - def call(params:) return nil if params.key?(:action) @@ -20,15 +16,6 @@ module Adamantium private - def slug(name:, default_slug:) - return default_slug if default_slug - - slugify.call( - text: name, - checker: post_repo.method(:slug_exists?) - ) - end - def content_type(params) return :bookmark if params[:"bookmark-of"] :post @@ -81,7 +68,7 @@ module Adamantium new_params[:content] = content end new_params[:url] = params[:"bookmark-of"] - new_params[:slug] = slug(name: new_params[:name], default_slug: params[:slug]) + new_params[:slug] = params[:slug] new_params end