Class: Jobs::GrantOnebox

Inherits:
Onceoff show all
Defined in:
app/jobs/onceoff/grant_onebox.rb

Instance Method Summary collapse

Methods inherited from Onceoff

enqueue_all, #execute, name_for, #running_key_name

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, #execute, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#badgeObject



38
39
40
# File 'app/jobs/onceoff/grant_onebox.rb', line 38

def badge
  Badge.find(Badge::FirstOnebox)
end

#execute_onceoff(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/jobs/onceoff/grant_onebox.rb', line 7

def execute_onceoff(args)
  return unless SiteSetting.enable_badges
  to_award = {}

  Post
    .secured(Guardian.new)
    .select(:id, :created_at, :raw, :user_id)
    .visible
    .public_posts
    .where("raw LIKE '%http%'")
    .find_in_batches do |group|
      group.each do |p|
        begin
          # Note we can't use `p.cooked` here because oneboxes have been cooked out
          cooked = PrettyText.cook(p.raw)
          doc = Nokogiri::HTML5.fragment(cooked)
          if doc.search("a.onebox").size > 0
            to_award[p.user_id] ||= { post_id: p.id, created_at: p.created_at }
          end
        rescue StandardError
          nil # if there is a problem cooking we don't care
        end
      end
    end

  to_award.each do |user_id, opts|
    user = User.where(id: user_id).first
    BadgeGranter.grant(badge, user, opts) if user
  end
end