Class: InstagramConnect::SyncConversationsJob

Inherits:
ApplicationJob
  • Object
show all
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.find_by(id: )
  return if .nil? || !.active?

  # The conversations edge lives on the Page for Facebook-Login accounts and
  # on the Instagram user for Instagram-Login ones.
  node = .page_id.presence || .ig_user_id
  threads = .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=#{.id} node=#{node}: #{threads.error_message}")
    return
  end

  Array(threads.data["data"]).each { |thread| sync_thread(, thread["id"]) }
end