2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-31 13:37:43 -06:00
|
|
|
class TopicEmbed < ActiveRecord::Base
|
2017-04-24 13:29:04 -05:00
|
|
|
include Trashable
|
|
|
|
|
2024-01-05 07:09:31 -06:00
|
|
|
EMBED_CONTENT_CACHE_MAX_LENGTH = 32_000
|
|
|
|
|
2013-12-31 13:37:43 -06:00
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :post
|
|
|
|
validates_presence_of :embed_url
|
2015-06-15 11:08:55 -05:00
|
|
|
validates_uniqueness_of :embed_url
|
2024-01-05 07:09:31 -06:00
|
|
|
validates :embed_content_cache, length: { maximum: EMBED_CONTENT_CACHE_MAX_LENGTH }
|
2013-12-31 13:37:43 -06:00
|
|
|
|
2017-04-24 13:29:04 -05:00
|
|
|
before_validation(on: :create) do
|
2023-01-09 06:20:10 -06:00
|
|
|
unless (
|
|
|
|
topic_embed =
|
|
|
|
TopicEmbed
|
|
|
|
.with_deleted
|
|
|
|
.where("deleted_at IS NOT NULL AND embed_url = ?", embed_url)
|
|
|
|
.first
|
|
|
|
).nil?
|
2017-04-24 13:29:04 -05:00
|
|
|
topic_embed.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-30 11:01:04 -05:00
|
|
|
class FetchResponse
|
2023-06-13 12:08:08 -05:00
|
|
|
attr_accessor :title, :body, :author, :url
|
2016-08-30 11:01:04 -05:00
|
|
|
end
|
|
|
|
|
2014-03-19 15:33:21 -05:00
|
|
|
def self.normalize_url(url)
|
2023-06-13 12:08:08 -05:00
|
|
|
# downcase
|
|
|
|
# remove trailing forward slash/
|
|
|
|
# remove consecutive hyphens
|
|
|
|
# remove leading and trailing whitespace
|
2023-01-20 12:52:49 -06:00
|
|
|
url.downcase.sub(%r{/\z}, "").sub(/\-+/, "-").strip
|
2014-03-19 15:33:21 -05:00
|
|
|
end
|
|
|
|
|
2014-04-02 14:54:21 -05:00
|
|
|
def self.imported_from_html(url)
|
2023-04-18 02:05:29 -05:00
|
|
|
url = UrlHelper.normalized_encode(url)
|
2022-08-02 13:49:28 -05:00
|
|
|
I18n.with_locale(SiteSetting.default_locale) do
|
2023-01-09 06:20:10 -06:00
|
|
|
"\n<hr>\n<small>#{I18n.t("embed.imported_from", link: "<a href='#{url}'>#{url}</a>")}</small>\n"
|
2022-08-02 13:49:28 -05:00
|
|
|
end
|
2014-04-02 14:54:21 -05:00
|
|
|
end
|
|
|
|
|
2013-12-31 13:37:43 -06:00
|
|
|
# Import an article from a source (RSS/Atom/Other)
|
2021-09-13 15:01:59 -05:00
|
|
|
def self.import(user, url, title, contents, category_id: nil, cook_method: nil, tags: nil)
|
2023-01-20 12:52:49 -06:00
|
|
|
return unless url =~ %r{\Ahttps?\://}
|
2013-12-31 13:37:43 -06:00
|
|
|
|
2024-01-05 07:09:31 -06:00
|
|
|
original_contents = contents.dup.truncate(EMBED_CONTENT_CACHE_MAX_LENGTH)
|
2023-01-09 06:20:10 -06:00
|
|
|
contents = first_paragraph_from(contents) if SiteSetting.embed_truncate && cook_method.nil?
|
|
|
|
contents ||= ""
|
2021-10-10 21:20:18 -05:00
|
|
|
contents = contents.dup << imported_from_html(url)
|
2013-12-31 13:37:43 -06:00
|
|
|
|
2014-03-26 22:24:57 -05:00
|
|
|
url = normalize_url(url)
|
|
|
|
|
2023-06-13 12:08:08 -05:00
|
|
|
embed = topic_embed_by_url(url)
|
2013-12-31 13:37:43 -06:00
|
|
|
content_sha1 = Digest::SHA1.hexdigest(contents)
|
|
|
|
post = nil
|
|
|
|
|
|
|
|
# If there is no embed, create a topic, post and the embed.
|
|
|
|
if embed.blank?
|
|
|
|
Topic.transaction do
|
2024-05-16 14:47:01 -05:00
|
|
|
if eh = EmbeddableHost.record_for_url(url)
|
|
|
|
tags = eh.tags.presence || tags
|
|
|
|
user = eh.user.presence || user
|
|
|
|
end
|
2015-08-18 16:15:46 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
cook_method ||=
|
|
|
|
if SiteSetting.embed_support_markdown
|
|
|
|
Post.cook_methods[:regular]
|
|
|
|
else
|
|
|
|
Post.cook_methods[:raw_html]
|
|
|
|
end
|
2018-03-10 20:26:47 -06:00
|
|
|
|
2020-04-13 14:17:02 -05:00
|
|
|
create_args = {
|
|
|
|
title: title,
|
|
|
|
raw: absolutize_urls(url, contents),
|
|
|
|
skip_validations: true,
|
|
|
|
cook_method: cook_method,
|
2021-09-13 15:01:59 -05:00
|
|
|
category: category_id || eh.try(:category_id),
|
|
|
|
tags: SiteSetting.tagging_enabled ? tags : nil,
|
2023-12-12 08:35:26 -06:00
|
|
|
embed_url: url,
|
|
|
|
embed_content_sha1: content_sha1,
|
2020-04-13 14:17:02 -05:00
|
|
|
}
|
2024-03-27 07:57:43 -05:00
|
|
|
create_args[:visible] = false if SiteSetting.import_embed_unlisted?
|
2020-04-13 14:17:02 -05:00
|
|
|
|
2024-04-05 10:37:53 -05:00
|
|
|
# always return `args` when using this modifier, e.g:
|
|
|
|
#
|
|
|
|
# plugin.register_modifier(:topic_embed_import_create_args) do |args|
|
|
|
|
# args[:title] = "MODIFIED: #{args[:title]}"
|
|
|
|
#
|
|
|
|
# args # returning args is important to prevent errors
|
|
|
|
# end
|
|
|
|
create_args =
|
|
|
|
DiscoursePluginRegistry.apply_modifier(:topic_embed_import_create_args, create_args) ||
|
|
|
|
create_args
|
|
|
|
|
2023-12-12 08:35:26 -06:00
|
|
|
post = PostCreator.create(user, create_args)
|
2024-01-05 07:09:31 -06:00
|
|
|
post.topic.topic_embed.update!(embed_content_cache: original_contents)
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
|
|
|
else
|
2014-03-18 17:02:33 -05:00
|
|
|
absolutize_urls(url, contents)
|
2013-12-31 13:37:43 -06:00
|
|
|
post = embed.post
|
2018-08-21 05:19:03 -05:00
|
|
|
|
2024-05-16 14:47:01 -05:00
|
|
|
if eh = EmbeddableHost.record_for_url(url)
|
|
|
|
tags = eh.tags.presence || tags
|
|
|
|
user = eh.user.presence || user
|
|
|
|
end
|
|
|
|
|
2013-12-31 13:37:43 -06:00
|
|
|
# Update the topic if it changed
|
2018-08-21 05:19:03 -05:00
|
|
|
if post&.topic
|
|
|
|
if post.user != user
|
|
|
|
PostOwnerChanger.new(
|
|
|
|
post_ids: [post.id],
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
new_owner: user,
|
2023-01-09 06:20:10 -06:00
|
|
|
acting_user: Discourse.system_user,
|
2018-08-21 05:19:03 -05:00
|
|
|
).change_owner!
|
|
|
|
|
|
|
|
# make sure the post returned has the right author
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
2024-05-16 14:47:01 -05:00
|
|
|
existing_tag_names = post.topic.tags.pluck(:name).sort
|
|
|
|
incoming_tag_names = Array(tags).map(&:name).sort
|
|
|
|
|
|
|
|
tags_changed = existing_tag_names != incoming_tag_names
|
|
|
|
|
|
|
|
if (content_sha1 != embed.content_sha1) || (title && title != post&.topic&.title) ||
|
|
|
|
tags_changed
|
2020-04-20 13:27:43 -05:00
|
|
|
changes = { raw: absolutize_urls(url, contents) }
|
2024-05-16 14:47:01 -05:00
|
|
|
|
|
|
|
changes[:tags] = incoming_tag_names if SiteSetting.tagging_enabled && tags_changed
|
2020-04-20 13:27:43 -05:00
|
|
|
changes[:title] = title if title.present?
|
|
|
|
|
|
|
|
post.revise(user, changes, skip_validations: true, bypass_rate_limiter: true)
|
2024-01-05 07:09:31 -06:00
|
|
|
embed.update!(content_sha1: content_sha1, embed_content_cache: original_contents)
|
2018-08-23 20:41:54 -05:00
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2014-04-01 17:16:56 -05:00
|
|
|
def self.find_remote(url)
|
2022-08-09 05:28:29 -05:00
|
|
|
url = UrlHelper.normalized_encode(url)
|
2023-04-27 04:57:06 -05:00
|
|
|
URI.parse(url) # ensure url parses, will raise if not
|
2023-01-09 06:20:10 -06:00
|
|
|
fd = FinalDestination.new(url, validate_uri: true, max_redirects: 5, follow_canonical: true)
|
2020-05-27 10:23:55 -05:00
|
|
|
|
2021-10-27 03:39:28 -05:00
|
|
|
uri = fd.resolve
|
|
|
|
return if uri.blank?
|
2020-05-22 23:56:13 -05:00
|
|
|
|
2023-04-27 04:57:06 -05:00
|
|
|
begin
|
2023-10-26 22:02:20 -05:00
|
|
|
html = FinalDestination::HTTP.get(uri)
|
|
|
|
rescue OpenURI::HTTPError, Net::OpenTimeout, FinalDestination::SSRFDetector::DisallowedIpError
|
2023-04-27 04:57:06 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-06-13 12:08:08 -05:00
|
|
|
parse_html(html, uri.to_s)
|
2023-04-27 04:57:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse_html(html, url)
|
|
|
|
require "ruby-readability"
|
|
|
|
|
2014-04-14 23:06:51 -05:00
|
|
|
opts = {
|
2023-04-27 04:57:06 -05:00
|
|
|
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote figure figcaption],
|
2015-09-24 17:20:59 -05:00
|
|
|
attributes: %w[href src class],
|
2023-01-09 06:20:10 -06:00
|
|
|
remove_empty_nodes: false,
|
2014-04-14 23:06:51 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
opts[
|
|
|
|
:whitelist
|
|
|
|
] = SiteSetting.allowed_embed_selectors if SiteSetting.allowed_embed_selectors.present?
|
|
|
|
opts[
|
|
|
|
:blacklist
|
|
|
|
] = SiteSetting.blocked_embed_selectors if SiteSetting.blocked_embed_selectors.present?
|
|
|
|
allowed_embed_classnames =
|
|
|
|
SiteSetting.allowed_embed_classnames if SiteSetting.allowed_embed_classnames.present?
|
2014-04-14 23:06:51 -05:00
|
|
|
|
2016-08-30 11:01:04 -05:00
|
|
|
response = FetchResponse.new
|
2014-04-02 14:54:21 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
raw_doc = Nokogiri.HTML5(html)
|
2023-06-13 12:08:08 -05:00
|
|
|
|
|
|
|
response.url = url
|
|
|
|
|
2023-02-28 06:31:59 -06:00
|
|
|
auth_element =
|
|
|
|
raw_doc.at('meta[@name="discourse-username"]') || raw_doc.at('meta[@name="author"]')
|
2016-08-30 11:01:04 -05:00
|
|
|
if auth_element.present?
|
|
|
|
response.author = User.where(username_lower: auth_element[:content].strip).first
|
|
|
|
end
|
|
|
|
|
|
|
|
read_doc = Readability::Document.new(html, opts)
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
title = +(raw_doc.title || "")
|
2016-08-22 11:43:02 -05:00
|
|
|
title.strip!
|
|
|
|
|
|
|
|
if SiteSetting.embed_title_scrubber.present?
|
2023-01-09 06:20:10 -06:00
|
|
|
title.sub!(Regexp.new(SiteSetting.embed_title_scrubber), "")
|
2016-08-22 11:43:02 -05:00
|
|
|
title.strip!
|
|
|
|
end
|
2016-08-30 11:01:04 -05:00
|
|
|
response.title = title
|
2023-01-09 06:20:10 -06:00
|
|
|
doc = Nokogiri.HTML5(read_doc.content)
|
|
|
|
|
|
|
|
tags = { "img" => "src", "script" => "src", "a" => "href" }
|
|
|
|
doc
|
|
|
|
.search(tags.keys.join(","))
|
|
|
|
.each do |node|
|
|
|
|
url_param = tags[node.name]
|
|
|
|
src = node[url_param]
|
|
|
|
unless (src.nil? || src.empty?)
|
|
|
|
begin
|
|
|
|
# convert URL to absolute form
|
|
|
|
node[url_param] = URI.join(url, UrlHelper.normalized_encode(src)).to_s
|
|
|
|
rescue URI::Error, Addressable::URI::InvalidURIError
|
|
|
|
# If there is a mistyped URL, just do nothing
|
|
|
|
end
|
2015-09-24 17:20:59 -05:00
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
# only allow classes in the allowlist
|
|
|
|
allowed_classes =
|
|
|
|
if allowed_embed_classnames.blank?
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
allowed_embed_classnames.split(/[ ,]+/i)
|
|
|
|
end
|
|
|
|
doc
|
|
|
|
.search('[class]:not([class=""])')
|
|
|
|
.each do |classnode|
|
|
|
|
classes =
|
|
|
|
classnode[:class]
|
|
|
|
.split(" ")
|
|
|
|
.select { |classname| allowed_classes.include?(classname) }
|
|
|
|
if classes.length === 0
|
|
|
|
classnode.delete("class")
|
|
|
|
else
|
|
|
|
classnode[:class] = classes.join(" ")
|
|
|
|
end
|
|
|
|
end
|
2015-09-24 17:20:59 -05:00
|
|
|
end
|
2014-04-02 14:54:21 -05:00
|
|
|
|
2023-04-27 04:57:06 -05:00
|
|
|
response.body = doc.at("body").children.to_html
|
2016-08-30 11:01:04 -05:00
|
|
|
response
|
2014-04-01 17:16:56 -05:00
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
|
2023-02-28 06:31:59 -06:00
|
|
|
def self.import_remote(url, opts = nil)
|
2014-04-01 17:16:56 -05:00
|
|
|
opts = opts || {}
|
2016-08-30 11:01:04 -05:00
|
|
|
response = find_remote(url)
|
2017-09-22 10:36:44 -05:00
|
|
|
return if response.nil?
|
|
|
|
|
2016-08-30 11:01:04 -05:00
|
|
|
response.title = opts[:title] if opts[:title].present?
|
2023-02-28 06:31:59 -06:00
|
|
|
import_user = opts[:user] if opts[:user].present?
|
2016-08-30 11:01:04 -05:00
|
|
|
import_user = response.author if response.author.present?
|
2023-06-13 12:08:08 -05:00
|
|
|
url = normalize_url(response.url) if response.url.present?
|
2016-08-30 11:01:04 -05:00
|
|
|
|
|
|
|
TopicEmbed.import(import_user, url, response.title, response.body)
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Convert any relative URLs to absolute. RSS is annoying for this.
|
|
|
|
def self.absolutize_urls(url, contents)
|
2014-03-19 15:33:21 -05:00
|
|
|
url = normalize_url(url)
|
2020-02-07 09:54:24 -06:00
|
|
|
begin
|
2022-08-09 05:28:29 -05:00
|
|
|
uri = URI(UrlHelper.normalized_encode(url))
|
2020-02-07 09:54:24 -06:00
|
|
|
rescue URI::Error
|
|
|
|
return contents
|
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
prefix = "#{uri.scheme}://#{uri.host}"
|
2020-03-25 10:57:31 -05:00
|
|
|
prefix += ":#{uri.port}" if uri.port != 80 && uri.port != 443
|
2013-12-31 13:37:43 -06:00
|
|
|
|
2020-05-04 22:46:57 -05:00
|
|
|
fragment = Nokogiri::HTML5.fragment("<div>#{contents}</div>")
|
2023-01-09 06:20:10 -06:00
|
|
|
fragment
|
|
|
|
.css("a")
|
|
|
|
.each do |a|
|
|
|
|
if a["href"].present?
|
|
|
|
begin
|
|
|
|
a["href"] = URI.join(prefix, a["href"]).to_s
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# NOOP, URL is malformed
|
|
|
|
end
|
2022-01-20 20:03:49 -06:00
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
2022-01-20 20:03:49 -06:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
fragment
|
|
|
|
.css("img")
|
|
|
|
.each do |a|
|
|
|
|
if a["src"].present?
|
|
|
|
begin
|
|
|
|
a["src"] = URI.join(prefix, a["src"]).to_s
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# NOOP, URL is malformed
|
|
|
|
end
|
2022-01-20 20:03:49 -06:00
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
2022-01-20 20:03:49 -06:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
fragment.at("div").inner_html
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
|
|
|
|
2023-06-13 12:08:08 -05:00
|
|
|
def self.topic_embed_by_url(embed_url)
|
2023-01-20 12:52:49 -06:00
|
|
|
embed_url = normalize_url(embed_url).sub(%r{\Ahttps?\://}, "")
|
2023-06-13 12:08:08 -05:00
|
|
|
TopicEmbed.where("embed_url ~* ?", "^https?://#{Regexp.escape(embed_url)}$").first
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.topic_id_for_embed(embed_url)
|
|
|
|
topic_embed = topic_embed_by_url(embed_url)
|
|
|
|
topic_embed&.topic_id
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
|
|
|
|
2014-03-18 17:02:33 -05:00
|
|
|
def self.first_paragraph_from(html)
|
2023-01-09 06:20:10 -06:00
|
|
|
doc = Nokogiri.HTML5(html)
|
2014-03-18 17:02:33 -05:00
|
|
|
|
2019-05-02 17:17:27 -05:00
|
|
|
result = +""
|
2023-01-09 06:20:10 -06:00
|
|
|
doc
|
|
|
|
.css("p")
|
|
|
|
.each do |p|
|
|
|
|
if p.text.present?
|
|
|
|
result << p.to_s
|
|
|
|
return result if result.size >= 100
|
|
|
|
end
|
2014-03-18 17:02:33 -05:00
|
|
|
end
|
|
|
|
return result unless result.blank?
|
|
|
|
|
2021-05-20 20:43:47 -05:00
|
|
|
# If there is no first paragraph, return the first div (onebox)
|
2023-01-09 06:20:10 -06:00
|
|
|
doc.css("div").first.to_s
|
2014-03-18 17:02:33 -05:00
|
|
|
end
|
2014-04-03 10:30:43 -05:00
|
|
|
|
|
|
|
def self.expanded_for(post)
|
2023-01-09 06:20:10 -06:00
|
|
|
Discourse
|
|
|
|
.cache
|
|
|
|
.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do
|
2023-02-12 22:39:45 -06:00
|
|
|
url = TopicEmbed.where(topic_id: post.topic_id).pick(:embed_url)
|
2023-01-09 06:20:10 -06:00
|
|
|
response = TopicEmbed.find_remote(url)
|
|
|
|
|
|
|
|
body = response.body
|
2024-01-05 07:09:31 -06:00
|
|
|
if post&.topic&.topic_embed && body.present?
|
|
|
|
post.topic.topic_embed.update!(
|
|
|
|
embed_content_cache: body.truncate(EMBED_CONTENT_CACHE_MAX_LENGTH),
|
|
|
|
)
|
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
body << TopicEmbed.imported_from_html(url)
|
|
|
|
body
|
|
|
|
end
|
2014-04-03 10:30:43 -05:00
|
|
|
end
|
2013-12-31 13:37:43 -06:00
|
|
|
end
|
2014-02-06 18:07:36 -06:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topic_embeds
|
|
|
|
#
|
2024-01-05 07:09:31 -06:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# topic_id :integer not null
|
|
|
|
# post_id :integer not null
|
|
|
|
# embed_url :string(1000) not null
|
|
|
|
# content_sha1 :string(40)
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# deleted_at :datetime
|
|
|
|
# deleted_by_id :integer
|
|
|
|
# embed_content_cache :text
|
2014-02-06 18:07:36 -06:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_topic_embeds_on_embed_url (embed_url) UNIQUE
|
|
|
|
#
|