2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class OneboxController < ApplicationController
|
2018-01-31 22:17:59 -06:00
|
|
|
requires_login
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
def show
|
2023-01-09 06:20:10 -06:00
|
|
|
unless params[:refresh] == "true"
|
2017-06-12 12:59:42 -05:00
|
|
|
preview = Oneboxer.cached_preview(params[:url])
|
2019-05-15 22:30:31 -05:00
|
|
|
preview = preview.strip if preview.present?
|
2017-06-12 12:59:42 -05:00
|
|
|
return render(plain: preview) if preview.present?
|
|
|
|
end
|
2016-12-19 17:31:10 -06:00
|
|
|
|
|
|
|
# only 1 outgoing preview per user
|
2017-11-22 22:32:19 -06:00
|
|
|
return render(body: nil, status: 429) if Oneboxer.is_previewing?(current_user.id)
|
2016-12-19 17:31:10 -06:00
|
|
|
|
2017-11-23 22:31:23 -06:00
|
|
|
user_id = current_user.id
|
2018-02-13 17:39:44 -06:00
|
|
|
category_id = params[:category_id].to_i
|
2018-02-19 15:40:14 -06:00
|
|
|
topic_id = params[:topic_id].to_i
|
2023-01-09 06:20:10 -06:00
|
|
|
invalidate = params[:refresh] == "true"
|
2017-11-23 22:31:23 -06:00
|
|
|
url = params[:url]
|
|
|
|
|
2019-11-27 15:48:29 -06:00
|
|
|
return render(body: nil, status: 404) if Oneboxer.recently_failed?(url)
|
|
|
|
|
2020-10-21 23:38:18 -05:00
|
|
|
hijack(info: "#{url} topic_id: #{topic_id} user_id: #{user_id}") do
|
2017-11-23 22:31:23 -06:00
|
|
|
Oneboxer.preview_onebox!(user_id)
|
2016-12-19 17:31:10 -06:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
preview =
|
|
|
|
Oneboxer.preview(
|
|
|
|
url,
|
|
|
|
invalidate_oneboxes: invalidate,
|
|
|
|
user_id: user_id,
|
|
|
|
category_id: category_id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
)
|
2018-02-13 17:39:44 -06:00
|
|
|
|
2019-05-15 22:30:31 -05:00
|
|
|
preview = preview.strip if preview.present?
|
2016-12-19 17:31:10 -06:00
|
|
|
|
2017-11-23 22:31:23 -06:00
|
|
|
Oneboxer.onebox_previewed!(user_id)
|
2016-12-19 17:31:10 -06:00
|
|
|
|
2017-11-23 22:31:23 -06:00
|
|
|
if preview.blank?
|
2019-11-27 15:48:29 -06:00
|
|
|
Oneboxer.cache_failed!(url)
|
2017-11-23 22:31:23 -06:00
|
|
|
render body: nil, status: 404
|
|
|
|
else
|
|
|
|
render plain: preview
|
|
|
|
end
|
2013-03-05 13:03:50 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|