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.

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