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.

280 lines
9.5 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. #
  45. class Account < ApplicationRecord
  46. MENTION_RE = /(?:^|[^\/[:word:]])@(([a-z0-9_]+)(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
  47. include AccountAvatar
  48. include AccountFinderConcern
  49. include AccountHeader
  50. include AccountInteractions
  51. include Attachmentable
  52. include Remotable
  53. include EmojiHelper
  54. enum protocol: [:ostatus, :activitypub]
  55. # Local users
  56. has_one :user, inverse_of: :account
  57. validates :username, presence: true
  58. # Remote user validations
  59. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && will_save_change_to_username? }
  60. # Local user validations
  61. 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? }
  62. validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
  63. validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
  64. validates :note, length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }
  65. # Timelines
  66. has_many :stream_entries, inverse_of: :account, dependent: :destroy
  67. has_many :statuses, inverse_of: :account, dependent: :destroy
  68. has_many :favourites, inverse_of: :account, dependent: :destroy
  69. has_many :mentions, inverse_of: :account, dependent: :destroy
  70. has_many :notifications, inverse_of: :account, dependent: :destroy
  71. # Media
  72. has_many :media_attachments, dependent: :destroy
  73. # PuSH subscriptions
  74. has_many :subscriptions, dependent: :destroy
  75. # Report relationships
  76. has_many :reports
  77. has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
  78. scope :remote, -> { where.not(domain: nil) }
  79. scope :local, -> { where(domain: nil) }
  80. scope :without_followers, -> { where(followers_count: 0) }
  81. scope :with_followers, -> { where('followers_count > 0') }
  82. scope :expiring, ->(time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers }
  83. scope :partitioned, -> { order('row_number() over (partition by domain)') }
  84. scope :silenced, -> { where(silenced: true) }
  85. scope :suspended, -> { where(suspended: true) }
  86. scope :recent, -> { reorder(id: :desc) }
  87. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  88. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  89. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  90. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  91. delegate :email,
  92. :current_sign_in_ip,
  93. :current_sign_in_at,
  94. :confirmed?,
  95. :locale,
  96. to: :user,
  97. prefix: true,
  98. allow_nil: true
  99. delegate :filtered_languages, to: :user, prefix: false, allow_nil: true
  100. def local?
  101. domain.nil?
  102. end
  103. def acct
  104. local? ? username : "#{username}@#{domain}"
  105. end
  106. def local_username_and_domain
  107. "#{username}@#{Rails.configuration.x.local_domain}"
  108. end
  109. def to_webfinger_s
  110. "acct:#{local_username_and_domain}"
  111. end
  112. def subscribed?
  113. subscription_expires_at.present?
  114. end
  115. def keypair
  116. OpenSSL::PKey::RSA.new(private_key || public_key)
  117. end
  118. def subscription(webhook_url)
  119. OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 30.days.seconds, webhook: webhook_url, hub: hub_url)
  120. end
  121. def save_with_optional_media!
  122. save!
  123. rescue ActiveRecord::RecordInvalid
  124. self.avatar = nil
  125. self.header = nil
  126. self[:avatar_remote_url] = ''
  127. self[:header_remote_url] = ''
  128. save!
  129. end
  130. def object_type
  131. :person
  132. end
  133. def to_param
  134. username
  135. end
  136. def excluded_from_timeline_account_ids
  137. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  138. end
  139. def excluded_from_timeline_domains
  140. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  141. end
  142. class << self
  143. def domains
  144. reorder(nil).pluck('distinct accounts.domain')
  145. end
  146. def inboxes
  147. reorder(nil).where(protocol: :activitypub).pluck("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)")
  148. end
  149. def triadic_closures(account, limit: 5, offset: 0)
  150. sql = <<-SQL.squish
  151. WITH first_degree AS (
  152. SELECT target_account_id
  153. FROM follows
  154. WHERE account_id = :account_id
  155. )
  156. SELECT accounts.*
  157. FROM follows
  158. INNER JOIN accounts ON follows.target_account_id = accounts.id
  159. WHERE
  160. account_id IN (SELECT * FROM first_degree)
  161. AND target_account_id NOT IN (SELECT * FROM first_degree)
  162. AND target_account_id NOT IN (:excluded_account_ids)
  163. AND accounts.suspended = false
  164. GROUP BY target_account_id, accounts.id
  165. ORDER BY count(account_id) DESC
  166. OFFSET :offset
  167. LIMIT :limit
  168. SQL
  169. excluded_account_ids = account.excluded_from_timeline_account_ids + [account.id]
  170. find_by_sql(
  171. [sql, { account_id: account.id, excluded_account_ids: excluded_account_ids, limit: limit, offset: offset }]
  172. )
  173. end
  174. def search_for(terms, limit = 10)
  175. textsearch, query = generate_query_for_search(terms)
  176. sql = <<-SQL.squish
  177. SELECT
  178. accounts.*,
  179. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  180. FROM accounts
  181. WHERE #{query} @@ #{textsearch}
  182. AND accounts.suspended = false
  183. ORDER BY rank DESC
  184. LIMIT ?
  185. SQL
  186. find_by_sql([sql, limit])
  187. end
  188. def advanced_search_for(terms, account, limit = 10)
  189. textsearch, query = generate_query_for_search(terms)
  190. sql = <<-SQL.squish
  191. SELECT
  192. accounts.*,
  193. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  194. FROM accounts
  195. 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 = ?)
  196. WHERE #{query} @@ #{textsearch}
  197. AND accounts.suspended = false
  198. GROUP BY accounts.id
  199. ORDER BY rank DESC
  200. LIMIT ?
  201. SQL
  202. find_by_sql([sql, account.id, account.id, limit])
  203. end
  204. private
  205. def generate_query_for_search(terms)
  206. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  207. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  208. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  209. [textsearch, query]
  210. end
  211. end
  212. before_create :generate_keys
  213. before_validation :normalize_domain
  214. before_validation :prepare_contents, if: :local?
  215. private
  216. def prepare_contents
  217. display_name&.strip!
  218. note&.strip!
  219. self.display_name = emojify(display_name)
  220. self.note = emojify(note)
  221. end
  222. def generate_keys
  223. return unless local?
  224. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  225. self.private_key = keypair.to_pem
  226. self.public_key = keypair.public_key.to_pem
  227. end
  228. def normalize_domain
  229. return if local?
  230. self.domain = TagManager.instance.normalize_domain(domain)
  231. end
  232. end