diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 9a2bb4cdc..efee6a1ce 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -256,6 +256,9 @@ class PostsController < ApplicationController # php seems to be sending this incorrectly, don't fight with it params[:skip_validations] = params[:skip_validations].to_s == "true" permitted << :skip_validations + + # We allow `embed_url` via the API + permitted << :embed_url end params.require(:raw) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 93216a582..4dacf457b 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -64,6 +64,7 @@ class PostCreator track_topic update_topic_stats update_user_counts + create_embedded_topic publish ensure_in_allowed_users if guardian.is_staff? @@ -82,7 +83,6 @@ class PostCreator @post end - def self.create(user, opts) PostCreator.new(user, opts).create end @@ -110,6 +110,14 @@ class PostCreator protected + # You can supply an `embed_url` for a post to set up the embedded relationship. + # This is used by the wp-discourse plugin to associate a remote post with a + # discourse post. + def create_embedded_topic + return unless @opts[:embed_url].present? + TopicEmbed.create!(topic_id: @post.topic_id, post_id: @post.id, embed_url: @opts[:embed_url]) + end + def handle_spam if @spam GroupMessage.create( Group[:moderators].name, diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index e2323cccc..21fa04354 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -420,5 +420,19 @@ describe PostCreator do end end + describe "embed_url" do + + let(:embed_url) { "http://eviltrout.com/stupid-url" } + + it "creates the topic_embed record" do + creator = PostCreator.new(user, + embed_url: embed_url, + title: 'Reviews of Science Ovens', + raw: 'Did you know that you can use microwaves to cook your dinner? Science!') + post = creator.create + TopicEmbed.where(embed_url: embed_url).exists?.should be_true + end + end + end