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

59 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: stream_entries
  5. #
  6. # id :integer not null, primary key
  7. # activity_id :integer
  8. # activity_type :string
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # hidden :boolean default(FALSE), not null
  12. # account_id :integer
  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, :conversation, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :conversation, :media_attachments, :tags, mentions: :account], thread: [:stream_entry, :account]].freeze
  21. default_scope { where(activity_type: 'Status') }
  22. scope :recent, -> { reorder(id: :desc) }
  23. scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES) }
  24. delegate :target, :title, :content, :thread,
  25. to: :status,
  26. allow_nil: true
  27. def object_type
  28. orphaned? || targeted? ? :activity : status.object_type
  29. end
  30. def verb
  31. orphaned? ? :delete : status.verb
  32. end
  33. def targeted?
  34. [:follow, :request_friend, :authorize, :reject, :unfollow, :block, :unblock, :share, :favorite].include? verb
  35. end
  36. def threaded?
  37. (verb == :favorite || object_type == :comment) && !thread.nil?
  38. end
  39. def mentions
  40. orphaned? ? [] : status.mentions.map(&:account)
  41. end
  42. private
  43. def orphaned?
  44. status.nil?
  45. end
  46. end