From 2dea596dbb31f3794744a1bd2c9ec6091d8dfdff Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 7 Dec 2024 21:22:22 -0500 Subject: Send webmentions when publishing/editing a blog post --- app/jobs/send_webmentions_job.rb | 13 +++++++++++++ app/models/blog.rb | 7 +++++++ test/jobs/send_webmentions_job_test.rb | 7 +++++++ 3 files changed, 27 insertions(+) create mode 100644 app/jobs/send_webmentions_job.rb create mode 100644 test/jobs/send_webmentions_job_test.rb diff --git a/app/jobs/send_webmentions_job.rb b/app/jobs/send_webmentions_job.rb new file mode 100644 index 0000000..359f5b6 --- /dev/null +++ b/app/jobs/send_webmentions_job.rb @@ -0,0 +1,13 @@ +require 'webmention' + +class SendWebmentionsJob < ApplicationJob + queue_as :default + + def perform(blog) + source = Rails.application.routes.url_helpers.blog_url(blog, host: "www.fourisland.com") + urls = Webmention.mentioned_urls(source) + urls.each do |url| + Webmention.send_webmention(source, url) + end + end +end diff --git a/app/models/blog.rb b/app/models/blog.rb index 8599bcd..db05432 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -19,6 +19,7 @@ class Blog < ApplicationRecord before_validation :set_draft_title before_save :set_published_at + after_save :send_webmentions def path "/says/#{slug}" @@ -72,4 +73,10 @@ class Blog < ApplicationRecord self.published_at = nil end end + + def send_webmentions + return unless self.published + + SendWebmentionsJob.perform_later self + end end diff --git a/test/jobs/send_webmentions_job_test.rb b/test/jobs/send_webmentions_job_test.rb new file mode 100644 index 0000000..72f6ed7 --- /dev/null +++ b/test/jobs/send_webmentions_job_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class SendWebmentionsJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end -- cgit 1.4.1