Remove rabl dependency (#5894)

* Remove rabl dependency

* Replicate old Oj configuration
signup-info-prompt
Eugen Rochko 2017-12-06 07:04:49 +01:00 committed by Yamagishi Kazutoshi
parent 0393a64a90
commit 42bcbd36b7
9 changed files with 45 additions and 49 deletions

View File

@ -49,7 +49,6 @@ gem 'oj', '~> 3.3'
gem 'ostatus2', '~> 2.0'
gem 'ox', '~> 2.8'
gem 'pundit', '~> 1.1'
gem 'rabl', '~> 0.13'
gem 'rack-attack', '~> 5.0'
gem 'rack-cors', '~> 0.4', require: 'rack/cors'
gem 'rack-timeout', '~> 0.4'

View File

@ -334,8 +334,6 @@ GEM
puma (3.11.0)
pundit (1.1.0)
activesupport (>= 3.0.0)
rabl (0.13.1)
activesupport (>= 2.3.14)
rack (2.0.3)
rack-attack (5.0.1)
rack
@ -606,7 +604,6 @@ DEPENDENCIES
pry-rails (~> 0.3)
puma (~> 3.10)
pundit (~> 1.1)
rabl (~> 0.13)
rack-attack (~> 5.0)
rack-cors (~> 0.4)
rack-timeout (~> 0.4)

View File

@ -6,12 +6,10 @@ module WellKnown
def show
@account = Account.find_local!(username_from_resource)
@canonical_account_uri = @account.to_webfinger_s
@magic_key = pem_to_magic_key(@account.keypair.public_key)
respond_to do |format|
format.any(:json, :html) do
render formats: :json, content_type: 'application/jrd+json'
render json: @account, serializer: WebfingerSerializer, content_type: 'application/jrd+json'
end
format.xml do
@ -35,21 +33,6 @@ module WellKnown
WebfingerResource.new(resource_user).username
end
def pem_to_magic_key(public_key)
modulus, exponent = [public_key.n, public_key.e].map do |component|
result = []
until component.zero?
result << [component % 256].pack('C')
component >>= 8
end
result.reverse.join
end
(['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
end
def resource_param
params.require(:resource)
end

View File

@ -184,6 +184,21 @@ class Account < ApplicationRecord
@keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
end
def magic_key
modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
result = []
until component.zero?
result << [component % 256].pack('C')
component >>= 8
end
result.reverse.join
end
(['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
end
def subscription(webhook_url)
@subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
class WebfingerSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :subject, :aliases, :links
def subject
object.to_webfinger_s
end
def aliases
[short_account_url(object), account_url(object)]
end
def links
[
{ rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: short_account_url(object) },
{ rel: 'http://schemas.google.com/g/2010#updates-from', type: 'application/atom+xml', href: account_url(object, format: 'atom') },
{ rel: 'self', type: 'application/activity+json', href: account_url(object) },
{ rel: 'salmon', href: api_salmon_url(object.id) },
{ rel: 'magic-public-key', href: "data:application/magic-public-key,#{object.magic_key}" },
{ rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_follow_url}?acct={uri}" },
]
end
end

View File

@ -1,18 +0,0 @@
object @account
node(:subject) { @canonical_account_uri }
node(:aliases) do
[short_account_url(@account), account_url(@account)]
end
node(:links) do
[
{ rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: short_account_url(@account) },
{ rel: 'http://schemas.google.com/g/2010#updates-from', type: 'application/atom+xml', href: account_url(@account, format: 'atom') },
{ rel: 'self', type: 'application/activity+json', href: account_url(@account) },
{ rel: 'salmon', href: api_salmon_url(@account.id) },
{ rel: 'magic-public-key', href: "data:application/magic-public-key,#{@magic_key}" },
{ rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_follow_url}?acct={uri}" },
]
end

View File

@ -1,13 +1,13 @@
Nokogiri::XML::Builder.new do |xml|
xml.XRD(xmlns: 'http://docs.oasis-open.org/ns/xri/xrd-1.0') do
xml.Subject @canonical_account_uri
xml.Subject @account.to_webfinger_s
xml.Alias short_account_url(@account)
xml.Alias account_url(@account)
xml.Link(rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: short_account_url(@account))
xml.Link(rel: 'http://schemas.google.com/g/2010#updates-from', type: 'application/atom+xml', href: account_url(@account, format: 'atom'))
xml.Link(rel: 'self', type: 'application/activity+json', href: account_url(@account))
xml.Link(rel: 'salmon', href: api_salmon_url(@account.id))
xml.Link(rel: 'magic-public-key', href: "data:application/magic-public-key,#{@magic_key}")
xml.Link(rel: 'magic-public-key', href: "data:application/magic-public-key,#{@account.magic_key}")
xml.Link(rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_follow_url}?acct={uri}")
end
end.to_xml

View File

@ -0,0 +1 @@
Oj.default_options = { mode: :compat, time_format: :ruby, use_to_json: true }

View File

@ -1,7 +0,0 @@
Rabl.configure do |config|
config.json_engine = Oj
config.cache_all_output = false
config.cache_sources = Rails.env.production?
config.include_json_root = false
config.view_paths = [Rails.root.join('app/views')]
end