enqueue user avatar job
This commit is contained in:
@@ -14,11 +14,17 @@ class Domain::Fa::Job::UserAvatarJob < Domain::Fa::Job::Base
|
||||
return
|
||||
end
|
||||
|
||||
if @avatar.state != "ok" && !@force_scan
|
||||
logger.warn("avatar is in error state, skipping")
|
||||
return
|
||||
end
|
||||
|
||||
unless @avatar.file_uri
|
||||
# try to find a corresponding log entry
|
||||
log_entry = @avatar.guess_user_page_log_entry || raise("no user page log entry found")
|
||||
@caused_by_entry ||= log_entry
|
||||
parser = Domain::Fa::Parser::Page.new(log_entry.response.contents, require_logged_in: false)
|
||||
@avatar.state_detail["guessed_log_entry_id"] = log_entry.id
|
||||
@avatar.file_uri = parser.user_page.profile_thumb_url
|
||||
end
|
||||
|
||||
|
||||
@@ -69,6 +69,17 @@ class Domain::Fa::Job::UserPageJob < Domain::Fa::Job::Base
|
||||
@user.profile_html = user_page.profile_html.encode("UTF-8", :invalid => :replace, :undef => :replace)
|
||||
@user.scanned_page_at = Time.now
|
||||
@user.log_entry_detail["last_user_page_id"] = response.log_entry.id
|
||||
|
||||
avatar = @user.avatar_or_create
|
||||
@user.avatar.file_uri = user_page.profile_thumb_url
|
||||
if @user.avatar.changed?
|
||||
@user.avatar.save!
|
||||
Domain::Fa::Job::UserAvatarJob.perform_later({
|
||||
user: @user,
|
||||
caused_by_entry: response.log_entry,
|
||||
})
|
||||
end
|
||||
|
||||
@user.save!
|
||||
logger.info "completed page scan"
|
||||
ensure
|
||||
|
||||
@@ -15,22 +15,6 @@ describe Domain::Fa::Job::UserAvatarJob do
|
||||
end
|
||||
end
|
||||
|
||||
shared_context "meesh avatar file mock" do
|
||||
before do
|
||||
@log_entries = SpecUtil.init_http_client_mock(
|
||||
http_client_mock, [
|
||||
{
|
||||
uri: "https://a.furaffinity.net/1635789297/meesh.gif",
|
||||
status_code: 200,
|
||||
content_type: "image/gif",
|
||||
contents: SpecUtil.read_fixture_file("domain/fa/job/meesh_avatar_file.gif", mode: "rb"),
|
||||
caused_by_entry: meesh_user_page_log_entry,
|
||||
},
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when the avatar model does not yet exist" do
|
||||
include_context "create meesh user"
|
||||
context "the user model has a last_user_page_id" do
|
||||
@@ -42,9 +26,20 @@ describe Domain::Fa::Job::UserAvatarJob do
|
||||
contents: SpecUtil.read_fixture_file("domain/fa/job/user_page_meesh.html"),
|
||||
)
|
||||
end
|
||||
include_context "meesh avatar file mock"
|
||||
|
||||
before do
|
||||
@log_entries = SpecUtil.init_http_client_mock(
|
||||
http_client_mock, [
|
||||
{
|
||||
uri: "https://a.furaffinity.net/1635789297/meesh.gif",
|
||||
status_code: 200,
|
||||
content_type: "image/gif",
|
||||
contents: SpecUtil.read_fixture_file("domain/fa/job/meesh_avatar_file.gif", mode: "rb"),
|
||||
caused_by_entry: meesh_user_page_log_entry,
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
user.log_entry_detail["last_user_page_id"] = meesh_user_page_log_entry.id
|
||||
user.save!
|
||||
end
|
||||
@@ -68,4 +63,35 @@ describe Domain::Fa::Job::UserAvatarJob do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "the avatar model exists with a file uri" do
|
||||
include_context "create meesh user"
|
||||
before do
|
||||
avatar = user.avatar_or_create
|
||||
avatar.file_uri = "https://www.furaffinity.net/a/test/uri.gif"
|
||||
avatar.save!
|
||||
|
||||
@log_entries = SpecUtil.init_http_client_mock(
|
||||
http_client_mock, [
|
||||
{
|
||||
uri: "https://www.furaffinity.net/a/test/uri.gif",
|
||||
status_code: 200,
|
||||
content_type: "image/gif",
|
||||
contents: SpecUtil.read_fixture_file("domain/fa/job/meesh_avatar_file.gif", mode: "rb"),
|
||||
},
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
it "succeeds" do
|
||||
ret = described_class.perform_now({ user: user })
|
||||
expect(ret).to_not be_a(Exception)
|
||||
user.reload
|
||||
avatar = user.avatar
|
||||
expect(avatar).not_to be_nil
|
||||
expect(avatar.log_entry).to eq(@log_entries[0])
|
||||
expect(HexUtil.bin2hex avatar.file_sha256).to eq("ebbafc07555df0a0656a9b32ec9b95723c62c5246937dc8434924d9241d1b570")
|
||||
expect(avatar.downloaded_file_at).to be_within(1.seconds).of(Time.now)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
35
spec/jobs/domain/fa/job/user_page_job_spec.rb
Normal file
35
spec/jobs/domain/fa/job/user_page_job_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Domain::Fa::Job::UserPageJob do
|
||||
let(:http_client_mock) { instance_double("::Scraper::HttpClient") }
|
||||
|
||||
before do
|
||||
Scraper::ClientFactory.http_client_mock = http_client_mock
|
||||
@log_entries = SpecUtil.init_http_client_mock(
|
||||
http_client_mock, [
|
||||
{
|
||||
uri: "https://www.furaffinity.net/user/meesh/",
|
||||
status_code: 200,
|
||||
content_type: "text/html",
|
||||
contents: SpecUtil.read_fixture_file("domain/fa/job/user_page_meesh.html"),
|
||||
},
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
it "succeeds" do
|
||||
ret = described_class.perform_now({ url_name: "meesh" })
|
||||
expect(ret).to_not be_a(Exception)
|
||||
user = Domain::Fa::User.find_by(url_name: "meesh")
|
||||
expect(user).to_not be_nil
|
||||
expect(user.avatar.file_uri.to_s).to eq("https://a.furaffinity.net/1635789297/meesh.gif")
|
||||
expect(SpecUtil.enqueued_jobs(Domain::Fa::Job::UserAvatarJob)).to match(
|
||||
[
|
||||
including(args: [{
|
||||
user: user,
|
||||
caused_by_entry: @log_entries[0],
|
||||
}]),
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user