2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class OneboxController < ApplicationController
|
2018-01-31 23:17:59 -05:00
|
|
|
requires_login
|
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])
|
2019-05-15 23:30:31 -04:00
|
|
|
preview = preview.strip if preview.present?
|
2017-06-12 13:59:42 -04:00
|
|
|
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
|
2018-02-13 18:39:44 -05:00
|
|
|
category_id = params[:category_id].to_i
|
2018-02-19 16:40:14 -05:00
|
|
|
topic_id = params[:topic_id].to_i
|
2017-11-23 23:31:23 -05:00
|
|
|
invalidate = params[:refresh] == "true"
|
|
|
|
url = params[:url]
|
|
|
|
|
2019-11-27 16:48:29 -05:00
|
|
|
return render(body: nil, status: 404) if Oneboxer.recently_failed?(url)
|
|
|
|
|
2020-10-22 00:38:18 -04:00
|
|
|
hijack(info: "#{url} topic_id: #{topic_id} user_id: #{user_id}") do
|
2017-11-23 23:31:23 -05:00
|
|
|
Oneboxer.preview_onebox!(user_id)
|
2016-12-19 18:31:10 -05:00
|
|
|
|
2018-02-13 18:39:44 -05:00
|
|
|
preview =
|
|
|
|
Oneboxer.preview(
|
|
|
|
url,
|
|
|
|
invalidate_oneboxes: invalidate,
|
|
|
|
user_id: user_id,
|
2018-02-19 16:40:14 -05:00
|
|
|
category_id: category_id,
|
|
|
|
topic_id: topic_id,
|
2018-02-13 18:39:44 -05:00
|
|
|
)
|
|
|
|
|
2019-05-15 23:30:31 -04:00
|
|
|
preview = 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?
|
2019-11-27 16:48:29 -05:00
|
|
|
Oneboxer.cache_failed!(url)
|
2017-11-23 23:31:23 -05:00
|
|
|
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
|