logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

status.rb (11102B)


  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. # text :text default(""), not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # in_reply_to_id :integer
  12. # reblog_of_id :integer
  13. # url :string
  14. # sensitive :boolean default(FALSE), not null
  15. # visibility :integer default("public"), not null
  16. # spoiler_text :text default(""), not null
  17. # reply :boolean default(FALSE), not null
  18. # favourites_count :integer default(0), not null
  19. # reblogs_count :integer default(0), not null
  20. # language :string
  21. # conversation_id :integer
  22. # local :boolean
  23. # account_id :integer not null
  24. # application_id :integer
  25. # in_reply_to_account_id :integer
  26. #
  27. class Status < ApplicationRecord
  28. include Paginable
  29. include Streamable
  30. include Cacheable
  31. include StatusThreadingConcern
  32. update_index('statuses#status', :proper) if Chewy.enabled?
  33. enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
  34. belongs_to :application, class_name: 'Doorkeeper::Application', optional: true
  35. belongs_to :account, inverse_of: :statuses, counter_cache: true
  36. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true
  37. belongs_to :conversation, optional: true
  38. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true
  39. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count, optional: true
  40. has_many :favourites, inverse_of: :status, dependent: :destroy
  41. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  42. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  43. has_many :mentions, dependent: :destroy
  44. has_many :media_attachments, dependent: :destroy
  45. has_and_belongs_to_many :tags
  46. has_and_belongs_to_many :preview_cards
  47. has_one :notification, as: :activity, dependent: :destroy
  48. has_one :stream_entry, as: :activity, inverse_of: :status
  49. validates :uri, uniqueness: true, presence: true, unless: :local?
  50. validates :text, presence: true, unless: -> { with_media? || reblog? }
  51. validates_with StatusLengthValidator
  52. validates :reblog, uniqueness: { scope: :account }, if: :reblog?
  53. default_scope { recent }
  54. scope :recent, -> { reorder(id: :desc) }
  55. scope :remote, -> { where(local: false).or(where.not(uri: nil)) }
  56. scope :local, -> { where(local: true).or(where(uri: nil)) }
  57. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  58. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  59. scope :with_public_visibility, -> { where(visibility: :public) }
  60. scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
  61. scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: false }) }
  62. scope :including_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: true }) }
  63. scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
  64. scope :not_domain_blocked_by_account, ->(account) { account.excluded_from_timeline_domains.blank? ? left_outer_joins(:account) : left_outer_joins(:account).where('accounts.domain IS NULL OR accounts.domain NOT IN (?)', account.excluded_from_timeline_domains) }
  65. cache_associated :account, :application, :media_attachments, :conversation, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, :conversation, mentions: :account], thread: :account
  66. delegate :domain, to: :account, prefix: true
  67. REAL_TIME_WINDOW = 6.hours
  68. def searchable_by(preloaded = nil)
  69. ids = [account_id]
  70. if preloaded.nil?
  71. ids += mentions.pluck(:account_id)
  72. ids += favourites.pluck(:account_id)
  73. ids += reblogs.pluck(:account_id)
  74. else
  75. ids += preloaded.mentions[id] || []
  76. ids += preloaded.favourites[id] || []
  77. ids += preloaded.reblogs[id] || []
  78. end
  79. ids.uniq
  80. end
  81. def reply?
  82. !in_reply_to_id.nil? || attributes['reply']
  83. end
  84. def local?
  85. attributes['local'] || uri.nil?
  86. end
  87. def reblog?
  88. !reblog_of_id.nil?
  89. end
  90. def within_realtime_window?
  91. created_at >= REAL_TIME_WINDOW.ago
  92. end
  93. def verb
  94. if destroyed?
  95. :delete
  96. else
  97. reblog? ? :share : :post
  98. end
  99. end
  100. def object_type
  101. reply? ? :comment : :note
  102. end
  103. def proper
  104. reblog? ? reblog : self
  105. end
  106. def content
  107. proper.text
  108. end
  109. def target
  110. reblog
  111. end
  112. def title
  113. if destroyed?
  114. "#{account.acct} deleted status"
  115. else
  116. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  117. end
  118. end
  119. def hidden?
  120. private_visibility? || direct_visibility?
  121. end
  122. def with_media?
  123. media_attachments.any?
  124. end
  125. def non_sensitive_with_media?
  126. !sensitive? && with_media?
  127. end
  128. def emojis
  129. CustomEmoji.from_text([spoiler_text, text].join(' '), account.domain)
  130. end
  131. after_create_commit :store_uri, if: :local?
  132. after_create_commit :update_statistics, if: :local?
  133. around_create Mastodon::Snowflake::Callbacks
  134. before_validation :prepare_contents, if: :local?
  135. before_validation :set_reblog
  136. before_validation :set_visibility
  137. before_validation :set_conversation
  138. before_validation :set_sensitivity
  139. before_validation :set_local
  140. class << self
  141. def not_in_filtered_languages(account)
  142. where(language: nil).or where.not(language: account.filtered_languages)
  143. end
  144. def as_home_timeline(account)
  145. where(account: [account] + account.following).where(visibility: [:public, :unlisted, :private])
  146. end
  147. def as_public_timeline(account = nil, local_only = false)
  148. query = timeline_scope(local_only).without_replies
  149. apply_timeline_filters(query, account, local_only)
  150. end
  151. def as_tag_timeline(tag, account = nil, local_only = false)
  152. query = timeline_scope(local_only).tagged_with(tag)
  153. apply_timeline_filters(query, account, local_only)
  154. end
  155. def as_outbox_timeline(account)
  156. where(account: account, visibility: :public)
  157. end
  158. def favourites_map(status_ids, account_id)
  159. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  160. end
  161. def reblogs_map(status_ids, account_id)
  162. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).map { |s| [s.reblog_of_id, true] }.to_h
  163. end
  164. def mutes_map(conversation_ids, account_id)
  165. ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h
  166. end
  167. def pins_map(status_ids, account_id)
  168. StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |p| [p.status_id, true] }.to_h
  169. end
  170. def reload_stale_associations!(cached_items)
  171. account_ids = []
  172. cached_items.each do |item|
  173. account_ids << item.account_id
  174. account_ids << item.reblog.account_id if item.reblog?
  175. end
  176. account_ids.uniq!
  177. return if account_ids.empty?
  178. accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
  179. cached_items.each do |item|
  180. item.account = accounts[item.account_id]
  181. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  182. end
  183. end
  184. def permitted_for(target_account, account)
  185. visibility = [:public, :unlisted]
  186. if account.nil?
  187. where(visibility: visibility)
  188. elsif target_account.blocking?(account) # get rid of blocked peeps
  189. none
  190. elsif account.id == target_account.id # author can see own stuff
  191. all
  192. else
  193. # followers can see followers-only stuff, but also things they are mentioned in.
  194. # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in.
  195. visibility.push(:private) if account.following?(target_account)
  196. where(visibility: visibility).or(where(id: account.mentions.select(:status_id)))
  197. end
  198. end
  199. private
  200. def timeline_scope(local_only = false)
  201. starting_scope = local_only ? Status.local : Status
  202. starting_scope
  203. .with_public_visibility
  204. .without_reblogs
  205. end
  206. def apply_timeline_filters(query, account, local_only)
  207. if account.nil?
  208. filter_timeline_default(query)
  209. else
  210. filter_timeline_for_account(query, account, local_only)
  211. end
  212. end
  213. def filter_timeline_for_account(query, account, local_only)
  214. query = query.not_excluded_by_account(account)
  215. query = query.not_domain_blocked_by_account(account) unless local_only
  216. query = query.not_in_filtered_languages(account) if account.filtered_languages.present?
  217. query.merge(account_silencing_filter(account))
  218. end
  219. def filter_timeline_default(query)
  220. query.excluding_silenced_accounts
  221. end
  222. def account_silencing_filter(account)
  223. if account.silenced?
  224. including_silenced_accounts
  225. else
  226. excluding_silenced_accounts
  227. end
  228. end
  229. end
  230. private
  231. def store_uri
  232. update_attribute(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
  233. end
  234. def prepare_contents
  235. text&.strip!
  236. spoiler_text&.strip!
  237. end
  238. def set_reblog
  239. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  240. end
  241. def set_visibility
  242. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  243. self.visibility = reblog.visibility if reblog?
  244. self.sensitive = false if sensitive.nil?
  245. end
  246. def set_sensitivity
  247. self.sensitive = sensitive || spoiler_text.present?
  248. end
  249. def set_conversation
  250. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  251. if reply? && !thread.nil?
  252. self.in_reply_to_account_id = carried_over_reply_to_account_id
  253. self.conversation_id = thread.conversation_id if conversation_id.nil?
  254. elsif conversation_id.nil?
  255. self.conversation = Conversation.new
  256. end
  257. end
  258. def carried_over_reply_to_account_id
  259. if thread.account_id == account_id && thread.reply?
  260. thread.in_reply_to_account_id
  261. else
  262. thread.account_id
  263. end
  264. end
  265. def set_local
  266. self.local = account.local?
  267. end
  268. def update_statistics
  269. return unless public_visibility? || unlisted_visibility?
  270. ActivityTracker.increment('activity:statuses:local')
  271. end
  272. end