Merge branch 'justinleveck-add_custom_embed_by_username'
This commit is contained in:
commit
61a3a6f544
|
@ -13,8 +13,7 @@ module Jobs
|
||||||
if args[:user_id]
|
if args[:user_id]
|
||||||
user = User.find_by(id: args[:user_id])
|
user = User.find_by(id: args[:user_id])
|
||||||
end
|
end
|
||||||
|
TopicRetriever.new(args[:embed_url], author_username: args[:author_username], no_throttle: user.try(:staff?)).retrieve
|
||||||
TopicRetriever.new(args[:embed_url], no_throttle: user.try(:staff?)).retrieve
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,8 +14,7 @@ module Jobs
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
poll_feed if SiteSetting.feed_polling_enabled? &&
|
poll_feed if SiteSetting.feed_polling_enabled? &&
|
||||||
SiteSetting.feed_polling_url.present? &&
|
SiteSetting.feed_polling_url.present?
|
||||||
SiteSetting.embed_by_username.present?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def feed_key
|
def feed_key
|
||||||
|
@ -23,22 +22,111 @@ module Jobs
|
||||||
end
|
end
|
||||||
|
|
||||||
def poll_feed
|
def poll_feed
|
||||||
user = User.find_by(username_lower: SiteSetting.embed_by_username.downcase)
|
feed = Feed.new
|
||||||
return if user.blank?
|
import_topics(feed.topics)
|
||||||
|
end
|
||||||
|
|
||||||
require 'simple-rss'
|
private
|
||||||
rss = SimpleRSS.parse open(SiteSetting.feed_polling_url)
|
|
||||||
|
|
||||||
rss.items.each do |i|
|
def import_topics(feed_topics)
|
||||||
url = i.link
|
feed_topics.each do |topic|
|
||||||
url = i.id if url.blank? || url !~ /^https?\:\/\//
|
import_topic(topic)
|
||||||
|
|
||||||
content = i.content || i.description
|
|
||||||
if content
|
|
||||||
TopicEmbed.import(user, url, i.title, CGI.unescapeHTML(content.scrub))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def import_topic(topic)
|
||||||
|
if topic.user
|
||||||
|
TopicEmbed.import(topic.user, topic.url, topic.title, CGI.unescapeHTML(topic.content.scrub))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Feed
|
||||||
|
require 'simple-rss'
|
||||||
|
SimpleRSS.item_tags << SiteSetting.embed_username_key_from_feed.to_sym
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@feed_url = SiteSetting.feed_polling_url
|
||||||
|
end
|
||||||
|
|
||||||
|
def topics
|
||||||
|
feed_topics = []
|
||||||
|
|
||||||
|
rss.items.each do |i|
|
||||||
|
current_feed_topic = FeedTopic.new(i)
|
||||||
|
feed_topics << current_feed_topic if current_feed_topic.content
|
||||||
|
end
|
||||||
|
|
||||||
|
return feed_topics
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def rss
|
||||||
|
SimpleRSS.parse open(@feed_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class FeedTopic
|
||||||
|
def initialize(article_rss_item)
|
||||||
|
@article_rss_item = article_rss_item
|
||||||
|
end
|
||||||
|
|
||||||
|
def url
|
||||||
|
link = @article_rss_item.link
|
||||||
|
if url?(link)
|
||||||
|
return link
|
||||||
|
else
|
||||||
|
return @article_rss_item.id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def content
|
||||||
|
@article_rss_item.content || @article_rss_item.description
|
||||||
|
end
|
||||||
|
|
||||||
|
def title
|
||||||
|
@article_rss_item.title
|
||||||
|
end
|
||||||
|
|
||||||
|
def user
|
||||||
|
author_user || default_user
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def url?(link)
|
||||||
|
if link.blank? || link !~ /^https?\:\/\//
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def author_username
|
||||||
|
begin
|
||||||
|
@article_rss_item.send(SiteSetting.embed_username_key_from_feed.to_sym)
|
||||||
|
rescue
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def default_user
|
||||||
|
find_user(SiteSetting.embed_by_username.downcase)
|
||||||
|
end
|
||||||
|
|
||||||
|
def author_user
|
||||||
|
return nil if !author_username.present?
|
||||||
|
|
||||||
|
find_user(author_username)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_user(user_name)
|
||||||
|
User.where(username_lower: user_name).first
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -903,6 +903,7 @@ en:
|
||||||
feed_polling_enabled: "Whether to import a RSS/ATOM feed as posts"
|
feed_polling_enabled: "Whether to import a RSS/ATOM feed as posts"
|
||||||
feed_polling_url: "URL of RSS/ATOM feed to import"
|
feed_polling_url: "URL of RSS/ATOM feed to import"
|
||||||
embed_by_username: "Discourse username of the user who creates the topics"
|
embed_by_username: "Discourse username of the user who creates the topics"
|
||||||
|
embed_username_key_from_feed: "Key to pull discourse username from feed"
|
||||||
embed_truncate: "Truncate the imported posts"
|
embed_truncate: "Truncate the imported posts"
|
||||||
embed_category: "Category of created topics"
|
embed_category: "Category of created topics"
|
||||||
embed_post_limit: "Maximum number of posts to embed"
|
embed_post_limit: "Maximum number of posts to embed"
|
||||||
|
|
|
@ -518,6 +518,7 @@ embedding:
|
||||||
embed_by_username:
|
embed_by_username:
|
||||||
default: ''
|
default: ''
|
||||||
type: username
|
type: username
|
||||||
|
embed_username_key_from_feed: ''
|
||||||
embed_category: ''
|
embed_category: ''
|
||||||
embed_post_limit: 100
|
embed_post_limit: 100
|
||||||
embed_truncate: false
|
embed_truncate: false
|
||||||
|
|
|
@ -2,6 +2,7 @@ class TopicRetriever
|
||||||
|
|
||||||
def initialize(embed_url, opts=nil)
|
def initialize(embed_url, opts=nil)
|
||||||
@embed_url = embed_url
|
@embed_url = embed_url
|
||||||
|
@author_username = opts[:author_username]
|
||||||
@opts = opts || {}
|
@opts = opts || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,7 +47,13 @@ class TopicRetriever
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_http
|
def fetch_http
|
||||||
user = User.find_by(username_lower: SiteSetting.embed_by_username.downcase)
|
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?
|
return if user.blank?
|
||||||
|
|
||||||
TopicEmbed.import_remote(user, @embed_url)
|
TopicEmbed.import_remote(user, @embed_url)
|
||||||
|
|
|
@ -1,10 +1,24 @@
|
||||||
/* global discourseUrl */
|
/* global discourseUrl */
|
||||||
|
/* global discourseUserName */
|
||||||
/* global discourseEmbedUrl */
|
/* global discourseEmbedUrl */
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
var comments = document.getElementById('discourse-comments'),
|
var comments = document.getElementById('discourse-comments'),
|
||||||
iframe = document.createElement('iframe');
|
iframe = document.createElement('iframe');
|
||||||
iframe.src = discourseUrl + "embed/comments?embed_url=" + encodeURIComponent(discourseEmbedUrl);
|
if (typeof discourseUserName === 'undefined') {
|
||||||
|
iframe.src =
|
||||||
|
[ discourseUrl,
|
||||||
|
'embed/comments?embed_url=',
|
||||||
|
encodeURIComponent(discourseEmbedUrl)
|
||||||
|
].join('');
|
||||||
|
} else {
|
||||||
|
iframe.src =
|
||||||
|
[ discourseUrl,
|
||||||
|
'embed/comments?embed_url=',
|
||||||
|
encodeURIComponent(discourseEmbedUrl),
|
||||||
|
'&discourse_username=',
|
||||||
|
discourseUserName
|
||||||
|
].join('');
|
||||||
|
}
|
||||||
iframe.id = 'discourse-embed-frame';
|
iframe.id = 'discourse-embed-frame';
|
||||||
iframe.width = "100%";
|
iframe.width = "100%";
|
||||||
iframe.frameBorder = "0";
|
iframe.frameBorder = "0";
|
||||||
|
|
|
@ -4,43 +4,59 @@ require_dependency 'topic_retriever'
|
||||||
describe TopicRetriever do
|
describe TopicRetriever do
|
||||||
|
|
||||||
let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }
|
let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }
|
||||||
let(:topic_retriever) { TopicRetriever.new(embed_url) }
|
let(:author_username) { "eviltrout" }
|
||||||
|
let(:topic_retriever) { TopicRetriever.new(embed_url, author_username: author_username) }
|
||||||
|
|
||||||
it "does not call perform_retrieve when embeddable_host is not set" do
|
describe "#retrieve" do
|
||||||
SiteSetting.stubs(:embeddable_host).returns(nil)
|
context "when host is invalid" do
|
||||||
topic_retriever.expects(:perform_retrieve).never
|
before do
|
||||||
topic_retriever.retrieve
|
topic_retriever.stubs(:invalid_host?).returns(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not call perform_retrieve when embeddable_host is different than the host of the URL" do
|
it "does not perform_retrieve" do
|
||||||
SiteSetting.stubs(:embeddable_host).returns("eviltuna.com")
|
topic_retriever.expects(:perform_retrieve).never
|
||||||
topic_retriever.expects(:perform_retrieve).never
|
topic_retriever.retrieve
|
||||||
topic_retriever.retrieve
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it "does not call perform_retrieve when the embed url is not a url" do
|
|
||||||
r = TopicRetriever.new("not a url")
|
|
||||||
r.expects(:perform_retrieve).never
|
|
||||||
r.retrieve
|
|
||||||
end
|
|
||||||
|
|
||||||
context "with a valid host" do
|
|
||||||
before do
|
|
||||||
SiteSetting.stubs(:embeddable_host).returns("eviltrout.com")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "calls perform_retrieve if it hasn't been retrieved recently" do
|
context "when topics have been retrieived recently" do
|
||||||
topic_retriever.expects(:perform_retrieve).once
|
before do
|
||||||
topic_retriever.expects(:retrieved_recently?).returns(false)
|
topic_retriever.stubs(:retrieved_recently?).returns(true)
|
||||||
topic_retriever.retrieve
|
end
|
||||||
|
|
||||||
|
it "does not perform_retrieve" do
|
||||||
|
topic_retriever.expects(:perform_retrieve).never
|
||||||
|
topic_retriever.retrieve
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't call perform_retrieve if it's been retrieved recently" do
|
context "when host is not invalid" do
|
||||||
topic_retriever.expects(:perform_retrieve).never
|
before do
|
||||||
topic_retriever.expects(:retrieved_recently?).returns(true)
|
topic_retriever.stubs(:invalid_host?).returns(false)
|
||||||
topic_retriever.retrieve
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
context "when topics have been retrieived recently" do
|
||||||
|
before do
|
||||||
|
topic_retriever.stubs(:retrieved_recently?).returns(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not perform_retrieve" do
|
||||||
|
topic_retriever.expects(:perform_retrieve).never
|
||||||
|
topic_retriever.retrieve
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when topics have not been retrieived recently" do
|
||||||
|
before do
|
||||||
|
topic_retriever.stubs(:retrieved_recently?).returns(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does perform_retrieve" do
|
||||||
|
topic_retriever.expects(:perform_retrieve).once
|
||||||
|
topic_retriever.retrieve
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,6 @@ require 'spec_helper'
|
||||||
require_dependency 'jobs/regular/process_post'
|
require_dependency 'jobs/regular/process_post'
|
||||||
|
|
||||||
describe Jobs::PollFeed do
|
describe Jobs::PollFeed do
|
||||||
|
|
||||||
let(:poller) { Jobs::PollFeed.new }
|
let(:poller) { Jobs::PollFeed.new }
|
||||||
|
|
||||||
context "execute" do
|
context "execute" do
|
||||||
|
@ -10,28 +9,22 @@ describe Jobs::PollFeed do
|
||||||
let(:embed_by_username) { "eviltrout" }
|
let(:embed_by_username) { "eviltrout" }
|
||||||
|
|
||||||
it "requires feed_polling_enabled?" do
|
it "requires feed_polling_enabled?" do
|
||||||
SiteSetting.stubs(:feed_polling_enabled?).returns(false)
|
SiteSetting.stubs(:feed_polling_enabled?).returns(true)
|
||||||
poller.expects(:poll_feed).never
|
SiteSetting.stubs(:feed_polling_url).returns(nil)
|
||||||
poller.execute({})
|
poller.expects(:poll_feed).never
|
||||||
|
poller.execute({})
|
||||||
end
|
end
|
||||||
|
|
||||||
it "requires feed_polling_url" do
|
it "requires feed_polling_url" do
|
||||||
SiteSetting.stubs(:feed_polling_url).returns(nil)
|
SiteSetting.stubs(:feed_polling_enabled?).returns(false)
|
||||||
poller.expects(:poll_feed).never
|
SiteSetting.stubs(:feed_polling_url).returns(nil)
|
||||||
poller.execute({})
|
poller.expects(:poll_feed).never
|
||||||
|
poller.execute({})
|
||||||
end
|
end
|
||||||
|
|
||||||
it "requires embed_by_username" do
|
|
||||||
SiteSetting.stubs(:embed_by_username).returns(nil)
|
|
||||||
poller.expects(:poll_feed).never
|
|
||||||
poller.execute({})
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
it "delegates to poll_feed" do
|
it "delegates to poll_feed" do
|
||||||
SiteSetting.stubs(:feed_polling_enabled?).returns(true)
|
SiteSetting.stubs(:feed_polling_enabled?).returns(true)
|
||||||
SiteSetting.stubs(:feed_polling_url).returns(url)
|
SiteSetting.stubs(:feed_polling_url).returns(url)
|
||||||
SiteSetting.stubs(:embed_by_username).returns(embed_by_username)
|
|
||||||
poller.expects(:poll_feed).once
|
poller.expects(:poll_feed).once
|
||||||
poller.execute({})
|
poller.execute({})
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue