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/webmentions_controller.rb | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 app/controllers/webmentions_controller.rb (limited to 'app/controllers/webmentions_controller.rb') 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 -- cgit 1.4.1