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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03507acb74efe4f9acac94ec797a4e9e2c763e0bea35a64442c2cc5be9a433bd
4
- data.tar.gz: d291bf8e88761714b282fb54273dc220615f53ea0d2647a95447c13ff0d3b7c1
3
+ metadata.gz: 1c7bfb83a8c6326af6500d6619774fa60cc9e2a927ea4d9ab9b783a76f3955cd
4
+ data.tar.gz: 52768679c15dad21d8f6ceb9572bf145e52990a3cd7cf60632e05c2faa164073
5
5
  SHA512:
6
- metadata.gz: 639668b2192c1e7c256de2d294582e96a1f9c3ddb701ca6102805e7a887265df6a7d73579b97fc5d4dcbe6564b5dbab852e798b22182824cefdf805312dcc17c
7
- data.tar.gz: 9d54c9a9567c1af53340d9d412a67cf43513c80c2af3642dc843949af0120165cf5a6e7eef8fcef106880e077273157fa84953a25842a735e32e229a617e7c42
6
+ metadata.gz: b4172a1b83d52eb02cfe212c918ac2b0e8a247915ebe41c1fa9628c066cb9ebbb1bd6c31471d1d4ba02a13868dd7fa6357220d053dca2e71e72713e0c38a68fb
7
+ data.tar.gz: d7ec761d26636bc7ccb37ee3d7cc7617d4990481d3f8d5e198823c9c36b45f72b5bb1ca632a44f26b5a64fc068f39f65455fbd0ec3441d871ee0d99851ad38c1
@@ -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
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require_relative 'client'
5
+
6
+ class DistributedPress
7
+ module V1
8
+ module Social
9
+ # Manages the activity's likes
10
+ class Likes
11
+ # @return [DistributedPress::V1::Social::Client]
12
+ attr_reader :client
13
+
14
+ # @return [String]
15
+ attr_reader :actor
16
+
17
+ # @return [String] Activity ID (URL)
18
+ attr_reader :activity
19
+
20
+ # @param :client [DistributedPress::V1::Social::Client]
21
+ # @param :actor [String]
22
+ # @param :activity [String]
23
+ def initialize(client:, actor:, activity:)
24
+ @client = client
25
+ @actor = actor
26
+ @activity = activity
27
+ end
28
+
29
+ # Get the activities replies collection. Authenticated requests
30
+ # contain the list of replies.
31
+ #
32
+ # @return [HTTParty::Response]
33
+ def get
34
+ client.get(endpoint: endpoint)
35
+ end
36
+
37
+ # Replies
38
+ #
39
+ # @return [String]
40
+ def endpoint
41
+ @endpoint ||= "/v1/#{actor}/inbox/likes/#{Base64.encode64(activity).delete("\n")}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -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
@@ -18,6 +20,8 @@ class DistributedPress
18
20
  inReplyTo
19
21
  object
20
22
  replies
23
+ likes
24
+ shares
21
25
  to
22
26
  publicKey
23
27
  audience
@@ -46,11 +50,10 @@ class DistributedPress
46
50
 
47
51
  def_delegators :referenced, :[], :dig, :to_h, :to_json, :keys, :key?
48
52
 
49
- def initialize(object:, dereferencer:)
53
+ def initialize(object:, dereferencer:, referenced: nil)
50
54
  @object = object
51
55
  @dereferencer = dereferencer
52
- @referenced = HTTParty::ModuleInheritableAttributes.hash_deep_dup(object)
53
- reference_object! referenced
56
+ @referenced = referenced || reference_object(object)
54
57
  end
55
58
 
56
59
  def _dump(_)
@@ -63,36 +66,54 @@ class DistributedPress
63
66
  new(object: object, dereferencer: dereferencer)
64
67
  end
65
68
 
69
+ def success?
70
+ true
71
+ end
72
+
73
+ def parsed_response
74
+ self
75
+ end
76
+
66
77
  private
67
78
 
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
79
+ def reference_object(object)
80
+ case object
81
+ when Array
82
+ object.map do |sub_object|
83
+ reference_object sub_object
84
+ end
85
+ when Hash
86
+ new_object = object.dup
87
+
88
+ new_object.each_key do |attribute|
89
+ next unless REFERENTIABLE_ATTRIBUTES.include? attribute
90
+
91
+ new_object[attribute] = reference_object object[attribute]
92
+ end
93
+
94
+ # XXX: Embedded objects may not have an ID, but they do have
95
+ # a type.
96
+ if new_object['type']
97
+ new_object =
98
+ case new_object['type']
99
+ when 'Collection', 'OrderedCollection', 'CollectionPage', 'OrderedCollectionPage'
100
+ Collection.new(object: object, referenced: new_object, dereferencer: dereferencer)
85
101
  else
86
- o
102
+ ReferencedObject.new(object: object, referenced: new_object, dereferencer: dereferencer)
87
103
  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
104
+
105
+ Reference.new(dereferencer: dereferencer, object: new_object, uri: new_object['id'])
106
+ else
107
+ new_object
108
+ end
109
+ when String
110
+ if object == DistributedPress::V1::Social::Reference::PUBLIC
111
+ object
112
+ else
113
+ dereferencer.references(dereferencer.uris(object.sub(URI_FIXES, '')))
95
114
  end
115
+ else
116
+ object
96
117
  end
97
118
  end
98
119
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require_relative 'client'
5
+
6
+ class DistributedPress
7
+ module V1
8
+ module Social
9
+ # Manages the activity's shares (boosts, announces)
10
+ class Shares
11
+ # @return [DistributedPress::V1::Social::Client]
12
+ attr_reader :client
13
+
14
+ # @return [String]
15
+ attr_reader :actor
16
+
17
+ # @return [String] Activity ID (URL)
18
+ attr_reader :activity
19
+
20
+ # @param :client [DistributedPress::V1::Social::Client]
21
+ # @param :actor [String]
22
+ # @param :activity [String]
23
+ def initialize(client:, actor:, activity:)
24
+ @client = client
25
+ @actor = actor
26
+ @activity = activity
27
+ end
28
+
29
+ # Get the activities replies collection. Authenticated requests
30
+ # contain the list of replies.
31
+ #
32
+ # @return [HTTParty::Response]
33
+ def get
34
+ client.get(endpoint: endpoint)
35
+ end
36
+
37
+ # Replies
38
+ #
39
+ # @return [String]
40
+ def endpoint
41
+ @endpoint ||= "/v1/#{actor}/inbox/shares/#{Base64.encode64(activity).delete("\n")}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ 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.0rc10'
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.0rc10
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-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -329,11 +329,13 @@ files:
329
329
  - lib/distributed_press/v1/social/followers.rb
330
330
  - lib/distributed_press/v1/social/hook.rb
331
331
  - lib/distributed_press/v1/social/inbox.rb
332
+ - lib/distributed_press/v1/social/likes.rb
332
333
  - lib/distributed_press/v1/social/outbox.rb
333
334
  - lib/distributed_press/v1/social/reference.rb
334
335
  - lib/distributed_press/v1/social/referenced_object.rb
335
336
  - lib/distributed_press/v1/social/replies.rb
336
337
  - lib/distributed_press/v1/social/schemas/webhook.rb
338
+ - lib/distributed_press/v1/social/shares.rb
337
339
  - lib/distributed_press/v1/social/signed_headers.rb
338
340
  - lib/distributed_press/v1/token.rb
339
341
  - lib/distributed_press/version.rb