activitypub 0.3.6 → 0.4.0

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: ed038947b69b69a8e0571743cbf28fdfaf7131c4922bc26b1e0d47a532dd6002
4
- data.tar.gz: fdda56411f0ebc9d50365eea7bb028eb8b6f718947c10a57f4a838b8201e7d50
3
+ metadata.gz: 1c8cae4b7204912811e11efa38d689ee0cb418c480a7e3240a70a8d4dcafd699
4
+ data.tar.gz: 4c732f4669c8ed60428d75b45bda5aac7ffa24e439d9fc4f8c733e51241438f3
5
5
  SHA512:
6
- metadata.gz: a1339e651c59ec7d18c9b82a48b15e15c7591821276c2d8934f9c8ecd5a014bbfed57e56cc20d95627bca5ac5182d6e6ba6e10e14c36f673948fa555f3cd76b8
7
- data.tar.gz: dba788189a64fff38c808db2734bd567bb25bae0ffd0db6503242f56e1edf52b299474eeab4a11bdd40fd994782ee255cada4fa68fc4fbbe23b3056ef8cd28a7
6
+ metadata.gz: 3f5e26e20e624d3c9ae77f66832e8ed6bee1516c84ec4cc1ef2d24f47b468c2e473ee15e4ecab7e000e910adef998870df26650aa7060753d5430dfb699c8717
7
+ data.tar.gz: 40cfe87f071e5b8be1e6848f86232523f206e90b8b78c25ad9be0b7aa29c7b72f3da6c64da669edc384424c1e7ea9b4b5f813a15c0b1816ee3360568ec5d40cf
@@ -1,17 +1,14 @@
1
1
 
2
- require 'webfinger'
3
- require 'faraday'
4
2
  require 'pp'
5
3
  $: << File.expand_path(__FILE__ + "/../../lib")
6
- require 'activitypub/types'
7
- require 'activitypub/base'
4
+ require 'activitypub'
8
5
 
9
6
  acct = ARGV.shift || "vidar@galaxybound.com"
10
7
 
11
8
  puts "Trying webfinger for #{acct} (to choose your own account, provide it as an argument)"
12
9
  puts
13
10
 
14
- wf = WebFinger.discover!(acct)
11
+ wf = ActivityPub.webfinger(acct)
15
12
  if !wf
16
13
  puts "Unable to find #{acct}"
17
14
  exit(1)
@@ -22,35 +19,22 @@ puts
22
19
  puts "Enter to continue"
23
20
  gets
24
21
 
25
- link = wf["links"].find{ _1["rel"] == "self" && _1["type"] == "application/activity+json" }
26
- if !link || !link["href"]
22
+ href = wf.activitypub
23
+ if !href
27
24
  puts "Unable to find a link to the actor"
28
25
  exit(2)
29
26
  end
30
27
 
31
- href = link["href"]
32
-
33
28
  puts "Trying to retrieve the Actor from #{href}"
34
29
  puts
35
30
 
36
- response = Faraday.get(href,{}, {"Accept": "application/activity+json"})
37
- if response.status != 200
38
- PP.pp response
39
- exit(3)
40
- end
41
-
42
- ob = ActivityPub.from_json(response.body)
31
+ ob = href.get
43
32
  PP.pp(ob)
33
+ puts ob.to_json
44
34
  puts
45
35
  puts "Enter to continue"
46
36
  gets
47
37
 
48
38
  puts "Trying to retrieve outbox"
49
- response = Faraday.get(ob.outbox, {}, {"Accept": "application/activity+json"})
50
- if response.status != 200
51
- PP.pp response
52
- exit(4)
53
- end
54
-
55
- ob = ActivityPub.from_json(response.body)
39
+ ob = ob.outbox.get
56
40
  PP.pp(ob)
@@ -36,6 +36,10 @@ module ActivityPub
36
36
  av.is_a?(Hash) && av["type"] ? from_hash(av) : av
37
37
  end
38
38
  end
39
+
40
+ if t = klass.ap_types[attr]
41
+ v = t.new(v)
42
+ end
39
43
  ob.instance_variable_set("@#{attr}", v) if !v.nil?
40
44
  end
41
45
  end
@@ -51,12 +55,20 @@ module ActivityPub
51
55
  def _type = self.class.name.split("::").last
52
56
 
53
57
 
54
- def self.ap_attr(*names)
58
+ # FIXME: Allow specifying a type (e.g. URI)
59
+ def self.ap_attr(name, type=nil)
55
60
  @ap_attributes ||= []
