Skip to content

Commit

Permalink
also send webhook deletes when deleting from the dashboard
Browse files Browse the repository at this point in the history
closes #90
  • Loading branch information
aaronpk committed Jun 16, 2019
1 parent bc84696 commit daea65e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
8 changes: 8 additions & 0 deletions controllers/controller.rb
Expand Up @@ -114,12 +114,17 @@ def require_login
# Mark this particular webmention as deleted
link.deleted = true
link.save

# Add this source URL to the blacklist for just this site
blacklist = Blacklist.new
blacklist.site = link.site
blacklist.source = link.href
blacklist.created_at = Time.now
blacklist.save

# Notify the callback URL
WebHooks.deleted link.site, link.href, link.page.href, link.is_private

else
redirect "/dashboard"
end
Expand All @@ -136,6 +141,9 @@ def require_login
links.each do |link|
link.deleted = true
link.save

# Notify the callback URL
WebHooks.deleted link.site, link.href, link.page.href, link.is_private
end
# Add this source URL to the blacklist for each site
@user.sites.each do |site|
Expand Down
57 changes: 57 additions & 0 deletions helpers/web_hooks.rb
@@ -0,0 +1,57 @@
class WebHooks

def self.notify(site, link, source, target, _private)
# If a callback URL is defined for this site, send to the callback now
if !site.callback_url.blank?
begin
puts "Sending to callback URL: #{site.callback_url}"

jf2 = Formats.build_jf2_from_link(link)

data = {
secret: site.callback_secret,
source: source,
target: target,
private: _private,
post: jf2
}

RestClient::Request.execute(:method => :post,
:url => site.callback_url,
:payload => data.to_json,
:headers => {:content_type => 'application/json'},
:ssl_ca_file => './helpers/ca-bundle.crt')
puts "... success!"
rescue => e
puts "Failed to send to callback URL #{site.callback_url} #{e.inspect}"
end
end
end

def self.deleted(site, source, target, _private)
# If a callback URL is defined for this site, send the delete notification to the callback now
if !site.callback_url.blank?
begin
puts "Sending DELETE to callback URL: #{site.callback_url}"

data = {
secret: site.callback_secret,
source: source,
target: target,
private: _private,
deleted: true
}

RestClient::Request.execute(:method => :post,
:url => site.callback_url,
:payload => data.to_json,
:headers => {:content_type => 'application/json'},
:ssl_ca_file => './helpers/ca-bundle.crt')
puts "... success!"
rescue => e
puts "Failed to send to callback URL #{site.callback_url} #{e.inspect}"
end
end
end

end
50 changes: 2 additions & 48 deletions helpers/webmention_processor.rb
Expand Up @@ -167,29 +167,7 @@ def process_mention(username, source, target, protocol, token, code=nil)
:private => link.is_private,
}

# If a callback URL is defined for this site, send the delete notification to the callback now
if !site.callback_url.blank?
begin
puts "Sending DELETE to callback URL: #{site.callback_url}"

data = {
secret: site.callback_secret,
source: source,
target: target,
private: code ? true : false,
deleted: true
}

RestClient::Request.execute(:method => :post,
:url => site.callback_url,
:payload => data.to_json,
:headers => {:content_type => 'application/json'},
:ssl_ca_file => './helpers/ca-bundle.crt')
puts "... success!"
rescue => e
puts "Failed to send to callback URL #{site.callback_url} #{e.inspect}"
end
end
WebHooks.deleted site, source, target, (code ? true : false)

return nil, 'deleted'
end
Expand Down Expand Up @@ -280,31 +258,7 @@ def process_mention(username, source, target, protocol, token, code=nil)
end # notification
end

# If a callback URL is defined for this site, send to the callback now
if !site.callback_url.blank?
begin
puts "Sending to callback URL: #{site.callback_url}"

jf2 = Formats.build_jf2_from_link(link)

data = {
secret: site.callback_secret,
source: source,
target: target,
private: code ? true : false,
post: jf2
}

RestClient::Request.execute(:method => :post,
:url => site.callback_url,
:payload => data.to_json,
:headers => {:content_type => 'application/json'},
:ssl_ca_file => './helpers/ca-bundle.crt')
puts "... success!"
rescue => e
puts "Failed to send to callback URL #{site.callback_url} #{e.inspect}"
end
end
WebHooks.notify site, link, source, target, (code ? true : false)

if !site.account.aperture_uri.empty?
begin
Expand Down
9 changes: 9 additions & 0 deletions views/webhooks.erb
Expand Up @@ -62,6 +62,15 @@
<li><code>rsvp</code></li>
<li><code>bookmark-of</code></li>
</ul>

<p>If a webmention is deleted, either because the post was deleted or you deleted it from the dashboard, you will get a web hook notification like the below.</p>

<pre><code>{
"secret": "1234abcd",
"source": "http://rhiaro.co.uk/2015/11/1446953889",
"target": "http://aaronparecki.com/notes/2015/11/07/4/indiewebcamp",
"deleted": true
}</code></pre>
</section>

</div>
Expand Down

0 comments on commit daea65e

Please sign in to comment.