File: bluesky.rb

package info (click to toggle)
ruby-noticed 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ruby: 1,186; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Noticed
  module BulkDeliveryMethods
    class Bluesky < BulkDeliveryMethod
      required_options :identifier, :password, :json

      # bulk_deliver_by :bluesky do |config|
      #   config.identifier = ENV["BLUESKY_ID"]
      #   config.password = ENV["BLUESKY_PASSWORD"]
      #   config.json = {text: "...", createdAt: "..."}
      # end

      def deliver
        Rails.logger.debug(evaluate_option(:json))
        post_request(
          "https://#{host}/xrpc/com.atproto.repo.createRecord",
          headers: {"Authorization" => "Bearer #{token}"},
          json: {
            repo: identifier,
            collection: "app.bsky.feed.post",
            record: evaluate_option(:json)
          }
        )
      end

      def token
        start_session.dig("accessJwt")
      end

      def start_session
        response = post_request(
          "https://#{host}/xrpc/com.atproto.server.createSession",
          json: {
            identifier: identifier,
            password: evaluate_option(:password)
          }
        )
        JSON.parse(response.body)
      end

      def host
        @host ||= evaluate_option(:host) || "bsky.social"
      end

      def identifier
        @identifier ||= evaluate_option(:identifier)
      end
    end
  end
end

ActiveSupport.run_load_hooks :noticed_bulk_delivery_methods_bluesky, Noticed::BulkDeliveryMethods::Bluesky