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