Files
redux-scraper/app/models/domain/post_file.rb
2025-03-03 23:28:05 +00:00

65 lines
1.7 KiB
Ruby

# typed: strict
class Domain::PostFile < ReduxApplicationRecord
include AttrJsonRecordAliases
self.table_name = "domain_post_files"
belongs_to :post, foreign_key: :post_id, class_name: "::Domain::Post"
belongs_to :log_entry, class_name: "::HttpLogEntry", optional: true
belongs_to :blob,
class_name: "::BlobFile",
optional: true,
foreign_key: :blob_sha256
has_many :thumbnails,
class_name: "::Domain::PostFileThumbnail",
foreign_key: :post_file_id,
dependent: :destroy,
inverse_of: :post_file
attr_json :state, :string
attr_json :url_str, :string
attr_json :error_message, :string
attr_json :last_status_code, :integer
attr_json :retry_count, :integer
enum :state,
{
pending: "pending",
ok: "ok",
file_error: "file_error",
retryable_error: "retryable_error",
terminal_error: "terminal_error",
removed: "removed",
},
prefix: "state"
validates :state, presence: true
after_initialize do
self.state ||= "pending" if new_record?
self.type ||= self.class.name if new_record?
end
sig { returns(T.nilable(BlobFile)) }
def blob
super ||
begin
@blob_file_model = T.let(@blob_file_model, T.nilable(BlobFile))
@blob_file_model ||=
((sha256 = self.blob_sha256) ? BlobFile.migrate_sha256!(sha256) : nil)
@blob_file_model
end
end
sig { params(le: T.nilable(HttpLogEntry)).returns(T.nilable(HttpLogEntry)) }
def log_entry=(le)
self.blob_sha256 ||= le.response_sha256 if le.present?
super(le)
end
sig { returns(Integer) }
def retry_count
super || 0
end
end