jekyll-activity-pub 0.1.0rc3 → 0.1.0rc5

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: 26bac0ac36c0c773eb36472016d30d6216f88ada00d9b8e1fe95724aad71d0ee
4
- data.tar.gz: 0b4a434b6822dce44b84e1686046a955f2a7bdc3dabc21c434ffe2d56ee14c21
3
+ metadata.gz: 01b5fc1068daaef69d177bdba6d3120bcd9bb43536fe1a9533047d23d8b9317e
4
+ data.tar.gz: 74281543a1b7e6c69fc286ec7549f5a5cbd82c0930eb26d0498bd92c7b7c149e
5
5
  SHA512:
6
- metadata.gz: c333f4bb7280710d5083fb09daabf4dd1dd84115a06859bdf4510c19dc7c63ee1a338cd0f1e80846388d3fde0468c33dfb358aa4987021259da645e9faa5893f
7
- data.tar.gz: abb2a481fb1371adca065370b66d56e9eccc71a43bfba2e73edde42c24dfddf85aca633a08e7e1e5a0b52ed58ab75561b567aa05bbd49d6985a23c63ae4a13f0
6
+ metadata.gz: 985da32ad3efb30ee9b7e597abde3d6fcfb3d674a4f9c517080c3cc9646e2a9fff78a76235b865fc16d7bcdd6970390a36bc60acbb021eff2825dc595bb3b413
7
+ data.tar.gz: df8bf14d76df6b1d6d2ecbae57bdb165b0e0eba0750155db538fb0ed7e578d62c073b4c696cfb709c57c56e9762738142989e6ae1c009ff0e8baa4e09559edee
@@ -1,14 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'jekyll/page'
3
4
  require_relative 'helper'
4
5
 
5
6
  module Jekyll
6
7
  module ActivityPub
7
8
  # Represents a Create activity
8
- class Create
9
+ class Create < Jekyll::Page
9
10
  include Helper
10
11
 
11
- attr_reader :data
12
+ # @param :object [Jekyll::ActivityPub::Activity]
13
+ attr_reader :object
14
+
15
+ # @param :actor [Jekyll::ActivityPub::Actor]
16
+ attr_reader :actor
12
17
 
13
18
  # Initialize with default data
14
19
  #
@@ -17,10 +22,26 @@ module Jekyll
17
22
  # @param :object [Jekyll::ActivityPub::Activity]
18
23
  def initialize(site, actor, object)
19
24
  @context = StubContext.new(registers: { site: site })
25
+ @object = object
26
+ @actor = actor
20
27
 
28
+ dest = Pathname.new(object.destination(site.dest)).relative_path_from(site.dest)
29
+ name = "#{type.downcase}.jsonld"
30
+ dir = File.join(
31
+ File.dirname(dest),
32
+ File.basename(dest, '.jsonld')
33
+ )
34
+
35
+ super(site, '', dir, name)
36
+
37
+ trigger_hooks :post_init
38
+ end
39
+
40
+ def read_yaml(*)
21
41
  @data = {
22
42
  '@context' => 'https://www.w3.org/ns/activitystreams',
23
- 'type' => 'Create',
43
+ 'type' => type,
44
+ 'id' => absolute_url(url),
24
45
  'actor' => absolute_url(actor.url),
25
46
  'published' => object.data['published'],
26
47
  'to' => object.data['to'],
@@ -28,8 +49,12 @@ module Jekyll
28
49
  'object' => absolute_url(object.url),
29
50
  'inReplyTo' => object.data['in_reply_to']
30
51
  }
52
+ end
31
53
 
32
- trigger_hooks :post_init
54
+ private
55
+
56
+ def type
57
+ @type ||= self.class.name.split('::').last
33
58
  end
34
59
  end
35
60
  end
@@ -1,9 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'helper'
4
+
3
5
  module Jekyll
4
6
  module ActivityPub
5
7
  # Represents a Delete activity
