2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'oneboxer'
|
|
|
|
|
|
|
|
class OneboxController < ApplicationController
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :ensure_logged_in
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def show
|
2017-06-12 13:59:42 -04:00
|
|
|
unless params[:refresh] == 'true'
|
|
|
|
preview = Oneboxer.cached_preview(params[:url])
|
|
|
|
preview.strip! if preview.present?
|
|
|
|
return render(plain: preview) if preview.present?
|
|
|
|
end
|
2016-12-19 18:31:10 -05:00
|
|
|
|
|
|
|
# only 1 outgoing preview per user
|
2017-11-22 23:32:19 -05:00
|
|
|
return render(body: nil, status: 429) if Oneboxer.is_previewing?(current_user.id)
|
2016-12-19 18:31:10 -05:00
|
|
|
|
2017-11-23 23:31:23 -05:00
|
|
|
user_id = current_user.id
|
|
|
|
invalidate = params[:refresh] == 'true'
|
|
|
|
url = params[:url]
|
|
|
|
|
|
|
|
hijack do
|
|
|
|
Oneboxer.preview_onebox!(user_id)
|
2016-12-19 18:31:10 -05:00
|
|
|
|
2017-11-23 23:31:23 -05:00
|
|
|
preview = Oneboxer.preview(url, invalidate_oneboxes: invalidate)
|
|
|
|
preview.strip! if preview.present?
|
2016-12-19 18:31:10 -05:00
|
|
|
|
2017-11-23 23:31:23 -05:00
|
|
|
Oneboxer.onebox_previewed!(user_id)
|
2016-12-19 18:31:10 -05:00
|
|
|
|
2017-11-23 23:31:23 -05:00
|
|
|
if preview.blank?
|
|
|
|
render body: nil, status: 404
|
|
|
|
else
|
|
|
|
render plain: preview
|
|
|
|
end
|
2013-03-05 14:03:50 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|