From 734352d25e415dc72c94267f850e26e148c0ca01 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 2 Dec 2023 11:30:00 +1100 Subject: [PATCH] =?UTF-8?q?Manage=20books=20with=20epilogue=20=E2=80=94=20?= =?UTF-8?q?https://epilogue.micro.blog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/relations/posts.rb | 5 +-- slices/micropub/actions/site/config.rb | 2 +- .../queries/posts/microformat_post.rb | 32 ++++++++++++++++++- slices/micropub/repos/post_repo.rb | 9 ++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/relations/posts.rb b/app/relations/posts.rb index 389a3e5..723201a 100644 --- a/app/relations/posts.rb +++ b/app/relations/posts.rb @@ -37,10 +37,7 @@ module Adamantium end def search(term:) - ref = dataset - .full_text_search([:content], [term]) - .select(:id) - where(id: ref) + where(Sequel.ilike(:content, "%#{term}%")) end end end diff --git a/slices/micropub/actions/site/config.rb b/slices/micropub/actions/site/config.rb index 6ef1731..d73f0ca 100644 --- a/slices/micropub/actions/site/config.rb +++ b/slices/micropub/actions/site/config.rb @@ -50,7 +50,7 @@ module Micropub 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 + res.body = microformat_post.call(url: req.params[:url], filter: req.params[:filter], properties: req.params[:properties]).to_json else res.redirect_to "/" end diff --git a/slices/micropub/queries/posts/microformat_post.rb b/slices/micropub/queries/posts/microformat_post.rb index 882b2b8..8081f37 100644 --- a/slices/micropub/queries/posts/microformat_post.rb +++ b/slices/micropub/queries/posts/microformat_post.rb @@ -6,7 +6,19 @@ module Micropub class MicroformatPost include Deps["repos.post_repo"] - def call(url:, properties:) + def call(url:, filter:, properties:) + if url + fetch_post(url: url, properties: properties) + end + + if filter + search_posts(filter: filter) + end + end + + private + + def fetch_post(url:, properties:) slug = URI(url).path.split("/").last post = post_repo.fetch_unpublished!(slug) @@ -31,6 +43,24 @@ module Micropub result[:properties][:photos] = post.photos if properties.include? "photos" result end + + def search_posts(filter:) + post_repo + .search(term: filter) + .map { |post| + content = ReverseMarkdown.convert(post.content, unknown_tags: :pass_through, github_flavored: true).to_s + + { + type: ["h-entry"], + properties: { + published: [post.published_at], + content: [content], + photo: post.photos, + category: post.tags.map { |t| t.label.to_s } + } + } + } + end end end end diff --git a/slices/micropub/repos/post_repo.rb b/slices/micropub/repos/post_repo.rb index e273b3c..e9d5ba5 100644 --- a/slices/micropub/repos/post_repo.rb +++ b/slices/micropub/repos/post_repo.rb @@ -121,6 +121,15 @@ module Micropub delete_post = posts.where(slug: slug).command(:update) delete_post.call(published_at: Time.now) end + + def search(term:) + posts + .published + .search(term: term) + .combine(:tags) + .order(Sequel.desc(:published_at)) + .to_a + end end end end