闭社主体 forked from https://github.com/tootsuite/mastodon
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.

58 lines
1.4 KiB

  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, :media_attachments, :tags, mentions: :account], thread: [:stream_entry, :account]].freeze
  9. default_scope { where(activity_type: 'Status') }
  10. scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES) }
  11. def object_type
  12. orphaned? || targeted? ? :activity : status.object_type
  13. end
  14. def verb
  15. orphaned? ? :delete : status.verb
  16. end
  17. def targeted?
  18. [:follow, :request_friend, :authorize, :reject, :unfollow, :block, :unblock, :share, :favorite].include? verb
  19. end
  20. def target
  21. orphaned? ? nil : status.target
  22. end
  23. def title
  24. orphaned? ? nil : status.title
  25. end
  26. def content
  27. orphaned? ? nil : status.content
  28. end
  29. def threaded?
  30. (verb == :favorite || object_type == :comment) && !thread.nil?
  31. end
  32. def thread
  33. orphaned? ? nil : status.thread
  34. end
  35. def mentions
  36. orphaned? ? [] : status.mentions.map(&:account)
  37. end
  38. private
  39. def orphaned?
  40. status.nil?
  41. end
  42. end