diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-12-07 15:54:39 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-12-07 15:54:39 -0500 |
commit | a996f3bd05fc480247fd112f23fa3e67f7d5d7b5 (patch) | |
tree | 2df0a56e8cc42ccd632db5b2c022b7dbf8d74444 /app | |
parent | 59f5508d5bc0cee856105a5bd52e1deaee44b842 (diff) | |
download | thoughts-a996f3bd05fc480247fd112f23fa3e67f7d5d7b5.tar.gz thoughts-a996f3bd05fc480247fd112f23fa3e67f7d5d7b5.tar.bz2 thoughts-a996f3bd05fc480247fd112f23fa3e67f7d5d7b5.zip |
Added support for liking blog posts via webmention
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/blogs_controller.rb | 34 | ||||
-rw-r--r-- | app/models/concerns/votable.rb | 8 | ||||
-rw-r--r-- | app/models/vote.rb | 1 | ||||
-rw-r--r-- | app/views/blogs/_blog.html.haml | 2 | ||||
-rw-r--r-- | app/views/blogs/show.html.haml | 1 | ||||
-rw-r--r-- | app/views/layouts/application.html.haml | 2 |
6 files changed, 46 insertions, 2 deletions
diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 033d6bb..4bbbc28 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb | |||
@@ -1,6 +1,9 @@ | |||
1 | require 'microformats' | ||
1 | require 'redcarpet/render_strip' | 2 | require 'redcarpet/render_strip' |
3 | require 'webmention' | ||
2 | 4 | ||
3 | class BlogsController < ApplicationController | 5 | class BlogsController < ApplicationController |
6 | skip_before_action :verify_authenticity_token, only: [:webmention] | ||
4 | 7 | ||
5 | def summary | 8 | def summary |
6 | @blogs = Blog.where(published: true).order(published_at: :desc).paginate(page: params[:page], per_page: 10) | 9 | @blogs = Blog.where(published: true).order(published_at: :desc).paginate(page: params[:page], per_page: 10) |
@@ -89,4 +92,35 @@ class BlogsController < ApplicationController | |||
89 | end | 92 | end |
90 | end | 93 | end |
91 | 94 | ||
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 | |||
92 | end | 126 | end |
diff --git a/app/models/concerns/votable.rb b/app/models/concerns/votable.rb index ba6e6d5..40b3a2a 100644 --- a/app/models/concerns/votable.rb +++ b/app/models/concerns/votable.rb | |||
@@ -39,5 +39,13 @@ module Votable | |||
39 | save! | 39 | save! |
40 | end | 40 | end |
41 | end | 41 | end |
42 | |||
43 | def like!(url, name) | ||
44 | return false unless votes.where(liker_url: url).empty? | ||
45 | |||
46 | votes.create(liker_url: url, liker_name: name, upvote: 1).save | ||
47 | self.upvotes += 1 | ||
48 | save! | ||
49 | end | ||
42 | end | 50 | end |
43 | end | 51 | end |
diff --git a/app/models/vote.rb b/app/models/vote.rb index e2d8386..fced5bd 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb | |||
@@ -2,5 +2,4 @@ class Vote < ApplicationRecord | |||
2 | belongs_to :votable, polymorphic: true | 2 | belongs_to :votable, polymorphic: true |
3 | 3 | ||
4 | validates :upvote, presence: true, inclusion: { in: [0, 1] } | 4 | validates :upvote, presence: true, inclusion: { in: [0, 1] } |
5 | validates :ip, presence: true | ||
6 | end | 5 | end |
diff --git a/app/views/blogs/_blog.html.haml b/app/views/blogs/_blog.html.haml index d0b81d1..878b1a2 100644 --- a/app/views/blogs/_blog.html.haml +++ b/app/views/blogs/_blog.html.haml | |||
@@ -19,7 +19,7 @@ | |||
19 | %cite.bubble.blog-cite | 19 | %cite.bubble.blog-cite |
20 | %span.p-author.h-card | 20 | %span.p-author.h-card |
21 | %strong.p-name= blog.user.login.capitalize | 21 | %strong.p-name= blog.user.login.capitalize |
22 | %data.u-url{ value: "/" } | 22 | %data.u-url{ value: root_url } |
23 | on | 23 | on |
24 | %time.dt-published{ datetime: blog.visible_date.strftime("%Y-%m-%dT%H:%M:%SZ%z") }= blog.visible_date.strftime("%B #{blog.visible_date.day.ordinalize}, %Y at %-I:%M:%S%P") | 24 | %time.dt-published{ datetime: blog.visible_date.strftime("%Y-%m-%dT%H:%M:%SZ%z") }= blog.visible_date.strftime("%B #{blog.visible_date.day.ordinalize}, %Y at %-I:%M:%S%P") |
25 | .post-vote{ id: "blog-vote-section-#{blog.id}" } | 25 | .post-vote{ id: "blog-vote-section-#{blog.id}" } |
diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml index 7558257..c849143 100644 --- a/app/views/blogs/show.html.haml +++ b/app/views/blogs/show.html.haml | |||
@@ -1,4 +1,5 @@ | |||
1 | - title @blog.title | 1 | - title @blog.title |
2 | - content_for :webmention_endpoint, webmention_blog_url(@blog) | ||
2 | - unless @prev.nil? | 3 | - unless @prev.nil? |
3 | .back-post= link_to "← #{@prev.title}", @prev | 4 | .back-post= link_to "← #{@prev.title}", @prev |
4 | - unless @next.nil? | 5 | - unless @next.nil? |
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 69cd6ba..ec37f73 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml | |||
@@ -6,6 +6,8 @@ | |||
6 | = stylesheet_link_tag 'main', media: 'all', 'data-turbolinks-track': 'reload' | 6 | = stylesheet_link_tag 'main', media: 'all', 'data-turbolinks-track': 'reload' |
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 | - if content_for?(:webmention_endpoint) | ||
10 | %link{rel: "webmention", href: yield(:webmention_endpoint)} | ||
9 | %body#main-body | 11 | %body#main-body |
10 | - if flash[:alert] | 12 | - if flash[:alert] |
11 | %div#flash.flash-alert= flash[:alert] | 13 | %div#flash.flash-alert= flash[:alert] |