Module: Bskyrb::RequestUtils

Included in:
RecordManager, Session
Defined in:
lib/bskyrb/session.rb

Overview

Goal is to replace with pure XRPC eventually

Instance Method Summary collapse

Instance Method Details

#at_post_link(pds, url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bskyrb/session.rb', line 47

def at_post_link(pds, url)
  url = url.to_s

  # Check if the URL is already in AT format
  if url.start_with?("at://")
    # Validate the username and post ID in the URL
    parts = url.split("/")
    if parts.length == 5 && parts[3] == "app.bsky.feed.post"
      username = parts[2]
      post_id = parts[4]
      if post_id
        return url
      end
    end

    # If the URL is not valid, raise an error
    raise "The provided URL #{url} is not a valid AT URL"
  end

  # Validate the format of the regular URL and extract the username and post ID
  regex = /https:\/\/[a-zA-Z0-9.-]+\/profile\/[a-zA-Z0-9.-]+\/post\/[a-zA-Z0-9.-]+/
  raise "The provided URL #{url} does not match the expected schema" unless regex.match?(url)
  username = url.split("/")[-3]
  post_id = url.split("/")[-1]

  # Validate the username and post ID in the AT URL
  did = resolve_handle(pds, username)["did"]

  # Construct the AT URL
  "at://#{did}/app.bsky.feed.post/#{post_id}"
end

#create_record_uri(pds) ⇒ Object



21
22
23
# File 'lib/bskyrb/session.rb', line 21

def create_record_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.createRecord"
end

#default_authenticated_headers(session) ⇒ Object



41
42
43
44
45
# File 'lib/bskyrb/session.rb', line 41

def default_authenticated_headers(session)
  default_headers.merge({
    Authorization: "Bearer #{session.access_token}",
  })
end

#default_headersObject



17
18
19
# File 'lib/bskyrb/session.rb', line 17

def default_headers
  { "Content-Type" => "application/json" }
end

#delete_record_uri(pds) ⇒ Object



25
26
27
# File 'lib/bskyrb/session.rb', line 25

def delete_record_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.deleteRecord"
end

#get_post_thread_uri(pds, query) ⇒ Object



37
38
39
# File 'lib/bskyrb/session.rb', line 37

def get_post_thread_uri(pds, query)
  "#{pds}/xrpc/app.bsky.feed.getPostThread#{query_obj_to_query_params(query)}"
end

#mute_actor_uri(pds) ⇒ Object



29
30
31
# File 'lib/bskyrb/session.rb', line 29

def mute_actor_uri(pds)
  "#{pds}/xrpc/app.bsky.graph.muteActor"
end

#query_obj_to_query_params(q) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/bskyrb/session.rb', line 9

def query_obj_to_query_params(q)
  out = "?"
  q.to_h.each do |key, value|
    out += "#{key}=#{value}&" unless value.nil? || (value.class.method_defined?(:empty?) && value.empty?)
  end
  out.slice(0...-1)
end

#resolve_handle(pds, username) ⇒ Object



5
6
7
# File 'lib/bskyrb/session.rb', line 5

def resolve_handle(pds, username)
  XRPC.request(pds, "com.atproto.identity.resolveHandle", handle: username)
end

#upload_blob_uri(pds) ⇒ Object



33
34
35
# File 'lib/bskyrb/session.rb', line 33

def upload_blob_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.uploadBlob"
end