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.

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