Skip to content

Commit

Permalink
added rake task for testing a feed via http
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Oct 30, 2023
1 parent 962c4c0 commit 38a0a40
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/tasks/feeds.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ require 'app/models/post'
require 'app/post_console_printer'
require 'app/utils'

require 'base64'
require 'json'
require 'open-uri'


def get_feed
if ENV['KEY'].to_s == ''
Expand All @@ -24,6 +28,14 @@ def get_feed
feed
end

def make_jwt(payload)
header = { typ: 'JWT', alg: 'ES256K' }
sig = 'fakesig'

fields = [header, payload].map { |d| Base64.encode64(JSON.generate(d)).chomp } + [sig]
fields.join('.')
end

desc "Print posts in the feed, starting from the newest ones (limit = N)"
task :print_feed do
feed = get_feed
Expand All @@ -41,6 +53,35 @@ task :print_feed do
end
end

desc "Print feed by making an HTTP connection to the XRPC endpoint"
task :test_feed do
feed = get_feed
limit = ENV['N'] ? ENV['N'].to_i : 100
actor = ENV['DID'] || BlueFactory.publisher_did
jwt = make_jwt({ iss: actor })

puts "Loading feed..."

feed_uri = "at://#{BlueFactory.publisher_did}/app.bsky.feed.generator/#{ENV['KEY']}"
port = ENV['PORT'] || BlueFactory::Server.settings.port
url = "http://localhost:#{port}/xrpc/app.bsky.feed.getFeedSkeleton?limit=#{limit}&feed=#{feed_uri}"
headers = { 'Authorization' => "Bearer #{jwt}" }

json = JSON.parse(URI.open(url, headers).read)
post_uris = json['feed'].map { |x| x['post'] }

puts "Loading posts..."

posts = post_uris.map { |uri| Post.find_by_at_uri(uri) }.compact

Signal.trap("SIGPIPE", "SYSTEM_DEFAULT")
printer = PostConsolePrinter.new(feed)

posts.each do |s|
printer.display(s)
end
end

desc "Remove a single post from a feed"
task :delete_feed_item do
feed = get_feed
Expand Down

0 comments on commit 38a0a40

Please sign in to comment.