Class: Jekyll::ActivityPubStatic::Generator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll/activitypub_static/generator.rb

Instance Method Summary collapse

Instance Method Details

#article_summary(site, post) ⇒ Object



295
296
297
298
299
300
301
302
# File 'lib/jekyll/activitypub_static/generator.rb', line 295

def article_summary(site, post)
  property = site.config.dig("activitypub", "summary_property") || "description"
  explicit = post.data[property]
  return explicit unless explicit.to_s.strip.empty?

  excerpt = post.data["excerpt"]
  excerpt.output
end

#build_actor(site) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/jekyll/activitypub_static/generator.rb', line 226

def build_actor(site)
  url  = site.config["url"]
  summary = site.config["description"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  update_interval = site.config.dig("activitypub", "update_interval") || "P1D"

  {
    "@context": [
      "https://www.w3.org/ns/activitystreams",
      "https://purl.archive.org/miscellany/1.0",
      "https://purl.archive.org/socialweb/webfinger",
      "https://w3id.org/fep/b06c"
    ],
    "type": "Person",
    "id": "#{url}/actor.jsonld",
    "pollOnly": true,
    "updateInterval": update_interval,
    "name": name(site),
    "preferredUsername": preferred_username(site),
    "summary": (summary unless summary.to_s.strip.empty?),
    "inbox": "#{url}/#{output_path}/inbox.jsonld",
    "outbox": "#{url}/#{output_path}/outbox.jsonld",
    "attributedTo": "#{url}/actor.jsonld",
    "url" => {
      "type" => "Link",
      "mediaType" => "text/html",
      "href" => homepage_url(site)
    },
    "webfinger" => webfinger_address(site),
    "cc": "as:Public"
  }
end

#build_page(site, page_number) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/jekyll/activitypub_static/generator.rb', line 275

def build_page(site, page_number)
  url = site.config["url"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  id = "#{url}/#{output_path}/outbox/page-#{page_number}.jsonld"

  page = {
    "@context" => "https://www.w3.org/ns/activitystreams",
    "id" => id,
    "attributedTo" => "#{url}/actor.jsonld",
    "type" => "OrderedCollectionPage",
    "partOf" => "#{url}/#{output_path}/outbox.jsonld",
    "summary" => "page #{page_number} of outbox of #{name(site)}",
    "orderedItems" => [],
    "to" => "as:Public"
  }

  page["prev"] = "#{url}/#{output_path}/outbox/page-#{page_number - 1}.jsonld" if page_number > 1
  page
end

#explicit_summary(site, post) ⇒ Object



320
321
322
323
# File 'lib/jekyll/activitypub_static/generator.rb', line 320

def explicit_summary(site, post)
  property = site.config.dig("activitypub", "summary_property") || "description"
  post.data[property]
end

#generate(site) ⇒ Object



29
30
31
32
33
# File 'lib/jekyll/activitypub_static/generator.rb', line 29

def generate(site)
  generate_webfinger(site)
  generate_actor(site)
  generate_inbox(site)
end

#generate_activities(site) ⇒ Object



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
# File 'lib/jekyll/activitypub_static/generator.rb', line 123

def generate_activities(site)
  url = site.config["url"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  output_dir = File.join(output_path, "activities")

  site.posts.docs.each do |post|
    slug = post.basename_without_ext.sub(/^\d{4}-\d{2}-\d{2}-/, "")
    filename = "create-#{slug}.jsonld"
    path = File.join(output_dir, filename)

    article_id = "#{url}/#{output_path}/posts/#{slug}.jsonld"
    activity_id = "#{url}/#{output_path}/activities/create-#{slug}.jsonld"

    activity = {
      "@context" => "https://www.w3.org/ns/activitystreams",
      "id" => activity_id,
      "actor" => "#{url}/actor.jsonld",
      "type" => "Create",
      "summary" => "#{name(site)} created #{post.data["title"]}",
      "published" => post.date.iso8601,
      "object" => {
        "id" => article_id,
        "type" => post_type(site, post)
      },
      "to" => "as:Public"
    }

    activity["object"]["name"] = post.data["title"] unless post.data["title"].to_s.strip.empty?

    site.static_files << JsonStaticFile.new(site, output_dir, filename, activity)
    Jekyll.logger.info LOG_TAG, "Wrote activity to #{path}"
  end
end

#generate_actor(site) ⇒ Object



61
62
63
64
65
# File 'lib/jekyll/activitypub_static/generator.rb', line 61

def generate_actor(site)
  Jekyll.logger.info LOG_TAG, "Generating actor.jsonld"
  actor = build_actor(site)
  site.static_files << JsonStaticFile.new(site, "", "actor.jsonld", actor)
end

#generate_articles(site) ⇒ Object



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
# File 'lib/jekyll/activitypub_static/generator.rb', line 90

def generate_articles(site)
  url = site.config["url"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  output_dir = File.join(output_path, "posts")

  site.posts.docs.each do |post|
    slug = post.basename_without_ext.sub(/^\d{4}-\d{2}-\d{2}-/, "")
    filename = "#{slug}.jsonld"

    article_id = "#{url}/#{output_path}/posts/#{slug}.jsonld"

    article = {
      "@context" => "https://www.w3.org/ns/activitystreams",
      "id" => article_id,
      "type" => post_type(site, post),
      "content" => post.content,
      "summary" => article_summary(site, post),
      "published" => post.date.iso8601,
      "attributedTo" => "#{url}/actor.jsonld",
      "url" => {
        "type" => "Link",
        "mediaType" => "text/html",
        "href" => "#{url}#{post.url}"
      },
      "to" => "as:Public"
    }

    article["name"] = post.data["title"] unless post.data["title"].to_s.strip.empty?

    site.static_files << JsonStaticFile.new(site, output_dir, filename, article)
  end
end

#generate_inbox(site) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jekyll/activitypub_static/generator.rb', line 67

def generate_inbox(site)
  Jekyll.logger.info LOG_TAG, "Generating inbox.jsonld"
  url  = site.config["url"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  inbox = {
    "@context": [
      "https://www.w3.org/ns/activitystreams",
      "https://purl.archive.org/miscellany/1.0",
      "https://w3id.org/fep/5711"
    ],
    "id": "#{url}/#{output_path}/inbox.jsonld",
    "type": "OrderedCollection",
    "attributedTo": "#{url}/actor.jsonld",
    "cc": "as:Public",
    "inboxOf": "#{url}/actor.jsonld",
    "summary": "Inbox of #{name(site)}",
    "totalItems": 0,
    "orderedItems": []
  }

  site.static_files << JsonStaticFile.new(site, output_path, "inbox.jsonld", inbox)
end

#generate_outbox(site) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/jekyll/activitypub_static/generator.rb', line 199

def generate_outbox(site)
  Jekyll.logger.info LOG_TAG, "Generating outbox.jsonld"
  url = site.config["url"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  total_items = site.posts.docs.length
  page_count = (total_items.to_f / PAGE_SIZE).ceil

  outbox = {
    "@context": [
      "https://www.w3.org/ns/activitystreams",
      "https://purl.archive.org/miscellany/1.0",
      "https://w3id.org/fep/5711"
    ],
    "id": "#{url}/#{output_path}/outbox.jsonld",
    "type": "OrderedCollection",
    "attributedTo": "#{url}/actor.jsonld",
    "cc": "as:Public",
    "outboxOf": "#{url}/actor.jsonld",
    "summary": "Outbox of #{name(site)}",
    "totalItems": total_items,
    "first": ("#{url}/#{output_path}/outbox/page-#{page_count}.jsonld" if total_items > 0)
  }

  site.static_files << JsonStaticFile.new(site, output_path, "outbox.jsonld", outbox)
  Jekyll.logger.info LOG_TAG, "Wrote outbox to #{File.join(site.dest, output_path, "outbox.jsonld")}"
end

#generate_outbox_pages(site) ⇒ Object



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
# File 'lib/jekyll/activitypub_static/generator.rb', line 157

def generate_outbox_pages(site)
  url = site.config["url"]
  output_path = site.config.dig("activitypub", "output_path") || "activitypub"
  output_dir = File.join(output_path, "outbox")

  page_number = 1
  page = build_page(site, page_number)

  site.posts.docs.each_with_index do |post, index|
    slug = post.basename_without_ext.sub(/^\d{4}-\d{2}-\d{2}-/, "")
    Jekyll.logger.info LOG_TAG, "Adding #{slug} to page #{page_number}"
    article_id = "#{url}/#{output_path}/posts/#{slug}.jsonld"
    activity_id = "#{url}/#{output_path}/activities/create-#{slug}.jsonld"

    if page["orderedItems"].length >= PAGE_SIZE
      site.static_files << JsonStaticFile.new(site, output_dir, "page-#{page_number}.jsonld", page)
      page_number += 1
      page = build_page(site, page_number)
    end

    activity = {
      "id" => activity_id,
      "type" => "Create",
      "object" => {
        "id" => article_id,
        "type" => "Article",
        "name" => post.data["title"]
      },
      "to" => "as:Public"
    }

    page["orderedItems"].unshift(activity)
  end

  if page["orderedItems"].any?
    path = File.join(output_dir, "page-#{page_number}.jsonld")

    site.static_files << JsonStaticFile.new(site, output_dir, "page-#{page_number}.jsonld", page)
    Jekyll.logger.info LOG_TAG, "Wrote outbox page #{page_number} to #{path}"
  end
end

#generate_webfinger(site) ⇒ Object



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
# File 'lib/jekyll/activitypub_static/generator.rb', line 35

def generate_webfinger(site)
  Jekyll.logger.info LOG_TAG, "Generating webfinger endpoint"

  url = site.config["url"]
  actor_url = "#{url}/actor.jsonld"

  webfinger = {
    "subject" => "acct:#{webfinger_address(site)}",
    "links" => [
      {
        "rel" => "self",
        "type" => "application/activity+json",
        "href" => actor_url
      }
    ]
  }

  webfinger_output_file = site.config.dig("activitypub", "webfinger_output_file") || ".well-known/webfinger"

  sf = JsonStaticFile.new(site, File.dirname(webfinger_output_file), File.basename(webfinger_output_file),
                          webfinger)
  site.static_files << sf

  Jekyll.logger.info LOG_TAG, "Added webfinger as #{sf.path}"
end

#homepage_url(site) ⇒ Object



337
338
339
340
# File 'lib/jekyll/activitypub_static/generator.rb', line 337

def homepage_url(site)
  url = site.config["url"]
  url.end_with?("/") ? url : "#{url}/"
end

#name(site) ⇒ Object



259
260
261
262
263
# File 'lib/jekyll/activitypub_static/generator.rb', line 259

def name(site)
  explicit = site.config["author"]
  return explicit unless explicit.to_s.strip.empty?
  "Anonymous"
end

#note?(site, post) ⇒ Boolean

Returns:

  • (Boolean)


304
305
306
307
308
309
310
311
312
313
314
# File 'lib/jekyll/activitypub_static/generator.rb', line 304

def note?(site, post)
  title = post.data["title"]
  return false unless title.to_s.strip.empty?

  summary = explicit_summary(site, post)
  return false unless summary.to_s.strip.empty?

  return false unless paragraph_count(post.content) == 1

  plain_text(post.content).length <= note_max_characters(site)
end

#note_max_characters(site) ⇒ Object



325
326
327
# File 'lib/jekyll/activitypub_static/generator.rb', line 325

def note_max_characters(site)
  site.config.dig("activitypub", "note_max_characters") || 500
end

#paragraph_count(content) ⇒ Object



329
330
331
# File 'lib/jekyll/activitypub_static/generator.rb', line 329

def paragraph_count(content)
  content.scan(/<p\b[^>]*>/).length
end

#plain_text(content) ⇒ Object



333
334
335
# File 'lib/jekyll/activitypub_static/generator.rb', line 333

def plain_text(content)
  content.gsub(/<[^>]*>/, "")
end

#post_type(site, post) ⇒ Object



316
317
318
# File 'lib/jekyll/activitypub_static/generator.rb', line 316

def post_type(site, post)
  note?(site, post) ? "Note" : "Article"
end

#preferred_username(site) ⇒ Object



265
266
267
268
269
270
271
272
273
# File 'lib/jekyll/activitypub_static/generator.rb', line 265

def preferred_username(site)
  explicit = site.config.dig("activitypub", "preferred_username")
  return explicit unless explicit.to_s.strip.empty?

  host = URI(site.config["url"]).host
  return host if host

  "anonymous"
end

#webfinger_address(site) ⇒ Object



342
343
344
345
346
# File 'lib/jekyll/activitypub_static/generator.rb', line 342

def webfinger_address(site)
  url = site.config["url"]
  host = URI(url).host
  "#{preferred_username(site)}@#{host}"
end