Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_info.rb « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05c96dcb109a6d882badb42f1d6ac7ef485393cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# frozen_string_literal: true

require "pathname"
require "json-schema"

module NodeInfo
  VERSIONS = %w[1.0 2.0 2.1].freeze
  SCHEMAS = {} # rubocop:disable Style/MutableConstant
  private_constant :SCHEMAS

  Document = Struct.new(:version, :software, :protocols, :services, :open_registrations, :usage, :metadata) do
    # rubocop:disable Lint/ConstantDefinitionInBlock
    Software = Struct.new(:name, :version, :repository, :homepage) do
      def initialize(name=nil, version=nil)
        super(name, version)
      end

      def version_10_hash
        {
          "name"    => name,
          "version" => version
        }
      end

      def version_21_hash
        version_10_hash.merge(
          "repository" => repository,
          "homepage"   => homepage
        )
      end
    end

    Protocols = Struct.new(:protocols) do
      def initialize(protocols=[])
        super(protocols)
      end

      def version_10_hash
        {
          "inbound"  => protocols,
          "outbound" => protocols
        }
      end

      def version_20_array
        protocols
      end
    end

    Services = Struct.new(:inbound, :outbound) do
      def initialize(inbound=[], outbound=[])
        super(inbound, outbound)
      end

      def version_10_hash
        {
          "inbound"  => inbound,
          "outbound" => outbound
        }
      end
    end

    Usage = Struct.new(:users, :local_posts, :local_comments) do
      Users = Struct.new(:total, :active_halfyear, :active_month) do
        def initialize(total=nil, active_halfyear=nil, active_month=nil)
          super(total, active_halfyear, active_month)
        end

        def version_10_hash
          {
            "total"          => total,
            "activeHalfyear" => active_halfyear,
            "activeMonth"    => active_month
          }
        end
      end

      def initialize(local_posts=nil, local_comments=nil)
        super(Users.new, local_posts, local_comments)
      end

      def version_10_hash
        {
          "users"         => users.version_10_hash,
          "localPosts"    => local_posts,
          "localComments" => local_comments
        }
      end
    end
    # rubocop:enable Lint/ConstantDefinitionInBlock

    def self.build
      new.tap do |doc|
        yield doc
        doc.validate
      end
    end

    def initialize(version=nil, open_registrations=nil, metadata={})
      super(version, Software.new, Protocols.new, Services.new, open_registrations, Usage.new, metadata)
    end

    def as_json(_options={})
      case version
      when "1.0"
        version_10_hash
      when "2.0"
        version_20_hash
      when "2.1"
        version_21_hash
      end
    end

    def content_type
      "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/#{version}#"
    end

    def schema
      NodeInfo.schema version
    end

    def validate
      assert NodeInfo.supported_version?(version), "Unknown version #{version}"
      JSON::Validator.validate!(schema, as_json)
    end

    private

    def assert(condition, message)
      raise ArgumentError, message unless condition
    end

    def version_10_hash
      deep_compact(
        "version"           => "1.0",
        "software"          => software.version_10_hash,
        "protocols"         => protocols.version_10_hash,
        "services"          => services.version_10_hash,
        "openRegistrations" => open_registrations,
        "usage"             => usage.version_10_hash,
        "metadata"          => metadata
      )
    end

    def version_20_hash
      deep_compact(
        "version"           => "2.0",
        "software"          => software.version_10_hash,
        "protocols"         => protocols.version_20_array,
        "services"          => services.version_10_hash,
        "openRegistrations" => open_registrations,
        "usage"             => usage.version_10_hash,
        "metadata"          => metadata
      )
    end

    def version_21_hash
      deep_compact(
        "version"           => "2.1",
        "software"          => software.version_21_hash,
        "protocols"         => protocols.version_20_array,
        "services"          => services.version_10_hash,
        "openRegistrations" => open_registrations,
        "usage"             => usage.version_10_hash,
        "metadata"          => metadata
      )
    end

    def deep_compact(hash)
      hash.tap do |hash|
        hash.reject! {|_, value|
          deep_compact value if value.is_a? Hash
          value.nil?
        }
      end
    end
  end

  def self.schema(version)
    SCHEMAS[version] ||= JSON.parse(
      Pathname.new(__dir__).join("..", "vendor", "nodeinfo", "schemas", "#{version}.json").expand_path.read
    )
  end

  def self.build(&block)
    Document.build(&block)
  end

  def self.jrd(endpoint)
    {
      "links" => VERSIONS.map {|version|
        {
          "rel"  => "http://nodeinfo.diaspora.software/ns/schema/#{version}",
          "href" => endpoint % {version: version}
        }
      }
    }
  end

  def self.supported_version?(version)
    VERSIONS.include? version
  end
end