闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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