diaspora/app/models/open_graph_cache.rb
Jonne Haß 74a6f42501 Bye opengraph_parser, hi open_graph_reader
opengraph_parser is basically unmainted, issues are ignored
or deliberately closed without fixing. It pollutes the global
namespace and has no verification of correctness.

The opengraph gem has basically the same issues, not really maintained,
unreleased patches on master since over a year, not really smart either.

So I created my own version and while at it, why not strive try to be
complete and robust, although it's still a work in progress.

This also improves general URL detection by parsing them
from the message after stripping markdown.

An additional dependency was added to support
fetching sites that require cookies to work at all.
For the same reason Faraday's default redirect limit was
bumped.
2014-12-08 02:01:31 +01:00

46 lines
1.1 KiB
Ruby

class OpenGraphCache < ActiveRecord::Base
validates :title, :presence => true
validates :ob_type, :presence => true
validates :image, :presence => true
validates :url, :presence => true
has_many :posts
acts_as_api
api_accessible :backbone do |t|
t.add :title
t.add :ob_type
t.add :image
t.add :description
t.add :url
end
def image
if AppConfig.privacy.camo.proxy_opengraph_thumbnails?
Diaspora::Camo.image_url(self[:image])
else
self[:image]
end
end
def self.find_or_create_by(opts)
cache = OpenGraphCache.find_or_initialize_by(opts)
cache.fetch_and_save_opengraph_data! unless cache.persisted?
cache if cache.persisted? # Make this an after create callback and drop this method ?
end
def fetch_and_save_opengraph_data!
object = OpenGraphReader.fetch!(self.url)
return unless object
self.title = object.og.title.truncate(255)
self.ob_type = object.og.type
self.image = object.og.image.url
self.url = object.og.url
self.description = object.og.description
self.save
rescue OpenGraphReader::NoOpenGraphDataError
end
end