2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'oneboxer'
|
|
|
|
|
|
|
|
class OneboxController < ApplicationController
|
2018-02-01 12:17:59 +08:00
|
|
|
requires_login
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def show
|
2017-06-13 01:59:42 +08: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-20 07:31:10 +08:00
|
|
|
|
|
|
|
# only 1 outgoing preview per user
|
2017-11-23 12:32:19 +08:00
|
|
|
return render(body: nil, status: 429) if Oneboxer.is_previewing?(current_user.id)
|
2016-12-20 07:31:10 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
user_id = current_user.id
|
2018-02-14 07:39:44 +08:00
|
|
|
category_id = params[:category_id].to_i
|
2018-02-20 05:40:14 +08:00
|
|
|
topic_id = params[:topic_id].to_i
|
2017-11-24 12:31:23 +08:00
|
|
|
invalidate = params[:refresh] == 'true'
|
|
|
|
url = params[:url]
|
|
|
|
|
|
|
|
hijack do
|
|
|
|
Oneboxer.preview_onebox!(user_id)
|
2016-12-20 07:31:10 +08:00
|
|
|
|
2018-02-14 07:39:44 +08:00
|
|
|
preview = Oneboxer.preview(url,
|
|
|
|
invalidate_oneboxes: invalidate,
|
|
|
|
user_id: user_id,
|
2018-02-20 05:40:14 +08:00
|
|
|
category_id: category_id,
|
|
|
|
topic_id: topic_id
|
2018-02-14 07:39:44 +08:00
|
|
|
)
|
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
preview.strip! if preview.present?
|
2016-12-20 07:31:10 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
Oneboxer.onebox_previewed!(user_id)
|
2016-12-20 07:31:10 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
if preview.blank?
|
|
|
|
render body: nil, status: 404
|
|
|
|
else
|
|
|
|
render plain: preview
|
|
|
|
end
|
2013-03-06 03:03:50 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|