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

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