discourse/lib/onebox/matcher.rb
Jarek Radosz 6db2c7696f
FEATURE: Add PR status icon to inline oneboxes (#39649)
Extends the live PR status indicator (#36313) to inline oneboxes

<img width="759" height="308" alt="image"
src="https://github.com/user-attachments/assets/10a379d2-78a9-4987-8f56-1df854fe33fa"
/>
2026-05-04 18:15:14 +02:00

37 lines
1 KiB
Ruby

# frozen_string_literal: true
module Onebox
class Matcher
def initialize(url, options = {})
begin
@uri = URI(url)
rescue URI::InvalidURIError
end
@options = options
end
def ordered_engines
@ordered_engines ||=
Engine.engines.sort_by { |e| e.respond_to?(:priority) ? e.priority : 100 }
end
def oneboxed
return if @uri.nil? || @uri.host.nil?
return if @uri.port && !Onebox.options.allowed_ports.include?(@uri.port)
return if @uri.scheme && !Onebox.options.allowed_schemes.include?(@uri.scheme)
ordered_engines.find do |engine|
(
engine.respond_to?(:handles_content_type?) &&
engine.handles_content_type?(@options[:content_type]) || engine === @uri
) && has_allowed_iframe_origins?(engine)
end
end
def has_allowed_iframe_origins?(engine)
allowed_regexes = @options[:allowed_iframe_regexes] || []
engine.iframe_origins.all? { |o| allowed_regexes.any? { |r| o =~ r } }
end
end
end