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.

432 lines
14 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. # statuses_count :integer default(0), not null
  36. # followers_count :integer default(0), not null
  37. # following_count :integer default(0), not null
  38. # last_webfingered_at :datetime
  39. # inbox_url :string default(""), not null
  40. # outbox_url :string default(""), not null
  41. # shared_inbox_url :string default(""), not null
  42. # followers_url :string default(""), not null
  43. # protocol :integer default("ostatus"), not null
  44. # memorial :boolean default(FALSE), not null
  45. # moved_to_account_id :bigint(8)
  46. # featured_collection_url :string
  47. # fields :jsonb
  48. #
  49. class Account < ApplicationRecord
  50. USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.]+[a-z0-9_]+)?/i
  51. MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
  52. include AccountAvatar
  53. include AccountFinderConcern
  54. include AccountHeader
  55. include AccountInteractions
  56. include Attachmentable
  57. include Paginable
  58. enum protocol: [:ostatus, :activitypub]
  59. # Local users
  60. has_one :user, inverse_of: :account
  61. validates :username, presence: true
  62. # Remote user validations
  63. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, 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. # Media
  81. has_many :media_attachments, dependent: :destroy
  82. # PuSH subscriptions
  83. has_many :subscriptions, dependent: :destroy
  84. # Report relationships
  85. has_many :reports
  86. has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
  87. has_many :report_notes, dependent: :destroy
  88. # Moderation notes
  89. has_many :account_moderation_notes, dependent: :destroy
  90. has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy
  91. # Lists
  92. has_many :list_accounts, inverse_of: :account, dependent: :destroy
  93. has_many :lists, through: :list_accounts
  94. # Account migrations
  95. belongs_to :moved_to_account, class_name: 'Account', optional: true
  96. scope :remote, -> { where.not(domain: nil) }
  97. scope :local, -> { where(domain: nil) }
  98. scope :without_followers, -> { where(followers_count: 0) }
  99. scope :with_followers, -> { where('followers_count > 0') }
  100. scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
  101. scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) }
  102. scope :silenced, -> { where(silenced: true) }
  103. scope :suspended, -> { where(suspended: true) }
  104. scope :without_suspended, -> { where(suspended: false) }
  105. scope :recent, -> { reorder(id: :desc) }
  106. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  107. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  108. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  109. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  110. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  111. delegate :email,
  112. :unconfirmed_email,
  113. :current_sign_in_ip,
  114. :current_sign_in_at,
  115. :confirmed?,
  116. :admin?,
  117. :moderator?,
  118. :staff?,
  119. :locale,
  120. to: :user,
  121. prefix: true,
  122. allow_nil: true
  123. delegate :filtered_languages, to: :user, prefix: false, allow_nil: true
  124. def local?
  125. domain.nil?
  126. end
  127. def moved?
  128. moved_to_account_id.present?
  129. end
  130. def acct
  131. local? ? username : "#{username}@#{domain}"
  132. end
  133. def local_username_and_domain
  134. "#{username}@#{Rails.configuration.x.local_domain}"
  135. end
  136. def to_webfinger_s
  137. "acct:#{local_username_and_domain}"
  138. end
  139. def subscribed?
  140. subscription_expires_at.present?
  141. end
  142. def possibly_stale?
  143. last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago
  144. end
  145. def refresh!
  146. return if local?
  147. ResolveAccountService.new.call(acct)
  148. end
  149. def unsuspend!
  150. transaction do
  151. user&.enable! if local?
  152. update!(suspended: false)
  153. end
  154. end
  155. def memorialize!
  156. transaction do
  157. user&.disable! if local?
  158. update!(memorial: true)
  159. end
  160. end
  161. def keypair
  162. @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
  163. end
  164. def fields
  165. (self[:fields] || []).map { |f| Field.new(self, f) }
  166. end
  167. def fields_attributes=(attributes)
  168. fields = []
  169. if attributes.is_a?(Hash)
  170. attributes.each_value do |attr|
  171. next if attr[:name].blank?
  172. fields << attr
  173. end
  174. end
  175. self[:fields] = fields
  176. end
  177. def build_fields
  178. return if fields.size >= 4
  179. raw_fields = self[:fields] || []
  180. add_fields = 4 - raw_fields.size
  181. add_fields.times { raw_fields << { name: '', value: '' } }
  182. self.fields = raw_fields
  183. end
  184. def magic_key
  185. modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
  186. result = []
  187. until component.zero?
  188. result << [component % 256].pack('C')
  189. component >>= 8
  190. end
  191. result.reverse.join
  192. end
  193. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  194. end
  195. def subscription(webhook_url)
  196. @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
  197. end
  198. def save_with_optional_media!
  199. save!
  200. rescue ActiveRecord::RecordInvalid
  201. self.avatar = nil
  202. self.header = nil
  203. self[:avatar_remote_url] = ''
  204. self[:header_remote_url] = ''
  205. save!
  206. end
  207. def object_type
  208. :person
  209. end
  210. def to_param
  211. username
  212. end
  213. def excluded_from_timeline_account_ids
  214. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  215. end
  216. def excluded_from_timeline_domains
  217. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  218. end
  219. def preferred_inbox_url
  220. shared_inbox_url.presence || inbox_url
  221. end
  222. class Field < ActiveModelSerializers::Model
  223. attributes :name, :value, :account, :errors
  224. def initialize(account, attr)
  225. @account = account
  226. @name = attr['name'].strip[0, 255]
  227. @value = attr['value'].strip[0, 255]
  228. @errors = {}
  229. end
  230. def to_h
  231. { name: @name, value: @value }
  232. end
  233. end
  234. class << self
  235. def readonly_attributes
  236. super - %w(statuses_count following_count followers_count)
  237. end
  238. def domains
  239. reorder(nil).pluck(Arel.sql('distinct accounts.domain'))
  240. end
  241. def inboxes
  242. urls = reorder(nil).where(protocol: :activitypub).pluck(Arel.sql("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)"))
  243. DeliveryFailureTracker.filter(urls)
  244. end
  245. def triadic_closures(account, limit: 5, offset: 0)
  246. sql = <<-SQL.squish
  247. WITH first_degree AS (
  248. SELECT target_account_id
  249. FROM follows
  250. WHERE account_id = :account_id
  251. )
  252. SELECT accounts.*
  253. FROM follows
  254. INNER JOIN accounts ON follows.target_account_id = accounts.id
  255. WHERE
  256. account_id IN (SELECT * FROM first_degree)
  257. AND target_account_id NOT IN (SELECT * FROM first_degree)
  258. AND target_account_id NOT IN (:excluded_account_ids)
  259. AND accounts.suspended = false
  260. GROUP BY target_account_id, accounts.id
  261. ORDER BY count(account_id) DESC
  262. OFFSET :offset
  263. LIMIT :limit
  264. SQL
  265. excluded_account_ids = account.excluded_from_timeline_account_ids + [account.id]
  266. find_by_sql(
  267. [sql, { account_id: account.id, excluded_account_ids: excluded_account_ids, limit: limit, offset: offset }]
  268. )
  269. end
  270. def search_for(terms, limit = 10)
  271. textsearch, query = generate_query_for_search(terms)
  272. sql = <<-SQL.squish
  273. SELECT
  274. accounts.*,
  275. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  276. FROM accounts
  277. WHERE #{query} @@ #{textsearch}
  278. AND accounts.suspended = false
  279. AND accounts.moved_to_account_id IS NULL
  280. ORDER BY rank DESC
  281. LIMIT ?
  282. SQL
  283. find_by_sql([sql, limit])
  284. end
  285. def advanced_search_for(terms, account, limit = 10, following = false)
  286. textsearch, query = generate_query_for_search(terms)
  287. if following
  288. sql = <<-SQL.squish
  289. WITH first_degree AS (
  290. SELECT target_account_id
  291. FROM follows
  292. WHERE account_id = ?
  293. )
  294. SELECT
  295. accounts.*,
  296. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  297. FROM accounts
  298. 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 = ?)
  299. WHERE accounts.id IN (SELECT * FROM first_degree)
  300. AND #{query} @@ #{textsearch}
  301. AND accounts.suspended = false
  302. AND accounts.moved_to_account_id IS NULL
  303. GROUP BY accounts.id
  304. ORDER BY rank DESC
  305. LIMIT ?
  306. SQL
  307. find_by_sql([sql, account.id, account.id, account.id, limit])
  308. else
  309. sql = <<-SQL.squish
  310. SELECT
  311. accounts.*,
  312. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  313. FROM accounts
  314. 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 = ?)
  315. WHERE #{query} @@ #{textsearch}
  316. AND accounts.suspended = false
  317. AND accounts.moved_to_account_id IS NULL
  318. GROUP BY accounts.id
  319. ORDER BY rank DESC
  320. LIMIT ?
  321. SQL
  322. find_by_sql([sql, account.id, account.id, limit])
  323. end
  324. end
  325. private
  326. def generate_query_for_search(terms)
  327. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  328. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  329. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  330. [textsearch, query]
  331. end
  332. end
  333. def emojis
  334. @emojis ||= CustomEmoji.from_text(emojifiable_text, domain)
  335. end
  336. before_create :generate_keys
  337. before_validation :normalize_domain
  338. before_validation :prepare_contents, if: :local?
  339. private
  340. def prepare_contents
  341. display_name&.strip!
  342. note&.strip!
  343. end
  344. def generate_keys
  345. return unless local? && !Rails.env.test?
  346. keypair = OpenSSL::PKey::RSA.new(2048)
  347. self.private_key = keypair.to_pem
  348. self.public_key = keypair.public_key.to_pem
  349. end
  350. def normalize_domain
  351. return if local?
  352. self.domain = TagManager.instance.normalize_domain(domain)
  353. end
  354. def emojifiable_text
  355. [note, display_name, fields.map(&:value)].join(' ')
  356. end
  357. end