Story: move archive URL to model, escape URL in case it has query params - merged with i18n

This commit is contained in:
Carl Chenet 2017-05-20 15:18:56 +02:00
parent db3384f974
commit 1cc5ee6c52
3 changed files with 10 additions and 6 deletions

View file

@ -138,6 +138,10 @@ class Story < ActiveRecord::Base
Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer"
end
def archive_url
"https://archive.is/#{CGI.escape(self.url)}"
end
def as_json(options = {})
h = [
:short_id,
@ -311,7 +315,7 @@ class Story < ActiveRecord::Base
def fetch_story_cache!
if self.url.present?
self.story_cache = StoryCacher.get_story_text(self.url)
self.story_cache = StoryCacher.get_story_text(self)
end
end

View file

@ -161,7 +161,7 @@ class="story <%= story.vote && story.vote[:vote] == 1 ? "upvoted" : "" %>
<% end %>
<% if story.url.present? %>
|
<a href="https://archive.is/<%= story.url %>" rel="nofollow"
<a href="<%= story.archive_url %>" rel="nofollow"
target="_new"><%= t('.cached') %></a>
<% end %>
<% if !story.is_gone? %>

View file

@ -6,18 +6,18 @@ class StoryCacher
DIFFBOT_API_URL = "http://www.diffbot.com/api/article"
def self.get_story_text(url)
def self.get_story_text(story)
if !@@DIFFBOT_API_KEY
return
end
# XXX: diffbot tries to read pdfs as text, so disable for now
if url.to_s.match(/\.pdf$/i)
if story.url.to_s.match(/\.pdf$/i)
return nil
end
db_url = "#{DIFFBOT_API_URL}?token=#{@@DIFFBOT_API_KEY}&url=" <<
CGI.escape(url)
CGI.escape(story.url)
begin
s = Sponge.new
@ -44,7 +44,7 @@ class StoryCacher
begin
s = Sponge.new
s.timeout = 45
s.fetch("https://archive.is/#{db_url}")
s.fetch(story.archive_url)
rescue => e
Rails.logger.error "error caching #{db_url}: #{e.message}"
end