Class: Bskyrb::RecordManager

Inherits:
Object
  • Object
show all
Includes:
RequestUtils
Defined in:
lib/bskyrb/records.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestUtils

#at_post_link, #create_record_uri, #default_authenticated_headers, #default_headers, #delete_record_uri, #get_author_feed_uri, #get_popular_uri, #get_post_thread_uri, #get_timeline_uri, #query_obj_to_query_params, #resolve_handle, #upload_blob_uri

Constructor Details

#initialize(session) ⇒ RecordManager

Returns a new instance of RecordManager.



7
8
9
# File 'lib/bskyrb/records.rb', line 7

def initialize(session)
  @session = session
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Instance Method Details

#block(username) ⇒ Object



139
140
141
# File 'lib/bskyrb/records.rb', line 139

def block(username)
  profile_action(username, "app.bsky.graph.block")
end

#create_post(text) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bskyrb/records.rb', line 56

def create_post(text)
  input = Bskyrb::ComAtprotoRepoCreaterecord::CreateRecord::Input.from_hash({
    "collection" => "app.bsky.feed.post",
    "$type" => "app.bsky.feed.post",
    "repo" => session.did,
    "record" => {
      "$type" => "app.bsky.feed.post",
      "createdAt" => DateTime.now.iso8601(3),
      "text" => text,
    },
  })
  create_record(input)
end

#create_record(input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/bskyrb/records.rb', line 36

def create_record(input)
  unless input.is_a?(Hash) || input.class.name.include?("Input")
    raise "`create_record` takes an Input class or a hash"
  end
  HTTParty.post(
    create_record_uri(session.pds),
    body: input.to_h.compact.to_json,
    headers: default_authenticated_headers(session),
  )
end

#create_reply(replylink, text) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bskyrb/records.rb', line 70

def create_reply(replylink, text)
  reply_to = get_post_by_url(replylink)
  input = Bskyrb::ComAtprotoRepoCreaterecord::CreateRecord::Input.from_hash({
    "collection" => "app.bsky.feed.post",
    "$type" => "app.bsky.feed.post",
    "repo" => session.did,

    "record" => {
      "reply" => {
        "parent" => {
          "uri" => reply_to.uri,
          "cid" => reply_to.cid,
        },
        "root" => {
          "uri" => reply_to.uri,
          "cid" => reply_to.cid,
        },
      },
      "$type" => "app.bsky.feed.post",
      "createdAt" => DateTime.now.iso8601(3),
      "text" => text,
    },
  })
  create_record(input)
end

#delete_record(collection, rkey) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/bskyrb/records.rb', line 47

def delete_record(collection, rkey)
  data = { collection: collection, repo: session.did, rkey: rkey }
  HTTParty.post(
    delete_record_uri(session),
    body: data.to_json,
    headers: default_authenticated_headers(session),
  )
end

#follow(username) ⇒ Object



135
136
137
# File 'lib/bskyrb/records.rb', line 135

def follow(username)
  profile_action(username, "app.bsky.graph.follow")
end

#get_latest_n_posts(username, n) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/bskyrb/records.rb', line 148

def get_latest_n_posts(username, n)
  query = Bskyrb::AppBskyFeedGetauthorfeed::GetAuthorFeed::Input.new.tap do |q|
    q.actor = username
    q.limit = n
  end
  hydrate_feed HTTParty.get(
    get_author_feed_uri(session.pds, query),
    headers: default_authenticated_headers(session),
  ), Bskyrb::AppBskyFeedGetauthorfeed::GetAuthorFeed::Output
end

#get_latest_post(username) ⇒ Object



143
144
145
146
# File 'lib/bskyrb/records.rb', line 143

def get_latest_post(username)
  feed = get_latest_n_posts(username, 1)
  feed.feed.first
end


169
170
171
172
173
174
175
176
177
# File 'lib/bskyrb/records.rb', line 169

