diff --git a/app/commands/.keep b/app/commands/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/entities/.keep b/app/entities/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/repos/post_repo.rb b/app/repos/post_repo.rb index 03e000e..921b41f 100644 --- a/app/repos/post_repo.rb +++ b/app/repos/post_repo.rb @@ -3,86 +3,6 @@ module Adamantium class PostRepo < Adamantium::Repo[:posts] Sequel.extension :pg_json Sequel.extension :pg_json_ops - commands update: :by_pk - - def create(post_attrs) - posts.transaction do - new_post = posts.changeset(:create, post_attrs).commit - - post_attrs[:category].each do |tag_name| - next if tag_name == "" - - tag = posts.tags.where(label: tag_name).one || - posts - .tags - .changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")}) - .commit - - posts.post_tags.changeset(:create, { - post_id: new_post.id, - tag_id: tag[:id] - }) - .commit - end - - new_post - end - end - - def tag_post(post_id:, tags:) - tags.each do |tag_name| - next if tag_name == "" - - tag = posts.tags.where(label: tag_name).one || - posts - .tags - .changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")}) - .commit - - posts.post_tags.changeset(:create, { - post_id: post_id, - tag_id: tag[:id] - }) - .commit - end - end - - def auto_tag_post(post_id:, tag_id:) - return if posts - .post_tags - .where( - post_id: post_id, - tag_id: tag_id - ).count > 0 - - posts - .post_tags - .changeset(:create, { - post_id: post_id, - tag_id: tag_id - }) - .commit - end - - def by_title(title_contains:) - posts - .where(post_type: "post") - .published - .where(Sequel.ilike(:name, "%#{title_contains}%")).to_a - end - - def by_content(body_contains:) - posts - .where(post_type: "post") - .published - .where(Sequel.ilike(:content, "%#{body_contains}%")).to_a - end - - def remove_tag(post_id:, tag:) - tag = posts.tags.where(label: tag).one - - posts.post_tags.where(post_id: post_id, tag_id: tag[:id]).changeset(:delete).commit if tag - end def by_year(year:) posts @@ -250,35 +170,12 @@ module Adamantium .one! end - def fetch_unpublished!(slug) - posts - .combine(:tags) - .where(slug: slug) - .one! - end - def find!(id) posts .by_pk(id) .one! end - def slug_exists?(slug) - !!posts - .where(slug: slug) - .one - end - - def delete!(slug) - delete_post = posts.where(slug: slug).command(:update) - delete_post.call(published_at: nil) - end - - def restore!(slug) - delete_post = posts.where(slug: slug).command(:update) - delete_post.call(published_at: Time.now) - end - def post_years posts .where(post_type: "post", location: nil) diff --git a/config/app.rb b/config/app.rb index aba974a..e4ed3c6 100644 --- a/config/app.rb +++ b/config/app.rb @@ -23,9 +23,16 @@ module Adamantium config.logger.stream = "log/hanami.log" config.shared_app_component_keys += [ + "post_utilities.slugify", "syndication.dayone", + "syndication.mastodon", + "syndication.blue_sky", + "syndication.raindrop", "renderers.markdown", - "post_utilities.link_finder" + "post_utilities.link_finder", + "param_parser.micropub_post", + "param_parser.webmention", + "post_utilities.page_cacher" ] end end diff --git a/config/providers/param_parser.rb b/config/providers/param_parser.rb index 4e88582..ae92a80 100644 --- a/config/providers/param_parser.rb +++ b/config/providers/param_parser.rb @@ -2,7 +2,7 @@ Hanami.app.register_provider :param_parser, namespace: true do start do - register "micropub_post", Adamantium::MicropubRequestParser.new + register "micropub_post", Micropub::RequestParser.new register "webmention", Adamantium::WebmentionRequestParser.new end end diff --git a/config/routes.rb b/config/routes.rb index 1d35b4a..dd57573 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,14 +9,7 @@ module Adamantium use Hanami::Middleware::BodyParser, [:form, :json] # use Adamantium::Middleware::ProcessParams - scope "micropub" do - get "/", to: "site.config" - post "/", to: "posts.handle" - post "/media", to: "media.create" - get "/media", to: "media.show" - - post "/webmentions", to: "webmentions.create" - end + slice :micropub, at: "/micropub" get "/", to: "site.home" get "/post/top_tracks/:slug", to: "posts.top_tracks" diff --git a/slices/micropub/action.rb b/slices/micropub/action.rb new file mode 100644 index 0000000..b2a4c3d --- /dev/null +++ b/slices/micropub/action.rb @@ -0,0 +1,7 @@ +# auto_register: false +# frozen_string_literal: true + +module Micropub + class Action < Adamantium::Action + end +end diff --git a/slices/micropub/actions/.keep b/slices/micropub/actions/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/actions/media/create.rb b/slices/micropub/actions/media/create.rb similarity index 97% rename from app/actions/media/create.rb rename to slices/micropub/actions/media/create.rb index 69246f3..940b0fa 100644 --- a/app/actions/media/create.rb +++ b/slices/micropub/actions/media/create.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Actions module Media class Create < Action diff --git a/app/actions/media/show.rb b/slices/micropub/actions/media/show.rb similarity index 97% rename from app/actions/media/show.rb rename to slices/micropub/actions/media/show.rb index 2a0227a..04b54c6 100644 --- a/app/actions/media/show.rb +++ b/slices/micropub/actions/media/show.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Actions module Media class Show < Action diff --git a/app/actions/posts/handle.rb b/slices/micropub/actions/posts/handle.rb similarity index 99% rename from app/actions/posts/handle.rb rename to slices/micropub/actions/posts/handle.rb index f7216b4..faf9d78 100644 --- a/app/actions/posts/handle.rb +++ b/slices/micropub/actions/posts/handle.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Actions module Posts class Handle < Action diff --git a/app/actions/site/config.rb b/slices/micropub/actions/site/config.rb similarity index 94% rename from app/actions/site/config.rb rename to slices/micropub/actions/site/config.rb index 270b98b..60c3f8a 100644 --- a/app/actions/site/config.rb +++ b/slices/micropub/actions/site/config.rb @@ -1,8 +1,8 @@ -module Adamantium +module Micropub module Actions module Site class Config < Action - include Deps["settings", "views.site.home", "queries.posts.microformat_post"] + include Deps["settings", "queries.posts.microformat_post"] before :authenticate! def handle(req, res) @@ -60,7 +60,7 @@ module Adamantium res.content_type = "Application/JSON" res.body = microformat_post.call(url: req.params[:url], properties: req.params[:properties]).to_json else - res.render home + res.redirect_to "/" end end end diff --git a/app/actions/webmentions/create.rb b/slices/micropub/actions/webmentions/create.rb similarity index 97% rename from app/actions/webmentions/create.rb rename to slices/micropub/actions/webmentions/create.rb index 71c5dd5..9c6080e 100644 --- a/app/actions/webmentions/create.rb +++ b/slices/micropub/actions/webmentions/create.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module Adamantium +module Micropub module Actions module Webmentions class Create < Adamantium::Action diff --git a/slices/micropub/assets/css/app.css b/slices/micropub/assets/css/app.css new file mode 100644 index 0000000..b5ed0f7 --- /dev/null +++ b/slices/micropub/assets/css/app.css @@ -0,0 +1,5 @@ +body { + background-color: #fff; + color: #000; + font-family: sans-serif; +} diff --git a/slices/micropub/assets/js/app.js b/slices/micropub/assets/js/app.js new file mode 100644 index 0000000..0692b84 --- /dev/null +++ b/slices/micropub/assets/js/app.js @@ -0,0 +1 @@ +import "../css/app.css"; diff --git a/app/commands/auto_tagging/tag.rb b/slices/micropub/commands/auto_tagging/tag.rb similarity index 97% rename from app/commands/auto_tagging/tag.rb rename to slices/micropub/commands/auto_tagging/tag.rb index adab784..258901d 100644 --- a/app/commands/auto_tagging/tag.rb +++ b/slices/micropub/commands/auto_tagging/tag.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Commands module AutoTagging class Tag diff --git a/app/commands/media/upload.rb b/slices/micropub/commands/media/upload.rb similarity index 98% rename from app/commands/media/upload.rb rename to slices/micropub/commands/media/upload.rb index b0162bd..4c58458 100644 --- a/app/commands/media/upload.rb +++ b/slices/micropub/commands/media/upload.rb @@ -6,10 +6,10 @@ require "filemagic" require "image_processing/vips" require "open3" -module Adamantium +module Micropub module Commands module Media - class Upload < Command + class Upload < Adamantium::Command include Deps["settings"] include Dry::Monads[:result] diff --git a/app/commands/posts/add_syndication_source.rb b/slices/micropub/commands/posts/add_syndication_source.rb similarity index 96% rename from app/commands/posts/add_syndication_source.rb rename to slices/micropub/commands/posts/add_syndication_source.rb index c04afab..1c0eafe 100644 --- a/app/commands/posts/add_syndication_source.rb +++ b/slices/micropub/commands/posts/add_syndication_source.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Commands module Posts class AddSyndicationSource diff --git a/app/commands/posts/create_book_post.rb b/slices/micropub/commands/posts/create_book_post.rb similarity index 81% rename from app/commands/posts/create_book_post.rb rename to slices/micropub/commands/posts/create_book_post.rb index f84889c..30d88cc 100644 --- a/app/commands/posts/create_book_post.rb +++ b/slices/micropub/commands/posts/create_book_post.rb @@ -1,9 +1,9 @@ require "dry/monads" -module Adamantium +module Micropub module Commands module Posts - class CreateBookPost < Command + class CreateBookPost < Adamantium::Command include Deps["repos.post_repo"] include Dry::Monads[:result] diff --git a/app/commands/posts/create_bookmark.rb b/slices/micropub/commands/posts/create_bookmark.rb similarity index 92% rename from app/commands/posts/create_bookmark.rb rename to slices/micropub/commands/posts/create_bookmark.rb index a7d3b73..5c722fa 100644 --- a/app/commands/posts/create_bookmark.rb +++ b/slices/micropub/commands/posts/create_bookmark.rb @@ -1,9 +1,9 @@ require "dry/monads" -module Adamantium +module Micropub module Commands module Posts - class CreateBookmark < Command + class CreateBookmark < Adamantium::Command include Deps["repos.post_repo", "post_utilities.page_cacher", syndicate: "commands.posts.syndicate", diff --git a/app/commands/posts/create_checkin.rb b/slices/micropub/commands/posts/create_checkin.rb similarity index 94% rename from app/commands/posts/create_checkin.rb rename to slices/micropub/commands/posts/create_checkin.rb index 28c13bb..0d5bfff 100644 --- a/app/commands/posts/create_checkin.rb +++ b/slices/micropub/commands/posts/create_checkin.rb @@ -1,9 +1,9 @@ require "dry/monads" -module Adamantium +module Micropub module Commands module Posts - class CreateCheckin < Command + class CreateCheckin < Adamantium::Command include Deps["repos.post_repo", "post_utilities.slugify", "logger", diff --git a/app/commands/posts/create_entry.rb b/slices/micropub/commands/posts/create_entry.rb similarity index 94% rename from app/commands/posts/create_entry.rb rename to slices/micropub/commands/posts/create_entry.rb index 079987f..1a4f0bc 100644 --- a/app/commands/posts/create_entry.rb +++ b/slices/micropub/commands/posts/create_entry.rb @@ -1,9 +1,9 @@ require "dry/monads" -module Adamantium +module Micropub module Commands module Posts - class CreateEntry < Command + class CreateEntry < Adamantium::Command include Deps["repos.post_repo", "post_utilities.slugify", renderer: "renderers.markdown", diff --git a/app/commands/posts/creation_resolver.rb b/slices/micropub/commands/posts/creation_resolver.rb similarity index 98% rename from app/commands/posts/creation_resolver.rb rename to slices/micropub/commands/posts/creation_resolver.rb index 9728d51..91e2167 100644 --- a/app/commands/posts/creation_resolver.rb +++ b/slices/micropub/commands/posts/creation_resolver.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Commands module Posts class CreationResolver diff --git a/app/commands/posts/delete.rb b/slices/micropub/commands/posts/delete.rb similarity index 80% rename from app/commands/posts/delete.rb rename to slices/micropub/commands/posts/delete.rb index 6bfff30..ba85726 100644 --- a/app/commands/posts/delete.rb +++ b/slices/micropub/commands/posts/delete.rb @@ -1,7 +1,7 @@ -module Adamantium +module Micropub module Commands module Posts - class Delete < Command + class Delete < Adamantium::Command include Deps["repos.post_repo"] def call(params:) slug = URI(params[:url]).path.split("/").last diff --git a/app/commands/posts/send_webmentions.rb b/slices/micropub/commands/posts/send_webmentions.rb similarity index 95% rename from app/commands/posts/send_webmentions.rb rename to slices/micropub/commands/posts/send_webmentions.rb index 63ca890..2ab7579 100644 --- a/app/commands/posts/send_webmentions.rb +++ b/slices/micropub/commands/posts/send_webmentions.rb @@ -1,7 +1,7 @@ require "httparty" require "que" -module Adamantium +module Micropub module Commands module Posts class SendWebmentions diff --git a/app/commands/posts/syndicate.rb b/slices/micropub/commands/posts/syndicate.rb similarity index 98% rename from app/commands/posts/syndicate.rb rename to slices/micropub/commands/posts/syndicate.rb index cef0143..677d478 100644 --- a/app/commands/posts/syndicate.rb +++ b/slices/micropub/commands/posts/syndicate.rb @@ -1,7 +1,7 @@ require "dry/monads" require "dry/monads/do" -module Adamantium +module Micropub module Commands module Posts class Syndicate diff --git a/app/commands/posts/undelete.rb b/slices/micropub/commands/posts/undelete.rb similarity index 79% rename from app/commands/posts/undelete.rb rename to slices/micropub/commands/posts/undelete.rb index 40086a7..2491834 100644 --- a/app/commands/posts/undelete.rb +++ b/slices/micropub/commands/posts/undelete.rb @@ -1,7 +1,7 @@ -module Adamantium +module Micropub module Commands module Posts - class Undelete < Command + class Undelete < Adamantium::Command include Deps["repos.post_repo"] def call(params:) slug = URI(params[:url]).path.split("/").last diff --git a/app/commands/posts/update.rb b/slices/micropub/commands/posts/update.rb similarity index 97% rename from app/commands/posts/update.rb rename to slices/micropub/commands/posts/update.rb index 57d8fdd..cb8892b 100644 --- a/app/commands/posts/update.rb +++ b/slices/micropub/commands/posts/update.rb @@ -1,7 +1,7 @@ -module Adamantium +module Micropub module Commands module Posts - class Update < Command + class Update < Adamantium::Command include Deps[ "repos.post_repo", "renderers.markdown", diff --git a/app/commands/workouts/create.rb b/slices/micropub/commands/workouts/create.rb similarity index 100% rename from app/commands/workouts/create.rb rename to slices/micropub/commands/workouts/create.rb diff --git a/slices/micropub/config/routes.rb b/slices/micropub/config/routes.rb new file mode 100644 index 0000000..b90dca5 --- /dev/null +++ b/slices/micropub/config/routes.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "hanami/middleware/body_parser" + +module Micropub + class Routes < Hanami::Routes + use Hanami::Middleware::BodyParser, [:form, :json] + + get "/", to: "site.config" + post "/", to: "posts.handle" + post "/media", to: "media.create" + get "/media", to: "media.show" + + post "/webmentions", to: "webmentions.create" + end +end diff --git a/slices/micropub/decorators/posts/decorator.rb b/slices/micropub/decorators/posts/decorator.rb new file mode 100644 index 0000000..c9bb39b --- /dev/null +++ b/slices/micropub/decorators/posts/decorator.rb @@ -0,0 +1,167 @@ +# frozen_string_literal: false + +# auto_register: false + +require "rexml/parsers/pullparser" +require "sanitize" + +module Micropub + module Decorators + module Posts + class Decorator < SimpleDelegator + def syndicated? + !syndication_sources.empty? + end + + def syndicated_to + syndication_sources.map do |source, url| + { + location: source, + url: url + } + end + end + + def photos? + __getobj__.photos.count { |p| !p["value"].end_with?("mp4") } > 0 + end + + def photos + __getobj__.photos.select { |p| !p["value"].end_with?("mp4") } + end + + def videos? + __getobj__.photos.count { |p| p["value"].end_with?("mp4") } > 0 + end + + def videos + __getobj__.photos.select { |p| p["value"].end_with?("mp4") } + end + + def prefix_emoji + if name + "" + elsif photos? && content == "" + "📷" + else + "💬" + end + end + + def display_title + title = name + "#{prefix_emoji} #{title}" + end + + def display_published_at + published_at.strftime("%e %B, %Y") + end + + def machine_published_at + published_at.rfc2822 + end + + def feed_content + photos? ? "
#{photos.map { |p| "" }.join("")} #{content}
" : content + end + + def raw_content + Sanitize.fragment(content) + end + + def excerpt + name ? truncate_html(content, 240, true) : content + end + + def permalink + "#{Hanami.app.settings.micropub_site_url}/post/#{slug}" + end + + def lat + geo[0] + end + + def lon + geo[1] + end + + def small_map + "https://api.mapbox.com/styles/v1/dnitza/cleb2o734000k01pbifls5620/static/pin-s+555555(#{lon},#{lat})/#{lon},#{lat},14,0/200x100@2x?access_token=pk.eyJ1IjoiZG5pdHphIiwiYSI6ImNsZWIzOHFmazBkODIzdm9kZHgxdDF4ajQifQ.mSneE-1SKeju8AOz5gp4BQ" + end + + def large_map + "https://api.mapbox.com/styles/v1/dnitza/cleb2o734000k01pbifls5620/static/pin-s+555555(#{lon},#{lat})/#{lon},#{lat},14,0/620x310@2x?access_token=pk.eyJ1IjoiZG5pdHphIiwiYSI6ImNsZWIzOHFmazBkODIzdm9kZHgxdDF4ajQifQ.mSneE-1SKeju8AOz5gp4BQ" + end + + def template_type + :post + end + + def posted_in + if name.nil? + :statuses + elsif post_type.to_sym == :book + :bookshelf + elsif location.nil? + :posts + else + :places + end + end + + def trips + __getobj__.trips + end + + private + + # e.g. geo:-37.75188,144.90417;u=35 + def geo + loc = location.split(":")[1] + p = loc.split(";")[0] + + p.split(",") + end + + def truncate_html(content, len = 30, at_end = nil) + return content if content.to_s.length <= len + + p = REXML::Parsers::PullParser.new(content) + tags = [] + new_len = len + results = "" + while p.has_next? && new_len > 0 + p_e = p.pull + case p_e.event_type + when :start_element + tags.push p_e[0] + results << "<#{tags.last}#{attrs_to_s(p_e[1])}>" + when :end_element + results << "" + when :text + results << p_e[0][0..new_len] + new_len -= p_e[0].length + else + results << "" + end + end + if at_end + results << "..." + end + tags.reverse_each do |tag| + results << "" + end + results + end + + def attrs_to_s(attrs) + if attrs.empty? + "" + else + " " + attrs.to_a.map { |attr| %(#{attr[0]}="#{attr[1]}") }.join(" ") + end + end + end + end + end +end diff --git a/app/entities/auto_tagging.rb b/slices/micropub/entities/auto_tagging.rb similarity index 57% rename from app/entities/auto_tagging.rb rename to slices/micropub/entities/auto_tagging.rb index 566feb9..927f191 100644 --- a/app/entities/auto_tagging.rb +++ b/slices/micropub/entities/auto_tagging.rb @@ -1,6 +1,7 @@ -module Adamantium +module Micropub module Entities class AutoTagging < Dry::Struct + attribute :id, Types::Coercible::Integer attribute? :title_contains, Types::Optional::String attribute? :body_contains, Types::Optional::String attribute :tag_id, Types::Coercible::Integer @@ -8,6 +9,14 @@ module Adamantium def title_only? !title_contains.empty? end + + def term + title_only? ? title_contains : body_contains + end + + class WithTag < AutoTagging + attribute :tag, Types::Tag + end end end end diff --git a/app/entities/book_request.rb b/slices/micropub/entities/book_request.rb similarity index 96% rename from app/entities/book_request.rb rename to slices/micropub/entities/book_request.rb index 6da55a7..ab487b0 100644 --- a/app/entities/book_request.rb +++ b/slices/micropub/entities/book_request.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Entities class BookRequest < Dry::Struct attribute :h, Types::Coercible::String diff --git a/app/entities/bookmark_request.rb b/slices/micropub/entities/bookmark_request.rb similarity index 97% rename from app/entities/bookmark_request.rb rename to slices/micropub/entities/bookmark_request.rb index 43bb89b..794eb38 100644 --- a/app/entities/bookmark_request.rb +++ b/slices/micropub/entities/bookmark_request.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Entities class BookmarkRequest < Dry::Struct attribute :h, Types::Coercible::String diff --git a/app/entities/checkin_request.rb b/slices/micropub/entities/checkin_request.rb similarity index 97% rename from app/entities/checkin_request.rb rename to slices/micropub/entities/checkin_request.rb index 979b94c..bd23145 100644 --- a/app/entities/checkin_request.rb +++ b/slices/micropub/entities/checkin_request.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Entities class CheckinRequest < Dry::Struct attribute :h, Types::Coercible::String diff --git a/app/entities/post_request.rb b/slices/micropub/entities/post_request.rb similarity index 97% rename from app/entities/post_request.rb rename to slices/micropub/entities/post_request.rb index adec9ff..3cc1d4d 100644 --- a/app/entities/post_request.rb +++ b/slices/micropub/entities/post_request.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Entities class PostRequest < Dry::Struct attribute :h, Types::Coercible::String diff --git a/app/queries/posts/microformat_post.rb b/slices/micropub/queries/posts/microformat_post.rb similarity index 98% rename from app/queries/posts/microformat_post.rb rename to slices/micropub/queries/posts/microformat_post.rb index d5f72e6..882b2b8 100644 --- a/app/queries/posts/microformat_post.rb +++ b/slices/micropub/queries/posts/microformat_post.rb @@ -1,6 +1,6 @@ require "reverse_markdown" -module Adamantium +module Micropub module Queries module Posts class MicroformatPost diff --git a/app/repos/auto_tagging_repo.rb b/slices/micropub/repos/auto_tagging_repo.rb similarity index 66% rename from app/repos/auto_tagging_repo.rb rename to slices/micropub/repos/auto_tagging_repo.rb index 8d776df..ea13d6b 100644 --- a/app/repos/auto_tagging_repo.rb +++ b/slices/micropub/repos/auto_tagging_repo.rb @@ -1,16 +1,16 @@ -module Adamantium +module Micropub module Repos class AutoTaggingRepo < Adamantium::Repo[:auto_taggings] def find(id) auto_taggings .where(id: id) - .map_to(Adamantium::Entities::AutoTagging) + .map_to(Micropub::Entities::AutoTagging) .to_a end def all auto_taggings - .map_to(Adamantium::Entities::AutoTagging) + .map_to(Micropub::Entities::AutoTagging) .to_a end end diff --git a/slices/micropub/repos/movie_repo.rb b/slices/micropub/repos/movie_repo.rb new file mode 100644 index 0000000..204d757 --- /dev/null +++ b/slices/micropub/repos/movie_repo.rb @@ -0,0 +1,7 @@ +module Micropub + module Repos + class MovieRepo < Adamantium::Repo[:movies] + commands :create + end + end +end diff --git a/slices/micropub/repos/podcast_repo.rb b/slices/micropub/repos/podcast_repo.rb new file mode 100644 index 0000000..e9d7fba --- /dev/null +++ b/slices/micropub/repos/podcast_repo.rb @@ -0,0 +1,15 @@ +module Micropub + module Repos + class PodcastRepo < Adamantium::Repo[:podcasts] + commands :create + + def listing + podcasts.order(:name).to_a + end + + def delete_all + podcasts.delete + end + end + end +end diff --git a/slices/micropub/repos/post_repo.rb b/slices/micropub/repos/post_repo.rb new file mode 100644 index 0000000..6abb340 --- /dev/null +++ b/slices/micropub/repos/post_repo.rb @@ -0,0 +1,112 @@ +module Micropub + module Repos + class PostRepo < Adamantium::Repo[:posts] + commands update: :by_pk + + def remove_tag(post_id:, tag:) + tag = posts.tags.where(label: tag).one + + posts.post_tags.where(post_id: post_id, tag_id: tag[:id]).changeset(:delete).commit if tag + end + + def create(post_attrs) + posts.transaction do + new_post = posts.changeset(:create, post_attrs).commit + + post_attrs[:category].each do |tag_name| + next if tag_name == "" + + tag = posts.tags.where(label: tag_name).one || + posts + .tags + .changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")}) + .commit + + posts.post_tags.changeset(:create, { + post_id: new_post.id, + tag_id: tag[:id] + }) + .commit + end + + new_post + end + end + + def slug_exists?(slug) + !!posts + .where(slug: slug) + .one + end + + def find!(id) + posts + .by_pk(id) + .one! + end + + def fetch!(slug) + posts + .published + .combine(:tags, :trips, :webmentions) + .node(:webmentions) { |webmention| + webmention.where(type: "reply") + } + .where(slug: slug) + .one! + end + + def fetch_unpublished!(slug) + posts + .combine(:tags) + .where(slug: slug) + .one! + end + + def tag_post(post_id:, tags:) + tags.each do |tag_name| + next if tag_name == "" + + tag = posts.tags.where(label: tag_name).one || + posts + .tags + .changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")}) + .commit + + posts.post_tags.changeset(:create, { + post_id: post_id, + tag_id: tag[:id] + }) + .commit + end + end + + def auto_tag_post(post_id:, tag_id:) + return if posts + .post_tags + .where( + post_id: post_id, + tag_id: tag_id + ).count > 0 + + posts + .post_tags + .changeset(:create, { + post_id: post_id, + tag_id: tag_id + }) + .commit + end + + def delete!(slug) + delete_post = posts.where(slug: slug).command(:update) + delete_post.call(published_at: nil) + end + + def restore!(slug) + delete_post = posts.where(slug: slug).command(:update) + delete_post.call(published_at: Time.now) + end + end + end +end diff --git a/slices/micropub/repos/webmentions_repo.rb b/slices/micropub/repos/webmentions_repo.rb new file mode 100644 index 0000000..1d78cb2 --- /dev/null +++ b/slices/micropub/repos/webmentions_repo.rb @@ -0,0 +1,7 @@ +module Micropub + module Repos + class WebmentionsRepo < Adamantium::Repo[:webmentions] + commands :create + end + end +end diff --git a/slices/micropub/repos/workout_repo.rb b/slices/micropub/repos/workout_repo.rb new file mode 100644 index 0000000..734eefd --- /dev/null +++ b/slices/micropub/repos/workout_repo.rb @@ -0,0 +1,7 @@ +module Micropub + module Repos + class WorkoutRepo < Adamantium::Repo[:workouts] + commands :create, update: :by_pk + end + end +end diff --git a/lib/adamantium/micropub_request_parser.rb b/slices/micropub/request_parser.rb similarity index 99% rename from lib/adamantium/micropub_request_parser.rb rename to slices/micropub/request_parser.rb index f589de5..6eef278 100644 --- a/lib/adamantium/micropub_request_parser.rb +++ b/slices/micropub/request_parser.rb @@ -1,5 +1,5 @@ -module Adamantium - class MicropubRequestParser +module Micropub + class RequestParser def call(params:) return nil if params.key?(:action) diff --git a/slices/micropub/templates/.keep b/slices/micropub/templates/.keep new file mode 100644 index 0000000..e69de29 diff --git a/slices/micropub/templates/layouts/.keep b/slices/micropub/templates/layouts/.keep new file mode 100644 index 0000000..e69de29 diff --git a/slices/micropub/templates/layouts/app.html.erb b/slices/micropub/templates/layouts/app.html.erb new file mode 100644 index 0000000..abec571 --- /dev/null +++ b/slices/micropub/templates/layouts/app.html.erb @@ -0,0 +1,14 @@ + + + + + + Adamantium - Micropub + <%= favicon_tag %> + <%= stylesheet_tag "micropub/app" %> + + + <%= yield %> + <%= javascript_tag "micropub/app" %> + + diff --git a/slices/micropub/types.rb b/slices/micropub/types.rb new file mode 100644 index 0000000..9cde0b5 --- /dev/null +++ b/slices/micropub/types.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require "dry/types" + +module Micropub + Types = Dry.Types + + module Types + class Tag < Dry::Struct + attribute :label, Types::Coercible::String + end + end +end diff --git a/app/validation/posts/book_contract.rb b/slices/micropub/validation/posts/book_contract.rb similarity index 96% rename from app/validation/posts/book_contract.rb rename to slices/micropub/validation/posts/book_contract.rb index d648839..14f0a03 100644 --- a/app/validation/posts/book_contract.rb +++ b/slices/micropub/validation/posts/book_contract.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Validation module Posts class BookContract < Dry::Validation::Contract diff --git a/app/validation/posts/bookmark_contract.rb b/slices/micropub/validation/posts/bookmark_contract.rb similarity index 97% rename from app/validation/posts/bookmark_contract.rb rename to slices/micropub/validation/posts/bookmark_contract.rb index 96091da..0b1eb57 100644 --- a/app/validation/posts/bookmark_contract.rb +++ b/slices/micropub/validation/posts/bookmark_contract.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Validation module Posts class BookmarkContract < Dry::Validation::Contract diff --git a/app/validation/posts/checkin_contract.rb b/slices/micropub/validation/posts/checkin_contract.rb similarity index 96% rename from app/validation/posts/checkin_contract.rb rename to slices/micropub/validation/posts/checkin_contract.rb index 9186755..573e3fc 100644 --- a/app/validation/posts/checkin_contract.rb +++ b/slices/micropub/validation/posts/checkin_contract.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Validation module Posts class CheckinContract < Dry::Validation::Contract diff --git a/app/validation/posts/post_contract.rb b/slices/micropub/validation/posts/post_contract.rb similarity index 96% rename from app/validation/posts/post_contract.rb rename to slices/micropub/validation/posts/post_contract.rb index 7ec606a..f35afad 100644 --- a/app/validation/posts/post_contract.rb +++ b/slices/micropub/validation/posts/post_contract.rb @@ -1,4 +1,4 @@ -module Adamantium +module Micropub module Validation module Posts class PostContract < Dry::Validation::Contract diff --git a/slices/micropub/view.rb b/slices/micropub/view.rb new file mode 100644 index 0000000..bcdc5a5 --- /dev/null +++ b/slices/micropub/view.rb @@ -0,0 +1,7 @@ +# auto_register: false +# frozen_string_literal: true + +module Micropub + class View < Adamantium::View + end +end diff --git a/slices/micropub/views/.keep b/slices/micropub/views/.keep new file mode 100644 index 0000000..e69de29 diff --git a/slices/micropub/views/helpers.rb b/slices/micropub/views/helpers.rb new file mode 100644 index 0000000..a155894 --- /dev/null +++ b/slices/micropub/views/helpers.rb @@ -0,0 +1,10 @@ +# auto_register: false +# frozen_string_literal: true + +module Micropub + module Views + module Helpers + # Add your view helpers here + end + end +end diff --git a/spec/adamantium/micropub_request_parser_spec.rb b/spec/adamantium/micropub_request_parser_spec.rb index 8d1e112..4903b96 100644 --- a/spec/adamantium/micropub_request_parser_spec.rb +++ b/spec/adamantium/micropub_request_parser_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe Adamantium::MicropubRequestParser do +RSpec.describe Micropub::RequestParser do subject { described_class.new } context "json request" do @@ -19,7 +19,7 @@ RSpec.describe Adamantium::MicropubRequestParser do it "parses the params in to the expected shape" do Timecop.freeze do result = subject.call(params: params) - expect(result).to be_a Adamantium::Entities::PostRequest + expect(result).to be_a Micropub::Entities::PostRequest end end end @@ -38,7 +38,7 @@ RSpec.describe Adamantium::MicropubRequestParser do it "parses the params in to the expected shape" do Timecop.freeze do result = subject.call(params: params) - expect(result).to be_a Adamantium::Entities::PostRequest + expect(result).to be_a Micropub::Entities::PostRequest end end end @@ -140,7 +140,7 @@ RSpec.describe Adamantium::MicropubRequestParser do it "parses the request" do Timecop.freeze do result = subject.call(params: params) - expect(result).to be_a Adamantium::Entities::CheckinRequest + expect(result).to be_a Micropub::Entities::CheckinRequest end end end diff --git a/spec/adamantium/unit/commands/media/upload_spec.rb b/spec/adamantium/unit/commands/media/upload_spec.rb index 4f7ef84..a908e39 100644 --- a/spec/adamantium/unit/commands/media/upload_spec.rb +++ b/spec/adamantium/unit/commands/media/upload_spec.rb @@ -3,7 +3,7 @@ require "dry/monads" require "base64" -RSpec.describe Adamantium::Commands::Media::Upload do +RSpec.describe Micropub::Commands::Media::Upload do subject { described_class.new } it "saves a file and returns its URL" do diff --git a/spec/adamantium/unit/commands/posts/add_syndication_source_spec.rb b/spec/adamantium/unit/commands/posts/add_syndication_source_spec.rb index 5a124ee..4717697 100644 --- a/spec/adamantium/unit/commands/posts/add_syndication_source_spec.rb +++ b/spec/adamantium/unit/commands/posts/add_syndication_source_spec.rb @@ -1,11 +1,11 @@ require "spec_helper" -RSpec.describe Adamantium::Commands::Posts::AddSyndicationSource, :db do +RSpec.describe Micropub::Commands::Posts::AddSyndicationSource, :db do subject { described_class.new } describe "setting a syndication source" do let(:post) { Test::Factory[:post] } - let(:repo) { Adamantium::Container["repos.post_repo"] } + let(:repo) { Micropub::Container["repos.post_repo"] } context "when no sources exist" do it "sets a new source" do diff --git a/spec/adamantium/unit/commands/posts/delete_spec.rb b/spec/adamantium/unit/commands/posts/delete_spec.rb index 97c2f9a..af0036f 100644 --- a/spec/adamantium/unit/commands/posts/delete_spec.rb +++ b/spec/adamantium/unit/commands/posts/delete_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" -RSpec.describe Adamantium::Commands::Posts::Delete, :db do - let(:post_repo) { spy(Adamantium::Repos::PostRepo) } +RSpec.describe Micropub::Commands::Posts::Delete, :db do + let(:post_repo) { spy(Micropub::Repos::PostRepo) } let(:subject) { described_class.new(post_repo: post_repo) } it "deletes a post" do diff --git a/spec/adamantium/unit/commands/posts/syndicate_spec.rb b/spec/adamantium/unit/commands/posts/syndicate_spec.rb index ae198b6..39c33db 100644 --- a/spec/adamantium/unit/commands/posts/syndicate_spec.rb +++ b/spec/adamantium/unit/commands/posts/syndicate_spec.rb @@ -1,14 +1,14 @@ require "spec_helper" require "dry/monads" -RSpec.describe Adamantium::Commands::Posts::Syndicate do +RSpec.describe Micropub::Commands::Posts::Syndicate do include Dry::Monads[:result] let(:settings) { double("settings", mastodon_server: "https://mastodon.example/@tester", blue_sky_url: "https://bluesky.app") } let(:mastodon_client) { double("Adamantium::Client::Mastodon") } let(:mastodon_syndicator) { Adamantium::Syndication::Mastodon.new(mastodon_client: mastodon_client) } let(:post) { {url: "example.com", syndicate_to: ["https://mastodon.example", "https://pinboard.in"], category: []} } - let(:add_post_syndication_source) { spy(Adamantium::Commands::Posts::AddSyndicationSource) } + let(:add_post_syndication_source) { spy(Micropub::Commands::Posts::AddSyndicationSource) } subject { described_class.new(mastodon: mastodon_syndicator, diff --git a/spec/adamantium/unit/commands/posts/undelete_spec.rb b/spec/adamantium/unit/commands/posts/undelete_spec.rb index 01f9520..f9f4c7b 100644 --- a/spec/adamantium/unit/commands/posts/undelete_spec.rb +++ b/spec/adamantium/unit/commands/posts/undelete_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" -RSpec.describe Adamantium::Commands::Posts::Undelete, :db do - let(:post_repo) { spy(Adamantium::Repos::PostRepo) } +RSpec.describe Micropub::Commands::Posts::Undelete, :db do + let(:post_repo) { spy(Micropub::Repos::PostRepo) } let(:subject) { described_class.new(post_repo: post_repo) } it "deletes a post" do diff --git a/spec/adamantium/unit/commands/posts/update_spec.rb b/spec/adamantium/unit/commands/posts/update_spec.rb index 5dc7305..4299376 100644 --- a/spec/adamantium/unit/commands/posts/update_spec.rb +++ b/spec/adamantium/unit/commands/posts/update_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -RSpec.describe Adamantium::Commands::Posts::Update, :db do +RSpec.describe Micropub::Commands::Posts::Update, :db do # Adding: add a property that didn't previously exist # If there are any existing values for this property, they are not changed, the new values are added. # If the property does not exist already, it is created. @@ -10,7 +10,7 @@ RSpec.describe Adamantium::Commands::Posts::Update, :db do describe "add" do let(:post) { Test::Factory[:post] } - let(:repo) { Adamantium::Container["repos.post_repo"] } + let(:repo) { Micropub::Container["repos.post_repo"] } let(:params) { { @@ -37,7 +37,7 @@ RSpec.describe Adamantium::Commands::Posts::Update, :db do # Replacing: Replace all values of the property. If the property does not exist already, it is created. describe "replace" do let(:post) { Test::Factory[:post] } - let(:repo) { Adamantium::Container["repos.post_repo"] } + let(:repo) { Micropub::Container["repos.post_repo"] } let(:params) { { @@ -62,7 +62,7 @@ RSpec.describe Adamantium::Commands::Posts::Update, :db do describe "remove" do let(:post1) { Test::Factory[:post] } let(:post2) { Test::Factory[:post] } - let(:repo) { Adamantium::Container["repos.post_repo"] } + let(:repo) { Micropub::Container["repos.post_repo"] } let(:complete_params) { { diff --git a/spec/syndication/unit/mastodon_spec.rb b/spec/syndication/unit/mastodon_spec.rb index b372397..d2b10af 100644 --- a/spec/syndication/unit/mastodon_spec.rb +++ b/spec/syndication/unit/mastodon_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Adamantium::Syndication::Mastodon do describe "syndication to mastodon" do let(:post) { - Adamantium::Entities::PostRequest.new( + Micropub::Entities::PostRequest.new( h: "h-type", action: nil, name: "My Post",