Refactor micropub specific things out to a slice

This commit is contained in:
2023-11-15 18:55:57 +11:00
parent 730ecb9ea4
commit 5b133363b3
63 changed files with 468 additions and 174 deletions

View File

@@ -0,0 +1,18 @@
module Micropub
module Commands
module Posts
class AddSyndicationSource
include Deps["repos.post_repo"]
def call(post_id, source, url)
post = post_repo.find!(post_id).to_h
syndication_sources = post[:syndication_sources] || {}
syndication_sources[source] = url
post[:syndication_sources] = syndication_sources
post_repo.update(post_id, post)
end
end
end
end
end

View File

@@ -0,0 +1,19 @@
require "dry/monads"
module Micropub
module Commands
module Posts
class CreateBookPost < Adamantium::Command
include Deps["repos.post_repo"]
include Dry::Monads[:result]
def call(post)
created_post = post_repo.create(post)
Success(created_post)
end
end
end
end
end

View File

@@ -0,0 +1,32 @@
require "dry/monads"
module Micropub
module Commands
module Posts
class CreateBookmark < Adamantium::Command
include Deps["repos.post_repo",
"post_utilities.page_cacher",
syndicate: "commands.posts.syndicate",
raindrop: "syndication.raindrop",
]
include Dry::Monads[:result]
def call(bookmark)
created_bookmark = post_repo.create(bookmark)
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(created_bookmark.id, cached_content: content)
end
end
Success(created_bookmark)
end
end
end
end
end

View File

@@ -0,0 +1,42 @@
require "dry/monads"
module Micropub
module Commands
module Posts
class CreateCheckin < Adamantium::Command
include Deps["repos.post_repo",
"post_utilities.slugify",
"logger",
renderer: "renderers.markdown",
add_post_syndication_source: "commands.posts.add_syndication_source"
]
include Dry::Monads[:result]
def call(post)
syndication_sources = post.delete(:syndication_sources)
post_params = prepare_params(params: post)
created_post = post_repo.create(post_params)
syndication_sources.each do |url|
add_post_syndication_source.call(created_post.id, :swarm, url)
end
# decorated_post = Decorators::Posts::Decorator.new(created_post)
# send_webmentions.call(post_content: attrs[: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]) if attrs[:content]
attrs
end
end
end
end
end

View File

@@ -0,0 +1,43 @@
require "dry/monads"
module Micropub
module Commands
module Posts
class CreateEntry < Adamantium::Command
include Deps["repos.post_repo",
"post_utilities.slugify",
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

@@ -0,0 +1,31 @@
module Micropub
module Commands
module Posts
class CreationResolver
include Deps[
"validation.posts.post_contract",
"validation.posts.bookmark_contract",
"validation.posts.checkin_contract",
"validation.posts.book_contract",
"commands.posts.create_entry",
"commands.posts.create_bookmark",
"commands.posts.create_checkin",
"commands.posts.create_book_post"
]
def call(entry_type:)
case entry_type
in Entities::BookmarkRequest
{command: create_bookmark, validation: bookmark_contract}
in Entities::CheckinRequest
{command: create_checkin, validation: checkin_contract}
in Entities::BookRequest
{command: create_book_post, validation: book_contract}
else
{command: create_entry, validation: post_contract}
end
end
end
end
end
end

View File

@@ -0,0 +1,14 @@
module Micropub
module Commands
module Posts
class Delete < Adamantium::Command
include Deps["repos.post_repo"]
def call(params:)
slug = URI(params[:url]).path.split("/").last
post_repo.delete!(slug)
end
end
end
end
end

View File

@@ -0,0 +1,18 @@
require "httparty"
require "que"
module Micropub
module Commands
module Posts
class SendWebmentions
include Deps["settings", "post_utilities.link_finder"]
def call(post_content:, post_url:)
Que.connection = Adamantium::Container["persistence.db"]
Adamantium::Jobs::SendWebMentions.enqueue(post_content: post_content, post_url: post_url)
end
end
end
end
end

View File

@@ -0,0 +1,51 @@
require "dry/monads"
require "dry/monads/do"
module Micropub
module Commands
module Posts
class Syndicate
include Dry::Monads[:result]
include Dry::Monads::Do.for(:call)
include Deps["settings",
"syndication.mastodon",
"syndication.blue_sky",
add_post_syndication_source: "commands.posts.add_syndication_source",
send_to_dayone: "syndication.dayone",
]
def call(post_id, post)
syndicate_to = syndication_targets(post[:syndicate_to])
if syndicate_to.include? :mastodon
res = mastodon.call(post: post)
add_post_syndication_source.call(post_id, :mastodon, res.value!) if res.success?
end
if syndicate_to.include? :blue_sky
res = blue_sky.call(post: post)
add_post_syndication_source.call(post_id, :blue_sky, res.value!) if res.success?
end
if post[:category].include? "weekly"
send_to_dayone.call(name: post[:name], content: post[:content])
end
Success()
end
private
def syndication_targets(syndicate_to)
targets = []
targets << :mastodon if syndicate_to.any? { |url| settings.mastodon_server.match(/#{url}/) }
targets << :blue_sky if syndicate_to.any? { |url| settings.blue_sky_url.match(/#{url}/) }
targets
end
end
end
end
end

View File

@@ -0,0 +1,14 @@
module Micropub
module Commands
module Posts
class Undelete < Adamantium::Command
include Deps["repos.post_repo"]
def call(params:)
slug = URI(params[:url]).path.split("/").last
post_repo.restore!(slug)
end
end
end
end
end

View File

@@ -0,0 +1,72 @@
module Micropub
module Commands
module Posts
class Update < Adamantium::Command
include Deps[
"repos.post_repo",
"renderers.markdown",
"commands.posts.add_syndication_source"
]
def call(params:)
slug = URI(params[:url]).path.split("/").last
post = post_repo.fetch!(slug)
if params.key? :replace
content = params[:replace].delete(:content).first
name = params[:replace].delete(:name)
attrs_to_replace = {}
attrs_to_replace[:name] = name if name
attrs_to_replace[:content] = markdown.call(content: content) if content
post_repo.update(post.id, attrs_to_replace)
end
if params.key? :add
attrs_to_add = {}
syndication = params[:add].delete(:syndication)&.first
tags = params[:add].delete(:category)
content = params[:add].delete(:content)&.first
name = params[:add].delete(:name)
attrs_to_add[:name] = name if post.name.empty?
attrs_to_add[:content] = markdown.call(content: content) if post.content.empty?
params[:add].keys.each_with_object(attrs_to_add) do |attr, memo|
memo[attr] = params[:add][attr].first if post.fetch(attr, nil).nil?
end
post_repo.update(post.id, attrs_to_add) unless attrs_to_add.empty?
post_repo.tag_post(post_id: post.id, tags: tags) if tags && !tags.empty?
add_syndication_source.call(post.id, "", syndication) if syndication && !syndication.empty?
end
if params.key? :delete
if params[:delete].is_a? Hash
tags = params[:delete][:category]
tags&.each do |tag|
post_repo.remove_tag(post_id: post.id, tag: tag)
end
elsif params[:delete].is_a? Array
if params[:delete].delete("category")
post.tags.each do |tag|
post_repo.remove_tag(post_id: post.id, tag: tag.label)
end
end
attrs = {}
params[:delete].each do |attr|
attrs[attr.to_sym] = nil
end
post_repo.update(post.id, attrs) unless attrs.empty?
end
end
end
end
end
end
end