2023-02-23 09:08:34 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
2023-02-24 05:53:43 -05:00
|
|
|
class EvaluatePostUploads < ::Jobs::Base
|
2023-02-23 09:08:34 -05:00
|
|
|
def execute(args)
|
2023-02-24 07:11:58 -05:00
|
|
|
return unless SiteSetting.ai_nsfw_detection_enabled
|
|
|
|
return if (post_id = args[:post_id]).blank?
|
2023-02-23 09:08:34 -05:00
|
|
|
|
2023-02-24 07:11:58 -05:00
|
|
|
post = Post.includes(:uploads).find_by_id(post_id)
|
|
|
|
return if post.nil? || post.uploads.empty?
|
2023-02-23 09:08:34 -05:00
|
|
|
|
2023-02-24 07:11:58 -05:00
|
|
|
nsfw_evaluation = DiscourseAI::NSFW::Evaluation.new
|
2023-02-23 09:08:34 -05:00
|
|
|
|
2023-02-24 07:11:58 -05:00
|
|
|
image_uploads = post.uploads.select { |upload| FileHelper.is_supported_image?(upload.url) }
|
|
|
|
|
|
|
|
results = image_uploads.map { |upload| nsfw_evaluation.perform(upload) }
|
|
|
|
|
|
|
|
DiscourseAI::FlagManager.new(post).flag! if results.any? { |r| r[:verdict] }
|
2023-02-23 09:08:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|