Skip to content

Commit

Permalink
Send webmentions on post creation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdawkins committed Feb 8, 2019
1 parent 9feb5f6 commit 784d576
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/jobs/send_webmention_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion app/models/webmention.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/services/webmention_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 784d576

Please sign in to comment.