Class: InstagramConnect::SyncCommentsJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/instagram_connect/sync_comments_job.rb

Overview

Imports a post's existing comments — the ones that happened before the webhook subscription existed, which the webhook can never deliver. The comments edge is walked with cursors, replies expanded inline, and every comment upserted by Meta's id, so re-running never duplicates and a webhook-created row is simply enriched.

Meta is the source of truth for hidden: a comment hidden from the app on the phone arrives hidden here, and one unhidden there un-hides here.

Constant Summary collapse

THROTTLE =
10.minutes
REPLY_FIELDS =
"id,text,timestamp,username,from,parent_id,hidden,like_count".freeze
COMMENT_FIELDS =
"#{REPLY_FIELDS},replies{#{REPLY_FIELDS}}".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enqueue_throttled(account, ig_media_id) ⇒ Object

One key per post: opening a busy post's comments repeatedly enqueues one import, not one per page view.



20
21
22
23
24
25
26
# File 'app/jobs/instagram_connect/sync_comments_job.rb', line 20

def self.enqueue_throttled(, ig_media_id)
  Rails.cache.fetch("instagram_connect:sync_comments:#{.id}:#{ig_media_id}",
                    expires_in: THROTTLE) do
    perform_later(.id, ig_media_id.to_s)
    true
  end
end

Instance Method Details

#perform(account_id, ig_media_id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/jobs/instagram_connect/sync_comments_job.rb', line 28

def perform(, ig_media_id)
   = Account.find_by(id: )
  return if .nil? || !.active?

  result = .client.collect("/#{ig_media_id}/comments",
                                  { fields: COMMENT_FIELDS, limit: 50 })
  unless result.success?
    return logger.error("[instagram_connect] comment sync failed for " \
                        "account=#{.id} media=#{ig_media_id}: #{result.error_message}")
  end

  Array(result.data["data"]).each do |item|
    upsert(, ig_media_id, item)
    Array(item.dig("replies", "data")).each do |reply|
      # Meta omits parent_id inside the replies expansion — the nesting
      # itself is the statement.
      upsert(, ig_media_id, reply.merge("parent_id" => reply["parent_id"] || item["id"]))
    end
  end
end