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 =

Only fields Meta documents for INSTAGRAM comments. from... and parent_id are Facebook-comment fields; asking for them can fail the whole call with (#100) — and one refused field means zero comments imported. Reply nesting is derived from the replies expansion itself, and the commenter's igsid still arrives via webhooks.

"id,text,timestamp,username,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.



25
26
27
28
29
30
31
# File 'app/jobs/instagram_connect/sync_comments_job.rb', line 25

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



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

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)
    import_replies(, ig_media_id, item)
  end
end