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.

455 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
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 AccountAssociations
  51. include AccountAvatar
  52. include AccountFinderConcern
  53. include AccountHeader
  54. include AccountInteractions
  55. include Attachmentable
  56. include Paginable
  57. include AccountCounters
  58. enum protocol: [:ostatus, :activitypub]
  59. validates :username, presence: true
  60. # Remote user validations
  61. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && will_save_change_to_username? }
  62. validates :username, format: { with: /\A#{USERNAME_RE}\z/i }, if: -> { !local? && will_save_change_to_username? }
  63. # Local user validations
  64. validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? }
  65. validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? }
  66. validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
  67. validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
  68. validates :note, length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }
  69. validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? }
  70. scope :remote, -> { where.not(domain: nil) }
  71. scope :local, -> { where(domain: nil) }
  72. scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
  73. scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) }
  74. scope :silenced, -> { where(silenced: true) }
  75. scope :suspended, -> { where(suspended: true) }
  76. scope :without_suspended, -> { where(suspended: false) }
  77. scope :recent, -> { reorder(id: :desc) }
  78. scope :bots, -> { where(actor_type: %w(Application Service)) }
  79. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  80. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  81. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  82. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  83. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  84. scope :searchable, -> { where(suspended: false).where(moved_to_account_id: nil) }
  85. delegate :email,
  86. :unconfirmed_email,
  87. :current_sign_in_ip,
  88. :current_sign_in_at,
  89. :confirmed?,
  90. :admin?,
  91. :moderator?,
  92. :staff?,
  93. :locale,
  94. :hides_network?,
  95. to: :user,
  96. prefix: true,
  97. allow_nil: true
  98. delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
  99. def local?
  100. domain.nil?
  101. end
  102. def moved?
  103. moved_to_account_id.present?
  104. end
  105. def bot?
  106. %w(Application Service).include? actor_type
  107. end
  108. alias bot bot?
  109. def bot=(val)
  110. self.actor_type = ActiveModel::Type::Boolean.new.cast(val) ? 'Service' : 'Person'
  111. end
  112. def acct
  113. local? ? username : "#{username}@#{domain}"
  114. end
  115. def local_username_and_domain
  116. "#{username}@#{Rails.configuration.x.local_domain}"
  117. end
  118. def to_webfinger_s
  119. "acct:#{local_username_and_domain}"
  120. end
  121. def subscribed?
  122. subscription_expires_at.present?
  123. end
  124. def possibly_stale?
  125. last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago
  126. end
  127. def refresh!
  128. return if local?
  129. ResolveAccountService.new.call(acct)
  130. end
  131. def suspend!
  132. transaction do
  133. user&.disable! if local?
  134. update!(suspended: true)
  135. end
  136. end
  137. def unsuspend!
  138. transaction do
  139. user&.enable! if local?
  140. update!(suspended: false)
  141. end
  142. end
  143. def memorialize!
  144. transaction do
  145. user&.disable! if local?
  146. update!(memorial: true)
  147. end
  148. end
  149. def keypair
  150. @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
  151. end
  152. def fields
  153. (self[:fields] || []).map { |f| Field.new(self, f) }
  154. end
  155. def fields_attributes=(attributes)
  156. fields = []
  157. old_fields = self[:fields] || []
  158. if attributes.is_a?(Hash)
  159. attributes.each_value do |attr|
  160. next if attr[:name].blank?
  161. previous = old_fields.find { |item| item['value'] == attr[:value] }
  162. if previous && previous['verified_at'].present?
  163. attr[:verified_at] = previous['verified_at']
  164. end
  165. fields << attr
  166. end
  167. end
  168. self[:fields] = fields
  169. end
  170. DEFAULT_FIELDS_SIZE = 4
  171. def build_fields
  172. return if fields.size >= DEFAULT_FIELDS_SIZE
  173. tmp = self[:fields] || []
  174. (DEFAULT_FIELDS_SIZE - tmp.size).times do
  175. tmp << { name: '', value: '' }
  176. end
  177. self.fields = tmp
  178. end
  179. def magic_key
  180. modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
  181. result = []
  182. until component.zero?
  183. result << [component % 256].pack('C')
  184. component >>= 8
  185. end
  186. result.reverse.join
  187. end
  188. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  189. end
  190. def subscription(webhook_url)
  191. @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
  192. end
  193. def save_with_optional_media!
  194. save!
  195. rescue ActiveRecord::RecordInvalid
  196. self.avatar = nil
  197. self.header = nil
  198. self[:avatar_remote_url] = ''
  199. self[:header_remote_url] = ''
  200. save!
  201. end
  202. def object_type
  203. :person
  204. end
  205. def to_param
  206. username
  207. end
  208. def excluded_from_timeline_account_ids
  209. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  210. end
  211. def excluded_from_timeline_domains
  212. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  213. end
  214. def preferred_inbox_url
  215. shared_inbox_url.presence || inbox_url
  216. end
  217. class Field < ActiveModelSerializers::Model
  218. attributes :name, :value, :verified_at, :account, :errors
  219. def initialize(account, attributes)
  220. @account = account
  221. @attributes = attributes
  222. @name = attributes['name'].strip[0, string_limit]
  223. @value = attributes['value'].strip[0, string_limit]
  224. @verified_at = attributes['verified_at']&.to_datetime
  225. @errors = {}
  226. end
  227. def verified?
  228. verified_at.present?
  229. end
  230. def value_for_verification
  231. @value_for_verification ||= begin
  232. if account.local?
  233. value
  234. else
  235. ActionController::Base.helpers.strip_tags(value)
  236. end
  237. end
  238. end
  239. def verifiable?
  240. value_for_verification.present? && value_for_verification.start_with?('http://', 'https://')
  241. end
  242. def mark_verified!
  243. @verified_at = Time.now.utc
  244. @attributes['verified_at'] = @verified_at
  245. end
  246. def to_h
  247. { name: @name, value: @value, verified_at: @verified_at }
  248. end
  249. private
  250. def string_limit
  251. if account.local?
  252. 255
  253. else
  254. 2047
  255. end
  256. end
  257. end
  258. class << self
  259. def readonly_attributes
  260. super - %w(statuses_count following_count followers_count)
  261. end
  262. def domains
  263. reorder(nil).pluck(Arel.sql('distinct accounts.domain'))
  264. end
  265. def inboxes
  266. urls = reorder(nil).where(protocol: :activitypub).pluck(Arel.sql("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)"))
  267. DeliveryFailureTracker.filter(urls)
  268. end
  269. def search_for(terms, limit = 10)
  270. textsearch, query = generate_query_for_search(terms)
  271. sql = <<-SQL.squish
  272. SELECT
  273. accounts.*,
  274. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  275. FROM accounts
  276. WHERE #{query} @@ #{textsearch}
  277. AND accounts.suspended = false
  278. AND accounts.moved_to_account_id IS NULL
  279. ORDER BY rank DESC
  280. LIMIT ?
  281. SQL
  282. records = find_by_sql([sql, limit])
  283. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  284. records
  285. end
  286. def advanced_search_for(terms, account, limit = 10, following = false)
  287. textsearch, query = generate_query_for_search(terms)
  288. if following
  289. sql = <<-SQL.squish
  290. WITH first_degree AS (
  291. SELECT target_account_id
  292. FROM follows
  293. WHERE account_id = ?
  294. )
  295. SELECT
  296. accounts.*,
  297. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  298. FROM accounts
  299. 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 = ?)
  300. WHERE accounts.id IN (SELECT * FROM first_degree)
  301. AND #{query} @@ #{textsearch}
  302. AND accounts.suspended = false
  303. AND accounts.moved_to_account_id IS NULL
  304. GROUP BY accounts.id
  305. ORDER BY rank DESC
  306. LIMIT ?
  307. SQL
  308. records = find_by_sql([sql, account.id, account.id, account.id, limit])
  309. else
  310. sql = <<-SQL.squish
  311. SELECT
  312. accounts.*,
  313. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  314. FROM accounts
  315. 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 = ?)
  316. WHERE #{query} @@ #{textsearch}
  317. AND accounts.suspended = false
  318. AND accounts.moved_to_account_id IS NULL
  319. GROUP BY accounts.id
  320. ORDER BY rank DESC
  321. LIMIT ?
  322. SQL
  323. records = find_by_sql([sql, account.id, account.id, limit])
  324. end
  325. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  326. records
  327. end
  328. private
  329. def generate_query_for_search(terms)
  330. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  331. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  332. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  333. [textsearch, query]
  334. end
  335. end
  336. def emojis
  337. @emojis ||= CustomEmoji.from_text(emojifiable_text, domain)
  338. end
  339. before_create :generate_keys
  340. before_validation :normalize_domain
  341. before_validation :prepare_contents, if: :local?
  342. before_destroy :clean_feed_manager
  343. private
  344. def prepare_contents
  345. display_name&.strip!
  346. note&.strip!
  347. end
  348. def generate_keys
  349. return unless local? && !Rails.env.test?
  350. keypair = OpenSSL::PKey::RSA.new(2048)
  351. self.private_key = keypair.to_pem
  352. self.public_key = keypair.public_key.to_pem
  353. end
  354. def normalize_domain
  355. return if local?
  356. self.domain = TagManager.instance.normalize_domain(domain)
  357. end
  358. def emojifiable_text
  359. [note, display_name, fields.map(&:value)].join(' ')
  360. end
  361. def clean_feed_manager
  362. reblog_key = FeedManager.instance.key(:home, id, 'reblogs')
  363. reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
  364. Redis.current.pipelined do
  365. Redis.current.del(FeedManager.instance.key(:home, id))
  366. Redis.current.del(reblog_key)
  367. reblogged_id_set.each do |reblogged_id|
  368. reblog_set_key = FeedManager.instance.key(:home, id, "reblogs:#{reblogged_id}")
  369. Redis.current.del(reblog_set_key)
  370. end
  371. end
  372. end
  373. end