Don't forget to lycansubscribe

added a helper for handling at:// URIs

+32
app/at_uri.rb
··· 1 + class AT_URI 2 + class InvalidURIError 3 + end 4 + 5 + attr_reader :repo, :collection, :rkey 6 + 7 + def initialize(uri) 8 + raise InvalidURIError, "Invalid AT URI: #{uri}" if uri.include?(' ') || !uri.start_with?('at://') 9 + 10 + parts = uri.split('/') 11 + parts.each(&:freeze) 12 + 13 + raise InvalidURIError, "Invalid AT URI: #{uri}" if parts.length != 5 || parts[2..4].any?(&:empty?) 14 + 15 + @uri = uri 16 + @repo, @collection, @rkey = parts[2..4] 17 + end 18 + 19 + def to_ary 20 + [@repo, @collection, @rkey] 21 + end 22 + 23 + alias split to_ary 24 + 25 + def to_s 26 + @uri 27 + end 28 + end 29 + 30 + def AT_URI(uri) 31 + AT_URI.new(uri) 32 + end
+6 -6
app/importer.rb
··· 1 1 require 'didkit' 2 2 require 'minisky' 3 3 4 + require_relative 'at_uri' 4 5 require_relative 'models/import' 5 6 require_relative 'models/like' 6 7 require_relative 'models/post' ··· 66 67 def process_likes(likes) 67 68 likes.each do |record| 68 69 begin 69 - like_rkey = record['uri'].split('/').last 70 + like_rkey = AT_URI(record['uri']).rkey 70 71 next if @user.likes.where(rkey: like_rkey).exists? 71 72 72 73 like_time = Time.parse(record['value']['createdAt']) 73 74 74 - post_uri = record['value']['subject']['uri'] 75 - parts = post_uri.split('/') 76 - next if parts[3] != 'app.bsky.feed.post' 75 + post_uri = AT_URI(record['value']['subject']['uri']) 76 + next if post_uri.collection != 'app.bsky.feed.post' 77 77 78 - post_did, _, post_rkey = parts[2..4] 78 + post_did = post_uri.repo 79 79 80 80 if @uid_cache[post_did].nil? 81 81 post_author = User.find_or_create_by!(did: post_did) 82 82 @uid_cache[post_did] = post_author.id 83 83 end 84 84 85 - post = Post.find_by(user_id: @uid_cache[post_did], rkey: post_rkey) 85 + post = Post.find_by(user_id: @uid_cache[post_did], rkey: post_uri.rkey) 86 86 87 87 if post 88 88 @user.likes.create!(rkey: like_rkey, time: like_time, post: post)
+2 -1
app/post_downloader.rb
··· 1 1 require 'didkit' 2 2 require 'minisky' 3 3 4 + require_relative 'at_uri' 4 5 require_relative 'models/post' 5 6 require_relative 'models/user' 6 7 ··· 50 51 end 51 52 52 53 def save_post(post_uri, record) 53 - did, _, rkey = post_uri.split('/')[2..4] 54 + did, _, rkey = AT_URI(post_uri) 54 55 55 56 text = record.delete('text') 56 57 created = record.delete('createdAt')