Refactor syndication

This commit is contained in:
2023-04-09 20:30:04 +10:00
parent d22edceadc
commit 4544baddd6
10 changed files with 553 additions and 299 deletions

View File

@@ -22,35 +22,41 @@ module Adamantium
# delete, undelete, update
if action
operation, permission_check = resolve_operation(action)
if permission_check.call(req)
operation.call(params: req.params.to_h)
res.status = 200
else
res.status = 401
end
perform_action(req: req, res: res, action: action)
elsif req_entity # create
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)
if validation.success?
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
create_entry(req: req, res: res, req_entity: req_entity)
end
end
private
def create_entry(req:, res:, req_entity:)
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)
if validation.success?
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
def perform_action(req:, res:, 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]

View File

@@ -5,7 +5,6 @@ module Adamantium
module Posts
class CreateBookmark < Command
include Deps["repos.post_repo",
add_post_syndication_source: "commands.posts.add_syndication_source",
syndicate: "commands.posts.syndicate"
]
@@ -14,10 +13,7 @@ module Adamantium
def call(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
syndicate.call(created_bookmark.id, bookmark)
Success(created_bookmark)
end

View File

@@ -9,7 +9,6 @@ module Adamantium
renderer: "renderers.markdown",
syndicate: "commands.posts.syndicate",
send_to_dayone: "syndication.dayone",
add_post_syndication_source: "commands.posts.add_syndication_source",
send_webmentions: "commands.posts.send_webmentions",
]
@@ -20,13 +19,7 @@ module Adamantium
created_post = post_repo.create(post_params)
send_to_dayone.call(name: post.name, content: post.content) if post[:category].include? "weekly"
syndicate.call(post).bind do |results|
results.each do |result|
source, url = result
add_post_syndication_source.call(created_post.id, source, url)
end
end
syndicate.call(created_post.id, post)
# decorated_post = Decorators::Posts::Decorator.new(created_post)

View File

@@ -10,27 +10,24 @@ module Adamantium
include Deps["settings",
"syndication.mastodon",
"syndication.pinboard"
add_post_syndication_source: "commands.posts.add_syndication_source",
send_to_dayone: "syndication.dayone",
]
def call(post)
def call(post_id, post)
syndicate_to = syndication_targets(post[:syndicate_to])
syndicated_to = []
if syndicate_to.include? :mastodon
res = mastodon.call(post: post)
syndicated_to << [:mastodon, res.value!] if res.success?
add_post_syndication_source.call(post_id, :mastodon, res.value!) if res.success?
end
if syndicate_to.include? :pinboard
res = pinboard.call(post: post)
syndicated_to << [:pinboard, res.value!] if res.success?
if post[:category].include? "weekly"
send_to_dayone.call(name: post.name, content: post.content)
end
return Success(syndicated_to) unless syndicated_to.empty?
Failure(:no_syndication_targets)
Success()
end
private
@@ -38,7 +35,6 @@ module Adamantium
def syndication_targets(syndicate_to)
targets = []
targets << :mastodon if syndicate_to.any? { |url| settings.mastodon_server.match(/#{url}/) }
targets << :pinboard if syndicate_to.any? { |url| "https://pinboard.in".match(/#{url}/) }
targets
end
end