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.

442 lines
14 KiB

Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
Fix Account model deprecation warnings (#3689) ``` DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:61) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:62) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:63) ``` Here's PR describing changes to Dirty API https://github.com/rails/rails/pull/25337
7 years ago
Fix Account model deprecation warnings (#3689) ``` DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:60) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:61) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:62) DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute?` instead. (called from block in <class:Account> at /Users/rene/Workspace/personal/ruby/mastodon/app/models/account.rb:63) ``` Here's PR describing changes to Dirty API https://github.com/rails/rails/pull/25337
7 years ago
8 years ago
8 years ago
8 years ago
Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: accounts
  5. #
  6. # id :bigint(8) not null, primary key
  7. # username :string default(""), not null
  8. # domain :string
  9. # secret :string default(""), not null
  10. # private_key :text
  11. # public_key :text default(""), not null
  12. # remote_url :string default(""), not null
  13. # salmon_url :string default(""), not null
  14. # hub_url :string default(""), not null
  15. # created_at :datetime not null
  16. # updated_at :datetime not null
  17. # note :text default(""), not null
  18. # display_name :string default(""), not null
  19. # uri :string default(""), not null
  20. # url :string
  21. # avatar_file_name :string
  22. # avatar_content_type :string
  23. # avatar_file_size :integer
  24. # avatar_updated_at :datetime
  25. # header_file_name :string
  26. # header_content_type :string
  27. # header_file_size :integer
  28. # header_updated_at :datetime
  29. # avatar_remote_url :string
  30. # subscription_expires_at :datetime
  31. # silenced :boolean default(FALSE), not null
  32. # suspended :boolean default(FALSE), not null
  33. # locked :boolean default(FALSE), not null
  34. # header_remote_url :string default(""), not null
  35. # statuses_count :integer default(0), not null
  36. # followers_count :integer default(0), not null
  37. # following_count :integer default(0), not null
  38. # last_webfingered_at :datetime
  39. # inbox_url :string default(""), not null
  40. # outbox_url :string default(""), not null
  41. # shared_inbox_url :string default(""), not null
  42. # followers_url :string default(""), not null
  43. # protocol :integer default("ostatus"), not null
  44. # memorial :boolean default(FALSE), not null
  45. # moved_to_account_id :bigint(8)
  46. # featured_collection_url :string
  47. # fields :jsonb
  48. # actor_type :string
  49. #
  50. class Account < ApplicationRecord
  51. USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.]+[a-z0-9_]+)?/i
  52. MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
  53. include AccountAvatar
  54. include AccountFinderConcern
  55. include AccountHeader
  56. include AccountInteractions
  57. include Attachmentable
  58. include Paginable
  59. MAX_NOTE_LENGTH = 500
  60. enum protocol: [:ostatus, :activitypub]
  61. # Local users
  62. has_one :user, inverse_of: :account
  63. validates :username, presence: true
  64. # Remote user validations
  65. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && will_save_change_to_username? }
  66. validates :username, format: { with: /\A#{USERNAME_RE}\z/i }, if: -> { !local? && will_save_change_to_username? }
  67. # Local user validations
  68. validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? }
  69. validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? }
  70. validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
  71. validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
  72. validate :note_length_does_not_exceed_length_limit, if: -> { local? && will_save_change_to_note? }
  73. validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? }
  74. # Timelines
  75. has_many :stream_entries, inverse_of: :account, dependent: :destroy
  76. has_many :statuses, inverse_of: :account, dependent: :destroy
  77. has_many :favourites, inverse_of: :account, dependent: :destroy
  78. has_many :bookmarks, inverse_of: :account, dependent: :destroy
  79. has_many :mentions, inverse_of: :account, dependent: :destroy
  80. has_many :notifications, inverse_of: :account, dependent: :destroy
  81. # Pinned statuses
  82. has_many :status_pins, inverse_of: :account, dependent: :destroy
  83. has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status
  84. # Endorsements
  85. has_many :account_pins, inverse_of: :account, dependent: :destroy
  86. has_many :endorsed_accounts, through: :account_pins, class_name: 'Account', source: :target_account
  87. # Media
  88. has_many :media_attachments, dependent: :destroy
  89. # PuSH subscriptions
  90. has_many :subscriptions, dependent: :destroy
  91. # Report relationships
  92. has_many :reports
  93. has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
  94. has_many :report_notes, dependent: :destroy
  95. has_many :custom_filters, inverse_of: :account, dependent: :destroy
  96. # Moderation notes
  97. has_many :account_moderation_notes, dependent: :destroy
  98. has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy
  99. # Lists
  100. has_many :list_accounts, inverse_of: :account, dependent: :destroy
  101. has_many :lists, through: :list_accounts
  102. # Account migrations
  103. belongs_to :moved_to_account, class_name: 'Account', optional: true
  104. scope :remote, -> { where.not(domain: nil) }
  105. scope :local, -> { where(domain: nil) }
  106. scope :without_followers, -> { where(followers_count: 0) }
  107. scope :with_followers, -> { where('followers_count > 0') }
  108. scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
  109. scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) }
  110. scope :silenced, -> { where(silenced: true) }
  111. scope :suspended, -> { where(suspended: true) }
  112. scope :without_suspended, -> { where(suspended: false) }
  113. scope :recent, -> { reorder(id: :desc) }
  114. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  115. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  116. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  117. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  118. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  119. scope :searchable, -> { where(suspended: false).where(moved_to_account_id: nil) }
  120. delegate :email,
  121. :unconfirmed_email,
  122. :current_sign_in_ip,
  123. :current_sign_in_at,
  124. :confirmed?,
  125. :admin?,
  126. :moderator?,
  127. :staff?,
  128. :locale,
  129. :hides_network?,
  130. to: :user,
  131. prefix: true,
  132. allow_nil: true
  133. delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
  134. def local?
  135. domain.nil?
  136. end
  137. def moved?
  138. moved_to_account_id.present?
  139. end
  140. def bot?
  141. %w(Application Service).include? actor_type
  142. end
  143. alias bot bot?
  144. def bot=(val)
  145. self.actor_type = ActiveModel::Type::Boolean.new.cast(val) ? 'Service' : 'Person'
  146. end
  147. def acct
  148. local? ? username : "#{username}@#{domain}"
  149. end
  150. def local_username_and_domain
  151. "#{username}@#{Rails.configuration.x.local_domain}"
  152. end
  153. def to_webfinger_s
  154. "acct:#{local_username_and_domain}"
  155. end
  156. def subscribed?
  157. subscription_expires_at.present?
  158. end
  159. def possibly_stale?
  160. last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago
  161. end
  162. def refresh!
  163. return if local?
  164. ResolveAccountService.new.call(acct)
  165. end
  166. def unsuspend!
  167. transaction do
  168. user&.enable! if local?
  169. update!(suspended: false)
  170. end
  171. end
  172. def memorialize!
  173. transaction do
  174. user&.disable! if local?
  175. update!(memorial: true)
  176. end
  177. end
  178. def keypair
  179. @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
  180. end
  181. def fields
  182. (self[:fields] || []).map { |f| Field.new(self, f) }
  183. end
  184. def fields_attributes=(attributes)
  185. fields = []
  186. if attributes.is_a?(Hash)
  187. attributes.each_value do |attr|
  188. next if attr[:name].blank?
  189. fields << attr
  190. end
  191. end
  192. self[:fields] = fields
  193. end
  194. def build_fields
  195. return if fields.size >= 4
  196. raw_fields = self[:fields] || []
  197. add_fields = 4 - raw_fields.size
  198. add_fields.times { raw_fields << { name: '', value: '' } }
  199. self.fields = raw_fields
  200. end
  201. def magic_key
  202. modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
  203. result = []
  204. until component.zero?
  205. result << [component % 256].pack('C')
  206. component >>= 8
  207. end
  208. result.reverse.join
  209. end
  210. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  211. end
  212. def subscription(webhook_url)
  213. @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
  214. end
  215. def save_with_optional_media!
  216. save!
  217. rescue ActiveRecord::RecordInvalid
  218. self.avatar = nil
  219. self.header = nil
  220. self[:avatar_remote_url] = ''
  221. self[:header_remote_url] = ''
  222. save!
  223. end
  224. def object_type
  225. :person
  226. end
  227. def to_param
  228. username
  229. end
  230. def excluded_from_timeline_account_ids
  231. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  232. end
  233. def excluded_from_timeline_domains
  234. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  235. end
  236. def preferred_inbox_url
  237. shared_inbox_url.presence || inbox_url
  238. end
  239. class Field < ActiveModelSerializers::Model
  240. attributes :name, :value, :account, :errors
  241. def initialize(account, attr)
  242. @account = account
  243. @name = attr['name'].strip[0, 255]
  244. @value = attr['value'].strip[0, 255]
  245. @errors = {}
  246. end
  247. def to_h
  248. { name: @name, value: @value }
  249. end
  250. end
  251. class << self
  252. def readonly_attributes
  253. super - %w(statuses_count following_count followers_count)
  254. end
  255. def domains
  256. reorder(nil).pluck(Arel.sql('distinct accounts.domain'))
  257. end
  258. def inboxes
  259. urls = reorder(nil).where(protocol: :activitypub).pluck(Arel.sql("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)"))
  260. DeliveryFailureTracker.filter(urls)
  261. end
  262. def search_for(terms, limit = 10)
  263. textsearch, query = generate_query_for_search(terms)
  264. sql = <<-SQL.squish
  265. SELECT
  266. accounts.*,
  267. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  268. FROM accounts
  269. WHERE #{query} @@ #{textsearch}
  270. AND accounts.suspended = false
  271. AND accounts.moved_to_account_id IS NULL
  272. ORDER BY rank DESC
  273. LIMIT ?
  274. SQL
  275. find_by_sql([sql, limit])
  276. end
  277. def advanced_search_for(terms, account, limit = 10, following = false)
  278. textsearch, query = generate_query_for_search(terms)
  279. if following
  280. sql = <<-SQL.squish
  281. WITH first_degree AS (
  282. SELECT target_account_id
  283. FROM follows
  284. WHERE account_id = ?
  285. )
  286. SELECT
  287. accounts.*,
  288. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  289. FROM accounts
  290. 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 = ?)
  291. WHERE accounts.id IN (SELECT * FROM first_degree)
  292. AND #{query} @@ #{textsearch}
  293. AND accounts.suspended = false
  294. AND accounts.moved_to_account_id IS NULL
  295. GROUP BY accounts.id
  296. ORDER BY rank DESC
  297. LIMIT ?
  298. SQL
  299. find_by_sql([sql, account.id, account.id, account.id, limit])
  300. else
  301. sql = <<-SQL.squish
  302. SELECT
  303. accounts.*,
  304. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  305. FROM accounts
  306. 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 = ?)
  307. WHERE #{query} @@ #{textsearch}
  308. AND accounts.suspended = false
  309. AND accounts.moved_to_account_id IS NULL
  310. GROUP BY accounts.id
  311. ORDER BY rank DESC
  312. LIMIT ?
  313. SQL
  314. find_by_sql([sql, account.id, account.id, limit])
  315. end
  316. end
  317. private
  318. def generate_query_for_search(terms)
  319. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  320. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  321. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  322. [textsearch, query]
  323. end
  324. end
  325. def emojis
  326. @emojis ||= CustomEmoji.from_text(emojifiable_text, domain)
  327. end
  328. before_create :generate_keys
  329. before_validation :normalize_domain
  330. before_validation :prepare_contents, if: :local?
  331. private
  332. def prepare_contents
  333. display_name&.strip!
  334. note&.strip!
  335. end
  336. def generate_keys
  337. return unless local? && !Rails.env.test?
  338. keypair = OpenSSL::PKey::RSA.new(2048)
  339. self.private_key = keypair.to_pem
  340. self.public_key = keypair.public_key.to_pem
  341. end
  342. YAML_START = "---\r\n"
  343. YAML_END = "\r\n...\r\n"
  344. def note_length_does_not_exceed_length_limit
  345. note_without_metadata = note
  346. if note.start_with? YAML_START
  347. idx = note.index YAML_END
  348. unless idx.nil?
  349. note_without_metadata = note[(idx + YAML_END.length) .. -1]
  350. end
  351. end
  352. if note_without_metadata.mb_chars.grapheme_length > MAX_NOTE_LENGTH
  353. errors.add(:note, "can't be longer than 500 graphemes")
  354. end
  355. end
  356. def normalize_domain
  357. return if local?
  358. self.domain = TagManager.instance.normalize_domain(domain)
  359. end
  360. def emojifiable_text
  361. [note, display_name, fields.map(&:value)].join(' ')
  362. end
  363. end