Class: Causeway::Publishers::Bluesky

Inherits:
Base
  • Object
show all
Defined in:
lib/causeway/publishers/bluesky.rb

Constant Summary collapse

API =
"https://bsky.social"

Instance Method Summary collapse

Instance Method Details

#nameObject



12
13
14
# File 'lib/causeway/publishers/bluesky.rb', line 12

def name
  "Bluesky"
end

#post(text) ⇒ Object



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
# File 'lib/causeway/publishers/bluesky.rb', line 16

def post(text)
  handle = Causeway.configuration.bluesky_handle
  app_password = Causeway.configuration.bluesky_app_password

  return { error: "Bluesky handle not configured." } unless handle && !handle.empty?
  return { error: "Bluesky app password not configured." } unless app_password && !app_password.empty?

  session = create_session(handle, app_password)
  return session if session[:error]

  facets = detect_facets(text)

  record = {
    "$type" => "app.bsky.feed.post",
    "text" => text,
    "createdAt" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ")
  }
  record["facets"] = facets unless facets.empty?

  body = {
    "repo" => session[:did],
    "collection" => "app.bsky.feed.post",
    "record" => record
  }

  uri = URI("#{API}/xrpc/com.atproto.repo.createRecord")
  response = https_post_json(uri, body, auth: session[:access_jwt])

  unless response.is_a?(Net::HTTPSuccess)
    return { error: "Bluesky API error: #{response.code} #{response.body}" }
  end

  data = JSON.parse(response.body)
  rkey = data["uri"].split("/").last
  { id: data["uri"], url: "https://bsky.app/profile/#{handle}/post/#{rkey}" }
end