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.

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