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 =
"id,text,timestamp,username,from,parent_id,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.
20 21 22 23 24 25 26 |
# File 'app/jobs/instagram_connect/sync_comments_job.rb', line 20 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
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(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) Array(item.dig("replies", "data")).each do |reply| # Meta omits parent_id inside the replies expansion — the nesting # itself is the statement. upsert(account, ig_media_id, reply.merge("parent_id" => reply["parent_id"] || item["id"])) end end end |