79 lines
2.0 KiB
Ruby
79 lines
2.0 KiB
Ruby
# typed: false
|
|
class Domain::Fa::UserAvatar < ReduxApplicationRecord
|
|
self.table_name = "domain_fa_user_avatars"
|
|
|
|
enum :state,
|
|
[
|
|
:ok, # got the file, no problem
|
|
:download_error, # other error processing the file
|
|
:no_file_on_guessed_user_page_error,
|
|
:file_not_found, # 404 from server
|
|
]
|
|
after_initialize do
|
|
self.state ||= :ok
|
|
self.state_detail ||= {}
|
|
end
|
|
|
|
belongs_to :user, class_name: "::Domain::Fa::User"
|
|
belongs_to :file,
|
|
foreign_key: :file_sha256,
|
|
class_name: "::BlobEntry",
|
|
optional: true
|
|
belongs_to :log_entry, class_name: "::HttpLogEntry", optional: true
|
|
|
|
def file
|
|
@file_model ||= BlobEntry.ensure(file_sha256) if file_sha256
|
|
end
|
|
|
|
def file_uri
|
|
Addressable::URI.parse(file_url_str) unless file_url_str.blank?
|
|
end
|
|
|
|
def file_uri=(uri)
|
|
uri = Addressable::URI.parse(uri) unless uri.is_a?(Addressable::URI)
|
|
uri.scheme = "https" if uri.scheme.blank?
|
|
self.file_url_str = uri.to_s
|
|
end
|
|
|
|
def guess_user_page_log_entry
|
|
user.guess_user_page_log_entry
|
|
end
|
|
|
|
def guess_file_uri_from_hles_with_info
|
|
hle = guess_user_page_log_entry
|
|
if hle
|
|
page =
|
|
Domain::Fa::Parser::Page.new(
|
|
hle.response.contents,
|
|
require_logged_in: false,
|
|
)
|
|
if page.probably_user_page? && (url = page.user_page.profile_thumb_url)
|
|
return :user_page, url
|
|
end
|
|
end
|
|
|
|
posts =
|
|
user
|
|
.posts
|
|
.where(state: [:ok, nil])
|
|
.where("file_url_str IS NOT NULL")
|
|
.order(created_at: :desc)
|
|
.limit(3)
|
|
|
|
for post in posts
|
|
if (hle = post.guess_last_submission_page)
|
|
page = Domain::Fa::Parser::Page.new(hle.response.contents)
|
|
next unless page.probably_submission?
|
|
url = page.submission.artist_avatar_url
|
|
return :post_page, url, post.fa_id if url
|
|
end
|
|
end
|
|
|
|
[:not_found, nil]
|
|
end
|
|
|
|
def guess_file_uri_from_hles
|
|
guess_file_uri_from_hles_with_info[1]
|
|
end
|
|
end
|