56
- @ap_attributes.concat(names)
57
- attr_accessor *names
61
+ @ap_attributes << name
62
+ @ap_types ||= {}
63
+ @ap_types[name] = type
64
+ attr_accessor name
58
65
  end
59
66
 
67
+ def self.ap_types
68
+ parent = superclass.respond_to?(:ap_types) ? superclass.ap_types : {}
69
+ parent.merge(@ap_types || {})
70
+ end
71
+
60
72
  def self.ap_attributes
61
73
  parent = superclass.respond_to?(:ap_attributes) ? superclass.ap_attributes : []
62
74
  parent.concat(@ap_attributes||[])
@@ -2,23 +2,57 @@
2
2
  # https://www.w3.org/TR/activitystreams-vocabulary/
3
3
 
4
4
  require_relative 'base'
5
+ require_relative 'uri'
5
6
 
6
7
  module ActivityPub
7
8
 
8
9
  class Link < Base
9
- ap_attr :href, :rel, :mediaType, :name, :hreflang, :height, :width, :preview
10
+ ap_attr :href
11
+ ap_attr :rel
12
+ ap_attr :mediaType
13
+ ap_attr :name
14
+ ap_attr :hreflang
15
+ ap_attr :height
16
+ ap_attr :width
17
+ ap_attr :preview
10
18
  end
11
19
 
12
20
  class Object < Base
13
21
  ap_attr :id
14
- ap_attr :attachment, :attributedTo, :audience, :content, :context,
15
- :name, :endTime, :generator, :icon, :image, :inReplyTo, :location,
16
- :preview, :published, :replies, :startTime, :summary, :tag, :updated,
17
- :url, :to, :bto, :cc, :mediaType, :duration
22
+ ap_attr :attachment
23
+ ap_attr :attributedTo
24
+ ap_attr :audience
25
+ ap_attr :content
26
+ ap_attr :context
27
+ ap_attr :name
28
+ ap_attr :endTime
29
+ ap_attr :generator
30
+ ap_attr :icon
31
+ ap_attr :image
32
+ ap_attr :inReplyTo
33
+ ap_attr :location
34
+ ap_attr :preview
35
+ ap_attr :published
36
+ ap_attr :replies
37
+ ap_attr :startTime
38
+ ap_attr :summary
39
+ ap_attr :tag
40
+ ap_attr :updated
41
+ ap_attr :url
42
+ ap_attr :to
43
+ ap_attr :bto
44
+ ap_attr :cc
45
+ ap_attr :mediaType
46
+ ap_attr :duration
18
47
  end
19
48
 
20
49
  class Activity < Object
21
- ap_attr :actor, :object, :target, :result, :origin, :instrument
50
+ ap_attr :actor
51
+ ap_attr :object
52
+ ap_attr :target
53
+ ap_attr :result
54
+ ap_attr :origin
55
+ ap_attr :instrument
22
56
  end
23
57
 
24
58
  # @FIXME
@@ -27,7 +61,11 @@ module ActivityPub
27
61
  end
28
62
 
29
63
  class Collection < Object
30
- ap_attr :totalItems, :current, :first, :last, :items
64
+ ap_attr :totalItems
65
+ ap_attr :current
66
+ ap_attr :first
67
+ ap_attr :last
68
+ ap_attr :items
31
69
  end
32
70
 
33
71
  class OrderedCollection < Collection
@@ -41,7 +79,9 @@ module ActivityPub
41
79
  end
42
80
 
43
81
  class CollectionPage < Collection
44
- ap_attr :partOf, :next, :prev
82
+ ap_attr :partOf
83
+ ap_attr :next
84
+ ap_attr :prev
45
85
  end
46
86
 
47
87
  class OrderedCollectionPage < CollectionPage
@@ -129,7 +169,9 @@ module ActivityPub
129
169
  end
130
170
 
131
171
  class Question < IntransitiveActivity
132
- ap_attr :oneOf, :anyOf, :closed
172
+ ap_attr :oneOf
173
+ ap_attr :anyOf
174
+ ap_attr :closed
133
175
  end
134
176
 
135
177
  # ## Actor Types
@@ -143,20 +185,27 @@ module ActivityPub
143
185
  # Section 4.1
144
186
 
145
187
  # MUST have:
146
- ap_attr :inbox, :outbox
188
+ ap_attr :inbox, URI
189
+ ap_attr :outbox, URI
147
190
 
148
191
  # SHOULD have:
149
- ap_attr :following, :followers
192
+ ap_attr :following
193
+ ap_attr :followers
150
194
 
151
195
  # MAY have:
152
- ap_attr :liked, :streams, :preferredUsername,
153
- :endpoints
196
+ ap_attr :liked
197
+ ap_attr :streams
198
+ ap_attr :preferredUsername
199
+ ap_attr :endpoints
154
200
 
