Class: InstagramConnect::SyncCommentsJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- InstagramConnect::SyncCommentsJob
- 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
-
.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.
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(account, ig_media_id) Rails.cache.fetch("instagram_connect:sync_comments:#{account.id}:#{ig_media_id}", expires_in: THROTTLE) do perform_later(account.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(account_id, ig_media_id) account = Account.find_by(id: account_id) return if account.nil? || !account.active? result = account.client.collect("/#{ig_media_id}/comments", { fields: COMMENT_FIELDS, limit: 50 }) unless result.success? return logger.error("[instagram_connect] comment sync failed for " \ "account=#{account.id} media=#{ig_media_id}: #{result.}") end Array(result.data["data"]).each do |item| upsert(account, ig_media_id, item) import_replies(account, ig_media_id, item) end end |