diff --git a/controllers/controller.rb b/controllers/controller.rb index f412242..f1f6fc9 100644 --- a/controllers/controller.rb +++ b/controllers/controller.rb @@ -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 @@ -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| diff --git a/helpers/web_hooks.rb b/helpers/web_hooks.rb new file mode 100644 index 0000000..7680f74 --- /dev/null +++ b/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 diff --git a/helpers/webmention_processor.rb b/helpers/webmention_processor.rb index 870a3d4..862c4f7 100644 --- a/helpers/webmention_processor.rb +++ b/helpers/webmention_processor.rb @@ -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 @@ -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 diff --git a/views/webhooks.erb b/views/webhooks.erb index 744caae..3e3040b 100644 --- a/views/webhooks.erb +++ b/views/webhooks.erb @@ -62,6 +62,15 @@
  • rsvp
  • bookmark-of
  • + +

    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.

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