Create code posts
This commit is contained in:
@@ -12,7 +12,7 @@ ROM::SQL.migration do
|
|||||||
$$ language 'plpgsql';
|
$$ language 'plpgsql';
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
alter_table :pages do
|
alter_table :pages do
|
||||||
add_column :updated_at, :timestamp
|
add_column :updated_at, :timestamp
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ ROM::SQL.migration do
|
|||||||
|
|
||||||
down do
|
down do
|
||||||
execute("DROP FUNCTION IF EXISTS update_updated_at_column();")
|
execute("DROP FUNCTION IF EXISTS update_updated_at_column();")
|
||||||
|
|
||||||
run <<-SQL
|
run <<-SQL
|
||||||
DROP TRIGGER IF EXISTS update_pages_updated_at ON pages;
|
DROP TRIGGER IF EXISTS update_pages_updated_at ON pages;
|
||||||
SQL
|
SQL
|
||||||
|
@@ -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
|
@@ -12,9 +12,8 @@ module Main
|
|||||||
class Decorator < Main::Decorators::Posts::Decorator
|
class Decorator < Main::Decorators::Posts::Decorator
|
||||||
def raw_content
|
def raw_content
|
||||||
Sanitize.fragment(content,
|
Sanitize.fragment(content,
|
||||||
elements: ["img", "p"],
|
elements: ["img", "p"],
|
||||||
attributes: {"img" => ["alt", "src", "title"]}
|
attributes: {"img" => ["alt", "src", "title"]})
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -24,27 +24,32 @@ article class="h-entry"
|
|||||||
a class="dark:text-gray-400" href="/places" places
|
a class="dark:text-gray-400" href="/places" places
|
||||||
- if post.photos? || post.videos?
|
- if post.photos? || post.videos?
|
||||||
a class="dark:text-gray-400" href="/photos" photos
|
a class="dark:text-gray-400" href="/photos" photos
|
||||||
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"
|
- if post.post_type == "code"
|
||||||
div class="e-content prose-code:bg-pink-100 prose-code:text-pink-900"
|
pre
|
||||||
== post.content
|
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
|
||||||
|
|
||||||
- if post.photos?
|
- if post.photos?
|
||||||
- post.photos.each_with_index do |photo, idx|
|
- post.photos.each_with_index do |photo, idx|
|
||||||
figure id="photo-#{idx}"
|
figure id="photo-#{idx}"
|
||||||
img loading="lazy" class="u-photo shadow-solid shadow-pink-100 dark:shadow-pink-200 mb-4" src=photo["value"] alt=photo["alt"]
|
img loading="lazy" class="u-photo shadow-solid shadow-pink-100 dark:shadow-pink-200 mb-4" src=photo["value"] alt=photo["alt"]
|
||||||
figcaption
|
figcaption
|
||||||
= photo["alt"]
|
= photo["alt"]
|
||||||
- if post.videos?
|
- if post.videos?
|
||||||
- post.videos.each_with_index do |video, index|
|
- post.videos.each_with_index do |video, index|
|
||||||
figure id="video-#{index}"
|
figure id="video-#{index}"
|
||||||
video loop=false muted=true controls=true
|
video loop=false muted=true controls=true
|
||||||
source type="video/mp4" src="#{video["value"]}"
|
source type="video/mp4" src="#{video["value"]}"
|
||||||
figcaption= video["alt"]
|
figcaption= video["alt"]
|
||||||
a href="#" data-replay="video-#{index}" Replay
|
a href="#" data-replay="video-#{index}" Replay
|
||||||
|
|
||||||
|
|
||||||
- if post.location
|
- if post.location
|
||||||
img loading="lazy" class="shadow-solid shadow-pink-100 dark:shadow-pink-200 rounded mb-4" src=post.large_map
|
img loading="lazy" class="shadow-solid shadow-pink-100 dark:shadow-pink-200 rounded mb-4" src=post.large_map
|
||||||
-if post.webmentions && post.webmentions.count > 0
|
-if post.webmentions && post.webmentions.count > 0
|
||||||
div class="mt-12"
|
div class="mt-12"
|
||||||
h3 #{post.webmentions.count} Comment#{post.webmentions.count != 1 ? "s" : ""}
|
h3 #{post.webmentions.count} Comment#{post.webmentions.count != 1 ? "s" : ""}
|
||||||
|
@@ -34,7 +34,7 @@ module Micropub
|
|||||||
|
|
||||||
# create
|
# create
|
||||||
if req_entity && verify_scope(req: req, scope: :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|
|
m.success do |post|
|
||||||
res.headers["Location"] = "#{settings.micropub_site_url}/#{post.value!.post_type}/#{post.value!.slug}"
|
res.headers["Location"] = "#{settings.micropub_site_url}/#{post.value!.post_type}/#{post.value!.slug}"
|
||||||
res.status = 201
|
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.bookmark_contract",
|
||||||
"validation.posts.checkin_contract",
|
"validation.posts.checkin_contract",
|
||||||
"validation.posts.book_contract",
|
"validation.posts.book_contract",
|
||||||
|
"validation.posts.code_contract",
|
||||||
"commands.posts.create_post",
|
"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",
|
||||||
|
"commands.posts.create_code_post"
|
||||||
]
|
]
|
||||||
|
|
||||||
def call(entry_type:)
|
def call(entry_type:)
|
||||||
@@ -21,6 +23,8 @@ module Micropub
|
|||||||
{command: create_checkin, validation: checkin_contract}
|
{command: create_checkin, validation: checkin_contract}
|
||||||
in Entities::BookRequest
|
in Entities::BookRequest
|
||||||
{command: create_book_post, validation: book_contract}
|
{command: create_book_post, validation: book_contract}
|
||||||
|
in Entities::CodeRequest
|
||||||
|
{command: create_code_post, validation: code_contract}
|
||||||
else
|
else
|
||||||
{command: create_post, validation: post_contract}
|
{command: create_post, validation: post_contract}
|
||||||
end
|
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
|
posts.transaction do
|
||||||
new_post = posts.changeset(:create, post_attrs).commit
|
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 == ""
|
next if tag_name == ""
|
||||||
|
|
||||||
tag = posts.tags.where(label: tag_name).one ||
|
tag = posts.tags.where(label: tag_name).one ||
|
||||||
|
@@ -16,6 +16,9 @@ module Micropub
|
|||||||
when :book
|
when :book
|
||||||
book_params = parse_book_params(params)
|
book_params = parse_book_params(params)
|
||||||
Entities::BookRequest.new(book_params)
|
Entities::BookRequest.new(book_params)
|
||||||
|
when :code
|
||||||
|
code_params = parse_code_params(params)
|
||||||
|
Entities::CodeRequest.new(code_params)
|
||||||
else
|
else
|
||||||
req_params = parse_post_params(req_type, cont_type, params)
|
req_params = parse_post_params(req_type, cont_type, params)
|
||||||
Entities::PostRequest.new(req_params)
|
Entities::PostRequest.new(req_params)
|
||||||
@@ -28,6 +31,7 @@ module Micropub
|
|||||||
return :bookmark if params[:"bookmark-of"]
|
return :bookmark if params[:"bookmark-of"]
|
||||||
return :book if params.dig(:properties, :"read-of")
|
return :book if params.dig(:properties, :"read-of")
|
||||||
return :checkin if params.dig(:properties, :checkin)
|
return :checkin if params.dig(:properties, :checkin)
|
||||||
|
return :code if params.dig(:properties, :programming_language)
|
||||||
:post
|
:post
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -43,6 +47,21 @@ module Micropub
|
|||||||
nil
|
nil
|
||||||
end
|
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)
|
def parse_post_params(req_type, post_type, params)
|
||||||
new_params = {}
|
new_params = {}
|
||||||
new_params[:h] = "entry"
|
new_params[:h] = "entry"
|
||||||
@@ -52,24 +71,24 @@ module Micropub
|
|||||||
publish_time = params[:published_at] || Time.now
|
publish_time = params[:published_at] || Time.now
|
||||||
|
|
||||||
new_params = if req_type == :json
|
new_params = if req_type == :json
|
||||||
content = if params[:properties][:content]
|
content = if params[:properties][:content]
|
||||||
if params[:properties][:content].is_a?(Array) && params[:properties][:content].first.is_a?(Hash)
|
if params[:properties][:content].is_a?(Array) && params[:properties][:content].first.is_a?(Hash)
|
||||||
params[:properties][:content].first[:html]
|
params[:properties][:content].first[:html]
|
||||||
else
|
else
|
||||||
params[:properties][:content].first&.tr("\n", " ")
|
params[:properties][:content].first&.tr("\n", " ")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
photos = if params[:properties][:photo].is_a?(Array)
|
photos = if params[:properties][:photo].is_a?(Array)
|
||||||
params[:properties][:photo].map do |p|
|
params[:properties][:photo].map do |p|
|
||||||
{value: p, alt: ""}
|
{value: p, alt: ""}
|
||||||
end
|
end
|
||||||
elsif params[:properties][:photo].is_a?(Hash)
|
elsif params[:properties][:photo].is_a?(Hash)
|
||||||
params[:properties][:photo]
|
params[:properties][:photo]
|
||||||
elsif params[:properties][:photo]
|
elsif params[:properties][:photo]
|
||||||
{value: params[:properties][:photo], alt: ""}
|
{value: params[:properties][:photo], alt: ""}
|
||||||
else
|
else
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
new_params.merge({
|
new_params.merge({
|
||||||
published_at: (params[:"post-status"] == "draft") ? nil : publish_time,
|
published_at: (params[:"post-status"] == "draft") ? nil : publish_time,
|
||||||
@@ -83,7 +102,7 @@ module Micropub
|
|||||||
})
|
})
|
||||||
else
|
else
|
||||||
photos = if params[:photo].is_a?(String)
|
photos = if params[:photo].is_a?(String)
|
||||||
[{value: params[:photo], alt: ""}]
|
[{value: params[:photo], alt: ""}]
|
||||||
elsif params[:photo].nil?
|
elsif params[:photo].nil?
|
||||||
[]
|
[]
|
||||||
else
|
else
|
||||||
@@ -137,10 +156,10 @@ module Micropub
|
|||||||
new_params[:post_type] = :checkin
|
new_params[:post_type] = :checkin
|
||||||
|
|
||||||
location = if params.dig(:properties, :location)
|
location = if params.dig(:properties, :location)
|
||||||
params.dig(:properties, :location).first[:properties]
|
params.dig(:properties, :location).first[:properties]
|
||||||
elsif checkin.dig(:properties, :latitude) && checkin.dig(:properties, :longitude)
|
elsif checkin.dig(:properties, :latitude) && checkin.dig(:properties, :longitude)
|
||||||
{latitude: checkin.dig(:properties, :latitude), longitude: checkin.dig(:properties, :longitude)}
|
{latitude: checkin.dig(:properties, :latitude), longitude: checkin.dig(:properties, :longitude)}
|
||||||
end
|
end
|
||||||
|
|
||||||
new_params[:photos] = params.dig(:properties, :photo)&.map { |p| {value: p, alt: new_params[:name]} } || []
|
new_params[:photos] = params.dig(:properties, :photo)&.map { |p| {value: p, alt: new_params[:name]} } || []
|
||||||
new_params[:location] = "geo:#{location.dig(:latitude).first},#{location.dig(:longitude).first};u=0"
|
new_params[:location] = "geo:#{location.dig(:latitude).first},#{location.dig(:longitude).first};u=0"
|
||||||
|
@@ -2,7 +2,6 @@ module Micropub
|
|||||||
module Validation
|
module Validation
|
||||||
module Posts
|
module Posts
|
||||||
class BookmarkContract < Dry::Validation::Contract
|
class BookmarkContract < Dry::Validation::Contract
|
||||||
|
|
||||||
include Deps["repos.post_repo"]
|
include Deps["repos.post_repo"]
|
||||||
|
|
||||||
params do
|
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
|
@@ -47,8 +47,8 @@ RSpec.describe "Post creation", :db, :requests do
|
|||||||
type: ["h-entry"],
|
type: ["h-entry"],
|
||||||
properties: {
|
properties: {
|
||||||
content: [{
|
content: [{
|
||||||
html: "<p>This post has <b>bold</b> and <i>italic</i> text.</p>"
|
html: "<p>This post has <b>bold</b> and <i>italic</i> text.</p>"
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +191,7 @@ RSpec.describe "Post creation", :db, :requests do
|
|||||||
expect(last_response).to be_successful
|
expect(last_response).to be_successful
|
||||||
expect(post_repo.bookmark_listing.count).to eq 1
|
expect(post_repo.bookmark_listing.count).to eq 1
|
||||||
expect(post_repo.bookmark_listing.last.cached_content).to_not eq nil
|
expect(post_repo.bookmark_listing.last.cached_content).to_not eq nil
|
||||||
|
|
||||||
post "/micropub", params
|
post "/micropub", params
|
||||||
expect(last_response).to_not be_successful
|
expect(last_response).to_not be_successful
|
||||||
expect(post_repo.bookmark_listing.count).to eq 1
|
expect(post_repo.bookmark_listing.count).to eq 1
|
||||||
|
Reference in New Issue
Block a user