distributed-press-api-client 0.5.0rc8 → 0.5.0rc9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03507acb74efe4f9acac94ec797a4e9e2c763e0bea35a64442c2cc5be9a433bd
4
- data.tar.gz: d291bf8e88761714b282fb54273dc220615f53ea0d2647a95447c13ff0d3b7c1
3
+ metadata.gz: 35ce06825d1ddabae961e86d8bdd698c5554f0e75a0dd74cc91741f508b23a2e
4
+ data.tar.gz: 74864dd3aaf4ac30d7a3a4cbba91ffe0c7d33dda6881a706e3bdef73682732ad
5
5
  SHA512:
6
- metadata.gz: 639668b2192c1e7c256de2d294582e96a1f9c3ddb701ca6102805e7a887265df6a7d73579b97fc5d4dcbe6564b5dbab852e798b22182824cefdf805312dcc17c
7
- data.tar.gz: 9d54c9a9567c1af53340d9d412a67cf43513c80c2af3642dc843949af0120165cf5a6e7eef8fcef106880e077273157fa84953a25842a735e32e229a617e7c42
6
+ metadata.gz: a66393b7fd19dba0355757c0590e2334671db099892d7ffa36471053fc07402247c74ab72f26681d2cc006beba68f5ed8d1eceab3c3d793308427d57e00cd863
7
+ data.tar.gz: 2e6c050daa4c9fc9f5192be716f1bf733c221b4020ded3e1ce637ef59e02a8f8020888f539d348f9e69a412dfc1819d330d0beb1ce21535725e7d67146371cd5
@@ -11,20 +11,47 @@ class DistributedPress
11
11
  class Collection < ReferencedObject
12
12
  include Enumerable
13
13
 
14
- RECURSIVE_ATTRIBUTES = %w[items orderedItems first next].freeze
14
+ ATTRIBUTES = %w[items orderedItems].freeze
15
+ RECURSIVE_ATTRIBUTES = %w[first next].freeze
15
16
 
16
17
  # If the Collection has items, yield them and keep processing
17
18
  # any pages forward until the end.
18
19
  #
19
- # @todo Use totalItems from the starting object for validation
20
20
  # @todo Implement lazy loading
21
21
  def each(&block)
22
+ counter = 0
23
+ total_items = object['totalItems']
24
+
22
25
  Enumerator.new do |enum|
23
- RECURSIVE_ATTRIBUTES.each do |attribute|
24
- next unless object.key?(attribute)
26
+ catch :total_items_reached do
27
+ ATTRIBUTES.each do |attribute|
28
+ next unless object.key?(attribute)
29
+
30
+ referenced[attribute].each(&block).each do |item|
31
+ counter += 1
32
+
33
+ enum << item
34
+
35
+ if total_items
36
+ throw :total_items_reached if total_items == counter
37
+ throw :total_items_reached if counter > 1000
38
+ end
39
+ end
40
+ end
41
+
42
+ RECURSIVE_ATTRIBUTES.each do |attribute|
43
+ next unless object.key?(attribute)
44
+ throw :total_items_reached if object['id'] && object['id'] == referenced[attribute]['id']
45
+
46
+ referenced[attribute].each(&block).each do |item|
47
+ counter += 1
48
+ enum << item
25
49
 
26
- object[attribute].each(&block).each do |item|
27
- enum << item
50
+ if total_items
51
+ throw :total_items_reached if total_items == counter
52
+ throw :total_items_reached if counter > 1000
53
+ end
54
+ end
28
55
  end
29
56
  end
30
57
  end
@@ -56,7 +56,7 @@ class DistributedPress
56
56
  loop do
57
57
  uri = uris(uri)
58
58
  client = clients(uri)
59
- response = client.get(endpoint: uri.path, parser: @parser)
59
+ response = client.get(endpoint: uri.request_uri, parser: @parser)
60
60
 
61
61
  # Break the loop if the request failed
62
62
  break unless response.success?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uri'
3
+ require 'base64'
4
4
  require_relative 'client'
5
5
 
6
6
  class DistributedPress
@@ -64,7 +64,7 @@ class DistributedPress
64
64
  # @param :id [String] Activity ID
65
65
  # @return [HTTParty::Response]
66
66
  def reject(id:)
67
- client.delete(endpoint: "#{endpoint}/#{URI.encode_uri_component(id)}")
67
+ client.delete(endpoint: "#{endpoint}/#{Base64.encode64(id).delete("\n")}")
68
68
  end
69
69
 
70
70
  # Accept an activity queued on the inbox
@@ -72,7 +72,7 @@ class DistributedPress
72
72
  # @param :id [String] Activity ID
73
73
  # @return [HTTParty::Response]
74
74
  def accept(id:)
75
- client.post(endpoint: "#{endpoint}/#{URI.encode_uri_component(id)}", body: {})
75
+ client.post(endpoint: "#{endpoint}/#{Base64.encode64(id).delete("\n")}", body: {})
76
76
  end
