--- /dev/null
+# frozen_string_literal: true
+
+module WellKnown
+ class NodeInfoController < ActionController::Base
+ include CacheConcern
+
+ before_action { response.headers['Vary'] = 'Accept' }
+
+ def index
+ expires_in 3.days, public: true
+ render_with_cache json: {}, serializer: NodeInfo::DiscoverySerializer, adapter: NodeInfo::Adapter, expires_in: 3.days, root: 'nodeinfo'
+ end
+
+ def show
+ expires_in 30.minutes, public: true
+ render_with_cache json: {}, serializer: NodeInfo::Serializer, adapter: NodeInfo::Adapter, expires_in: 30.minutes, root: 'nodeinfo'
+ end
+ end
+end
# frozen_string_literal: true
class ActivityTracker
- EXPIRE_AFTER = 90.days.seconds
+ EXPIRE_AFTER = 6.months.seconds
class << self
include Redisable
--- /dev/null
+# frozen_string_literal: true
+
+class NodeInfo::Adapter < ActiveModelSerializers::Adapter::Attributes
+ def self.default_key_transform
+ :camel_lower
+ end
+end
Rails.cache.fetch('user_count') { User.confirmed.joins(:account).merge(Account.without_suspended).count }
end
- def active_user_count
- Rails.cache.fetch('active_user_count') { Redis.current.pfcount(*(0..3).map { |i| "activity:logins:#{i.weeks.ago.utc.to_date.cweek}" }) }
+ def active_user_count(weeks = 4)
+ Rails.cache.fetch('active_user_count') { Redis.current.pfcount(*(0...weeks).map { |i| "activity:logins:#{i.weeks.ago.utc.to_date.cweek}" }) }
end
def status_count
--- /dev/null
+# frozen_string_literal: true
+
+class NodeInfo::DiscoverySerializer < ActiveModel::Serializer
+ include RoutingHelper
+
+ attribute :links
+
+ def links
+ [{ rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', href: nodeinfo_schema_url }]
+ end
+end
--- /dev/null
+# frozen_string_literal: true
+
+class NodeInfo::Serializer < ActiveModel::Serializer
+ include RoutingHelper
+
+ attributes :version, :software, :protocols, :usage
+
+ def version
+ '2.0'
+ end
+
+ def software
+ { name: 'mastodon', version: Mastodon::Version.to_s }
+ end
+
+ def services
+ { outbound: [], inbound: [] }
+ end
+
+ def protocols
+ %w(activitypub)
+ end
+
+ def usage
+ {
+ users: {
+ total: instance_presenter.user_count,
+ active_month: instance_presenter.active_user_count(4),
+ active_halfyear: instance_presenter.active_user_count(24),
+ },
+
+ local_posts: instance_presenter.status_count,
+ }
+ end
+
+ private
+
+ def instance_presenter
+ @instance_presenter ||= InstancePresenter.new
+ end
+end
inflect.acronym 'PubSubHubbub'
inflect.acronym 'ActivityStreams'
inflect.acronym 'JsonLd'
+ inflect.acronym 'NodeInfo'
end
end
get '.well-known/host-meta', to: 'well_known/host_meta#show', as: :host_meta, defaults: { format: 'xml' }
+ get '.well-known/nodeinfo', to: 'well_known/nodeinfo#index', as: :nodeinfo, defaults: { format: 'json' }
get '.well-known/webfinger', to: 'well_known/webfinger#show', as: :webfinger
get '.well-known/change-password', to: redirect('/auth/edit')
get '.well-known/keybase-proof-config', to: 'well_known/keybase_proof_config#show'
+ get '/nodeinfo/2.0', to: 'well_known/nodeinfo#show', as: :nodeinfo_schema
+
get 'manifest', to: 'manifests#show', defaults: { format: 'json' }
get 'intent', to: 'intents#show'
get 'custom.css', to: 'custom_css#show', as: :custom_css
--- /dev/null
+require 'rails_helper'
+
+describe WellKnown::NodeInfoController, type: :controller do
+ render_views
+
+ describe 'GET #index' do
+ it 'returns json document pointing to node info' do
+ get :index
+
+ expect(response).to have_http_status(200)
+ expect(response.content_type).to eq 'application/json'
+
+ json = body_as_json
+
+ expect(json[:links]).to be_an Array
+ expect(json[:links][0][:rel]).to eq 'http://nodeinfo.diaspora.software/ns/schema/2.0'
+ expect(json[:links][0][:href]).to include 'nodeinfo/2.0'
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns json document with node info properties' do
+ get :show
+
+ expect(response).to have_http_status(200)
+ expect(response.content_type).to eq 'application/json'
+
+ json = body_as_json
+
+ expect(json[:version]).to eq '2.0'
+ expect(json[:usage]).to be_a Hash
+ expect(json[:software]).to be_a Hash
+ expect(json[:protocols]).to be_an Array
+ end
+ end
+end