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.
 
 
 
 

145 lines
5.2 KiB

  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 :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
  24. cache_associated :account, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  25. def local?
  26. uri.nil?
  27. end
  28. def reblog?
  29. !reblog_of_id.nil?
  30. end
  31. def reply?
  32. !in_reply_to_id.nil?
  33. end
  34. def verb
  35. reblog? ? :share : :post
  36. end
  37. def object_type
  38. reply? ? :comment : :note
  39. end
  40. def content
  41. reblog? ? reblog.text : text
  42. end
  43. def target
  44. reblog
  45. end
  46. def title
  47. content
  48. end
  49. def reblogs_count
  50. attributes['reblogs_count'] || reblogs.count
  51. end
  52. def favourites_count
  53. attributes['favourites_count'] || favourites.count
  54. end
  55. def ancestors(account = nil)
  56. 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)
  57. statuses = Status.where(id: ids).with_includes.group_by(&:id)
  58. results = ids.map { |id| statuses[id].first }
  59. results = results.reject { |status| account.blocking?(status.account) } unless account.nil?
  60. results
  61. end
  62. def descendants(account = nil)
  63. 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)
  64. statuses = Status.where(id: ids).with_includes.group_by(&:id)
  65. results = ids.map { |id| statuses[id].first }
  66. results = results.reject { |status| account.blocking?(status.account) } unless account.nil?
  67. results
  68. end
  69. class << self
  70. def as_home_timeline(account)
  71. where(account: [account] + account.following).with_includes
  72. end
  73. def as_mentions_timeline(account)
  74. where(id: Mention.where(account: account).pluck(:status_id)).with_includes
  75. end
  76. def as_public_timeline(account = nil)
  77. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  78. .where(visibility: :public)
  79. .where('accounts.silenced = FALSE')
  80. .where('statuses.in_reply_to_id IS NULL')
  81. .where('statuses.reblog_of_id IS NULL')
  82. query = filter_timeline(query, account) unless account.nil?
  83. query
  84. end
  85. def as_tag_timeline(tag, account = nil)
  86. query = tag.statuses
  87. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  88. .where(visibility: :public)
  89. .where('accounts.silenced = FALSE')
  90. .where('statuses.in_reply_to_id IS NULL')
  91. .where('statuses.reblog_of_id IS NULL')
  92. query = filter_timeline(query, account) unless account.nil?
  93. query
  94. end
  95. def favourites_map(status_ids, account_id)
  96. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  97. end
  98. def reblogs_map(status_ids, account_id)
  99. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  100. end
  101. private
  102. def filter_timeline(query, account)
  103. blocked = Block.where(account: account).pluck(:target_account_id)
  104. return query if blocked.empty?
  105. query.where('statuses.account_id NOT IN (?)', blocked)
  106. end
  107. end
  108. before_validation do
  109. text.strip!
  110. end
  111. end