Refactor post creation + syndication

This commit is contained in:
2023-02-12 21:58:18 +11:00
parent b3b287bbf4
commit 2b81839127
5 changed files with 46 additions and 33 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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