Create code posts
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
ROM::SQL.migration do
|
||||
change do
|
||||
alter_table :posts do
|
||||
add_column :programming_language, :text
|
||||
end
|
||||
end
|
||||
end
|
@@ -13,8 +13,7 @@ module Main
|
||||
def raw_content
|
||||
Sanitize.fragment(content,
|
||||
elements: ["img", "p"],
|
||||
attributes: {"img" => ["alt", "src", "title"]}
|
||||
)
|
||||
attributes: {"img" => ["alt", "src", "title"]})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -24,6 +24,11 @@ article class="h-entry"
|
||||
a class="dark:text-gray-400" href="/places" places
|
||||
- if post.photos? || post.videos?
|
||||
a class="dark:text-gray-400" href="/photos" photos
|
||||
- if post.post_type == "code"
|
||||
pre
|
||||
code
|
||||
== post.content
|
||||
- else
|
||||
article class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200 prose-a:text-blue-600 prose-a:no-underline hover:prose-a:underline prose-img:rounded prose-video:rounded"
|
||||
div class="e-content prose-code:bg-pink-100 prose-code:text-pink-900"
|
||||
== post.content
|
||||
|
@@ -34,7 +34,7 @@ module Micropub
|
||||
|
||||
# create
|
||||
if req_entity && verify_scope(req: req, scope: :create)
|
||||
Dry::Matcher::ResultMatcher.(create_entry.call(req_entity: req_entity)) do |m|
|
||||
Dry::Matcher::ResultMatcher.call(create_entry.call(req_entity: req_entity)) do |m|
|
||||
m.success do |post|
|
||||
res.headers["Location"] = "#{settings.micropub_site_url}/#{post.value!.post_type}/#{post.value!.slug}"
|
||||
res.status = 201
|
||||
|
35
slices/micropub/commands/posts/create_code_post.rb
Normal file
35
slices/micropub/commands/posts/create_code_post.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
require "dry/monads"
|
||||
|
||||
module Micropub
|
||||
module Commands
|
||||
module Posts
|
||||
class CreateCodePost < Adamantium::Command
|
||||
include Deps["repos.post_repo",
|
||||
renderer: "renderers.markdown",
|
||||
syndicate: "commands.posts.syndicate",
|
||||
]
|
||||
|
||||
include Dry::Monads[:result]
|
||||
|
||||
def call(post)
|
||||
post_params = prepare_params(params: post)
|
||||
created_post = post_repo.create(post_params)
|
||||
|
||||
# syndicate.call(created_post.id, post)
|
||||
|
||||
# decorated_post = Decorators::Posts::Decorator.new(created_post)
|
||||
|
||||
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
|
@@ -7,10 +7,12 @@ module Micropub
|
||||
"validation.posts.bookmark_contract",
|
||||
"validation.posts.checkin_contract",
|
||||
"validation.posts.book_contract",
|
||||
"validation.posts.code_contract",
|
||||
"commands.posts.create_post",
|
||||
"commands.posts.create_bookmark",
|
||||
"commands.posts.create_checkin",
|
||||
"commands.posts.create_book_post"
|
||||
"commands.posts.create_book_post",
|
||||
"commands.posts.create_code_post"
|
||||
]
|
||||
|
||||
def call(entry_type:)
|
||||
@@ -21,6 +23,8 @@ module Micropub
|
||||
{command: create_checkin, validation: checkin_contract}
|
||||
in Entities::BookRequest
|
||||
{command: create_book_post, validation: book_contract}
|
||||
in Entities::CodeRequest
|
||||
{command: create_code_post, validation: code_contract}
|
||||
else
|
||||
{command: create_post, validation: post_contract}
|
||||
end
|
||||
|
15
slices/micropub/entities/code_request.rb
Normal file
15
slices/micropub/entities/code_request.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module Micropub
|
||||
module Entities
|
||||
class CodeRequest < Dry::Struct
|
||||
attribute :h, Types::Coercible::String
|
||||
attribute :action, Types::Coercible::String.optional
|
||||
attribute :name, Types::Coercible::String.optional
|
||||
attribute :content, Types::Coercible::String
|
||||
attribute :slug, Types::Coercible::String
|
||||
attribute :programming_language, Types::Coercible::String
|
||||
attribute :published_at, Types::Nominal::DateTime.optional
|
||||
attribute :post_type, Types::Coercible::String
|
||||
attribute :syndicate_to, Types::Array.of(Types::Coercible::String)
|
||||
end
|
||||
end
|
||||
end
|
@@ -13,7 +13,7 @@ module Micropub
|
||||
posts.transaction do
|
||||
new_post = posts.changeset(:create, post_attrs).commit
|
||||
|
||||
post_attrs[:category].each do |tag_name|
|
||||
post_attrs[:category]&.each do |tag_name|
|
||||
next if tag_name == ""
|
||||
|
||||
tag = posts.tags.where(label: tag_name).one ||
|
||||
|
@@ -16,6 +16,9 @@ module Micropub
|
||||
when :book
|
||||
book_params = parse_book_params(params)
|
||||
Entities::BookRequest.new(book_params)
|
||||
when :code
|
||||
code_params = parse_code_params(params)
|
||||
Entities::CodeRequest.new(code_params)
|
||||
else
|
||||
req_params = parse_post_params(req_type, cont_type, params)
|
||||
Entities::PostRequest.new(req_params)
|
||||
@@ -28,6 +31,7 @@ module Micropub
|
||||
return :bookmark if params[:"bookmark-of"]
|
||||
return :book if params.dig(:properties, :"read-of")
|
||||
return :checkin if params.dig(:properties, :checkin)
|
||||
return :code if params.dig(:properties, :programming_language)
|
||||
:post
|
||||
end
|
||||
|
||||
@@ -43,6 +47,21 @@ module Micropub
|
||||
nil
|
||||
end
|
||||
|
||||
def parse_code_params(params)
|
||||
new_params = {}
|
||||
new_params[:h] = "entry"
|
||||
new_params[:action] = params[:action]
|
||||
new_params[:content] = params[:properties][:content].first
|
||||
new_params[:slug] = SecureRandom.uuid
|
||||
new_params[:programming_language] = params[:properties][:programming_language]
|
||||
new_params[:published_at] = Time.now
|
||||
new_params[:post_type] = :code
|
||||
new_params[:name] = params[:properties][:name]&.first
|
||||
new_params[:syndicate_to] = Array(params[:properties][:"mp-syndicate-to"]) || []
|
||||
|
||||
new_params
|
||||
end
|
||||
|
||||
def parse_post_params(req_type, post_type, params)
|
||||
new_params = {}
|
||||
new_params[:h] = "entry"
|
||||
|
@@ -2,7 +2,6 @@ module Micropub
|
||||
module Validation
|
||||
module Posts
|
||||
class BookmarkContract < Dry::Validation::Contract
|
||||
|
||||
include Deps["repos.post_repo"]
|
||||
|
||||
params do
|
||||
|
17
slices/micropub/validation/posts/code_contract.rb
Normal file
17
slices/micropub/validation/posts/code_contract.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Micropub
|
||||
module Validation
|
||||
module Posts
|
||||
class CodeContract < Dry::Validation::Contract
|
||||
params do
|
||||
required(:name).maybe(:string)
|
||||
required(:content).filled(:string)
|
||||
required(:programming_language).filled(:string)
|
||||
required(:published_at).maybe(:time)
|
||||
required(:slug).filled(:string)
|
||||
required(:post_type).value(included_in?: %w[code])
|
||||
required(:syndicate_to).array(:string)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user