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.

145 lines
5.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class Status < ApplicationRecord
  3. include Paginable
  4. include Streamable
  5. include Cacheable
  6. enum visibility: [:public, :unlisted], _suffix: :visibility
  7. belongs_to :account, inverse_of: :statuses
  8. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  9. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, touch: true
  10. has_many :favourites, inverse_of: :status, dependent: :destroy
  11. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  12. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  13. has_many :mentions, dependent: :destroy
  14. has_many :media_attachments, dependent: :destroy
  15. has_and_belongs_to_many :tags
  16. has_one :notification, as: :activity, dependent: :destroy
  17. validates :account, presence: true
  18. validates :uri, uniqueness: true, unless: 'local?'
  19. validates :text, presence: true, length: { maximum: 500 }, if: proc { |s| s.local? && !s.reblog? }
  20. validates :text, presence: true, if: proc { |s| !s.local? && !s.reblog? }
  21. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  22. default_scope { order('id desc') }
  23. scope :remote, -> { where.not(uri: nil) }
  24. scope :local, -> { where(uri: nil) }
  25. cache_associated :account, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  26. def local?
  27. uri.nil?
  28. end
  29. def reblog?
  30. !reblog_of_id.nil?
  31. end
  32. def reply?
  33. !in_reply_to_id.nil?
  34. end
  35. def verb
  36. reblog? ? :share : :post
  37. end
  38. def object_type
  39. reply? ? :comment : :note
  40. end
  41. def content
  42. reblog? ? reblog.text : text
  43. end
  44. def target
  45. reblog
  46. end
  47. def title
  48. content
  49. end
  50. def reblogs_count
  51. attributes['reblogs_count'] || reblogs.count
  52. end
  53. def favourites_count
  54. attributes['favourites_count'] || favourites.count
  55. end
  56. def ancestors(account = nil)
  57. ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id)
  58. statuses = Status.where(id: ids).with_includes.group_by(&:id)
  59. results = ids.map { |id| statuses[id].first }
  60. results = results.reject { |status| account.blocking?(status.account) } unless account.nil?
  61. results
  62. end
  63. def descendants(account = nil)
  64. ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', id]) - [self]).pluck(:id)
  65. statuses = Status.where(id: ids).with_includes.group_by(&:id)
  66. results = ids.map { |id| statuses[id].first }
  67. results = results.reject { |status| account.blocking?(status.account) } unless account.nil?
  68. results
  69. end
  70. class << self
  71. def as_home_timeline(account)
  72. where(account: [account] + account.following).with_includes
  73. end
  74. def as_mentions_timeline(account)
  75. where(id: Mention.where(account: account).pluck(:status_id)).with_includes
  76. end
  77. def as_public_timeline(account = nil)
  78. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  79. .where(visibility: :public)
  80. .where('accounts.silenced = FALSE')
  81. .where('statuses.in_reply_to_id IS NULL')
  82. .where('statuses.reblog_of_id IS NULL')
  83. query = filter_timeline(query, account) unless account.nil?
  84. query
  85. end
  86. def as_tag_timeline(tag, account = nil)
  87. query = tag.statuses
  88. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  89. .where(visibility: :public)
  90. .where('accounts.silenced = FALSE')
  91. .where('statuses.in_reply_to_id IS NULL')
  92. .where('statuses.reblog_of_id IS NULL')
  93. query = filter_timeline(query, account) unless account.nil?
  94. query
  95. end
  96. def favourites_map(status_ids, account_id)
  97. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  98. end
  99. def reblogs_map(status_ids, account_id)
  100. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  101. end
  102. private
  103. def filter_timeline(query, account)
  104. blocked = Block.where(account: account).pluck(:target_account_id)
  105. return query if blocked.empty?
  106. query.where('statuses.account_id NOT IN (?)', blocked)
  107. end
  108. end
  109. before_validation do
  110. text.strip!
  111. end
  112. end