You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

  1. class StreamEntry < ApplicationRecord
  2. include Paginable
  3. belongs_to :account, inverse_of: :stream_entries
  4. belongs_to :activity, polymorphic: true
  5. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id'
  6. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id'
  7. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
  8. validates :account, :activity, presence: true
  9. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, mentions: :account, reblog: [:stream_entry, :account, mentions: :account], thread: [:stream_entry, :account]].freeze
  10. scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES, favourite: [:account, :stream_entry, status: STATUS_INCLUDES], follow: [:target_account, :stream_entry]) }
  11. def object_type
  12. orphaned? ? :activity : (targeted? ? :activity : activity.object_type)
  13. end
  14. def verb
  15. orphaned? ? :delete : activity.verb
  16. end
  17. def targeted?
  18. [:follow, :share, :favorite].include? verb
  19. end
  20. def target
  21. orphaned? ? nil : activity.target
  22. end
  23. def title
  24. orphaned? ? nil : activity.title
  25. end
  26. def content
  27. orphaned? ? nil : activity.content
  28. end
  29. def threaded?
  30. verb == :favorite || object_type == :comment
  31. end
  32. def thread
  33. orphaned? ? nil : activity.thread
  34. end
  35. def mentions
  36. activity.respond_to?(:mentions) ? activity.mentions.map { |x| x.account } : []
  37. end
  38. def activity
  39. send(activity_type.downcase.to_sym)
  40. end
  41. private
  42. def orphaned?
  43. activity.nil?
  44. end
  45. end