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 +++++++ 2 files changed, 20 insertions(+) create mode 100644 app/jobs/send_webmentions_job.rb (limited to 'app') 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 -- cgit 1.4.1