+41
lib/tasks/feeds.rake
+41
lib/tasks/feeds.rake
···
6
6
require 'app/post_console_printer'
7
7
require 'app/utils'
8
8
9
+
require 'base64'
10
+
require 'json'
11
+
require 'open-uri'
12
+
9
13
10
14
def get_feed
11
15
if ENV['KEY'].to_s == ''
···
24
28
feed
25
29
end
26
30
31
+
def make_jwt(payload)
32
+
header = { typ: 'JWT', alg: 'ES256K' }
33
+
sig = 'fakesig'
34
+
35
+
fields = [header, payload].map { |d| Base64.encode64(JSON.generate(d)).chomp } + [sig]
36
+
fields.join('.')
37
+
end
38
+
27
39
desc "Print posts in the feed, starting from the newest ones (limit = N)"
28
40
task :print_feed do
29
41
feed = get_feed
···
34
46
# this fixes an error when piping a long output to less and then closing without reading it all
35
47
Signal.trap("SIGPIPE", "SYSTEM_DEFAULT")
36
48
49
+
printer = PostConsolePrinter.new(feed)
50
+
51
+
posts.each do |s|
52
+
printer.display(s)
53
+
end
54
+
end
55
+
56
+
desc "Print feed by making an HTTP connection to the XRPC endpoint"
57
+
task :test_feed do
58
+
feed = get_feed
59
+
limit = ENV['N'] ? ENV['N'].to_i : 100
60
+
actor = ENV['DID'] || BlueFactory.publisher_did
61
+
jwt = make_jwt({ iss: actor })
62
+
63
+
puts "Loading feed..."
64
+
65
+
feed_uri = "at://#{BlueFactory.publisher_did}/app.bsky.feed.generator/#{ENV['KEY']}"
66
+
port = ENV['PORT'] || BlueFactory::Server.settings.port
67
+
url = "http://localhost:#{port}/xrpc/app.bsky.feed.getFeedSkeleton?limit=#{limit}&feed=#{feed_uri}"
68
+
headers = { 'Authorization' => "Bearer #{jwt}" }
69
+
70
+
json = JSON.parse(URI.open(url, headers).read)
71
+
post_uris = json['feed'].map { |x| x['post'] }
72
+
73
+
puts "Loading posts..."
74
+
75
+
posts = post_uris.map { |uri| Post.find_by_at_uri(uri) }.compact
76
+
77
+
Signal.trap("SIGPIPE", "SYSTEM_DEFAULT")
37
78
printer = PostConsolePrinter.new(feed)
38
79
39
80
posts.each do |s|