logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe
commit: 42bcbd36b72c7ddc5ae1ecced09614863ca382bd
parent: 0393a64a90c2dee17e7a9d0c1481fd4155825b85
Author: Eugen Rochko <eugen@zeonfederated.com>
Date:   Wed,  6 Dec 2017 07:04:49 +0100

Remove rabl dependency (#5894)

* Remove rabl dependency

* Replicate old Oj configuration

Diffstat:

MGemfile1-
MGemfile.lock3---
Mapp/controllers/well_known/webfinger_controller.rb19+------------------
Mapp/models/account.rb15+++++++++++++++
Aapp/serializers/webfinger_serializer.rb26++++++++++++++++++++++++++
Dapp/views/well_known/webfinger/show.json.rabl18------------------
Mapp/views/well_known/webfinger/show.xml.ruby4++--
Aconfig/initializers/oj.rb1+
Dconfig/initializers/rabl_init.rb7-------
9 files changed, 45 insertions(+), 49 deletions(-)

diff --git a/Gemfile b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock @@ -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) diff --git a/app/controllers/well_known/webfinger_controller.rb b/app/controllers/well_known/webfinger_controller.rb @@ -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 diff --git a/app/models/account.rb b/app/models/account.rb @@ -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 diff --git a/app/serializers/webfinger_serializer.rb b/app/serializers/webfinger_serializer.rb @@ -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 diff --git a/app/views/well_known/webfinger/show.json.rabl b/app/views/well_known/webfinger/show.json.rabl @@ -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 diff --git a/app/views/well_known/webfinger/show.xml.ruby b/app/views/well_known/webfinger/show.xml.ruby @@ -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 diff --git a/config/initializers/oj.rb b/config/initializers/oj.rb @@ -0,0 +1 @@ +Oj.default_options = { mode: :compat, time_format: :ruby, use_to_json: true } diff --git a/config/initializers/rabl_init.rb b/config/initializers/rabl_init.rb @@ -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