6
8
  class Delete
9
+ include Helper
10
+
11
+ attr_reader :data
12
+
7
13
  # Initialize with default data
8
14
  #
9
15
  # @param :site [Jekyll::Site]
@@ -14,6 +20,7 @@ module Jekyll
14
20
 
15
21
  @data = {
16
22
  '@context' => 'https://www.w3.org/ns/activitystreams',
23
+ 'id' => absolute_url(object.url).sub('.jsonld', '/delete.jsonld'),
17
24
  'type' => 'Delete',
18
25
  'actor' => absolute_url(actor.url),
19
26
  'object' => absolute_url(object.url)
@@ -15,12 +15,12 @@ module Jekyll
15
15
  #
16
16
  # @return [String]
17
17
  def content
18
- data.to_json
18
+ pruned_data.to_json
19
19
  end
20
20
 
21
21
  # JSONify this object
22
22
  def to_json(*args)
23
- data.to_json(*args)
23
+ pruned_data.to_json(*args)
24
24
  end
25
25
 
26
26
  # There's no excerpt to be generated
@@ -153,6 +153,12 @@ module Jekyll
153
153
 
154
154
  absolute_url url
155
155
  end
156
+
157
+ def pruned_data
158
+ data.reject do |_, v|
159
+ v.nil? || (v.respond_to?(:empty?) && v.empty?)
160
+ end
161
+ end
156
162
  end
157
163
  end
158
164
  end
@@ -31,6 +31,11 @@ module Jekyll
31
31
  end
32
32
  end
33
33
 
34
+ # @return [Jekyll::Site]
35
+ def site
36
+ @@site
37
+ end
38
+
34
39
  # @return [String]
35
40
  def followers_url
36
41
  "#{config['url']}/v1/#{actor}/followers"
@@ -108,10 +113,12 @@ module Jekyll
108
113
  end
109
114
 
110
115
  # Remove notifications already performed and notify
111
- data['notifications'].reject do |object_url, status|
112
- done? object_url, status
116
+ data['notifications'].reject do |object_url, _|
117
+ done? object_url
113
118
  end.each do |object_url, status|
114
- object = PseudoObject.new(url: object_url, data: JSON.parse(File.read(site.in_dest_dir(object_url))))
119
+ path = site.in_dest_dir(object_url)
120
+ data = JSON.parse(File.read(path)) if File.exist? path
121
+ object = PseudoObject.new(url: object_url, data: data || {})
115
122
  process_object(outbox_endpoint, actor_object, object, status)
116
123
  end
117
124
 
@@ -134,34 +141,72 @@ module Jekyll
134
141
  # Removes an activity if it was previously created
135
142
  #
136
143
  # @param :path [String]
137
- # @return [Hash]
144
+ # @return [nil]
138
145
  def delete(path)
139
- action(path, 'delete') if exist? path
146
+ action(path, 'delete') if exist?(path) && !deleted?(path)
140
147
  end
141
148
 
142
149
  # Updates an activity if it was previously created, otherwise
143
150
  # create it
144
151
  #
145
152
  # @param :path [String]
146
- # @return [Hash]
153
+ # @return [nil]
147
154
  def update(path)
148
- action(path, exist?(path) ? 'update' : 'create')
155
+ if created?(path)
156
+ action(path, 'update')
157
+ else
158
+ create(path)
159
+ end
160
+
161
+ nil
149
162
  end
150
163
 
151
- # Creates an activity
164
+ # Creates an activity unless it has been already created
152
165
  #
153
166
  # @param :path [String]
154
- # @return [Hash]
167
+ # @return [nil]
155
168
  def create(path)
156
- action(path, 'create')
169
+ action(path, 'create') unless created?(path)
170
+
171
+ nil
172
+ end
173
+
174
+ # Gets status
175
+ #
176
+ # @param :path [String]
177
+ # @return [Hash]
178
+ def status(path)
179
+ data['notifications'][path_relative_to_dest(path)] || {}
180
+ end
181
+
182
+ # Detects if an action was already done
183
+ #
184
+ # @param :path [String]
185
+ def done?(path)
186
+ (status(path)['action'] == 'done').tap do |done|
187
+ Jekyll.logger.debug('ActivityPub:', "Skipping notification for #{path}") if done
188
+ end
157
189
  end
158
190
 
159
- # Check if activity existed
191
+ # Already created
160
192
  #
161
193
  # @param :path [String]
162
- # @return [Boolean]
194
+ def created?(path)
195
+ !(status(path)['created_at'].nil?)
196
+ end
197
+
198
+ # @param :path [String]
199
+ def updated?(path)
200
+ !(status(path)['updated_at'].nil?)
201
+ end
202
+
203
+ # @param :path [String]
204
+ def deleted?(path)
205
+ !(status(path)['deleted_at'].nil?)
206
+ end
207
+
163
208
  def exist?(path)
164
- data['notifications'].key? path_relative_to_dest(path)
209
+ !(status(path)['action'].nil?)
165
210
  end
166
211
 
167
212
  # Stores data back to a file and optionally commits it
@@ -176,7 +221,7 @@ module Jekyll
176
221
  File.open(path, 'w') do |f|
177
222
  f.flock(File::LOCK_EX)
178
223
  f.rewind
179
- f.write(YAML.dump(data))
224
+ f.write(YAML.dump(data.reject { |_, v| v.empty? }))
180
225
  f.flush
181
226
  f.truncate(f.pos)
182
227
  end
@@ -253,14 +298,8 @@ module Jekyll
253
298
  # @param :path [String]
254
299
  # @return [String]
255
300
  def path_relative_to_dest(path)
256
- Pathname.new(site.in_dest_dir(path)).relative_path_from(site.dest).to_s
257
- end
258
-
259
- # Detects if an action was already done
260
- def done?(url, status)
261
- (status['action'] == 'done').tap do |done|
262
- Jekyll.logger.debug('ActivityPub:', "Skipping notification for #{url}") if done
263
- end
301
+ @@path_relative_to_dest ||= {}
302
+ @@path_relative_to_dest[path] ||= Pathname.new(site.in_dest_dir(path)).relative_path_from(site.dest).to_s
264
303
  end
265
304
 
266
305
  # Turns an object into an activity and notifies outbox
@@ -43,19 +43,23 @@ module Jekyll
43
43
  @content ||=
44
44
  begin
45
45
  order_items!
46
+ items_to_links!
46
47
 
47
- return original_content unless always_paginate? || paginable?
48
- return original_content if data['orderedItems'].empty?
49
-
50
- paged_data = data.dup
51
- pages = paginate(paged_data.delete('orderedItems'))
48
+ if !always_paginate? || !paginable?
49
+ original_content
50
+ elsif data['orderedItems'].empty?
51
+ original_content
52
+ else
53
+ paged_data = data.dup
54
+ pages = paginate(paged_data.delete('orderedItems'))
52
55
 
53
- assign_links! pages
56
+ assign_links! pages
54
57
 
55
- paged_data['first'] = absolute_url(pages.first.url)
56
- paged_data['last'] = absolute_url(pages.last.url)
58
+ paged_data['first'] = absolute_url(pages.first.url)
59
+ paged_data['last'] = absolute_url(pages.last.url)
57
60
 
58
- paged_data.to_json
61
+ paged_data.to_json
62
+ end
59
63
  end
60
64
  end
61
65
 
@@ -132,6 +136,12 @@ module Jekyll
132
136
  b.data['published'] <=> a.data['published']
133
137
  end
134
138
  end
139
+
140
+ def items_to_links!
141
+ data['orderedItems'] = data['orderedItems'].map do |item|
142
+ item.data['id']
143
+ end
144
+ end
135
145
  end
136
146
  end
137
147
  end
@@ -5,19 +5,6 @@ require_relative 'create'
5
5
  module Jekyll
6
6
  module ActivityPub
7
7
  # Represents an Update activity
8
- class Update < Create
9
- # Initialize with default data
10
- #
11
- # @param :site [Jekyll::Site]
12
- # @param :actor [Jekyll::ActivityPub::Actor]
13
- # @param :object [Jekyll::ActivityPub::Activity]
14
- def initialize(site, actor, object)
15
- super
16
-
17
- data['type'] = 'Update'
18
-
19
- trigger_hooks :post_init
20
- end
21
- end
8
+ class Update < Create; end
22
9
  end
23
10
  end
@@ -14,9 +14,11 @@ Jekyll::Hooks.register :site, :post_read, priority: :low do |site|
14
14
  begin
15
15
  private_key = File.read(site.config['activity_pub_private_key'])
16
16
  rescue StandardError
17
- Jekyll.logger.warn 'ActivityPub:', 'There\'s an issue with your private key'
17
+ Jekyll.logger.warn 'ActivityPub:', 'There\'s an issue with your private key, skipping generation'
18
18
 
19
- raise
19
+ site.config['activity_pub_disable'] = true
20
+
21
+ next
20
22
  end
21
23
 
22
24
  # We only need the public key at this point
@@ -42,15 +44,24 @@ end
42
44
  # Generate an activity for each document after the content has been
43
45
  # rendered as HTML.
44
46
  Jekyll::Hooks.register(:documents, :post_convert, priority: :high) do |doc|
47
+ next if site.config['activity_pub_disable']
45
48
  next unless doc.write?
46
49
  next if doc.data['sitemap'] == false
47
50
  next if doc.data['activity'] == false
48
51
 
49
52
  site = doc.site
50
- dir = File.dirname(doc.url)
51
- name = "#{File.basename(doc.url, '.html')}.jsonld"
52
53
  actor = site.config['actor']
53
54
  outbox = site.config['outbox']
55
+ dest = doc.destination(site.dest)
56
+ dir = Pathname.new(File.dirname(dest)).relative_path_from(site.dest).to_s
57
+
58
+ # /slug/ => /slug.jsonld
59
+ if (n = File.basename(dest, '.html')) == 'index'
60
+ name = "#{File.basename(dir)}.jsonld"
61
+ dir = File.dirname(dir)
62
+ else
63
+ name = "#{n}.jsonld"
64
+ end
54
65
 
55
66
  Jekyll::ActivityPub::Activity.new(site, actor, doc, nil, dir, name).tap do |activity|
56
67
  doc.data['activity'] = activity
@@ -70,6 +81,7 @@ Jekyll::Hooks.register(:documents, :post_convert, priority: :high) do |doc|
70
81
 
71
82
  outbox.data['totalItems'] += 1
72
83
  outbox.data['orderedItems'] << action
84
+ site.pages << action
73
85
  end
74
86
 
75
87
  site.pages << activity
@@ -78,6 +90,8 @@ end
78
90
 
79
91
  # This hook is shit because we don't have access to the site!
80
92
  Jekyll::Hooks.register(:clean, :on_obsolete, priority: :low) do |list|
93
+ next if Jekyll::ActivityPub::Notifier.site.config['activity_pub_disable']
94
+
81
95
  list.each do |path|
82
96
  Jekyll::ActivityPub::Notifier.delete path
83
97
  end
@@ -86,5 +100,7 @@ end
86
100
  # Store data generated between builds and commits if
87
101
  # jekyll-write-and-commit-changes is enabled.
88
102
  Jekyll::Hooks.register(:site, :post_write, priority: :low) do |_|
103
+ next if site.config['activity_pub_disable']
104
+
89
105
  Jekyll::ActivityPub::Notifier.save
90
106
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-activity-pub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0rc3
4
+ version: 0.1.0rc5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sutty