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.

68 lines
1.6 KiB

8 years ago
  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'
  7. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id'
  8. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
  9. validates :account, :activity, presence: true
  10. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, mentions: :account], thread: [:stream_entry, :account]].freeze
  11. scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES, favourite: [:account, :stream_entry, status: STATUS_INCLUDES], follow: [:target_account, :stream_entry]) }
  12. def object_type
  13. if orphaned?
  14. :activity
  15. else
  16. targeted? ? :activity : activity.object_type
  17. end
  18. end
  19. def verb
  20. orphaned? ? :delete : activity.verb
  21. end
  22. def targeted?
  23. [:follow, :share, :favorite].include? verb
  24. end
  25. def target
  26. orphaned? ? nil : activity.target
  27. end
  28. def title
  29. orphaned? ? nil : activity.title
  30. end
  31. def content
  32. orphaned? ? nil : activity.content
  33. end
  34. def threaded?
  35. (verb == :favorite || object_type == :comment) && !thread.nil?
  36. end
  37. def thread
  38. orphaned? ? nil : activity.thread
  39. end
  40. def mentions
  41. activity.respond_to?(:mentions) ? activity.mentions.map(&:account) : []
  42. end
  43. def activity
  44. send(activity_type.downcase.to_sym)
  45. end
  46. private
  47. def orphaned?
  48. activity.nil?
  49. end
  50. end