diff --git a/app/templates/site/home.html.slim b/app/templates/site/home.html.slim index d0b28df..41c0339 100644 --- a/app/templates/site/home.html.slim +++ b/app/templates/site/home.html.slim @@ -4,15 +4,15 @@ div class="h-card prose dark:prose-invert mb-12 prose-a:decoration-wavy hover:pr div class="mb-8 max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600" -div class="mb-2 flex max-w-prose mx-auto" - p Latest status -div class="mb-12 p-2 grid grid-cols-7 gap-4 min-h-16 max-w-prose mx-auto bg-fuchsia-100 dark:bg-fuchsia-800 dark:text-gray-200 rounded" - div class="col-span-4 sm:col-span-6 text-left" - a class="block my-auto hover:underline decoration-wavy" href=latest_status.permalink - == "#{latest_status.content}" - a class="col-span-3 sm:col-span-1 px-2 text-center transition-colors rounded bg-fuchsia-50 hover:bg-fuchsia-200 dark:bg-fuchsia-900 dark:hover:bg-fuchsia-700 inline-block grid content-center max-h-12 my-auto" href="/statuses" - p See all - +- if latest_status + div class="mb-2 flex max-w-prose mx-auto" + p Latest status + div class="mb-12 p-2 grid grid-cols-7 gap-4 min-h-16 max-w-prose mx-auto bg-fuchsia-100 dark:bg-fuchsia-800 dark:text-gray-200 rounded" + div class="col-span-4 sm:col-span-6 text-left" + a class="block my-auto hover:underline decoration-wavy" href=latest_status.permalink + == latest_status.content + a class="col-span-3 sm:col-span-1 px-2 text-center transition-colors rounded bg-fuchsia-50 hover:bg-fuchsia-200 dark:bg-fuchsia-900 dark:hover:bg-fuchsia-700 inline-block grid content-center max-h-12 my-auto" href="/statuses" + p See all div class="mb-4 flex max-w-prose mx-auto" h2 class="text-l text-gray-600 dark:text-gray-200" Posts @@ -30,9 +30,10 @@ div class="grid grid-cols-3 gap-4 max-w-prose mx-auto" - photo_posts.each do |post| == render :photo_post, post: post -div class="mb-12 mt-6 max-w-prose mx-auto text-gray-600 dark:text-gray-200 rounded p-2 bg-blue-50 dark:bg-blue-900/60" - div - = "🗺️ Last seen at " - a class="text-blue-400 hover:text-blue-600" href=last_location.permalink - = last_location.display_title +- if last_location + div class="mb-12 mt-6 max-w-prose mx-auto text-gray-600 dark:text-gray-200 rounded p-2 bg-blue-50 dark:bg-blue-900/60" + div + = "🗺️ Last seen at " + a class="text-blue-400 hover:text-blue-600" href=last_location.permalink + = last_location.display_title div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600" diff --git a/app/views/site/home.rb b/app/views/site/home.rb index 9ad364a..84856fe 100644 --- a/app/views/site/home.rb +++ b/app/views/site/home.rb @@ -23,11 +23,11 @@ module Adamantium end expose :latest_status do - Decorators::Posts::Decorator.new(post_repo.latest_status) + post_repo.latest_status ? Decorators::Posts::Decorator.new(post_repo.latest_status) : nil end expose :last_location do - Decorators::Posts::Decorator.new(post_repo.last_location) + post_repo.last_location ? Decorators::Posts::Decorator.new(post_repo.last_location) : nil end end end diff --git a/lib/adamantium/client/mastodon.rb b/lib/adamantium/client/mastodon.rb index b7380f0..7613c7e 100644 --- a/lib/adamantium/client/mastodon.rb +++ b/lib/adamantium/client/mastodon.rb @@ -3,6 +3,7 @@ require "digest" require "tempfile" require "open-uri" require "dry/monads" +require "sanitize" module Adamantium module Client @@ -18,7 +19,7 @@ module Adamantium content = if post[:name] "#{post[:name]} — #{settings.micropub_site_url}/post/#{post[:slug]}" else - post[:content] + sanitze_post(post[:content]) end tags = post[:category].map { |tag| "##{tag}" }.join(" ") @@ -74,6 +75,19 @@ module Adamantium JSON.parse(response.body, symbolize_names: true).fetch(:id, nil) end + + private + + def sanitze_post(content) + replace_links = lambda { |env| + return unless env[:node_name] == "a" + node = env[:node] + url = node[:href] + env[:node].replace(url) + } + + Sanitize.fragment(content, transformers: [replace_links]).strip + end end end end diff --git a/spec/requests/create_post_spec.rb b/spec/requests/create_post_spec.rb index feb2fbb..5c7d6a7 100644 --- a/spec/requests/create_post_spec.rb +++ b/spec/requests/create_post_spec.rb @@ -38,8 +38,8 @@ RSpec.describe "Post creation", :db, :requests do post "/micropub", params expect(last_response).to be_successful - expect(post_repo.post_listing.count).to eq 1 - expect(post_repo.post_listing.first.tags.map(&:label)).to eq ["ruby", "rspec"] + expect(post_repo.statuses_listing.count).to eq 1 + expect(post_repo.statuses_listing.first.tags.map(&:label)).to eq ["ruby", "rspec"] end end