diff options
-rw-r--r-- | app/controllers/blogs_controller.rb | 34 | ||||
-rw-r--r-- | app/controllers/webmentions_controller.rb | 36 | ||||
-rw-r--r-- | app/helpers/webmentions_helper.rb | 2 | ||||
-rw-r--r-- | app/views/blogs/show.html.haml | 1 | ||||
-rw-r--r-- | app/views/layouts/application.html.haml | 3 | ||||
-rw-r--r-- | app/views/webmentions/.keep | 0 | ||||
-rw-r--r-- | config/routes.rb | 3 | ||||
-rw-r--r-- | test/controllers/webmentions_controller_test.rb | 8 |
8 files changed, 49 insertions, 38 deletions
diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 4bbbc28..033d6bb 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb | |||
@@ -1,9 +1,6 @@ | |||
1 | require 'microformats' | ||
2 | require 'redcarpet/render_strip' | 1 | require 'redcarpet/render_strip' |
3 | require 'webmention' | ||
4 | 2 | ||
5 | class BlogsController < ApplicationController | 3 | class BlogsController < ApplicationController |
6 | skip_before_action :verify_authenticity_token, only: [:webmention] | ||
7 | 4 | ||
8 | def summary | 5 | def summary |
9 | @blogs = Blog.where(published: true).order(published_at: :desc).paginate(page: params[:page], per_page: 10) | 6 | @blogs = Blog.where(published: true).order(published_at: :desc).paginate(page: params[:page], per_page: 10) |
@@ -92,35 +89,4 @@ class BlogsController < ApplicationController | |||
92 | end | 89 | end |
93 | end | 90 | end |
94 | 91 | ||
95 | def webmention | ||
96 | @blog = Blog.find_by_slug(params[:slug]) | ||
97 | |||
98 | raise ActiveRecord::RecordNotFound unless @blog | ||
99 | raise ActiveRecord::RecordNotFound unless @blog.published | ||
100 | |||
101 | permalink = url_for(@blog) | ||
102 | |||
103 | target = params[:target] | ||
104 | unless target == permalink | ||
105 | render json: { error: "Incorrect target for webmention endpoint (#{target} != #{permalink})." } | ||
106 | return | ||
107 | end | ||
108 | |||
109 | source = params[:source] | ||
110 | verification = Webmention.verify_webmention(source, target) | ||
111 | unless verification.verified? | ||
112 | render json: { error: "Webmention could not be verified." } | ||
113 | return | ||
114 | end | ||
115 | |||
116 | response = Webmention::Request.get(source) | ||
117 | parsed = Microformats.parse(response.body.to_s) | ||
118 | |||
119 | if parsed.entry.properties.to_hash.include?("like-of") and parsed.entry.like_of(:all).map(&:to_s).include? permalink | ||
120 | @blog.like!(parsed.entry.author.url, parsed.entry.author.name) | ||
121 | end | ||
122 | |||
123 | head :ok | ||
124 | end | ||
125 | |||
126 | end | 92 | end |
diff --git a/app/controllers/webmentions_controller.rb b/app/controllers/webmentions_controller.rb new file mode 100644 index 0000000..d8aefc7 --- /dev/null +++ b/app/controllers/webmentions_controller.rb | |||
@@ -0,0 +1,36 @@ | |||
1 | require 'microformats' | ||
2 | require 'webmention' | ||
3 | |||
4 | class WebmentionsController < ApplicationController | ||
5 | skip_before_action :verify_authenticity_token | ||
6 | |||
7 | def create | ||
8 | source = params[:source] | ||
9 | target = params[:target] | ||
10 | |||
11 | verification = Webmention.verify_webmention(source, target) | ||
12 | unless verification.verified? | ||
13 | render json: { error: "Webmention could not be verified." } | ||
14 | return | ||
15 | end | ||
16 | |||
17 | target_parts = URI.parse(target).path.split("/").drop(1) | ||
18 | |||
19 | if target_parts[0] == "blog" | ||
20 | blog = Blog.find_by_slug(target_parts[1]) | ||
21 | |||
22 | raise ActiveRecord::RecordNotFound unless blog | ||
23 | raise ActiveRecord::RecordNotFound unless blog.published | ||
24 | |||
25 | response = Webmention::Request.get(source) | ||
26 | parsed = Microformats.parse(response.body.to_s) | ||
27 | |||
28 | if parsed.entry.properties.to_hash.include?("like-of") and parsed.entry.like_of(:all).map(&:to_s).include? target | ||
29 | blog.like!(parsed.entry.author.url, parsed.entry.author.name) | ||
30 | end | ||
31 | end | ||
32 | |||
33 | head :ok | ||
34 | end | ||
35 | |||
36 | end | ||
diff --git a/app/helpers/webmentions_helper.rb b/app/helpers/webmentions_helper.rb new file mode 100644 index 0000000..4b22e43 --- /dev/null +++ b/app/helpers/webmentions_helper.rb | |||
@@ -0,0 +1,2 @@ | |||
1 | module WebmentionsHelper | ||
2 | end | ||
diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml index c849143..7558257 100644 --- a/app/views/blogs/show.html.haml +++ b/app/views/blogs/show.html.haml | |||
@@ -1,5 +1,4 @@ | |||
1 | - title @blog.title | 1 | - title @blog.title |
2 | - content_for :webmention_endpoint, webmention_blog_url(@blog) | ||
3 | - unless @prev.nil? | 2 | - unless @prev.nil? |
4 | .back-post= link_to "← #{@prev.title}", @prev | 3 | .back-post= link_to "← #{@prev.title}", @prev |
5 | - unless @next.nil? | 4 | - unless @next.nil? |
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index d776b9c..b3928f2 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml | |||
@@ -7,8 +7,7 @@ | |||
7 | = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' | 7 | = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' |
8 | = display_meta_tags og: { site_name: "Four Island" } | 8 | = display_meta_tags og: { site_name: "Four Island" } |
9 | %link{rel: "me", href: "mailto:fefferburbia+thoughts@gmail.com"} | 9 | %link{rel: "me", href: "mailto:fefferburbia+thoughts@gmail.com"} |
10 | - if content_for?(:webmention_endpoint) | 10 | %link{rel: "webmention", href: webmentions_url} |
11 | %link{rel: "webmention", href: yield(:webmention_endpoint)} | ||
12 | %body#main-body | 11 | %body#main-body |
13 | - if flash[:alert] | 12 | - if flash[:alert] |
14 | %div#flash.flash-alert= flash[:alert] | 13 | %div#flash.flash-alert= flash[:alert] |
diff --git a/app/views/webmentions/.keep b/app/views/webmentions/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/views/webmentions/.keep | |||
diff --git a/config/routes.rb b/config/routes.rb index 21e1517..76378c4 100644 --- a/config/routes.rb +++ b/config/routes.rb | |||
@@ -51,7 +51,6 @@ Rails.application.routes.draw do | |||
51 | member do | 51 | member do |
52 | post 'upvote' | 52 | post 'upvote' |
53 | post 'downvote' | 53 | post 'downvote' |
54 | post 'webmention' | ||
55 | 54 | ||
56 | resources :comments, only: [:create] | 55 | resources :comments, only: [:create] |
57 | end | 56 | end |
@@ -82,4 +81,6 @@ Rails.application.routes.draw do | |||
82 | post 'downvote' | 81 | post 'downvote' |
83 | end | 82 | end |
84 | end | 83 | end |
84 | |||
85 | resources :webmentions, only: [:create] | ||
85 | end | 86 | end |
diff --git a/test/controllers/webmentions_controller_test.rb b/test/controllers/webmentions_controller_test.rb new file mode 100644 index 0000000..cc7bb42 --- /dev/null +++ b/test/controllers/webmentions_controller_test.rb | |||
@@ -0,0 +1,8 @@ | |||
1 | require "test_helper" | ||
2 | |||
3 | class WebmentionsControllerTest < ActionDispatch::IntegrationTest | ||
4 | test "should get create" do | ||
5 | get webmentions_create_url | ||
6 | assert_response :success | ||
7 | end | ||
8 | end | ||