2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class OneboxController < ApplicationController
|
2018-02-01 15:17:59 +11: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-16 11:30:31 +08: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-20 00:31:10 +01:00
|
|
|
|
|
|
|
# only 1 outgoing preview per user
|
2017-11-23 15:32:19 +11:00
|
|
|
return render(body: nil, status: 429) if Oneboxer.is_previewing?(current_user.id)
|
2016-12-20 00:31:10 +01:00
|
|
|
|
2017-11-24 15:31:23 +11:00
|
|
|
user_id = current_user.id
|
2018-02-14 10:39:44 +11:00
|
|
|
category_id = params[:category_id].to_i
|
2018-02-19 22:40:14 +01:00
|
|
|
topic_id = params[:topic_id].to_i
|
2017-11-24 15:31:23 +11:00
|
|
|
invalidate = params[:refresh] == "true"
|
|
|
|
url = params[:url]
|
|
|
|
|
2019-11-28 07:48:29 +10:00
|
|
|
return render(body: nil, status: 404) if Oneboxer.recently_failed?(url)
|
|
|
|
|
2020-10-22 15:38:18 +11:00
|
|
|
hijack(info: "#{url} topic_id: #{topic_id} user_id: #{user_id}") do
|
2017-11-24 15:31:23 +11:00
|
|
|
Oneboxer.preview_onebox!(user_id)
|
2016-12-20 00:31:10 +01:00
|
|
|
|
2018-02-14 10:39:44 +11:00
|
|
|
preview =
|
|
|
|
Oneboxer.preview(
|
|
|
|
url,
|
|
|
|
invalidate_oneboxes: invalidate,
|
|
|
|
user_id: user_id,
|
2018-02-19 22:40:14 +01:00
|
|
|
category_id: category_id,
|
|
|
|
topic_id: topic_id,
|
2018-02-14 10:39:44 +11:00
|
|
|
)
|
|
|
|
|
2019-05-16 11:30:31 +08:00
|
|
|
preview = preview.strip if preview.present?
|
2016-12-20 00:31:10 +01:00
|
|
|
|
2017-11-24 15:31:23 +11:00
|
|
|
Oneboxer.onebox_previewed!(user_id)
|
2016-12-20 00:31:10 +01:00
|
|
|
|
2017-11-24 15:31:23 +11:00
|
|
|
if preview.blank?
|
2019-11-28 07:48:29 +10:00
|
|
|
Oneboxer.cache_failed!(url)
|
2017-11-24 15:31:23 +11: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
|