def get_popular(n)
  query = Bskyrb::AppBskyUnspeccedGetpopular::GetPopular::Input.new.tap do |q|
    q.limit = n
  end
  hydrate_feed HTTParty.get(
    get_popular_uri(session.pds, query),
    headers: default_authenticated_headers(session),
  ), Bskyrb::AppBskyUnspeccedGetpopular::GetPopular::Output
end

#get_post_by_url(url, depth = 10) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bskyrb/records.rb', line 11

def get_post_by_url(url, depth = 10)
  # e.g. "https://staging.bsky.app/profile/naia.bsky.social/post/3jszsrnruws27"
  # or "at://did:plc:scx5mrfxxrqlfzkjcpbt3xfr/app.bsky.feed.post/3jszsrnruws27"
  # regex by chatgpt:
  query = Bskyrb::AppBskyFeedGetpostthread::GetPostThread::Input.new.tap do |q|
    q.uri = at_post_link(session.pds, url)
    q.depth = depth
  end
  res = HTTParty.get(
    get_post_thread_uri(session.pds, query),
    headers: default_authenticated_headers(session),
  )
  Bskyrb::AppBskyFeedDefs::PostView.from_hash res["thread"]["post"]
end

#get_skyline(n) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/bskyrb/records.rb', line 159

def get_skyline(n)
  query = Bskyrb::AppBskyFeedGettimeline::GetTimeline::Input.new.tap do |q|
    q.limit = n
  end
  hydrate_feed HTTParty.get(
    get_timeline_uri(session.pds, query),
    headers: default_authenticated_headers(session),
  ), Bskyrb::AppBskyFeedGettimeline::GetTimeline::Output
end

#hydrate_feed(response_hash, klass) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/bskyrb/records.rb', line 179

def hydrate_feed(response_hash, klass)
  klass.from_hash(response_hash).tap do |feed|
    feed.feed = response_hash["feed"].map do |h|
      Bskyrb::AppBskyFeedDefs::FeedViewPost.from_hash(h).tap do |obj|
        obj.post = Bskyrb::AppBskyFeedDefs::PostView.from_hash h["post"]
        obj.reply = Bskyrb::AppBskyFeedDefs::ReplyRef.from_hash h["reply"] if h["reply"]
      end
    end
  end
end

#like(post_url) ⇒ Object



125
126
127
128
# File 'lib/bskyrb/records.rb', line 125

def like(post_url)
  post = get_post_by_url(post_url)
  post_action(post, "app.bsky.feed.like")
end

#post_action(post, action_type) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bskyrb/records.rb', line 109

def post_action(post, action_type)
  data = {
    collection: action_type,
    repo: session.did,
    record: {
      subject: {
        uri: post.uri,
        cid: post.cid,
      },
      createdAt: DateTime.now.iso8601(3),
      "$type": action_type,
    },
  }
  create_record(data)
end

#profile_action(username, type) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bskyrb/records.rb', line 96

def profile_action(username, type)
  input = Bskyrb::ComAtprotoRepoCreaterecord::CreateRecord::Input.from_hash({
    "collection" => type,
    "repo" => session.did,
    "record" => {
      "subject" => resolve_handle(session.pds, username)["did"],
      "createdAt" => DateTime.now.iso8601(3),
      "$type" => type,
    },
  })
  create_record(input)
end

#repost(post_url) ⇒ Object



130
131
132
133
# File 'lib/bskyrb/records.rb', line 130

def repost(post_url)
  post = get_post_by_url(post_url)
  post_action(post, "app.bsky.feed.repost")
end

#upload_blob(blob_path, content_type) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/bskyrb/records.rb', line 26

def upload_blob(blob_path, content_type)
  # only images?
  image_bytes = File.binread(blob_path)
  HTTParty.post(
    upload_blob_uri(session.pds),
    body: image_bytes,
    headers: default_authenticated_headers(session),
  )
end