2013-12-31 14:37:43 -05:00
|
|
|
class TopicRetriever
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(embed_url, opts = nil)
|
2013-12-31 14:37:43 -05:00
|
|
|
@embed_url = embed_url
|
2014-04-24 12:48:45 -04:00
|
|
|
@author_username = opts[:author_username]
|
2013-12-31 14:37:43 -05:00
|
|
|
@opts = opts || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve
|
2016-08-23 14:55:52 -04:00
|
|
|
perform_retrieve unless (invalid_url? || retrieved_recently?)
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
def invalid_url?
|
|
|
|
!EmbeddableHost.url_allowed?(@embed_url)
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def retrieved_recently?
|
|
|
|
# We can disable the throttle for some users, such as staff
|
|
|
|
return false if @opts[:no_throttle]
|
|
|
|
|
|
|
|
# Throttle other users to once every 60 seconds
|
2016-11-16 12:04:16 -05:00
|
|
|
retrieved_key = "retrieved_topic"
|
2013-12-31 14:37:43 -05:00
|
|
|
if $redis.setnx(retrieved_key, "1")
|
|
|
|
$redis.expire(retrieved_key, 60)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_retrieve
|
|
|
|
# It's possible another process or job found the embed already. So if that happened bail out.
|
|
|
|
return if TopicEmbed.where(embed_url: @embed_url).exists?
|
|
|
|
|
|
|
|
# First check RSS if that is enabled
|
|
|
|
if SiteSetting.feed_polling_enabled?
|
|
|
|
Jobs::PollFeed.new.execute({})
|
|
|
|
return if TopicEmbed.where(embed_url: @embed_url).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
fetch_http
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_http
|
2014-04-24 12:48:45 -04:00
|
|
|
if @author_username.nil?
|
|
|
|
username = SiteSetting.embed_by_username.downcase
|
|
|
|
else
|
|
|
|
username = @author_username
|
|
|
|
end
|
|
|
|
|
|
|
|
user = User.where(username_lower: username.downcase).first
|
2013-12-31 14:37:43 -05:00
|
|
|
return if user.blank?
|
|
|
|
|
|
|
|
TopicEmbed.import_remote(user, @embed_url)
|
|
|
|
end
|
|
|
|
|
2014-02-12 15:55:44 -05:00
|
|
|
end
|