Don't forget to lycansubscribe

better error handling for unfetched posts

Changed files
+49
app
+49
app/post_downloader.rb
··· 13 13 14 14 @total_count = 0 15 15 @oldest_imported = Time.now 16 + @account_status_cache = {} 16 17 end 17 18 18 19 def import_from_queue(queue) ··· 61 62 puts "Error in PostDownloader: #{item.post_uri}: #{e.class}: #{e}" 62 63 end 63 64 end 65 + 66 + check_missing_items(items) 64 67 rescue StandardError => e 65 68 puts "Error in PostDownloader: #{e.class}: #{e}" 66 69 end ··· 100 103 @report&.update(downloader: { downloaded_posts: @total_count, oldest_date: @oldest_imported }) 101 104 102 105 item.destroy 106 + end 107 + 108 + def check_missing_items(items) 109 + return if items.empty? 110 + 111 + dids = items.map { |x| AT_URI(x.post_uri).repo }.uniq 112 + response = @sky.get_request('app.bsky.actor.getProfiles', { actors: dids }) 113 + active_dids = response['profiles'].map { |x| x['did'] } 114 + 115 + items.each do |item| 116 + did = AT_URI(item.post_uri).repo 117 + did_obj = DID.new(did) 118 + 119 + if active_dids.include?(did) 120 + # account exists but post doesn't, delete the post reference 121 + item.destroy 122 + else 123 + begin 124 + status = if @account_status_cache.has_key?(did) # don't retry if status was nil 125 + @account_status_cache[did] 126 + else 127 + @account_status_cache[did] ||= did_obj.account_status 128 + end 129 + 130 + case status 131 + when :active 132 + # account is active but wasn't returned in getProfiles, probably was suspended on the AppView 133 + puts "#{item.post_uri}: account #{did} exists on the PDS, account must have been taken down" 134 + item.destroy 135 + when nil 136 + # account was deleted, so all posts were deleted too 137 + puts "#{item.post_uri}: account #{did} doesn't exist on the PDS, post must have been deleted" 138 + item.destroy 139 + else 140 + # account is inactive/suspended, but could come back, so leave it for now 141 + puts "#{item.post_uri}: account #{did} is inactive: #{status}" 142 + end 143 + rescue StandardError => e 144 + hostname = did_obj.document.pds_host rescue "???" 145 + puts "#{item.post_uri}: couldn't check account status for #{did} on #{hostname}: #{e.class}: #{e}" 146 + 147 + # delete reference if the account's PDS is the old bsky.social (so it must have been deleted pre Nov 2023) 148 + item.destroy if hostname == 'bsky.social' 149 + end 150 + end 151 + end 103 152 end 104 153 end