The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

446 lines
14 KiB

  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. enum protocol: [:ostatus, :activitypub]
  60. # Local users
  61. has_one :user, inverse_of: :account
  62. validates :username, presence: true
  63. # Remote user validations
  64. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, 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. # Timelines
  73. has_many :stream_entries, inverse_of: :account, dependent: :destroy
  74. has_many :statuses, inverse_of: :account, dependent: :destroy
  75. has_many :favourites, inverse_of: :account, dependent: :destroy
  76. has_many :mentions, inverse_of: :account, dependent: :destroy
  77. has_many :notifications, inverse_of: :account, dependent: :destroy
  78. # Pinned statuses
  79. has_many :status_pins, inverse_of: :account, dependent: :destroy
  80. has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status
  81. # Media
  82. has_many :media_attachments, dependent: :destroy
  83. # PuSH subscriptions
  84. has_many :subscriptions, dependent: :destroy
  85. # Report relationships
  86. has_many :reports
  87. has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
  88. has_many :report_notes, dependent: :destroy
  89. has_many :custom_filters, inverse_of: :account, dependent: :destroy
  90. # Moderation notes
  91. has_many :account_moderation_notes, dependent: :destroy
  92. has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy
  93. # Lists
  94. has_many :list_accounts, inverse_of: :account, dependent: :destroy
  95. has_many :lists, through: :list_accounts
  96. # Account migrations
  97. belongs_to :moved_to_account, class_name: 'Account', optional: true
  98. scope :remote, -> { where.not(domain: nil) }
  99. scope :local, -> { where(domain: nil) }
  100. scope :without_followers, -> { where(followers_count: 0) }
  101. scope :with_followers, -> { where('followers_count > 0') }
  102. scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
  103. scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) }
  104. scope :silenced, -> { where(silenced: true) }
  105. scope :suspended, -> { where(suspended: true) }
  106. scope :without_suspended, -> { where(suspended: false) }
  107. scope :recent, -> { reorder(id: :desc) }
  108. scope :alphabetic, -> { order(domain: :asc, username: :asc) }
  109. scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
  110. scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
  111. scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
  112. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  113. delegate :email,
  114. :unconfirmed_email,
  115. :current_sign_in_ip,
  116. :current_sign_in_at,
  117. :confirmed?,
  118. :admin?,
  119. :moderator?,
  120. :staff?,
  121. :locale,
  122. :hides_network?,
  123. to: :user,
  124. prefix: true,
  125. allow_nil: true
  126. delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
  127. def local?
  128. domain.nil?
  129. end
  130. def moved?
  131. moved_to_account_id.present?
  132. end
  133. def bot?
  134. %w(Application Service).include? actor_type
  135. end
  136. alias bot bot?
  137. def bot=(val)
  138. self.actor_type = ActiveModel::Type::Boolean.new.cast(val) ? 'Service' : 'Person'
  139. end
  140. def acct
  141. local? ? username : "#{username}@#{domain}"
  142. end
  143. def local_username_and_domain
  144. "#{username}@#{Rails.configuration.x.local_domain}"
  145. end
  146. def to_webfinger_s
  147. "acct:#{local_username_and_domain}"
  148. end
  149. def subscribed?
  150. subscription_expires_at.present?
  151. end
  152. def possibly_stale?
  153. last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago
  154. end
  155. def refresh!
  156. return if local?
  157. ResolveAccountService.new.call(acct)
  158. end
  159. def unsuspend!
  160. transaction do
  161. user&.enable! if local?
  162. update!(suspended: false)
  163. end
  164. end
  165. def memorialize!
  166. transaction do
  167. user&.disable! if local?
  168. update!(memorial: true)
  169. end
  170. end
  171. def keypair
  172. @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
  173. end
  174. def fields
  175. (self[:fields] || []).map { |f| Field.new(self, f) }
  176. end
  177. def fields_attributes=(attributes)
  178. fields = []
  179. if attributes.is_a?(Hash)
  180. attributes.each_value do |attr|
  181. next if attr[:name].blank?
  182. fields << attr
  183. end
  184. end
  185. self[:fields] = fields
  186. end
  187. def build_fields
  188. return if fields.size >= 4
  189. raw_fields = self[:fields] || []
  190. add_fields = 4 - raw_fields.size
  191. add_fields.times { raw_fields << { name: '', value: '' } }
  192. self.fields = raw_fields
  193. end
  194. def magic_key
  195. modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
  196. result = []
  197. until component.zero?
  198. result << [component % 256].pack('C')
  199. component >>= 8
  200. end
  201. result.reverse.join
  202. end
  203. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  204. end
  205. def subscription(webhook_url)
  206. @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
  207. end
  208. def save_with_optional_media!
  209. save!
  210. rescue ActiveRecord::RecordInvalid
  211. self.avatar = nil
  212. self.header = nil
  213. self[:avatar_remote_url] = ''
  214. self[:header_remote_url] = ''
  215. save!
  216. end
  217. def object_type
  218. :person
  219. end
  220. def to_param
  221. username
  222. end
  223. def excluded_from_timeline_account_ids
  224. Rails.cache.fetch("exclude_account_ids_for:#{id}") { blocking.pluck(:target_account_id) + blocked_by.pluck(:account_id) + muting.pluck(:target_account_id) }
  225. end
  226. def excluded_from_timeline_domains
  227. Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
  228. end
  229. def preferred_inbox_url
  230. shared_inbox_url.presence || inbox_url
  231. end
  232. class Field < ActiveModelSerializers::Model
  233. attributes :name, :value, :account, :errors
  234. def initialize(account, attr)
  235. @account = account
  236. @name = attr['name'].strip[0, 255]
  237. @value = attr['value'].strip[0, 255]
  238. @errors = {}
  239. end
  240. def to_h
  241. { name: @name, value: @value }
  242. end
  243. end
  244. class << self
  245. def readonly_attributes
  246. super - %w(statuses_count following_count followers_count)
  247. end
  248. def domains
  249. reorder(nil).pluck(Arel.sql('distinct accounts.domain'))
  250. end
  251. def inboxes
  252. urls = reorder(nil).where(protocol: :activitypub).pluck(Arel.sql("distinct coalesce(nullif(accounts.shared_inbox_url, ''), accounts.inbox_url)"))
  253. DeliveryFailureTracker.filter(urls)
  254. end
  255. def triadic_closures(account, limit: 5, offset: 0)
  256. sql = <<-SQL.squish
  257. WITH first_degree AS (
  258. SELECT target_account_id
  259. FROM follows
  260. WHERE account_id = :account_id
  261. )
  262. SELECT accounts.*
  263. FROM follows
  264. INNER JOIN accounts ON follows.target_account_id = accounts.id
  265. WHERE
  266. account_id IN (SELECT * FROM first_degree)
  267. AND target_account_id NOT IN (SELECT * FROM first_degree)
  268. AND target_account_id NOT IN (:excluded_account_ids)
  269. AND accounts.suspended = false
  270. GROUP BY target_account_id, accounts.id
  271. ORDER BY count(account_id) DESC
  272. OFFSET :offset
  273. LIMIT :limit
  274. SQL
  275. excluded_account_ids = account.excluded_from_timeline_account_ids + [account.id]
  276. find_by_sql(
  277. [sql, { account_id: account.id, excluded_account_ids: excluded_account_ids, limit: limit, offset: offset }]
  278. )
  279. end
  280. def search_for(terms, limit = 10)
  281. textsearch, query = generate_query_for_search(terms)
  282. sql = <<-SQL.squish
  283. SELECT
  284. accounts.*,
  285. ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  286. FROM accounts
  287. WHERE #{query} @@ #{textsearch}
  288. AND accounts.suspended = false
  289. AND accounts.moved_to_account_id IS NULL
  290. ORDER BY rank DESC
  291. LIMIT ?
  292. SQL
  293. find_by_sql([sql, limit])
  294. end
  295. def advanced_search_for(terms, account, limit = 10, following = false)
  296. textsearch, query = generate_query_for_search(terms)
  297. if following
  298. sql = <<-SQL.squish
  299. WITH first_degree AS (
  300. SELECT target_account_id
  301. FROM follows
  302. WHERE account_id = ?
  303. )
  304. SELECT
  305. accounts.*,
  306. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  307. FROM accounts
  308. 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 = ?)
  309. WHERE accounts.id IN (SELECT * FROM first_degree)
  310. AND #{query} @@ #{textsearch}
  311. AND accounts.suspended = false
  312. AND accounts.moved_to_account_id IS NULL
  313. GROUP BY accounts.id
  314. ORDER BY rank DESC
  315. LIMIT ?
  316. SQL
  317. find_by_sql([sql, account.id, account.id, account.id, limit])
  318. else
  319. sql = <<-SQL.squish
  320. SELECT
  321. accounts.*,
  322. (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
  323. FROM accounts
  324. 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 = ?)
  325. WHERE #{query} @@ #{textsearch}
  326. AND accounts.suspended = false
  327. AND accounts.moved_to_account_id IS NULL
  328. GROUP BY accounts.id
  329. ORDER BY rank DESC
  330. LIMIT ?
  331. SQL
  332. find_by_sql([sql, account.id, account.id, limit])
  333. end
  334. end
  335. private
  336. def generate_query_for_search(terms)
  337. terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
  338. textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
  339. query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
  340. [textsearch, query]
  341. end
  342. end
  343. def emojis
  344. @emojis ||= CustomEmoji.from_text(emojifiable_text, domain)
  345. end
  346. before_create :generate_keys
  347. before_validation :normalize_domain
  348. before_validation :prepare_contents, if: :local?
  349. private
  350. def prepare_contents
  351. display_name&.strip!
  352. note&.strip!
  353. end
  354. def generate_keys
  355. return unless local? && !Rails.env.test?
  356. keypair = OpenSSL::PKey::RSA.new(2048)
  357. self.private_key = keypair.to_pem
  358. self.public_key = keypair.public_key.to_pem
  359. end
  360. def normalize_domain
  361. return if local?
  362. self.domain = TagManager.instance.normalize_domain(domain)
  363. end
  364. def emojifiable_text
  365. [note, display_name, fields.map(&:value)].join(' ')
  366. end
  367. end