77
77
 
78
78
  # Inbox
@@ -21,7 +21,9 @@ class DistributedPress
21
21
 
22
22
  # @param :uri [String]
23
23
  # @param :dereferencer [DistributedPress::V1::Social::Dereferencer]
24
- def initialize(uri:, dereferencer:)
24
+ # @param :object [ReferencedObject,nil]
25
+ def initialize(uri:, dereferencer:, object: nil)
26
+ @object = object
25
27
  @uri = uri
26
28
  @dereferencer = dereferencer
27
29
  end
@@ -58,7 +60,7 @@ class DistributedPress
58
60
  "#{self.class.name}(#{uri})"
59
61
  end
60
62
 
61
- def_delegators :object, :[], :dig, :to_h, :to_json, :slice, :key?, :keys
63
+ def_delegators :object, :[], :dig, :to_h, :to_json, :slice, :key?, :keys, :each
62
64
  end
63
65
  end
64
66
  end
@@ -9,6 +9,8 @@ class DistributedPress
9
9
  class ReferencedObject
10
10
  extend Forwardable
11
11
 
12
+ # Mastodon hides self-replies
13
+ URI_FIXES = %r{&?only_other_accounts=true&?}
12
14
  REFERENTIABLE_ATTRIBUTES =
13
15
  %w[
14
16
  actor
@@ -46,11 +48,10 @@ class DistributedPress
46
48
 
47
49
  def_delegators :referenced, :[], :dig, :to_h, :to_json, :keys, :key?
48
50
 
49
- def initialize(object:, dereferencer:)
51
+ def initialize(object:, dereferencer:, referenced: nil)
50
52
  @object = object
51
53
  @dereferencer = dereferencer
52
- @referenced = HTTParty::ModuleInheritableAttributes.hash_deep_dup(object)
53
- reference_object! referenced
54
+ @referenced = referenced || reference_object(object)
54
55
  end
55
56
 
56
57
  def _dump(_)
@@ -63,36 +64,54 @@ class DistributedPress
63
64
  new(object: object, dereferencer: dereferencer)
64
65
  end
65
66
 
67
+ def success?
68
+ true
69
+ end
70
+
71
+ def parsed_response
72
+ self
73
+ end
74
+
66
75
  private
67
76
 
68
- def reference_object!(object)
69
- REFERENTIABLE_ATTRIBUTES.each do |attribute|
70
- next unless object.key? attribute
71
-
72
- case object[attribute]
73
- when Array
74
- object[attribute].map! do |o|
75
- case o
76
- when Hash
77
- reference_object!(o)
78
- o
79
- when String
80
- if o == DistributedPress::V1::Social::Reference::PUBLIC
81
- o
82
- else
83
- dereferencer.references(dereferencer.uris(o))
84
- end
77
+ def reference_object(object)
78
+ case object
79
+ when Array
80
+ object.map do |sub_object|
81
+ reference_object sub_object
82
+ end
83
+ when Hash
84
+ new_object = object.dup
85
+
86
+ new_object.each_key do |attribute|
87
+ next unless REFERENTIABLE_ATTRIBUTES.include? attribute
88
+
89
+ new_object[attribute] = reference_object object[attribute]
90
+ end
91
+
92
+ # XXX: Embedded objects may not have an ID, but they do have
93
+ # a type.
94
+ if new_object['type']
95
+ new_object =
96
+ case new_object['type']
97
+ when 'Collection', 'OrderedCollection', 'CollectionPage', 'OrderedCollectionPage'
98
+ Collection.new(object: object, referenced: new_object, dereferencer: self)
85
99
  else
86
- o
100
+ ReferencedObject.new(object: object, referenced: new_object, dereferencer: self)
87
101
  end
88
- end
89
- when Hash
90
- reference_object!(object[attribute])
91
- when String
92
- unless object[attribute] == DistributedPress::V1::Social::Reference::PUBLIC
93
- object[attribute] = dereferencer.references(dereferencer.uris(object[attribute]))
94
- end
102
+
103
+ Reference.new(dereferencer: dereferencer, object: new_object, uri: new_object['id'])
104
+ else
105
+ new_object
106
+ end
107
+ when String
108
+ if object == DistributedPress::V1::Social::Reference::PUBLIC
109
+ object
110
+ else
111
+ dereferencer.references(dereferencer.uris(object.sub(URI_FIXES, '')))
95
112
  end
113
+ else
114
+ object
96
115
  end
97
116
  end
98
117
  end
@@ -3,5 +3,5 @@
3
3
  # API client
4
4
  class DistributedPress
5
5
  # Version
6
- VERSION = '0.5.0rc8'
6
+ VERSION = '0.5.0rc9'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distributed-press-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0rc8
4
+ version: 0.5.0rc9
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-24 00:00:00.000000000 Z
11
+ date: 2024-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable