mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 02:50:00 +08:00
a78df3d57d
Feature to allow each imported post to be created using a different discourse username. A possible use case of this is a multi-author blog where discourse is being used to track comments. This feature allows authors to receive updates when someone leaves a comment on one of their articles because each of the imported posts can be created using the discourse username of the author.
63 lines
1.5 KiB
Ruby
63 lines
1.5 KiB
Ruby
class TopicRetriever
|
|
|
|
def initialize(embed_url, opts=nil)
|
|
@embed_url = embed_url
|
|
@author_username = opts[:author_username]
|
|
@opts = opts || {}
|
|
end
|
|
|
|
def retrieve
|
|
perform_retrieve unless (invalid_host? || retrieved_recently?)
|
|
end
|
|
|
|
private
|
|
|
|
def invalid_host?
|
|
SiteSetting.normalized_embeddable_host != URI(@embed_url).host
|
|
rescue URI::InvalidURIError
|
|
# An invalid URI is an invalid host
|
|
true
|
|
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
|
|
retrieved_key = "retrieved:#{@embed_url}"
|
|
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
|
|
if @author_username.nil?
|
|
username = SiteSetting.embed_by_username.downcase
|
|
else
|
|
username = @author_username
|
|
end
|
|
|
|
user = User.where(username_lower: username.downcase).first
|
|
return if user.blank?
|
|
|
|
TopicEmbed.import_remote(user, @embed_url)
|
|
end
|
|
|
|
end
|