Permalink
Browse files

Send webmentions on post creation

  • Loading branch information...
adamdawkins committed Feb 8, 2019
1 parent 9feb5f6 commit 784d5769530756a5b80fb497059ba0c601291295
Showing with 25 additions and 3 deletions.
  1. +1 −1 app/jobs/send_webmention_job.rb
  2. +19 −1 app/models/post.rb
  3. +3 −1 app/models/webmention.rb
  4. +2 −0 app/services/webmention_sender.rb
@@ -2,7 +2,7 @@ class SendWebmentionJob < ApplicationJob
queue_as :default

def perform(webmention)
@sender = WebmentionSender.new(webmention.source, webmention.target)
@sender = WebmentionSender.new(webmention.post.andand.full_url, webmention.target)
@sender.send

webmention.update(status: @sender.status)
@@ -16,6 +16,7 @@ class Post < ApplicationRecord
has_many :webmentions, dependent: :destroy

before_validation :generate_slug
after_save :create_webmentions
before_destroy :delete_post_from_silos


@@ -48,10 +49,19 @@ def slug_candidates

def slug_already_exists?
unless published_at.nil?
Post.where(published_at: published_at.all_day, slug: slug).exists?
existing_post = Post.where(published_at: published_at.all_day, slug: slug).first
existing_post.present? && existing_post.id != id
end
end

def links
URI.extract(content)
end

def full_url
Rails.application.routes.url_helpers.long_post_url(params)
end

private

def clean_slug!
@@ -82,4 +92,12 @@ def delete_post_from_silos
"Delete post [#{id}]: Checking for syndicates to delete..."
syndicates.each { |s| s.delete_post_from_silo }
end

def create_webmentions
links.each do |link|
Webmention.where(post_id: id, target: link).first_or_initialize.tap do |wm|
wm.save
end
end
end
end
@@ -1,6 +1,8 @@
class Webmention < ApplicationRecord
belongs_to :post
validates_presence_of :target

validates :target, presence: true, uniqueness: { scope: :post_id }

before_create :set_pending_status
after_create :send_webmention

@@ -21,7 +21,9 @@ def success?

def send
return @status = "no_endpoint" if @endpoint.nil?
pp "endpoint found", @endpoint
response = HTTParty.post(@endpoint, { body: {target: @target, source: @source }})
pp "response", response.code
@status = "success" if response.code.between?(200, 299)
end

0 comments on commit 784d576

Please sign in to comment.