Cleanup and refactoring

This commit is contained in:
2024-02-24 08:51:25 +11:00
parent 96c1df617f
commit 2c11fda9ce
5 changed files with 118 additions and 81 deletions

View File

@@ -25,7 +25,8 @@ module Adamantium
latdiff = maxlat - minlat latdiff = maxlat - minlat
londiff = maxlon - minlon londiff = maxlon - minlon
svg = Gnuplot.open do |gp| svg = with_silent_output do
Gnuplot.open do |gp|
Gnuplot::Plot.new(gp) do |plot| Gnuplot::Plot.new(gp) do |plot|
plot.arbitrary_lines << "unset border" plot.arbitrary_lines << "unset border"
plot.arbitrary_lines << "unset xtics" plot.arbitrary_lines << "unset xtics"
@@ -42,11 +43,22 @@ module Adamantium
end end
end end
end end
end
svg.gsub!('width="600" height="480"', 'width="100%" height="100%"') svg.gsub!('width="600" height="480"', 'width="100%" height="100%"')
Success({svg: svg, distance: gpx.distance(units: "kilometers"), duration: gpx.duration}) Success({svg: svg, distance: gpx.distance(units: "kilometers"), duration: gpx.duration})
end end
private
define_method(:with_silent_output) do |&block|
orig_verbose = $VERBOSE
$VERBOSE = nil
result = block.call
$VERBOSE = orig_verbose
result
end
end end
end end
end end

View File

@@ -6,10 +6,8 @@ module Micropub
include Deps[ include Deps[
"settings", "settings",
"post_utilities.slugify", "commands.posts.create_entry",
"repos.post_repo",
post_param_parser: "param_parser.micropub_post", post_param_parser: "param_parser.micropub_post",
create_resolver: "commands.posts.creation_resolver",
delete_post: "commands.posts.delete", delete_post: "commands.posts.delete",
undelete_post: "commands.posts.undelete", undelete_post: "commands.posts.undelete",
update_post: "commands.posts.update", update_post: "commands.posts.update",
@@ -18,52 +16,46 @@ module Micropub
def handle(req, res) def handle(req, res)
req_entity = post_param_parser.call(params: req.params.to_h) req_entity = post_param_parser.call(params: req.params.to_h)
action = req.params[:action] action_type = req.params[:action]
# delete, undelete, update # delete, undelete, update
if action if action_type
perform_action(req: req, res: res, action: action) case resolve_command(req: req, action_type: action_type)
elsif req_entity # create in Success[command]
create_entry(req: req, res: res, req_entity: req_entity) command.call(params: req.params.to_h)
res.status = 200
in Failure[:not_permitted]
halt 401
end
end
# create
if req_entity && verify_scope(req: req, scope: :create)
create_entry.call(req_entity: req_entity) do |m|
m.success do |post|
res.status = 201
res.headers["Location"] = "#{settings.micropub_site_url}/#{post.post_type}/#{post.slug}"
end
m.failure do |validation|
res.body = {error: validation.errors.to_h}.to_json
res.status = 422
end
end
end end
end end
private private
def create_entry(req:, res:, req_entity:) def resolve_command(req:, action_type:)
halt 401 unless verify_scope(req: req, scope: :create) command, permission_check = commands(action_type)
command, contract = create_resolver.call(entry_type: req_entity).values_at(:command, :validation) return Failure(:not_permitted) unless permission_check.call(req)
post_params = prepare_params(req_entity.to_h)
validation = contract.call(post_params)
if validation.success? Success(command)
command.call(validation.to_h).bind do |post|
res.status = 201
res.headers["Location"] = "#{settings.micropub_site_url}/#{post.post_type}/#{post.slug}"
end
else
res.body = {error: validation.errors.to_h}.to_json
res.status = 422
end
end end
def perform_action(req:, res:, action:) def commands(action)
operation, permission_check = resolve_operation(action)
halt 401 unless permission_check.call(req)
operation.call(params: req.params.to_h)
res.status = 200
end
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 case action
when "delete" when "delete"
[delete_post, ->(req) { verify_scope(req: req, scope: :delete) }] [delete_post, ->(req) { verify_scope(req: req, scope: :delete) }]

View File

@@ -1,41 +1,32 @@
require "dry/monads"
module Micropub module Micropub
module Commands module Commands
module Posts module Posts
class CreateEntry < Adamantium::Command class CreateEntry < Adamantium::Command
include Deps["repos.post_repo", include Deps[
"post_utilities.slugify", "post_utilities.slugify",
renderer: "renderers.markdown", "repos.post_repo",
syndicate: "commands.posts.syndicate", create_resolver: "commands.posts.creation_resolver"
send_to_dayone: "syndication.dayone",
send_webmentions: "commands.posts.send_webmentions",
auto_tag: "commands.auto_tagging.tag",
] ]
include Dry::Monads[:result] def call(req_entity:)
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)
def call(post) if validation.success?
post_params = prepare_params(params: post) post = command.call(validation.to_h)
created_post = post_repo.create(post_params) Success(post)
else
auto_tag.call Failure(validation)
end
syndicate.call(created_post.id, post)
decorated_post = Decorators::Posts::Decorator.new(created_post)
send_webmentions.call(post_content: created_post.content, post_url: decorated_post.permalink)
Success(created_post)
end end
private private
def prepare_params(params:) def prepare_params(post_params)
attrs = params.to_h post = post_params.to_h
attrs[:content] = renderer.call(content: attrs[:content]) post[:slug] = post[:slug].empty? ? slugify.call(text: post[:name], checker: post_repo.method(:slug_exists?)) : post[:slug]
attrs post
end end
end end
end end

View File

@@ -0,0 +1,42 @@
require "dry/monads"
module Micropub
module Commands
module Posts
class CreatePost < Adamantium::Command
include Deps["repos.post_repo",
renderer: "renderers.markdown",
syndicate: "commands.posts.syndicate",
send_to_dayone: "syndication.dayone",
send_webmentions: "commands.posts.send_webmentions",
auto_tag: "commands.auto_tagging.tag",
]
include Dry::Monads[:result]
def call(post)
post_params = prepare_params(params: post)
created_post = post_repo.create(post_params)
auto_tag.call
syndicate.call(created_post.id, post)
decorated_post = Decorators::Posts::Decorator.new(created_post)
send_webmentions.call(post_content: created_post.content, post_url: decorated_post.permalink)
Success(created_post)
end
private
def prepare_params(params:)
attrs = params.to_h
attrs[:content] = renderer.call(content: attrs[:content])
attrs
end
end
end
end
end

View File

@@ -7,7 +7,7 @@ module Micropub
"validation.posts.bookmark_contract", "validation.posts.bookmark_contract",
"validation.posts.checkin_contract", "validation.posts.checkin_contract",
"validation.posts.book_contract", "validation.posts.book_contract",
"commands.posts.create_entry", "commands.posts.create_post",
"commands.posts.create_bookmark", "commands.posts.create_bookmark",
"commands.posts.create_checkin", "commands.posts.create_checkin",
"commands.posts.create_book_post" "commands.posts.create_book_post"
@@ -22,7 +22,7 @@ module Micropub
in Entities::BookRequest in Entities::BookRequest
{command: create_book_post, validation: book_contract} {command: create_book_post, validation: book_contract}
else else
{command: create_entry, validation: post_contract} {command: create_post, validation: post_contract}
end end
end end
end end