Class: ATProto::Record

Inherits:
T::Struct
  • Object
show all
Extended by:
RequestUtils, T::Sig
Defined in:
lib/at_protocol/record.rb,
lib/at_protocol/record/strongref.rb

Defined Under Namespace

Classes: StrongRef

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestUtils

at_uri, create_session_uri, default_authenticated_headers, default_headers, delete_record_uri, delete_session_uri, get_paginated_data, get_post_thread_uri, get_session_uri, mute_actor_uri, query_obj_to_query_params, refresh_session_uri, refresh_token_headers, resolve_handle, upload_blob_uri

Class Method Details

.create(content_hash, session, rkey = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/at_protocol/record.rb', line 41

def create(content_hash, session, rkey = nil)
  return nil if content_hash["$type"].nil?
  params = {
    repo: session.did,
    collection: content_hash["$type"],
    record: content_hash,
  }
  params[:rkey] = rkey unless rkey.nil?
  from_uri(at_uri(session.xrpc.post.com_atproto_repo_createRecord(**params)["uri"]))
end

.from_hash(json_hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/at_protocol/record.rb', line 15

def from_hash(json_hash)
  return nil if json_hash["value"].nil?
  timestamp = nil
  timestamp = Time.parse json_hash["value"]["createdAt"] if json_hash["value"] && json_hash["value"]["createdAt"]
  raw_content = json_hash["value"]
  new(
    uri: at_uri(
      T.must(json_hash["uri"])
    ),
    cid: CID.from_json(json_hash["cid"]),
    timestamp: timestamp, content: raw_content,
  )
end

.from_uri(uri, pds = "https://bsky.social") ⇒ Object



31
32
33
34
35
36
37
# File 'lib/at_protocol/record.rb', line 31

def from_uri(uri, pds = "https://bsky.social")
  from_hash(XRPC::Client.new(pds).get.com_atproto_repo_getRecord(
    repo: uri.repo.to_s,
    collection: "#{uri.collection}",
    rkey: uri.rkey,
  ))
end

Instance Method Details

#==(other) ⇒ Object



86
87
88
# File 'lib/at_protocol/record.rb', line 86

def ==(other)
  self.cid.to_s == other.cid.to_s && self.uri.to_s == other.uri.to_s
end

#delete(session) ⇒ Object



67
68
69
# File 'lib/at_protocol/record.rb', line 67

def delete(session)
  session.then(&to_write)
end

#put(session) ⇒ Object



61
62
63
# File 'lib/at_protocol/record.rb', line 61

def put(session)
  session.then(&to_write(:update)).uri.resolve(pds: session.pds)
end

#refresh(pds = "https://bsky.social") ⇒ Object



55
56
57
# File 'lib/at_protocol/record.rb', line 55

def refresh(pds = "https://bsky.social")
  self.class.from_uri(self.uri, pds)
end

#to_write(type = :delete) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/at_protocol/record.rb', line 73

def to_write(type = :delete)
  ATProto::Writes::Write.new(
    **({
      action: Writes::Write::Action.deserialize(type),
      value: (self.content if type == :update),
      collection: self.uri.collection,
      rkey: self.uri.rkey,
    }.compact),
  )
end