Class: InstagramConnect::MessageAttachment

Inherits:
ApplicationRecord show all
Defined in:
app/models/instagram_connect/message_attachment.rb

Overview

One file on a message. Rows are created at ingest with everything already decided except the bytes, which a job fetches separately.

Constant Summary collapse

STATES =

Instagram gives no second chance at inbound media: the CDN URL expires and there is no endpoint to ask again. So there is no "downloadable" state here as there is for transports that keep the file — an attachment either lands in storage or is permanently unavailable.

%w[pending attached unavailable skipped].freeze
KINDS =

Meta's own vocabulary for what the attachment is, which is not the same question as what MIME type the bytes turn out to be.

%w[image video audio file share story_mention ig_reel reel template].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enable_file_attachment!Object

Called from the engine's to_prepare when Active Storage is present, the same shape as Account.enable_token_encryption!. The gem does not depend on Active Storage, so a host without it still boots and simply keeps the attachment rows as metadata without ever fetching bytes.



28
29
30
# File 'app/models/instagram_connect/message_attachment.rb', line 28

def self.enable_file_attachment!
  has_one_attached :file
end

Instance Method Details

#attached?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/instagram_connect/message_attachment.rb', line 39

def attached?
  state == "attached"
end

#fetchable?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/instagram_connect/message_attachment.rb', line 35

def fetchable?
  state == "pending" && source_url.present?
end

#mark_attached!(mime:, size:, filename: nil) ⇒ Object



43
44
45
# File 'app/models/instagram_connect/message_attachment.rb', line 43

def mark_attached!(mime:, size:, filename: nil)
  update!(state: "attached", mime: mime, size: size, filename: filename, error: nil)
end

#mark_unavailable!(reason) ⇒ Object

Terminal. Nothing retries after this, because retrying a URL Instagram has already expired only burns the rate budget.



49
50
51
# File 'app/models/instagram_connect/message_attachment.rb', line 49

def mark_unavailable!(reason)
  update!(state: "unavailable", error: reason.to_s.truncate(255))
end