logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

account.rb (13616B)


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