闭社主体 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.

70 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: stream_entries
  5. #
  6. # id :integer not null, primary key
  7. # account_id :integer
  8. # activity_id :integer
  9. # activity_type :string
  10. # created_at :datetime not null
  11. # updated_at :datetime not null
  12. # hidden :boolean default(FALSE), not null
  13. #
  14. class StreamEntry < ApplicationRecord
  15. include Paginable
  16. belongs_to :account, inverse_of: :stream_entries
  17. belongs_to :activity, polymorphic: true
  18. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id', inverse_of: :stream_entry
  19. validates :account, :activity, presence: true
  20. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account], thread: [:stream_entry, :account]].freeze
  21. default_scope { where(activity_type: 'Status') }
  22. scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES) }
  23. def object_type
  24. orphaned? || targeted? ? :activity : status.object_type
  25. end
  26. def verb
  27. orphaned? ? :delete : status.verb
  28. end
  29. def targeted?
  30. [:follow, :request_friend, :authorize, :reject, :unfollow, :block, :unblock, :share, :favorite].include? verb
  31. end
  32. def target
  33. orphaned? ? nil : status.target
  34. end
  35. def title
  36. orphaned? ? nil : status.title
  37. end
  38. def content
  39. orphaned? ? nil : status.content
  40. end
  41. def threaded?
  42. (verb == :favorite || object_type == :comment) && !thread.nil?
  43. end
  44. def thread
  45. orphaned? ? nil : status.thread
  46. end
  47. def mentions
  48. orphaned? ? [] : status.mentions.map(&:account)
  49. end
  50. private
  51. def orphaned?
  52. status.nil?
  53. end
  54. end