Refactor micropub specific things out to a slice
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Media
|
||||
class Create < Action
|
||||
include Deps["commands.media.upload"]
|
||||
before :authenticate!
|
||||
|
||||
def handle(req, res)
|
||||
data = req.params[:file]
|
||||
|
||||
halt 401 unless verify_scope(req: req, scope: :create) || verify_scope(req: req, scope: :media)
|
||||
|
||||
upload_result = upload.call(file: data)
|
||||
|
||||
res.status = 422 if upload_result.failure?
|
||||
|
||||
if upload_result.success?
|
||||
res.status = 201
|
||||
res.headers["Location"] = upload_result.value!
|
||||
res.headers["HX-Refresh"] = true
|
||||
res.body = {
|
||||
url: upload_result.value!
|
||||
}.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,29 +0,0 @@
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Media
|
||||
class Show < Action
|
||||
include Deps["settings"]
|
||||
|
||||
def handle(req, res)
|
||||
res.body = if req.params[:q] == "source"
|
||||
{
|
||||
items: media_url(req.params[:file])
|
||||
}.to_json
|
||||
else
|
||||
"Micropub media endpoint"
|
||||
end
|
||||
|
||||
res.status = 200
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def media_url(filename)
|
||||
pathname = Time.now.strftime("%m-%Y")
|
||||
|
||||
File.join(settings.micropub_site_url, "/media/", "/#{pathname}/", filename).to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,79 +0,0 @@
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Posts
|
||||
class Handle < Action
|
||||
before :authenticate!
|
||||
|
||||
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",
|
||||
add_post_syndication_source: "commands.posts.add_syndication_source"
|
||||
]
|
||||
|
||||
def handle(req, res)
|
||||
req_entity = post_param_parser.call(params: req.params.to_h)
|
||||
action = req.params[:action]
|
||||
|
||||
# delete, undelete, update
|
||||
if action
|
||||
perform_action(req: req, res: res, action: action)
|
||||
elsif req_entity # create
|
||||
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]
|
||||
post
|
||||
end
|
||||
|
||||
def resolve_operation(action)
|
||||
case action
|
||||
when "delete"
|
||||
[delete_post, ->(req) { verify_scope(req: req, scope: :delete) }]
|
||||
when "undelete"
|
||||
[undelete_post, ->(req) { verify_scope(req: req, scope: :undelete) }]
|
||||
when "update"
|
||||
[update_post, ->(req) { verify_scope(req: req, scope: :update) }]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,69 +0,0 @@
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Site
|
||||
class Config < Action
|
||||
include Deps["settings", "views.site.home", "queries.posts.microformat_post"]
|
||||
before :authenticate!
|
||||
|
||||
def handle(req, res)
|
||||
if req.params[:q] == "config"
|
||||
res.status = 200
|
||||
res.content_type = "application/json"
|
||||
res.body = {
|
||||
"media-endpoint" => settings.micropub_media_endpoint,
|
||||
"destination" => [
|
||||
{uid: settings.micropub_site_id, name: settings.micropub_site_name}
|
||||
],
|
||||
"post-types" => [
|
||||
{type: "note", name: "Note", properties: %w[content photo category]},
|
||||
{type: "article", name: "Article", properties: %w[name content category]},
|
||||
{type: "photo", name: "Photo", properties: %w[name photo content category]},
|
||||
{type: "video", name: "Video", properties: %w[name video content category]},
|
||||
{type: "bookmark", name: "Bookmark", properties: %w[name content category]}
|
||||
],
|
||||
"syndicate-to" => [
|
||||
{
|
||||
uid: "https://social.dnitza.com",
|
||||
name: "Mastodon"
|
||||
},
|
||||
{
|
||||
uid: "https://pinboard.in",
|
||||
name: "Pinboard"
|
||||
},
|
||||
{
|
||||
uid: "https://bsky.app",
|
||||
name: "Blue Sky"
|
||||
}
|
||||
]
|
||||
}.to_json
|
||||
elsif req.params[:q] == "syndicate-to"
|
||||
res.status = 200
|
||||
res.content_type = "Application/JSON"
|
||||
res.body = {
|
||||
"syndicate-to" => [
|
||||
{
|
||||
uid: "https://social.dnitza.com",
|
||||
name: "Mastodon"
|
||||
},
|
||||
{
|
||||
uid: "https://pinboard.in",
|
||||
name: "Pinboard"
|
||||
},
|
||||
{
|
||||
uid: "https://bsky.app",
|
||||
name: "Blue Sky"
|
||||
}
|
||||
]
|
||||
}.to_json
|
||||
elsif req.params[:q] == "source"
|
||||
res.status = 200
|
||||
res.content_type = "Application/JSON"
|
||||
res.body = microformat_post.call(url: req.params[:url], properties: req.params[:properties]).to_json
|
||||
else
|
||||
res.render home
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,32 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Webmentions
|
||||
class Create < Adamantium::Action
|
||||
include Deps["repos.webmentions_repo",
|
||||
"repos.post_repo",
|
||||
webmention_parser: "param_parser.webmention"
|
||||
]
|
||||
|
||||
def handle(req, res)
|
||||
webmention = webmention_parser.call(params: req.params)
|
||||
case webmention
|
||||
in Success[:reply, reply]
|
||||
slug = req.params[:"in-reply-to"].split("/").last
|
||||
post = post_repo.fetch!(slug)
|
||||
|
||||
reply[:post_id] = post.id
|
||||
|
||||
webmentions_repo.create(reply)
|
||||
res.status = 201
|
||||
in Failure(:invalid_request)
|
||||
res.status = 429
|
||||
in Failure(:not_implemented)
|
||||
res.status = 429
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user