86 lines
2.4 KiB
Ruby
86 lines
2.4 KiB
Ruby
# typed: false
|
|
module Domain::Inkbunny::Job
|
|
class UserGalleryJob < Base
|
|
def perform(args)
|
|
user = args[:user] || raise("user must exist")
|
|
caused_by_entry = args[:caused_by_entry]
|
|
logger.prefix = "[#{user.name.bold} / #{user.ib_user_id.to_s.bold}]"
|
|
|
|
if user.scanned_gallery_at&.after?(1.week.ago)
|
|
logger.warn(
|
|
"gallery scanned #{time_ago_in_words(user.scanned_gallery_at)}, skipping",
|
|
)
|
|
return
|
|
end
|
|
|
|
processor = ApiSearchPageProcessor.new
|
|
first_log_entry = nil
|
|
rid = nil
|
|
page = 1
|
|
loop_count = 0
|
|
|
|
while true
|
|
loop_count += 1
|
|
if loop_count > ApiSearchPageProcessor::MAX_LOOP_COUNT
|
|
raise("loop_count: #{loop_count}")
|
|
end
|
|
|
|
url =
|
|
ApiSearchPageProcessor.build_api_search_url(
|
|
ib_user_id: user.ib_user_id,
|
|
rid: rid,
|
|
page: page,
|
|
)
|
|
|
|
response =
|
|
http_client.post(
|
|
url,
|
|
caused_by_entry: first_log_entry || caused_by_entry,
|
|
)
|
|
first_log_entry ||= response.log_entry
|
|
if response.status_code != 200
|
|
fatal_error("api_search failed: #{response.status_code}")
|
|
end
|
|
result =
|
|
processor.process!(
|
|
JSON.parse(response.body),
|
|
caused_by_entry: response.log_entry,
|
|
)
|
|
num_new_posts = result[:num_new_posts]
|
|
logger.info(
|
|
[
|
|
"[rid: #{rid}]",
|
|
"[page: #{page}]",
|
|
"[new posts: #{num_new_posts}]",
|
|
"[total new posts: #{result[:num_total_new_posts]}]",
|
|
"[total changed posts: #{result[:num_total_changed_posts]}]",
|
|
"[total posts: #{result[:num_total_posts]}]",
|
|
].join(" "),
|
|
)
|
|
|
|
if user.scanned_gallery_at.present? && num_new_posts == 0
|
|
logger.info("[no new posts, stopping]")
|
|
break
|
|
end
|
|
rid = result[:rid] || raise("no rid")
|
|
break if result[:num_pages] == page
|
|
page += 1
|
|
end
|
|
|
|
logger.info("[total new posts: #{result[:num_total_new_posts]}]")
|
|
user.scanned_gallery_at = Time.current
|
|
user.save!
|
|
|
|
if processor.changed_posts.any?
|
|
defer_job(
|
|
Domain::Inkbunny::Job::UpdatePostsJob,
|
|
{
|
|
ib_post_ids: processor.changed_posts.map(&:ib_post_id),
|
|
caused_by_entry: first_log_entry,
|
|
},
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|