Class: InstagramConnect::SyncConversationsJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- InstagramConnect::SyncConversationsJob
- Defined in:
- app/jobs/instagram_connect/sync_conversations_job.rb
Overview
Pulls the threads that already exist on the account.
Webhooks only ever announce NEW activity, so a freshly connected account shows an empty inbox until somebody happens to message in — even though the operator has years of conversations sitting in Instagram. This fetches what Meta will give us: every thread, and the 20 most recent messages in each, which is the entire history the API exposes to any app.
Safe to re-run. Threads are located by igsid and messages are deduped on ig_message_id, so a second pass updates rather than duplicates.
Instance Method Summary collapse
Instance Method Details
#perform(account_id) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/jobs/instagram_connect/sync_conversations_job.rb', line 15 def perform(account_id) account = Account.find_by(id: account_id) return if account.nil? || !account.active? # The conversations edge lives on the Page for Facebook-Login accounts and # on the Instagram user for Instagram-Login ones. node = account.page_id.presence || account.ig_user_id threads = account.client.list_conversations(node_id: node) unless threads.success? # A sync that fails must say so. Swallowing this once cost a production # afternoon: the job "succeeded" in 600ms and imported nothing. logger.error("[instagram_connect] conversation sync failed for " \ "account=#{account.id} node=#{node}: #{threads.}") return end Array(threads.data["data"]).each { |thread| sync_thread(account, thread["id"]) } end |