Remote fetching of blog contents

This commit is contained in:
Robin Ward 2014-04-01 18:16:56 -04:00
parent 50fb048b99
commit b250aa36a0
3 changed files with 15 additions and 6 deletions

View File

@ -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);
});
},

View File

@ -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

View File

@ -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