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.

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