2013-02-06 03:16:51 +08:00
|
|
|
require 'uri'
|
|
|
|
require_dependency 'slug'
|
2017-09-15 13:02:11 +08:00
|
|
|
require_dependency 'discourse'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class TopicLink < ActiveRecord::Base
|
2015-09-26 02:07:04 +08:00
|
|
|
|
|
|
|
def self.max_domain_length
|
|
|
|
100
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.max_url_length
|
|
|
|
500
|
|
|
|
end
|
2014-06-26 09:38:23 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :link_topic, class_name: 'Topic'
|
2014-03-18 10:12:07 +08:00
|
|
|
belongs_to :link_post, class_name: 'Post'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
validates_presence_of :url
|
|
|
|
|
|
|
|
validates_length_of :url, maximum: 500
|
|
|
|
|
|
|
|
validates_uniqueness_of :url, scope: [:topic_id, :post_id]
|
|
|
|
|
2013-06-14 01:41:45 +08:00
|
|
|
has_many :topic_link_clicks, dependent: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
validate :link_to_self
|
|
|
|
|
2014-04-06 02:47:25 +08:00
|
|
|
after_commit :crawl_link_title
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Make sure a topic can't link to itself
|
|
|
|
def link_to_self
|
|
|
|
errors.add(:base, "can't link to the same topic") if (topic_id == link_topic_id)
|
|
|
|
end
|
|
|
|
|
2013-11-16 01:15:46 +08:00
|
|
|
def self.topic_map(guardian, topic_id)
|
2013-06-05 14:10:26 +08:00
|
|
|
|
|
|
|
# Sam: complicated reports are really hard in AR
|
2019-04-12 21:55:27 +08:00
|
|
|
builder = DB.build(<<~SQL)
|
|
|
|
SELECT ftl.url,
|
|
|
|
COALESCE(ft.title, ftl.title) AS title,
|
|
|
|
ftl.link_topic_id,
|
|
|
|
ftl.reflection,
|
|
|
|
ftl.internal,
|
|
|
|
ftl.domain,
|
|
|
|
MIN(ftl.user_id) AS user_id,
|
|
|
|
SUM(clicks) AS clicks
|
|
|
|
FROM topic_links AS ftl
|
|
|
|
LEFT JOIN topics AS ft ON ftl.link_topic_id = ft.id
|
|
|
|
LEFT JOIN categories AS c ON c.id = ft.category_id
|
|
|
|
/*where*/
|
|
|
|
GROUP BY ftl.url, ft.title, ftl.title, ftl.link_topic_id, ftl.reflection, ftl.internal, ftl.domain
|
|
|
|
ORDER BY clicks DESC, count(*) DESC
|
|
|
|
LIMIT 50
|
|
|
|
SQL
|
2013-06-05 14:10:26 +08:00
|
|
|
|
|
|
|
builder.where('ftl.topic_id = :topic_id', topic_id: topic_id)
|
|
|
|
builder.where('ft.deleted_at IS NULL')
|
2017-07-23 04:18:15 +08:00
|
|
|
# note that ILIKE means "case insensitive LIKE"
|
|
|
|
builder.where("NOT(ftl.url ILIKE '%.png' OR ftl.url ILIKE '%.jpg' OR ftl.url ILIKE '%.gif')")
|
2014-05-12 03:53:57 +08:00
|
|
|
builder.where("COALESCE(ft.archetype, 'regular') <> :archetype", archetype: Archetype.private_message)
|
2018-07-18 12:14:50 +08:00
|
|
|
builder.where("clicks > 0")
|
2013-06-05 14:10:26 +08:00
|
|
|
|
|
|
|
builder.secure_category(guardian.secure_category_ids)
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
builder.query
|
2013-06-05 14:10:26 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.counts_for(guardian, topic, posts)
|
2013-06-05 14:10:26 +08:00
|
|
|
return {} if posts.blank?
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
# Sam: this is not tidy in AR and also happens to be a critical path
|
|
|
|
# for topic view
|
|
|
|
builder = DB.build("SELECT
|
2013-06-05 14:10:26 +08:00
|
|
|
l.post_id,
|
|
|
|
l.url,
|
|
|
|
l.clicks,
|
2014-04-06 02:47:25 +08:00
|
|
|
COALESCE(t.title, l.title) AS title,
|
2013-06-05 14:10:26 +08:00
|
|
|
l.internal,
|
2014-04-06 02:47:25 +08:00
|
|
|
l.reflection,
|
|
|
|
l.domain
|
2013-06-05 14:10:26 +08:00
|
|
|
FROM topic_links l
|
|
|
|
LEFT JOIN topics t ON t.id = l.link_topic_id
|
|
|
|
LEFT JOIN categories AS c ON c.id = t.category_id
|
|
|
|
/*where*/
|
|
|
|
ORDER BY reflection ASC, clicks DESC")
|
|
|
|
|
|
|
|
builder.where('t.deleted_at IS NULL')
|
2014-05-12 03:53:57 +08:00
|
|
|
builder.where("COALESCE(t.archetype, 'regular') <> :archetype", archetype: Archetype.private_message)
|
2013-06-05 14:10:26 +08:00
|
|
|
|
|
|
|
# not certain if pluck is right, cause it may interfere with caching
|
2018-06-19 14:13:14 +08:00
|
|
|
builder.where('l.post_id in (:post_ids)', post_ids: posts.map(&:id))
|
2013-06-05 14:10:26 +08:00
|
|
|
builder.secure_category(guardian.secure_category_ids)
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
result = {}
|
|
|
|
builder.query.each do |l|
|
2013-06-05 14:10:26 +08:00
|
|
|
result[l.post_id] ||= []
|
2017-07-28 09:20:09 +08:00
|
|
|
result[l.post_id] << { url: l.url,
|
|
|
|
clicks: l.clicks,
|
|
|
|
title: l.title,
|
|
|
|
internal: l.internal,
|
|
|
|
reflection: l.reflection }
|
2013-06-05 14:10:26 +08:00
|
|
|
end
|
2018-06-19 14:13:14 +08:00
|
|
|
result
|
2013-06-05 14:10:26 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.extract_from(post)
|
2017-12-21 00:55:15 +08:00
|
|
|
return if post.blank? || post.whisper?
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2018-12-06 01:21:50 +08:00
|
|
|
current_urls = []
|
2018-10-18 10:52:45 +08:00
|
|
|
reflected_ids = []
|
|
|
|
|
|
|
|
PrettyText
|
|
|
|
.extract_links(post.cooked)
|
|
|
|
.map do |u|
|
2018-12-11 15:03:13 +08:00
|
|
|
uri = UrlHelper.relaxed_parse(u.url)
|
2018-10-18 10:52:45 +08:00
|
|
|
[u, uri]
|
|
|
|
end
|
|
|
|
.reject { |_, p| p.nil? || "mailto".freeze == p.scheme }
|
|
|
|
.uniq { |_, p| p }
|
|
|
|
.each do |link, parsed|
|
2018-03-28 16:20:08 +08:00
|
|
|
|
2018-10-18 10:52:45 +08:00
|
|
|
TopicLink.transaction do
|
2013-02-06 03:16:51 +08:00
|
|
|
begin
|
2018-12-06 01:21:50 +08:00
|
|
|
url, reflected_id = self.ensure_entry_for(post, link, parsed)
|
|
|
|
current_urls << url unless url.nil?
|
|
|
|
reflected_ids << reflected_id unless reflected_id.nil?
|
2018-08-14 18:23:32 +08:00
|
|
|
rescue URI::Error
|
2013-02-06 03:16:51 +08:00
|
|
|
# if the URI is invalid, don't store it.
|
|
|
|
rescue ActionController::RoutingError
|
2013-02-07 23:45:24 +08:00
|
|
|
# If we can't find the route, no big deal
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2018-12-06 01:16:27 +08:00
|
|
|
end
|
|
|
|
|
2018-12-06 01:21:50 +08:00
|
|
|
self.cleanup_entries(post, current_urls, reflected_ids)
|
2018-12-06 01:16:27 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-04-06 02:47:25 +08:00
|
|
|
|
|
|
|
# Crawl a link's title after it's saved
|
|
|
|
def crawl_link_title
|
|
|
|
Jobs.enqueue(:crawl_topic_link, topic_link_id: id)
|
|
|
|
end
|
2016-06-07 04:58:35 +08:00
|
|
|
|
|
|
|
def self.duplicate_lookup(topic)
|
2016-06-09 00:35:11 +08:00
|
|
|
results = TopicLink
|
2017-07-28 09:20:09 +08:00
|
|
|
.includes(:post, :user)
|
|
|
|
.joins(:post, :user)
|
|
|
|
.where("posts.id IS NOT NULL AND users.id IS NOT NULL")
|
|
|
|
.where(topic_id: topic.id, reflection: false)
|
|
|
|
.last(200)
|
2016-06-07 04:58:35 +08:00
|
|
|
|
|
|
|
lookup = {}
|
2016-06-09 00:35:11 +08:00
|
|
|
results.each do |tl|
|
2016-06-09 05:20:32 +08:00
|
|
|
normalized = tl.url.downcase.sub(/^https?:\/\//, '').sub(/\/$/, '')
|
2016-06-09 00:35:11 +08:00
|
|
|
lookup[normalized] = { domain: tl.domain,
|
2016-06-13 17:11:25 +08:00
|
|
|
username: tl.user.username_lower,
|
2016-06-10 01:02:44 +08:00
|
|
|
posted_at: tl.post.created_at,
|
|
|
|
post_number: tl.post.post_number }
|
2016-06-07 04:58:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
lookup
|
|
|
|
end
|
2018-12-06 01:21:50 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.ensure_entry_for(post, link, parsed)
|
|
|
|
url = link.url
|
|
|
|
internal = false
|
|
|
|
topic_id = nil
|
|
|
|
post_number = nil
|
|
|
|
|
|
|
|
if upload = Upload.get_from_url(url)
|
|
|
|
internal = Discourse.store.internal?
|
|
|
|
# Store the same URL that will be used in the cooked version of the post
|
|
|
|
url = UrlHelper.cook_url(upload.url)
|
|
|
|
elsif route = Discourse.route_for(parsed)
|
|
|
|
internal = true
|
|
|
|
|
|
|
|
# We aren't interested in tracking internal links to users
|
|
|
|
return nil if route[:controller] == 'users'
|
|
|
|
|
|
|
|
topic_id = route[:topic_id].to_i
|
|
|
|
post_number = route[:post_number] || 1
|
|
|
|
|
|
|
|
# Store the canonical URL
|
|
|
|
topic = Topic.find_by(id: topic_id)
|
|
|
|
topic_id = nil unless topic
|
|
|
|
|
|
|
|
if topic.present?
|
|
|
|
url = "#{Discourse.base_url_no_prefix}#{topic.relative_url}"
|
|
|
|
url << "/#{post_number}" if post_number.to_i > 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Skip linking to ourselves
|
|
|
|
return nil if topic_id == post.topic_id
|
|
|
|
|
|
|
|
reflected_post = nil
|
|
|
|
if post_number && topic_id
|
|
|
|
reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i)
|
|
|
|
end
|
|
|
|
|
2019-01-03 19:59:22 +08:00
|
|
|
url = url[0...TopicLink.max_url_length]
|
2018-12-06 01:21:50 +08:00
|
|
|
return nil if parsed && parsed.host && parsed.host.length > TopicLink.max_domain_length
|
|
|
|
|
|
|
|
unless TopicLink.exists?(topic_id: post.topic_id, post_id: post.id, url: url)
|
|
|
|
file_extension = File.extname(parsed.path)[1..10].downcase unless parsed.path.nil? || File.extname(parsed.path).empty?
|
|
|
|
begin
|
|
|
|
TopicLink.create!(post_id: post.id,
|
|
|
|
user_id: post.user_id,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
url: url,
|
|
|
|
domain: parsed.host || Discourse.current_hostname,
|
|
|
|
internal: internal,
|
|
|
|
link_topic_id: topic_id,
|
|
|
|
link_post_id: reflected_post.try(:id),
|
|
|
|
quote: link.is_quote,
|
|
|
|
extension: file_extension)
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# it's fine
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
reflected_id = nil
|
|
|
|
|
|
|
|
# Create the reflection if we can
|
|
|
|
if topic_id.present?
|
|
|
|
topic = Topic.find_by(id: topic_id)
|
|
|
|
|
|
|
|
if topic && post.topic && topic.archetype != 'private_message' && post.topic.archetype != 'private_message' && post.topic.visible?
|
|
|
|
prefix = Discourse.base_url_no_prefix
|
|
|
|
reflected_url = "#{prefix}#{post.topic.relative_url(post.post_number)}"
|
|
|
|
tl = TopicLink.find_by(topic_id: topic_id,
|
|
|
|
post_id: reflected_post.try(:id),
|
|
|
|
url: reflected_url)
|
|
|
|
|
|
|
|
unless tl
|
|
|
|
tl = TopicLink.create(user_id: post.user_id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
post_id: reflected_post.try(:id),
|
|
|
|
url: reflected_url,
|
|
|
|
domain: Discourse.current_hostname,
|
|
|
|
reflection: true,
|
|
|
|
internal: true,
|
|
|
|
link_topic_id: post.topic_id,
|
|
|
|
link_post_id: post.id)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
reflected_id = tl.id if tl.persisted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[url, reflected_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cleanup_entries(post, current_urls, current_reflected_ids)
|
|
|
|
# Remove links that aren't there anymore
|
|
|
|
if current_urls.present?
|
|
|
|
TopicLink.where(
|
|
|
|
"(url not in (:urls)) AND (post_id = :post_id AND NOT reflection)",
|
|
|
|
urls: current_urls, post_id: post.id
|
|
|
|
).delete_all
|
|
|
|
|
|
|
|
current_reflected_ids.compact!
|
|
|
|
if current_reflected_ids.present?
|
|
|
|
TopicLink.where(
|
|
|
|
"(id not in (:reflected_ids)) AND (link_post_id = :post_id AND reflection)",
|
|
|
|
reflected_ids: current_reflected_ids, post_id: post.id
|
|
|
|
).delete_all
|
|
|
|
else
|
|
|
|
TopicLink
|
|
|
|
.where("link_post_id = :post_id AND reflection", post_id: post.id)
|
|
|
|
.delete_all
|
|
|
|
end
|
|
|
|
else
|
|
|
|
TopicLink
|
|
|
|
.where(
|
|
|
|
"(post_id = :post_id AND NOT reflection) OR (link_post_id = :post_id AND reflection)",
|
|
|
|
post_id: post.id
|
|
|
|
)
|
|
|
|
.delete_all
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topic_links
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# topic_id :integer not null
|
|
|
|
# post_id :integer
|
|
|
|
# user_id :integer not null
|
|
|
|
# url :string(500) not null
|
|
|
|
# domain :string(100) not null
|
|
|
|
# internal :boolean default(FALSE), not null
|
|
|
|
# link_topic_id :integer
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-24 10:48:32 +08:00
|
|
|
# reflection :boolean default(FALSE)
|
|
|
|
# clicks :integer default(0), not null
|
|
|
|
# link_post_id :integer
|
2019-01-12 03:29:56 +08:00
|
|
|
# title :string
|
2014-04-08 23:35:44 +08:00
|
|
|
# crawled_at :datetime
|
2014-07-15 09:29:44 +08:00
|
|
|
# quote :boolean default(FALSE), not null
|
2017-08-16 22:38:11 +08:00
|
|
|
# extension :string(10)
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-01-12 03:29:56 +08:00
|
|
|
# index_topic_links_on_extension (extension)
|
|
|
|
# index_topic_links_on_link_post_id_and_reflection (link_post_id,reflection)
|
|
|
|
# index_topic_links_on_post_id (post_id)
|
|
|
|
# index_topic_links_on_topic_id (topic_id)
|
2019-04-26 16:23:27 +08:00
|
|
|
# index_topic_links_on_user_id (user_id)
|
2019-01-12 03:29:56 +08:00
|
|
|
# unique_post_links (topic_id,post_id,url) UNIQUE
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|