The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

182 lines
6.3 KiB

  1. # frozen_string_literal: true
  2. class Status < ApplicationRecord
  3. include Paginable
  4. include Streamable
  5. include Cacheable
  6. enum visibility: [:public, :unlisted, :private], _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 hidden?
  51. private_visibility?
  52. end
  53. def permitted?(other_account = nil)
  54. private_visibility? ? (account.id == other_account&.id || other_account&.following?(account)) : true
  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| filter_from_context?(status, account) }
  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| filter_from_context?(status, account) }
  68. results
  69. end
  70. class << self
  71. def as_home_timeline(account)
  72. where(account: [account] + account.following)
  73. end
  74. def as_mentions_timeline(account)
  75. where(id: Mention.where(account: account).select(:status_id))
  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('(statuses.in_reply_to_id IS NULL OR statuses.in_reply_to_account_id = statuses.account_id)')
  81. .where('statuses.reblog_of_id IS NULL')
  82. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  83. end
  84. def as_tag_timeline(tag, account = nil)
  85. query = tag.statuses
  86. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  87. .where(visibility: :public)
  88. .where('(statuses.in_reply_to_id IS NULL OR statuses.in_reply_to_account_id = statuses.account_id)')
  89. .where('statuses.reblog_of_id IS NULL')
  90. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  91. end
  92. def favourites_map(status_ids, account_id)
  93. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  94. end
  95. def reblogs_map(status_ids, account_id)
  96. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  97. end
  98. def permitted_for(target_account, account)
  99. if account&.id == target_account.id || account&.following?(target_account)
  100. self
  101. else
  102. where.not(visibility: :private)
  103. end
  104. end
  105. def reload_stale_associations!(cached_items)
  106. account_ids = []
  107. cached_items.each do |item|
  108. account_ids << item.account_id
  109. account_ids << item.reblog.account_id if item.reblog?
  110. end
  111. accounts = Account.where(id: account_ids.uniq).map { |a| [a.id, a] }.to_h
  112. cached_items.each do |item|
  113. item.account = accounts[item.account_id]
  114. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  115. end
  116. end
  117. private
  118. def filter_timeline(query, account)
  119. blocked = Block.where(account: account).pluck(:target_account_id)
  120. query = query.where('statuses.account_id NOT IN (?)', blocked) unless blocked.empty?
  121. query = query.where('accounts.silenced = TRUE') if account.silenced?
  122. query
  123. end
  124. def filter_timeline_default(query)
  125. query.where('accounts.silenced = FALSE')
  126. end
  127. end
  128. before_validation do
  129. text.strip!
  130. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  131. self.in_reply_to_account_id = thread.account_id if reply?
  132. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  133. end
  134. private
  135. def filter_from_context?(status, account)
  136. account&.blocking?(status.account) || !status.permitted?(account)
  137. end
  138. end