From b250aa36a0315ccc1005b8f2e8e474233db44f4a Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 1 Apr 2014 18:16:56 -0400 Subject: [PATCH] Remote fetching of blog contents --- app/assets/javascripts/discourse/models/post.js | 4 ++-- app/controllers/posts_controller.rb | 8 +++++++- app/models/topic_embed.rb | 9 ++++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/models/post.js b/app/assets/javascripts/discourse/models/post.js index 08c626a67af..1c894afbf9a 100644 --- a/app/assets/javascripts/discourse/models/post.js +++ b/app/assets/javascripts/discourse/models/post.js @@ -183,8 +183,8 @@ Discourse.Post = Discourse.Model.extend({ **/ expand: function() { var self = this; - return Discourse.ajax("/posts/" + this.get('id') + "/expand-embed").then(function(result) { - self.set('cooked', result.cooked); + return Discourse.ajax("/posts/" + this.get('id') + "/expand-embed").then(function(post) { + self.set('cooked', post.cooked); }); }, diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 1ed8b47041e..ed787622e1d 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -144,7 +144,13 @@ class PostsController < ApplicationController end def expand_embed - render json: {cooked: "NEW COOKED CONTENT"} + post = find_post_from_params + content = Rails.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do + url = TopicEmbed.where(topic_id: post.topic_id).pluck(:embed_url).first + doc = TopicEmbed.find_remote(url) + doc.content + end + render json: {cooked: content} end def recover diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index 8623571a4c8..afa912ae72d 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -56,15 +56,18 @@ class TopicEmbed < ActiveRecord::Base post end - def self.import_remote(user, url, opts=nil) + def self.find_remote(url) require 'ruby-readability' url = normalize_url(url) - opts = opts || {} - doc = Readability::Document.new(open(url).read, + Readability::Document.new(open(url).read, tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol], attributes: %w[href src]) + end + def self.import_remote(user, url, opts=nil) + opts = opts || {} + doc = find_remote(url) TopicEmbed.import(user, url, opts[:title] || doc.title, doc.content) end