Template of a custom feed generator service for the Bluesky network in Ruby
1require 'active_record'
2require 'json'
3
4class Post < ActiveRecord::Base
5 validates_presence_of :repo, :time, :data, :rkey
6 validates :text, length: { minimum: 0, allow_nil: false }
7 validates_length_of :repo, maximum: 60
8 validates_length_of :rkey, maximum: 16
9 validates_length_of :text, maximum: 1000
10 validates_length_of :data, maximum: 10000
11
12 has_many :feed_posts, dependent: :destroy
13
14 attr_writer :record
15
16 def self.find_by_repo_rkey(repo, rkey)
17 # the '+' is to make sure that SQLite uses the rkey index and not a different one
18 Post.where("+repo = ?", repo).where(rkey: rkey).first
19 end
20
21 def self.find_by_at_uri(uri)
22 parts = uri.gsub(%r(^at://), '').split('/')
23 return nil unless parts.length == 3 && parts[1] == 'app.bsky.feed.post'
24
25 find_by_repo_rkey(parts[0], parts[2])
26 end
27
28 def record
29 @record ||= JSON.parse(data)
30 end
31
32 def at_uri
33 "at://#{repo}/app.bsky.feed.post/#{rkey}"
34 end
35
36 def trim_too_long_data
37 if embed = record['embed']
38 if external = embed['external']
39 external['description'] = ''
40 end
41 end
42
43 if record['bridgyOriginalText']
44 record['bridgyOriginalText'] = ''
45 end
46
47 self.data = JSON.generate(record)
48 end
49end