2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'oneboxer'
|
|
|
|
|
|
|
|
class OneboxController < ApplicationController
|
2017-08-31 12:06:56 +08:00
|
|
|
before_action :ensure_logged_in
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def show
|
2016-12-20 07:31:10 +08:00
|
|
|
params.require(:user_id)
|
2013-03-06 03:03:50 +08:00
|
|
|
|
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-08-31 12:06:56 +08:00
|
|
|
return render(body: nil, status: 429) if Oneboxer.is_previewing?(params[:user_id])
|
2016-12-20 07:31:10 +08:00
|
|
|
|
|
|
|
Oneboxer.preview_onebox!(params[:user_id])
|
|
|
|
|
|
|
|
preview = Oneboxer.preview(params[:url], invalidate_oneboxes: params[:refresh] == 'true')
|
|
|
|
preview.strip! if preview.present?
|
|
|
|
|
|
|
|
Scheduler::Defer.later("Onebox previewed") {
|
|
|
|
Oneboxer.onebox_previewed!(params[:user_id])
|
|
|
|
}
|
|
|
|
|
|
|
|
if preview.blank?
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2013-03-06 03:03:50 +08:00
|
|
|
else
|
2017-04-10 20:01:25 +08:00
|
|
|
render plain: preview
|
2013-03-06 03:03:50 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|