155
201
 
156
202
  # Per https://docs-p.joinmastodon.org/spec/activitypub/#extensions
157
203
  # These are extensions used by Mastodon for Actor types
158
- ap_attr :publicKey, :featured, :featuredTags,
159
- :discoverable, :suspended
204
+ ap_attr :publicKey
205
+ ap_attr :featured
206
+ ap_attr :featuredTags
207
+ ap_attr :discoverable
208
+ ap_attr :suspended
160
209
  end
161
210
 
162
211
  # FIXME: Add "toot:Emoji
@@ -172,7 +221,9 @@ module ActivityPub
172
221
 
173
222
  # Mastodon extension per
174
223
  # https://docs-p.joinmastodon.org/spec/activitypub/#extensions
175
- ap_attr :likes, :bookmarks, :manuallyApprovesFollowers
224
+ ap_attr :likes
225
+ ap_attr :bookmarks
226
+ ap_attr :manuallyApprovesFollowers
176
227
  end
177
228
 
178
229
  class Service < Actor
@@ -182,7 +233,9 @@ module ActivityPub
182
233
  # ## Object types
183
234
 
184
235
  class Relationship < Object
185
- ap_attr :subject, :object, :relationship
236
+ ap_attr :subject
237
+ ap_attr :object
238
+ ap_attr :relationship
186
239
  end
187
240
 
188
241
  class Article < Object
@@ -198,7 +251,8 @@ module ActivityPub
198
251
 
199
252
  # Mastodon extension per
200
253
  # https://docs-p.joinmastodon.org/spec/activitypub/#extensions
201
- ap_attr :focalPoint, :blurhash
254
+ ap_attr :focalPoint
255
+ ap_attr :blurhash
202
256
  end
203
257
 
204
258
  class Video < Document
@@ -223,7 +277,8 @@ module ActivityPub
223
277
  end
224
278
 
225
279
  class Tombstone < Object
226
- ap_attr :formerType, :deleted
280
+ ap_attr :formerType
281
+ ap_attr :deleted
227
282
  end
228
283
 
229
284
  # Mastodon extensions
@@ -0,0 +1,23 @@
1
+
2
+ require 'uri'
3
+ require 'faraday'
4
+
5
+ module ActivityPub
6
+ class URI
7
+ def initialize(href)
8
+ @href = href
9
+ end
10
+
11
+ def to_s = @href
12
+ def to_json(...) = @href
13
+
14
+ def get
15
+ response = Faraday.get(self.to_s, {}, {"Accept": "application/activity+json"})
16
+ if response.status == 200
17
+ ActivityPub.from_json(response.body)
18
+ else
19
+ response
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActivityPub
4
- VERSION = "0.3.6"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -0,0 +1,27 @@
1
+
2
+ require 'webfinger'
3
+
4
+ module ActivityPub
5
+ class WebFingerResult
6
+ def initialize(hash)
7
+ @h = hash
8
+
9
+ @h["aliases"] = @h["aliases"].map{ ActivityPub::URI.new(_1) }
10
+ @h["links"].each do |v|
11
+ if v["href"]
12
+ v["href"] = ActivityPub::URI.new(v["href"])
13
+ end
14
+ end
15
+ end
16
+
17
+ def[](i) = @h[i]
18
+
19
+ def activitypub
20
+ @activitypub ||= self["links"].find{ _1["rel"] == "self" && _1["type"] == "application/activity+json" }&.dig("href")
21
+ end
22
+ end
23
+
24
+ def self.webfinger(acct)
25
+ WebFingerResult.new(WebFinger.discover!(acct))
26
+ end
27
+ end
data/lib/activitypub.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "activitypub/version"
4
+ require_relative "activitypub/base"
4
5
  require_relative "activitypub/types"
6
+ require_relative "activitypub/uri"
7
+ require_relative "activitypub/webfinger"
5
8
 
6
9
  module ActivityPub
7
10
  class Error < StandardError; end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activitypub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vidar Hokstad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-08 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: webfinger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.3
13
27
  description:
14
28
  email:
15
29
  - vidar@hokstad.com
@@ -27,7 +41,9 @@ files:
27
41
  - lib/activitypub.rb
28
42
  - lib/activitypub/base.rb
29
43
  - lib/activitypub/types.rb
44
+ - lib/activitypub/uri.rb
30
45
  - lib/activitypub/version.rb
46
+ - lib/activitypub/webfinger.rb
31
47
  - sig/activitypub.rbs
32
48
  homepage: https://github.com/vidarh/activitypub
33
49
  licenses: