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.

480 lines
15 KiB

Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
Fix Account model deprecation warnings (#3689) ``` DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:61) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:62) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:63) ``` Here's PR describing changes to Dirty API https://github.com/rails/rails/pull/25337
7 years ago
Fix Account model deprecation warnings (#3689) ``` DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:61) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:62) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:63) ``` Here's PR describing changes to Dirty API https://github.com/rails/rails/pull/25337
7 years ago
8 years ago
8 years ago
8 years ago
Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: accounts
  5. #
  6. # id :bigint(8) not null, primary key
  7. # username :string default(""), not null
  8. # domain :string
  9. # secret :string default(""), not null
  10. # private_key :text
  11. # public_key :text default(""), not null
  12. # remote_url :string default(""), not null
  13. # salmon_url :string default(""), not null
  14. # hub_url :string default(""), not null
  15. # created_at :datetime not null
  16. # updated_at :datetime not null
  17. # note :text default(""), not null
  18. # display_name :string default(""), not null
  19. # uri :string default(""), not null
  20. # url :string
  21. # avatar_file_name :string
  22. # avatar_content_type :string
  23. # avatar_file_size :integer
  24. # avatar_updated_at :datetime
  25. # header_file_name :string
  26. # header_content_type :string
  27. # header_file_size :integer
  28. # header_updated_at :datetime
  29. # avatar_remote_url :string
  30. # subscription_expires_at :datetime
  31. # silenced :boolean default(FALSE), not null
  32. # suspended :boolean default(FALSE), not null
  33. # locked :boolean default(FALSE), not null
  34. # header_remote_url :string default(""), not null
  35. # last_webfingered_at :datetime
  36. # inbox_url :string default(""), not null
  37. # outbox_url :string default(""), not null
  38. # shared_inbox_url :string default(""), not null
  39. # followers_url :string default(""), not null
  40. # protocol :integer default("ostatus"), not null
  41. # memorial :boolean default(FALSE), not null
  42. # moved_to_account_id :bigint(8)
  43. # featured_collection_url :string
  44. # fields :jsonb
  45. # actor_type :string
  46. #
  47. class Account < ApplicationRecord
  48. USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.]+[a-z0-9_]+)?/i
  49. MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
  50. include AccountAvatar
  51. include AccountFinderConcern
  52. include AccountHeader
  53. include AccountInteractions
  54. include Attachmentable
  55. include Paginable
  56. include AccountCounters
  57. enum protocol: [:ostatus, :activitypub]
  58. # Local users
  59. has_one :user, inverse_of: :account
  60. validates :username, presence: true
  61. # Remote user validations
  62. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && will_save_change_to_username? }
  63. validates :username, format: { with: /\A#{USERNAME_RE}\z/i }, if: -> { !local? && will_save_change_to_username? }
  64. # Local user validations
  65. validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? }
  66. validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? }
  67. validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
  68. validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
  69. validates :note, length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }
  70. validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? }
  71. # Timelines
  72. has_many :stream_entries, inverse_of: :account, dependent: :destroy
  73. has_many :statuses, inverse_of: :account, dependent: :destroy
  74. has_many :favourites, inverse_of: :account, dependent: :destroy
  75. has_many :mentions, inverse_of: :account, dependent: :destroy
  76. has_many :notifications, inverse_of: :account, dependent: :destroy
  77. # Pinned statuses
  78. has_many :status_pins, inverse_of: :account, dependent: :destroy
  79. has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status
  80. # Endorsements
  81. has_many :account_pins, inverse_of: :account, dependent: :destroy
  82. has_many :endorsed_accounts, through: :account_pins, class_name: 'Account', source: :target_account
  83. # Media
  84. has_many :media_attachments, dependent: :destroy
  85. # PuSH subscriptions
  86. has_many :subscriptions, dependent: :destroy
  87. # Report relationships
  88. has_many :reports
  89. has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
  90. has_many :report_notes, dependent: :destroy
  91. has_many :custom_filters, inverse_of: :account, dependent: :destroy
  92. # Moderation notes
  93. has_many :account_moderation_notes, dependent: :destroy
  94. has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy
  95. # Lists
  96. has_many :list_accounts, inverse_of: :account, dependent: :destroy
  97. has_many :lists, through: :list_accounts
  98. # Account migrations
  99. belongs_to :moved_to_account, class_name: 'Account', optional: true
  100. scope :remote, -> { where.not(domain: nil) }
  101. scope :local, -> { where(domain: nil) }
  102. scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
  103. scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) }
  104. scope :silenced, -> { where(silenced: true) }
  105. scope :suspended, -> { where(suspended: true) }
  106. scope :without_suspended, -> { where(suspended: false) }
  107. scope :recent, -> { reorder(id: :desc) }
  108. scope :bots, -> { where(actor_type: %w(Application Service)) }
  109. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  110. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  111. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  112. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  113. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  114. scope :searchable, -> { where(suspended: false).where(moved_to_account_id: nil) }
  115. delegate :email,
  116. :unconfirmed_email,
  117. :current_sign_in_ip,
  118. :current_sign_in_at,
  119. :confirmed?,
  120. :admin?,
  121. :moderator?,
  122. :staff?,
  123. :locale,
  124. :hides_network?,
  125. to: :user,
  126. prefix: true,
  127. allow_nil: true
  128. delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
  129. def local?
  130. domain.nil?
  131. end
  132. def moved?
  133. moved_to_account_id.present?
  134. end
  135. def bot?
  136. %w(Application Service).include? actor_type
  137. end
  138. alias bot bot?
  139. def bot=(val)
  140. self.actor_type = ActiveModel::Type::Boolean.new.cast(val) ? 'Service' : 'Person'
  141. end
  142. def acct
  143. local? ? username : "#{username}@#{domain}"
  144. end
  145. def local_username_and_domain
  146. "#{username}@#{Rails.configuration.x.local_domain}"
  147. end
  148. def to_webfinger_s
  149. "acct:#{local_username_and_domain}"
  150. end
  151. def subscribed?
  152. subscription_expires_at.present?
  153. end
  154. def possibly_stale?
  155. last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago
  156. end
  157. def refresh!
  158. return if local?
  159. ResolveAccountService.new.call(acct)
  160. end
  161. def suspend!
  162. transaction do
  163. user&.disable! if local?
  164. update!(suspended: true)
  165. end
  166. end
  167. def unsuspend!
  168. transaction do
  169. user&.enable! if local?
  170. update!(suspended: false)
  171. end
  172. end
  173. def memorialize!
  174. transaction do
  175. user&.disable! if local?
  176. update!(memorial: true)
  177. end
  178. end
  179. def keypair
  180. @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
  181. end
  182. def fields
  183. (self[:fields] || []).map { |f| Field.new(self, f) }
  184. end
  185. def fields_attributes=(attributes)
  186. fields = []
  187. old_fields = self[:fields] || []
  188. if attributes.is_a?(Hash)
  189. attributes.each_value do |attr|
  190. next if attr[:name].blank?
  191. previous = old_fields.find { |item| item['value'] == attr[:value] }
  192. if previous && previous['verified_at'].present?
  193. attr[:verified_at] = previous['verified_at']
  194. end
  195. fields << attr
  196. end
  197. end
  198. self[:fields] = fields
  199. end
  200. DEFAULT_FIELDS_SIZE = 4
  201. def build_fields
  202. return if fields.size >= DEFAULT_FIELDS_SIZE
  203. tmp = self[:fields] || []
  204. (DEFAULT_FIELDS_SIZE - tmp.size).times do
  205. tmp << { name: '', value: '' }
  206. end
  207. self.fields = tmp
  208. end
  209. def magic_key
  210. modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
  211. result = []
  212. until component.zero?
  213. result << [component % 256].pack('C')
  214. component >>= 8
  215. end
  216. result.reverse.join
  217. end
  218. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  219. end
  220. def subscription(webhook_url)
  221. @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
  222. end
  223. def save_with_optional_media!
  224. save!
  225. rescue ActiveRecord::RecordInvalid
  226. self.avatar = nil
  227. self.header = nil
  228. self[:avatar_remote_url] = ''
  229. self[:header_remote_url] = ''
  230. save!
  231. end
  232. def object_type
  233. :person
  234. end
  235. def to_param
  236. username
  237. end
  238. def excluded_from_timeline_account_ids
  239. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  240. end
  241. def excluded_from_timeline_domains
  242. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  243. end
  244. def preferred_inbox_url
  245. shared_inbox_url.presence || inbox_url
  246. end
  247. class Field < ActiveModelSerializers::Model
  248. attributes :name, :value, :verified_at, :account, :errors
  249. def initialize(account, attributes)
  250. @account = account
  251. @attributes = attributes
  252. @name = attributes['name'].strip[0, string_limit]
  253. @value = attributes['value'].strip[0, string_limit]
  254. @verified_at = attributes['verified_at']&.to_datetime
  255. @errors = {}
  256. end
  257. def verified?
  258. verified_at.present?
  259. end
  260. def value_for_verification
  261. @value_for_verification ||= begin
  262. if account.local?
  263. value
  264. else
  265. ActionController::Base.helpers.strip_tags(value)
  266. end
  267. end
  268. end
  269. def verifiable?
  270. value_for_verification.present? && value_for_verification.start_with?('http://', 'https://')
  271. end
  272. def mark_verified!
  273. @verified_at = Time.now.utc
  274. @attributes['verified_at'] = @verified_at
  275. end
  276. def to_h
  277. { name: @name, value: @value, verified_at: @verified_at }
  278. end
  279. private
  280. def string_limit
  281. if account.local?
  282. 255
  283. else
  284. 2047
  285. end
  286. end
  287. end
  288. class << self
  289. def readonly_attributes
  290. super - %w(statuses_count following_count followers_count)
  291. end
  292. def domains
  293. reorder(nil).pluck(Arel.sql('distinct accounts.domain'))
  294. end
  295. def inboxes
  296. urls = reorder(nil).where(protocol: :activitypub).pluck(Arel.sql("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)"))
  297. DeliveryFailureTracker.filter(urls)
  298. end
  299. def search_for(terms, limit = 10)
  300. textsearch, query = generate_query_for_search(terms)
  301. sql = <<-SQL.squish
  302. SELECT
  303. accounts.*,
  304. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  305. FROM accounts
  306. WHERE #{query} @@ #{textsearch}
  307. AND accounts.suspended = false
  308. AND accounts.moved_to_account_id IS NULL
  309. ORDER BY rank DESC
  310. LIMIT ?
  311. SQL
  312. records = find_by_sql([sql, limit])
  313. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  314. records
  315. end
  316. def advanced_search_for(terms, account, limit = 10, following = false)
  317. textsearch, query = generate_query_for_search(terms)
  318. if following
  319. sql = <<-SQL.squish
  320. WITH first_degree AS (
  321. SELECT target_account_id
  322. FROM follows
  323. WHERE account_id = ?
  324. )
  325. SELECT
  326. accounts.*,
  327. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  328. FROM accounts
  329. LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
  330. WHERE accounts.id IN (SELECT * FROM first_degree)
  331. AND #{query} @@ #{textsearch}
  332. AND accounts.suspended = false
  333. AND accounts.moved_to_account_id IS NULL
  334. GROUP BY accounts.id
  335. ORDER BY rank DESC
  336. LIMIT ?
  337. SQL
  338. records = find_by_sql([sql, account.id, account.id, account.id, limit])
  339. else
  340. sql = <<-SQL.squish
  341. SELECT
  342. accounts.*,
  343. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  344. FROM accounts
  345. LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
  346. WHERE #{query} @@ #{textsearch}
  347. AND accounts.suspended = false
  348. AND accounts.moved_to_account_id IS NULL
  349. GROUP BY accounts.id
  350. ORDER BY rank DESC
  351. LIMIT ?
  352. SQL
  353. records = find_by_sql([sql, account.id, account.id, limit])
  354. end
  355. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  356. records
  357. end
  358. private
  359. def generate_query_for_search(terms)
  360. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  361. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  362. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  363. [textsearch, query]
  364. end
  365. end
  366. def emojis
  367. @emojis ||= CustomEmoji.from_text(emojifiable_text, domain)
  368. end
  369. before_create :generate_keys
  370. before_validation :normalize_domain
  371. before_validation :prepare_contents, if: :local?
  372. private
  373. def prepare_contents
  374. display_name&.strip!
  375. note&.strip!
  376. end
  377. def generate_keys
  378. return unless local? && !Rails.env.test?
  379. keypair = OpenSSL::PKey::RSA.new(2048)
  380. self.private_key = keypair.to_pem
  381. self.public_key = keypair.public_key.to_pem
  382. end
  383. def normalize_domain
  384. return if local?
  385. self.domain = TagManager.instance.normalize_domain(domain)
  386. end
  387. def emojifiable_text
  388. [note, display_name, fields.map(&:value)].join(' ')
  389. end
  390. end