stream_entry.rb 1.4 KB

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