Class: InstagramConnect::MediaItem

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

Overview

A post, reel or story on the connected account — "IG Media" in Meta's vocabulary. Named MediaItem because InstagramConnect::Media is already the namespace for inbound file handling (Media::Limits, Media::Attaching); the table keeps Meta's noun.

Constant Summary collapse

PRODUCT_TYPES =
%w[FEED STORY REELS AD].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.record(account:, ig_media_id:, **attributes) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/models/instagram_connect/media_item.rb', line 35

def self.record(account:, ig_media_id:, **attributes)
  media = find_or_initialize_by(account_id: .id, ig_media_id: ig_media_id.to_s)
  media.assign_attributes(attributes.compact)
  media.save!
  media
rescue ActiveRecord::RecordNotUnique
  find_by!(account_id: .id, ig_media_id: ig_media_id.to_s)
end

Instance Method Details

#live_story?Boolean

A story lives for 24 hours from posting; after that Instagram drops it and its CDN URLs die. Unknown posted_at counts as expired — better a grayed card than a player pointed at a dead URL.

Returns:

  • (Boolean)


51
52
53
# File 'app/models/instagram_connect/media_item.rb', line 51

def live_story?
  story? && posted_at.present? && posted_at > 24.hours.ago
end

#removed?Boolean

Upserts by Meta's id. Media arrives from several directions — a webhook about a comment, a story expiring, a sync — and whichever gets there first should create the row without the others having to care. Stamped by SyncMediaJob when a completed listing no longer contains this post. Deleted on Instagram never means deleted here.

Returns:

  • (Boolean)


31
32
33
# File 'app/models/instagram_connect/media_item.rb', line 31

def removed?
  removed_from_instagram_at.present?
end

#story?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/instagram_connect/media_item.rb', line 44

def story?
  media_product_type == "STORY"
end