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.
 
 
 
 

294 lines
10 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: statuses
  5. #
  6. # id :integer not null, primary key
  7. # uri :string
  8. # account_id :integer not null
  9. # text :text default(""), not null
  10. # created_at :datetime not null
  11. # updated_at :datetime not null
  12. # in_reply_to_id :integer
  13. # reblog_of_id :integer
  14. # url :string
  15. # sensitive :boolean default(FALSE)
  16. # visibility :integer default("public"), not null
  17. # in_reply_to_account_id :integer
  18. # application_id :integer
  19. # spoiler_text :text default(""), not null
  20. # reply :boolean default(FALSE)
  21. # favourites_count :integer default(0), not null
  22. # reblogs_count :integer default(0), not null
  23. # language :string default("en"), not null
  24. # conversation_id :integer
  25. #
  26. class Status < ApplicationRecord
  27. include Paginable
  28. include Streamable
  29. include Cacheable
  30. enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
  31. belongs_to :application, class_name: 'Doorkeeper::Application'
  32. belongs_to :account, inverse_of: :statuses, counter_cache: true, required: true
  33. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
  34. belongs_to :conversation
  35. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  36. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count
  37. has_many :favourites, inverse_of: :status, dependent: :destroy
  38. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  39. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  40. has_many :mentions, dependent: :destroy
  41. has_many :media_attachments, dependent: :destroy
  42. has_and_belongs_to_many :tags
  43. has_one :notification, as: :activity, dependent: :destroy
  44. has_one :preview_card, dependent: :destroy
  45. validates :uri, uniqueness: true, unless: 'local?'
  46. validates :text, presence: true, unless: 'reblog?'
  47. validates_with StatusLengthValidator
  48. validates :reblog, uniqueness: { scope: :account }, if: 'reblog?'
  49. default_scope { order('id desc') }
  50. scope :remote, -> { where.not(uri: nil) }
  51. scope :local, -> { where(uri: nil) }
  52. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  53. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  54. scope :with_public_visibility, -> { where(visibility: :public) }
  55. scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
  56. scope :local_only, -> { left_outer_joins(:account).where(accounts: { domain: nil }) }
  57. scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: false }) }
  58. scope :including_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: true }) }
  59. scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
  60. cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  61. def reply?
  62. !in_reply_to_id.nil? || super
  63. end
  64. def local?
  65. uri.nil?
  66. end
  67. def reblog?
  68. !reblog_of_id.nil?
  69. end
  70. def verb
  71. reblog? ? :share : :post
  72. end
  73. def object_type
  74. reply? ? :comment : :note
  75. end
  76. def proper
  77. reblog? ? reblog : self
  78. end
  79. def content
  80. proper.text
  81. end
  82. def target
  83. reblog
  84. end
  85. def title
  86. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  87. end
  88. def hidden?
  89. private_visibility? || direct_visibility?
  90. end
  91. def permitted?(other_account = nil)
  92. if direct_visibility?
  93. account.id == other_account&.id || mentions.where(account: other_account).exists?
  94. elsif private_visibility?
  95. account.id == other_account&.id || other_account&.following?(account) || mentions.where(account: other_account).exists?
  96. else
  97. other_account.nil? || !account.blocking?(other_account)
  98. end
  99. end
  100. def ancestors(account = nil)
  101. ids = Rails.cache.fetch("ancestors:#{id}") { (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) }
  102. statuses = Status.where(id: ids).group_by(&:id)
  103. results = ids.map { |id| statuses[id]&.first }.compact
  104. results = results.reject { |status| filter_from_context?(status, account) }
  105. results
  106. end
  107. def descendants(account = nil)
  108. 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)
  109. statuses = Status.where(id: ids).group_by(&:id)
  110. results = ids.map { |id| statuses[id].first }
  111. results = results.reject { |status| filter_from_context?(status, account) }
  112. results
  113. end
  114. def non_sensitive_with_media?
  115. !sensitive? && media_attachments.any?
  116. end
  117. before_validation :prepare_contents
  118. before_create :set_reblog
  119. before_create :set_visibility
  120. before_create :set_conversation
  121. class << self
  122. def in_allowed_languages(account)
  123. where(language: account.allowed_languages)
  124. end
  125. def as_home_timeline(account)
  126. where(account: [account] + account.following)
  127. end
  128. def as_public_timeline(account = nil, local_only = false)
  129. query = timeline_scope(local_only).without_replies
  130. apply_timeline_filters(query, account)
  131. end
  132. def as_tag_timeline(tag, account = nil, local_only = false)
  133. query = timeline_scope(local_only).tagged_with(tag)
  134. apply_timeline_filters(query, account)
  135. end
  136. def as_outbox_timeline(account)
  137. where(account: account, visibility: :public)
  138. end
  139. def favourites_map(status_ids, account_id)
  140. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  141. end
  142. def reblogs_map(status_ids, account_id)
  143. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  144. end
  145. def mutes_map(conversation_ids, account_id)
  146. ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h
  147. end
  148. def reload_stale_associations!(cached_items)
  149. account_ids = []
  150. cached_items.each do |item|
  151. account_ids << item.account_id
  152. account_ids << item.reblog.account_id if item.reblog?
  153. end
  154. accounts = Account.where(id: account_ids.uniq).map { |a| [a.id, a] }.to_h
  155. cached_items.each do |item|
  156. item.account = accounts[item.account_id]
  157. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  158. end
  159. end
  160. def permitted_for(target_account, account)
  161. return where.not(visibility: [:private, :direct]) if account.nil?
  162. if target_account.blocking?(account) # get rid of blocked peeps
  163. none
  164. elsif account.id == target_account.id # author can see own stuff
  165. all
  166. elsif account.following?(target_account) # followers can see followers-only stuff, but also things they are mentioned in
  167. joins('LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = ' + account.id.to_s)
  168. .where('statuses.visibility != ? OR mentions.id IS NOT NULL', Status.visibilities[:direct])
  169. else # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in
  170. joins('LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = ' + account.id.to_s)
  171. .where('statuses.visibility NOT IN (?) OR mentions.id IS NOT NULL', [Status.visibilities[:direct], Status.visibilities[:private]])
  172. end
  173. end
  174. private
  175. def timeline_scope(local_only = false)
  176. starting_scope = local_only ? Status.local_only : Status
  177. starting_scope
  178. .with_public_visibility
  179. .without_reblogs
  180. end
  181. def apply_timeline_filters(query, account)
  182. if account.nil?
  183. filter_timeline_default(query)
  184. else
  185. filter_timeline_for_account(query, account)
  186. end
  187. end
  188. def filter_timeline_for_account(query, account)
  189. query = query.not_excluded_by_account(account)
  190. query = query.in_allowed_languages(account) if account.allowed_languages.present?
  191. query.merge(account_silencing_filter(account))
  192. end
  193. def filter_timeline_default(query)
  194. query.excluding_silenced_accounts
  195. end
  196. def account_silencing_filter(account)
  197. if account.silenced?
  198. including_silenced_accounts
  199. else
  200. excluding_silenced_accounts
  201. end
  202. end
  203. end
  204. private
  205. def prepare_contents
  206. text&.strip!
  207. spoiler_text&.strip!
  208. end
  209. def set_reblog
  210. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  211. end
  212. def set_visibility
  213. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  214. end
  215. def set_conversation
  216. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  217. if reply? && !thread.nil?
  218. self.in_reply_to_account_id = carried_over_reply_to_account_id
  219. self.conversation_id = thread.conversation_id if conversation_id.nil?
  220. elsif conversation_id.nil?
  221. create_conversation
  222. end
  223. end
  224. def carried_over_reply_to_account_id
  225. if thread.account_id == account_id && thread.reply?
  226. thread.in_reply_to_account_id
  227. else
  228. thread.account_id
  229. end
  230. end
  231. def filter_from_context?(status, account)
  232. account&.blocking?(status.account_id) || account&.muting?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
  233. end
  234. end