From 9d8deddb0a9df7d4d7d4b649ddf01b15570719e0 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 7 Dec 2024 18:58:50 -0500 Subject: Moved webmentions to a single endpoint --- app/controllers/blogs_controller.rb | 34 ----------------------------- app/controllers/webmentions_controller.rb | 36 +++++++++++++++++++++++++++++++ app/helpers/webmentions_helper.rb | 2 ++ app/views/blogs/show.html.haml | 1 - app/views/layouts/application.html.haml | 3 +-- app/views/webmentions/.keep | 0 6 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 app/controllers/webmentions_controller.rb create mode 100644 app/helpers/webmentions_helper.rb create mode 100644 app/views/webmentions/.keep (limited to 'app') 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 @@ -require 'microformats' require 'redcarpet/render_strip' -require 'webmention' class BlogsController < ApplicationController - skip_before_action :verify_authenticity_token, only: [:webmention] def summary @blogs = Blog.where(published: true).order(published_at: :desc).paginate(page: params[:page], per_page: 10) @@ -92,35 +89,4 @@ class BlogsController < ApplicationController end end - def webmention - @blog = Blog.find_by_slug(params[:slug]) - - raise ActiveRecord::RecordNotFound unless @blog - raise ActiveRecord::RecordNotFound unless @blog.published - - permalink = url_for(@blog) - - target = params[:target] - unless target == permalink - render json: { error: "Incorrect target for webmention endpoint (#{target} != #{permalink})." } - return - end - - source = params[:source] - verification = Webmention.verify_webmention(source, target) - unless verification.verified? - render json: { error: "Webmention could not be verified." } - return - end - - response = Webmention::Request.get(source) - parsed = Microformats.parse(response.body.to_s) - - if parsed.entry.properties.to_hash.include?("like-of") and parsed.entry.like_of(:all).map(&:to_s).include? permalink - @blog.like!(parsed.entry.author.url, parsed.entry.author.name) - end - - head :ok - end - 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 @@ +require 'microformats' +require 'webmention' + +class WebmentionsController < ApplicationController + skip_before_action :verify_authenticity_token + + def create + source = params[:source] + target = params[:target] + + verification = Webmention.verify_webmention(source, target) + unless verification.verified? + render json: { error: "Webmention could not be verified." } + return + end + + target_parts = URI.parse(target).path.split("/").drop(1) + + if target_parts[0] == "blog" + blog = Blog.find_by_slug(target_parts[1]) + + raise ActiveRecord::RecordNotFound unless blog + raise ActiveRecord::RecordNotFound unless blog.published + + response = Webmention::Request.get(source) + parsed = Microformats.parse(response.body.to_s) + + if parsed.entry.properties.to_hash.include?("like-of") and parsed.entry.like_of(:all).map(&:to_s).include? target + blog.like!(parsed.entry.author.url, parsed.entry.author.name) + end + end + + head :ok + end + +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 @@ +module WebmentionsHelper +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 @@ - title @blog.title -- content_for :webmention_endpoint, webmention_blog_url(@blog) - unless @prev.nil? .back-post= link_to "← #{@prev.title}", @prev - 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 @@ = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' = display_meta_tags og: { site_name: "Four Island" } %link{rel: "me", href: "mailto:fefferburbia+thoughts@gmail.com"} - - if content_for?(:webmention_endpoint) - %link{rel: "webmention", href: yield(:webmention_endpoint)} + %link{rel: "webmention", href: webmentions_url} %body#main-body - if flash[:alert] %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 -- cgit 1.4.1